Bug 35979: (follow-up) Add check in ->enqueue
[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 => 12;
21 use t::lib::TestBuilder;
22 use t::lib::Mocks;
23
24 use Koha::Database;
25 use Koha::List::Patron
26     qw( AddPatronList AddPatronsToList DelPatronList DelPatronsFromList GetPatronLists ModPatronList );
27 use Koha::Patrons;
28
29 my $schema = Koha::Database->schema;
30 $schema->storage->txn_begin;
31
32 my $builder = t::lib::TestBuilder->new;
33
34 t::lib::Mocks::mock_userenv();
35
36 # Create 10 sample borrowers
37 my @borrowers = ();
38 foreach (1..10) {
39     push @borrowers, $builder->build({ source => 'Borrower' });
40 }
41
42 my $owner  = $borrowers[0]->{borrowernumber};
43 my $owner2 = $borrowers[1]->{borrowernumber};
44
45 my @lists = GetPatronLists( { owner => $owner } );
46 my $list_count_original = @lists;
47
48 my $list1 = AddPatronList( { name => 'Test List 1', owner => $owner } );
49 is( $list1->name(), 'Test List 1', 'AddPatronList works' );
50
51 my $list2 = AddPatronList( { name => 'Test List 2', owner => $owner } );
52
53 ModPatronList(
54     {
55         patron_list_id => $list2->patron_list_id(),
56         name           => 'Test List 3',
57         owner          => $owner
58     }
59 );
60 $list2->discard_changes();
61 is( $list2->name(), 'Test List 3', 'ModPatronList works' );
62
63 AddPatronsToList(
64     { list => $list1, cardnumbers => [ map { $_->{cardnumber} } @borrowers ] }
65 );
66 is(
67     scalar @borrowers,
68       $list1->patron_list_patrons()->search_related('borrowernumber')->all(),
69     'AddPatronsToList works for cardnumbers'
70 );
71
72 AddPatronsToList(
73     {
74         list            => $list2,
75         borrowernumbers => [ map { $_->{borrowernumber} } @borrowers ]
76     }
77 );
78 is(
79     scalar @borrowers,
80       $list2->patron_list_patrons()->search_related('borrowernumber')->all(),
81     'AddPatronsToList works for borrowernumbers'
82 );
83
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');
90
91 my @ids =
92   $list1->patron_list_patrons()->get_column('patron_list_patron_id')->all();
93 DelPatronsFromList(
94     {
95         list                => $list1,
96         patron_list_patrons => \@ids,
97     }
98 );
99 $list1->discard_changes();
100 is( $list1->patron_list_patrons()->count(), 0, 'DelPatronsFromList works.' );
101
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' );
106
107 @lists = GetPatronLists( { owner => $owner } );
108 is( scalar @lists, $list_count_original + 2, 'GetPatronLists works' );
109
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' );
113
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' );
117
118 DelPatronList( { patron_list_id => $list1->patron_list_id(), owner => $owner } );
119 DelPatronList( { patron_list_id => $list2->patron_list_id(), owner => $owner } );
120
121 @lists =
122   GetPatronLists( { patron_list_id => $list1->patron_list_id(), owner => $owner } );
123 is( scalar @lists, 0, 'DelPatronList works' );
124
125 $schema->storage->txn_rollback;
126