View | Details | Raw Unified | Return to bug 19618
Collapse All | Expand All

(-)a/t/db_dependent/Koha/Club/Enrollment.t (+48 lines)
Line 0 Link Here
1
#!/usr/bin/perl
2
3
# Copyright 2015 Koha Development team
4
#
5
# This file is part of Koha
6
#
7
# Koha is free software; you can redistribute it and/or modify it
8
# under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# Koha is distributed in the hope that it will be useful, but
13
# WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20
use Modern::Perl;
21
22
use Test::More tests => 1;
23
use Test::Exception;
24
25
use Koha::Patron;
26
use Koha::Database;
27
use Koha::DateUtils qw(dt_from_string);
28
29
use t::lib::TestBuilder;
30
31
my $builder = t::lib::TestBuilder->new;
32
my $schema = Koha::Database->new->schema;
33
34
subtest 'is_canceled' => sub {
35
    plan tests => 2;
36
37
    $schema->storage->txn_begin;
38
39
    my $enrollment = $builder->build_object({ class => 'Koha::Club::Enrollments', value => { date_canceled => undef } });
40
41
    ok(!$enrollment->is_canceled, 'Enrollment should not be canceled');
42
43
    $enrollment->date_canceled(dt_from_string+"")->store;
44
45
    ok($enrollment->is_canceled, 'Enrollment should be canceled');
46
47
    $schema->storage->txn_rollback;
48
}
(-)a/t/db_dependent/Koha/Club/Hold.t (+87 lines)
Line 0 Link Here
1
#!/usr/bin/perl
2
3
# Copyright 2015 Koha Development team
4
#
5
# This file is part of Koha
6
#
7
# Koha is free software; you can redistribute it and/or modify it
8
# under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# Koha is distributed in the hope that it will be useful, but
13
# WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20
use Modern::Perl;
21
22
use Test::More tests => 1;
23
use Test::Exception;
24
use Try::Tiny;
25
26
use Koha::Club::Hold;
27
use Koha::Club::Hold::PatronHolds;
28
use Koha::Holds;
29
use Koha::Database;
30
use Koha::DateUtils qw(dt_from_string);
31
use Scalar::Util qw(blessed);
32
33
use t::lib::TestBuilder;
34
35
my $builder = t::lib::TestBuilder->new;
36
my $schema = Koha::Database->new->schema;
37
38
subtest 'add' => sub {
39
    plan tests => 5;
40
41
    $schema->storage->txn_begin;
42
43
    my $club = $builder->build_object({ class => 'Koha::Clubs' });
44
    my $library = $builder->build_object({ class => 'Koha::Libraries' });
45
    my $item1 = $builder->build_sample_item({ branchcode => $library->branchcode });
46
    my $item2 = $builder->build_sample_item({ branchcode => $library->branchcode });
47
48
    use Data::Printer colored => 1;
49
    try {
50
        Koha::Club::Hold::add({ club_id => $club->id, biblio_id => $item1->biblionumber, pickup_library_id => $library->branchcode });
51
    } catch {
52
        my $class = ref $_;
53
        ok($class eq 'Koha::Exceptions::ClubHold::NoPatrons', 'Exception thrown when no patron is enrolled in club');
54
    };
55
56
    my $patron = $builder->build_object({ class => 'Koha::Patrons', value => { branchcode => $library->branchcode } });
57
    my $e = $builder->build_object({ class => 'Koha::Club::Enrollments' , value => { club_id => $club->id, borrowernumber => $patron->borrowernumber, date_canceled => undef }} );
58
59
    my $club_hold = Koha::Club::Hold::add({
60
        club_id => $club->id,
61
        biblio_id => $item1->biblionumber,
62
        pickup_library_id => $library->branchcode
63
    });
64
65
    is(blessed($club_hold), 'Koha::Club::Hold', 'add returns a Koha::Club::Hold');
66
67
    $e->date_canceled(dt_from_string)->store;
68
69
    try {
70
        Koha::Club::Hold::add({ club_id => $club->id, biblio_id => $item2->biblionumber, pickup_library_id => $library->branchcode });
71
    } catch {
72
        my $class = ref $_;
73
        ok($class eq 'Koha::Exceptions::ClubHold::NoPatrons', 'Exception thrown when no patron is enrolled in club');
74
    };
75
76
    my $patron_holds = Koha::Club::Hold::PatronHolds->search({ club_hold_id => $club_hold->id });
77
78
    ok($patron_holds->count, "There must be at least one patron_hold");
79
80
    my $patron_hold = $patron_holds->next;
81
82
    my $hold = Koha::Holds->find($patron_hold->hold_id);
83
84
    is($patron_hold->patron_id, $hold->borrowernumber, 'Patron must be the same');
85
86
    $schema->storage->txn_rollback;
87
}
(-)a/t/db_dependent/api/v1/clubs_holds.t (-1 / +149 lines)
Line 0 Link Here
0
- 
1
2
#!/usr/bin/env perl
3
4
# This file is part of Koha.
5
#
6
# Koha is free software; you can redistribute it and/or modify it under the
7
# terms of the GNU General Public License as published by the Free Software
8
# Foundation; either version 3 of the License, or (at your option) any later
9
# version.
10
#
11
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
12
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License along
16
# with Koha; if not, write to the Free Software Foundation, Inc.,
17
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
19
use Modern::Perl;
20
21
use Test::More tests => 1;
22
use Test::Mojo;
23
use Test::Warn;
24
25
use t::lib::TestBuilder;
26
use t::lib::Mocks;
27
28
use C4::Auth;
29
use Koha::Database;
30
31
my $schema  = Koha::Database->new->schema;
32
my $builder = t::lib::TestBuilder->new;
33
34
# FIXME: sessionStorage defaults to mysql, but it seems to break transaction handling
35
# this affects the other REST api tests
36
t::lib::Mocks::mock_preference( 'SessionStorage', 'tmp' );
37
38
my $remote_address = '127.0.0.1';
39
my $t              = Test::Mojo->new('Koha::REST::V1');
40
41
subtest 'add() tests' => sub {
42
    plan tests => 2;
43
44
    $schema->storage->txn_begin;
45
46
    my ($club_with_enrollments, $club_without_enrollments, $item, @enrollments) = create_test_data();
47
48
    unauthorized_access_tests('POST', "/api/v1/clubs/".$club_with_enrollments->id."/holds", undef, {
49
        biblio_id => $item->biblionumber,
50
        pickup_library_id => $item->home_branch->branchcode
51
    });
52
53
    $schema->storage->txn_rollback;
54
55
    subtest 'librarian access tests' => sub {
56
        plan tests => 8;
57
58
        $schema->storage->txn_begin;
59
60
        my ($club_with_enrollments, $club_without_enrollments, $item, @enrollments) = create_test_data();
61
62
        my ( undef, $session_id ) = create_user_and_session({ authorized => 1 });
63
        my $data = {
64
            biblio_id => $item->biblionumber,
65
            pickup_library_id => $item->home_branch->branchcode
66
        };
67
        my $tx = $t->ua->build_tx(POST => "/api/v1/clubs/".$club_without_enrollments->id."/holds" => json => $data);
68
        $tx->req->cookies({ name => 'CGISESSID', value => $session_id });
69
        $t->request_ok($tx)
70
            ->status_is(500)
71
            ->json_is('/error' => "Cannot place a hold on a club without patrons.");
72
73
        $tx = $t->ua->build_tx(POST => "/api/v1/clubs/".$club_with_enrollments->id."/holds" => json => $data);
74
        $tx->req->cookies({ name => 'CGISESSID', value => $session_id });
75
        $t->request_ok($tx)
76
          ->status_is(201, 'Created Hold')
77
          ->json_has('/club_hold_id', 'got a club hold id')
78
          ->json_is( '/club_id' => $club_with_enrollments->id)
79
          ->json_is( '/biblio_id'    => $item->biblionumber);
80
81
        $schema->storage->txn_rollback;
82
    };
83
};
84
85
sub unauthorized_access_tests {
86
    my ($verb, $endpoint, $club_hold_id, $json) = @_;
87
88
    $endpoint .= ($club_hold_id) ? "/$club_hold_id" : '';
89
90
    subtest 'unauthorized access tests' => sub {
91
        plan tests => 5;
92
93
        my $tx = $t->ua->build_tx($verb => $endpoint => json => $json);
94
        $t->request_ok($tx)
95
          ->status_is(401);
96
97
        my ($borrowernumber, $session_id) = create_user_and_session({
98
            authorized => 0 });
99
100
        $tx = $t->ua->build_tx($verb => $endpoint => json => $json);
101
        $tx->req->cookies({name => 'CGISESSID', value => $session_id});
102
        $t->request_ok($tx)
103
          ->status_is(403)
104
          ->json_has('/required_permissions');
105
    };
106
}
107
108
sub create_user_and_session {
109
110
    my $args  = shift;
111
    my $flags = ( $args->{authorized} ) ? 64 : 0;
112
113
    my $user = $builder->build(
114
        {
115
            source => 'Borrower',
116
            value  => {
117
                flags => $flags,
118
                gonenoaddress => 0,
119
                lost => 0,
120
                email => 'nobody@example.com',
121
                emailpro => 'nobody@example.com',
122
                B_email => 'nobody@example.com'
123
            }
124
        }
125
    );
126
127
    # Create a session for the authorized user
128
    my $session = C4::Auth::get_session('');
129
    $session->param( 'number',   $user->{borrowernumber} );
130
    $session->param( 'id',       $user->{userid} );
131
    $session->param( 'ip',       '127.0.0.1' );
132
    $session->param( 'lasttime', time() );
133
    $session->flush;
134
135
    return ( $user->{borrowernumber}, $session->id );
136
}
137
138
sub create_test_data {
139
    my $club_with_enrollments = $builder->build_object( { class => 'Koha::Clubs' } );
140
    my $club_without_enrollments = $builder->build_object( { class => 'Koha::Clubs' } );
141
    my $enrollment1 = $builder->build_object( { class => 'Koha::Club::Enrollments', value => { club_id => $club_with_enrollments->id, date_canceled => undef } } );
142
    my $enrollment2 = $builder->build_object( { class => 'Koha::Club::Enrollments', value => { club_id => $club_with_enrollments->id, date_canceled => undef } } );
143
    my $enrollment3 = $builder->build_object( { class => 'Koha::Club::Enrollments', value => { club_id => $club_with_enrollments->id, date_canceled => undef } } );
144
    my $enrollment4 = $builder->build_object( { class => 'Koha::Club::Enrollments', value => { club_id => $club_with_enrollments->id, date_canceled => undef } } );
145
    my $enrollment5 = $builder->build_object( { class => 'Koha::Club::Enrollments', value => { club_id => $club_with_enrollments->id, date_canceled => undef } } );
146
    my $enrollment6 = $builder->build_object( { class => 'Koha::Club::Enrollments', value => { club_id => $club_with_enrollments->id, date_canceled => undef } } );
147
    my $item        = $builder->build_sample_item();
148
    return ( $club_with_enrollments, $club_without_enrollments, $item, [ $enrollment1, $enrollment2, $enrollment3, $enrollment4, $enrollment5, $enrollment6 ] );
149
}

Return to bug 19618