Bug 14778: Make Barcodes_ValueBuilder.t db dependent
[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 $owner = $borrowers[0]->{borrowernumber};
18
19 my @lists = GetPatronLists( { owner => $owner } );
20 my $list_count_original = @lists;
21
22 my $list1 = AddPatronList( { name => 'Test List 1', owner => $owner } );
23 ok( $list1->name() eq 'Test List 1', 'AddPatronList works' );
24
25 my $list2 = AddPatronList( { name => 'Test List 2', owner => $owner } );
26
27 ModPatronList(
28     {
29         patron_list_id => $list2->patron_list_id(),
30         name           => 'Test List 3',
31         owner          => $owner
32     }
33 );
34 $list2->discard_changes();
35 ok( $list2->name() eq 'Test List 3', 'ModPatronList works' );
36
37 AddPatronsToList(
38     { list => $list1, cardnumbers => [ map { $_->{cardnumber} } @borrowers ] }
39 );
40 ok(
41     scalar @borrowers ==
42       $list1->patron_list_patrons()->search_related('borrowernumber')->all(),
43     'AddPatronsToList works for cardnumbers'
44 );
45
46 AddPatronsToList(
47     {
48         list            => $list2,
49         borrowernumbers => [ map { $_->{borrowernumber} } @borrowers ]
50     }
51 );
52 ok(
53     scalar @borrowers ==
54       $list2->patron_list_patrons()->search_related('borrowernumber')->all(),
55     'AddPatronsToList works for borrowernumbers'
56 );
57
58 my @ids =
59   $list1->patron_list_patrons()->get_column('patron_list_patron_id')->all();
60 DelPatronsFromList(
61     {
62         list                => $list1,
63         patron_list_patrons => \@ids,
64     }
65 );
66 $list1->discard_changes();
67 ok( !$list1->patron_list_patrons()->count(), 'DelPatronsFromList works.' );
68
69 @lists = GetPatronLists( { owner => $owner } );
70 ok( @lists == $list_count_original + 2, 'GetPatronLists works' );
71
72 DelPatronList( { patron_list_id => $list1->patron_list_id(), owner => $owner } );
73 DelPatronList( { patron_list_id => $list2->patron_list_id(), owner => $owner } );
74
75 @lists =
76   GetPatronLists( { patron_list_id => $list1->patron_list_id(), owner => $owner } );
77 ok( !@lists, 'DelPatronList works' );