Bug 10565: Add a "Patron List" feature for storing and manipulating collections of...
[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 my $dbh = C4::Context->dbh;
13 my $sth = $dbh->prepare("SELECT * FROM borrowers ORDER BY RAND() LIMIT 10");
14 $sth->execute();
15 my @borrowers = @{ $sth->fetchall_arrayref( {} ) };
16
17 my @lists = GetPatronLists( { owner => 1 } );
18 my $list_count_original = @lists;
19
20 my $list1 = AddPatronList( { name => 'Test List 1', owner => 1 } );
21 ok( $list1->name() eq 'Test List 1', 'AddPatronList works' );
22
23 my $list2 = AddPatronList( { name => 'Test List 2', owner => 1 } );
24
25 ModPatronList(
26     {
27         patron_list_id => $list2->patron_list_id(),
28         name           => 'Test List 3',
29         owner          => 1
30     }
31 );
32 $list2->discard_changes();
33 ok( $list2->name() eq 'Test List 3', 'ModPatronList works' );
34
35 AddPatronsToList(
36     { list => $list1, cardnumbers => [ map { $_->{cardnumber} } @borrowers ] }
37 );
38 ok(
39     scalar @borrowers ==
40       $list1->patron_list_patrons()->search_related('borrowernumber')->all(),
41     'AddPatronsToList works for cardnumbers'
42 );
43
44 AddPatronsToList(
45     {
46         list            => $list2,
47         borrowernumbers => [ map { $_->{borrowernumber} } @borrowers ]
48     }
49 );
50 ok(
51     scalar @borrowers ==
52       $list2->patron_list_patrons()->search_related('borrowernumber')->all(),
53     'AddPatronsToList works for borrowernumbers'
54 );
55
56 my @ids =
57   $list1->patron_list_patrons()->get_column('patron_list_patron_id')->all();
58 DelPatronsFromList(
59     {
60         list                => $list1,
61         patron_list_patrons => \@ids,
62     }
63 );
64 $list1->discard_changes();
65 ok( !$list1->patron_list_patrons()->count(), 'DelPatronsFromList works.' );
66
67 @lists = GetPatronLists( { owner => 1 } );
68 ok( @lists == $list_count_original + 2, 'GetPatronLists works' );
69
70 DelPatronList( { patron_list_id => $list1->patron_list_id(), owner => 1 } );
71 DelPatronList( { patron_list_id => $list2->patron_list_id(), owner => 1 } );
72
73 @lists =
74   GetPatronLists( { patron_list_id => $list1->patron_list_id(), owner => 1 } );
75 ok( !@lists, 'DelPatronList works' );