Bug 22944: avoid AnonymousPatron in search_patrons_to_anonymise
[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 => 9;
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
28 my $schema = Koha::Database->schema;
29 $schema->storage->txn_begin;
30
31 my $builder = t::lib::TestBuilder->new;
32
33 t::lib::Mocks::mock_userenv();
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 my $owner2 = $borrowers[1]->{borrowernumber};
43
44 my @lists = GetPatronLists( { owner => $owner } );
45 my $list_count_original = @lists;
46
47 my $list1 = AddPatronList( { name => 'Test List 1', owner => $owner } );
48 is( $list1->name(), 'Test List 1', 'AddPatronList works' );
49
50 my $list2 = AddPatronList( { name => 'Test List 2', owner => $owner } );
51
52 ModPatronList(
53     {
54         patron_list_id => $list2->patron_list_id(),
55         name           => 'Test List 3',
56         owner          => $owner
57     }
58 );
59 $list2->discard_changes();
60 is( $list2->name(), 'Test List 3', 'ModPatronList works' );
61
62 AddPatronsToList(
63     { list => $list1, cardnumbers => [ map { $_->{cardnumber} } @borrowers ] }
64 );
65 is(
66     scalar @borrowers,
67       $list1->patron_list_patrons()->search_related('borrowernumber')->all(),
68     'AddPatronsToList works for cardnumbers'
69 );
70
71 AddPatronsToList(
72     {
73         list            => $list2,
74         borrowernumbers => [ map { $_->{borrowernumber} } @borrowers ]
75     }
76 );
77 is(
78     scalar @borrowers,
79       $list2->patron_list_patrons()->search_related('borrowernumber')->all(),
80     'AddPatronsToList works for borrowernumbers'
81 );
82
83 my @ids =
84   $list1->patron_list_patrons()->get_column('patron_list_patron_id')->all();
85 DelPatronsFromList(
86     {
87         list                => $list1,
88         patron_list_patrons => \@ids,
89     }
90 );
91 $list1->discard_changes();
92 is( $list1->patron_list_patrons()->count(), 0, 'DelPatronsFromList works.' );
93
94 @lists = GetPatronLists( { owner => $owner } );
95 is( scalar @lists, $list_count_original + 2, 'GetPatronLists works' );
96
97 my $list3 = AddPatronList( { name => 'Test List 3', owner => $owner2, shared => 0 } );
98 @lists = GetPatronLists( { owner => $owner } );
99 is( scalar @lists, $list_count_original + 2, 'GetPatronLists does not return non-shared list' );
100
101 my $list4 = AddPatronList( { name => 'Test List 4', owner => $owner2, shared => 1 } );
102 @lists = GetPatronLists( { owner => $owner } );
103 is( scalar @lists, $list_count_original + 3, 'GetPatronLists does return shared list' );
104
105 DelPatronList( { patron_list_id => $list1->patron_list_id(), owner => $owner } );
106 DelPatronList( { patron_list_id => $list2->patron_list_id(), owner => $owner } );
107
108 @lists =
109   GetPatronLists( { patron_list_id => $list1->patron_list_id(), owner => $owner } );
110 is( scalar @lists, 0, 'DelPatronList works' );
111
112 $schema->storage->txn_rollback;
113