Bug 16452: Remove the warnings raised by PatronLists.t
[koha.git] / t / db_dependent / PatronLists.t
1 #!/usr/bin/perl
2 #
3 use Modern::Perl;
4
5 use Test::More tests => 9;
6
7 BEGIN {
8     use_ok('C4::Context');
9     use_ok('Koha::List::Patron');
10 }
11
12 C4::Context->_new_userenv('DUMMY SESSION');
13 C4::Context->set_userenv(0,0,0,'firstname','surname', 'BRANCH1', 'Library 1', 0, ', ');
14
15 my $dbh = C4::Context->dbh;
16 my $sth = $dbh->prepare("SELECT * FROM borrowers ORDER BY RAND() LIMIT 10");
17 $sth->execute();
18 my @borrowers = @{ $sth->fetchall_arrayref( {} ) };
19
20 my $owner = $borrowers[0]->{borrowernumber};
21
22 my @lists = GetPatronLists( { owner => $owner } );
23 my $list_count_original = @lists;
24
25 my $list1 = AddPatronList( { name => 'Test List 1', owner => $owner } );
26 ok( $list1->name() eq 'Test List 1', 'AddPatronList works' );
27
28 my $list2 = AddPatronList( { name => 'Test List 2', owner => $owner } );
29
30 ModPatronList(
31     {
32         patron_list_id => $list2->patron_list_id(),
33         name           => 'Test List 3',
34         owner          => $owner
35     }
36 );
37 $list2->discard_changes();
38 ok( $list2->name() eq 'Test List 3', 'ModPatronList works' );
39
40 AddPatronsToList(
41     { list => $list1, cardnumbers => [ map { $_->{cardnumber} } @borrowers ] }
42 );
43 ok(
44     scalar @borrowers ==
45       $list1->patron_list_patrons()->search_related('borrowernumber')->all(),
46     'AddPatronsToList works for cardnumbers'
47 );
48
49 AddPatronsToList(
50     {
51         list            => $list2,
52         borrowernumbers => [ map { $_->{borrowernumber} } @borrowers ]
53     }
54 );
55 ok(
56     scalar @borrowers ==
57       $list2->patron_list_patrons()->search_related('borrowernumber')->all(),
58     'AddPatronsToList works for borrowernumbers'
59 );
60
61 my @ids =
62   $list1->patron_list_patrons()->get_column('patron_list_patron_id')->all();
63 DelPatronsFromList(
64     {
65         list                => $list1,
66         patron_list_patrons => \@ids,
67     }
68 );
69 $list1->discard_changes();
70 ok( !$list1->patron_list_patrons()->count(), 'DelPatronsFromList works.' );
71
72 @lists = GetPatronLists( { owner => $owner } );
73 ok( @lists == $list_count_original + 2, 'GetPatronLists works' );
74
75 DelPatronList( { patron_list_id => $list1->patron_list_id(), owner => $owner } );
76 DelPatronList( { patron_list_id => $list2->patron_list_id(), owner => $owner } );
77
78 @lists =
79   GetPatronLists( { patron_list_id => $list1->patron_list_id(), owner => $owner } );
80 ok( !@lists, 'DelPatronList works' );