Bug 16330: Move patches to OpenAPI
[koha.git] / t / db_dependent / PatronLists.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use Test::More tests => 7;
21 use t::lib::TestBuilder;
22
23 use Koha::Database;
24 use Koha::List::Patron
25     qw( AddPatronList AddPatronsToList DelPatronList DelPatronsFromList GetPatronLists ModPatronList );
26
27 my $schema = Koha::Database->schema;
28 $schema->storage->txn_begin;
29
30 my $builder = t::lib::TestBuilder->new;
31
32 C4::Context->_new_userenv('DUMMY SESSION');
33 C4::Context->set_userenv( 0 ); # Koha::List::Patron only needs a number
34
35 # Create 10 sample borrowers
36 my @borrowers = ();
37 foreach (1..10) {
38     push @borrowers, $builder->build({ source => 'Borrower' });
39 }
40
41 my $owner = $borrowers[0]->{borrowernumber};
42
43 my @lists = GetPatronLists( { owner => $owner } );
44 my $list_count_original = @lists;
45
46 my $list1 = AddPatronList( { name => 'Test List 1', owner => $owner } );
47 ok( $list1->name() eq 'Test List 1', 'AddPatronList works' );
48
49 my $list2 = AddPatronList( { name => 'Test List 2', owner => $owner } );
50
51 ModPatronList(
52     {
53         patron_list_id => $list2->patron_list_id(),
54         name           => 'Test List 3',
55         owner          => $owner
56     }
57 );
58 $list2->discard_changes();
59 ok( $list2->name() eq 'Test List 3', 'ModPatronList works' );
60
61 AddPatronsToList(
62     { list => $list1, cardnumbers => [ map { $_->{cardnumber} } @borrowers ] }
63 );
64 ok(
65     scalar @borrowers ==
66       $list1->patron_list_patrons()->search_related('borrowernumber')->all(),
67     'AddPatronsToList works for cardnumbers'
68 );
69
70 AddPatronsToList(
71     {
72         list            => $list2,
73         borrowernumbers => [ map { $_->{borrowernumber} } @borrowers ]
74     }
75 );
76 ok(
77     scalar @borrowers ==
78       $list2->patron_list_patrons()->search_related('borrowernumber')->all(),
79     'AddPatronsToList works for borrowernumbers'
80 );
81
82 my @ids =
83   $list1->patron_list_patrons()->get_column('patron_list_patron_id')->all();
84 DelPatronsFromList(
85     {
86         list                => $list1,
87         patron_list_patrons => \@ids,
88     }
89 );
90 $list1->discard_changes();
91 ok( !$list1->patron_list_patrons()->count(), 'DelPatronsFromList works.' );
92
93 @lists = GetPatronLists( { owner => $owner } );
94 ok( @lists == $list_count_original + 2, 'GetPatronLists works' );
95
96 DelPatronList( { patron_list_id => $list1->patron_list_id(), owner => $owner } );
97 DelPatronList( { patron_list_id => $list2->patron_list_id(), owner => $owner } );
98
99 @lists =
100   GetPatronLists( { patron_list_id => $list1->patron_list_id(), owner => $owner } );
101 ok( !@lists, 'DelPatronList works' );
102
103 $schema->storage->txn_rollback;
104