3 # This file is part of Koha.
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.
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.
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>.
20 use Test::More tests => 12;
21 use t::lib::TestBuilder;
25 use Koha::List::Patron
26 qw( AddPatronList AddPatronsToList DelPatronList DelPatronsFromList GetPatronLists ModPatronList );
29 my $schema = Koha::Database->schema;
30 $schema->storage->txn_begin;
32 my $builder = t::lib::TestBuilder->new;
34 t::lib::Mocks::mock_userenv();
36 # Create 10 sample borrowers
39 push @borrowers, $builder->build({ source => 'Borrower' });
42 my $owner = $borrowers[0]->{borrowernumber};
43 my $owner2 = $borrowers[1]->{borrowernumber};
45 my @lists = GetPatronLists( { owner => $owner } );
46 my $list_count_original = @lists;
48 my $list1 = AddPatronList( { name => 'Test List 1', owner => $owner } );
49 is( $list1->name(), 'Test List 1', 'AddPatronList works' );
51 my $list2 = AddPatronList( { name => 'Test List 2', owner => $owner } );
55 patron_list_id => $list2->patron_list_id(),
56 name => 'Test List 3',
60 $list2->discard_changes();
61 is( $list2->name(), 'Test List 3', 'ModPatronList works' );
64 { list => $list1, cardnumbers => [ map { $_->{cardnumber} } @borrowers ] }
68 $list1->patron_list_patrons()->search_related('borrowernumber')->all(),
69 'AddPatronsToList works for cardnumbers'
75 borrowernumbers => [ map { $_->{borrowernumber} } @borrowers ]
80 $list2->patron_list_patrons()->search_related('borrowernumber')->all(),
81 'AddPatronsToList works for borrowernumbers'
84 my $deleted_patron = $builder->build_object({ class => 'Koha::Patrons' });
85 $deleted_patron->delete;
86 my @result = AddPatronsToList({list => $list2,borrowernumbers => [ $deleted_patron->borrowernumber ]});
87 is( scalar @result, 0,'Invalid borrowernumber not added');
88 @result = AddPatronsToList({list => $list2,cardnumbers => [ $deleted_patron->cardnumber ]});
89 is( scalar @result, 0,'Invalid cardnumber not added');
92 $list1->patron_list_patrons()->get_column('patron_list_patron_id')->all();
96 patron_list_patrons => \@ids,
99 $list1->discard_changes();
100 is( $list1->patron_list_patrons()->count(), 0, 'DelPatronsFromList works.' );
102 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
103 AddPatronsToList({list => $list2,borrowernumbers => [ $patron->borrowernumber ]});
104 @lists = $patron->get_lists_with_patron;
105 is( scalar @lists, 1, 'get_lists_with_patron works' );
107 @lists = GetPatronLists( { owner => $owner } );
108 is( scalar @lists, $list_count_original + 2, 'GetPatronLists works' );
110 my $list3 = AddPatronList( { name => 'Test List 3', owner => $owner2, shared => 0 } );
111 @lists = GetPatronLists( { owner => $owner } );
112 is( scalar @lists, $list_count_original + 2, 'GetPatronLists does not return non-shared list' );
114 my $list4 = AddPatronList( { name => 'Test List 4', owner => $owner2, shared => 1 } );
115 @lists = GetPatronLists( { owner => $owner } );
116 is( scalar @lists, $list_count_original + 3, 'GetPatronLists does return shared list' );
118 DelPatronList( { patron_list_id => $list1->patron_list_id(), owner => $owner } );
119 DelPatronList( { patron_list_id => $list2->patron_list_id(), owner => $owner } );
122 GetPatronLists( { patron_list_id => $list1->patron_list_id(), owner => $owner } );
123 is( scalar @lists, 0, 'DelPatronList works' );
125 $schema->storage->txn_rollback;