Bug 15632 [QA Followup] - Change method type to _type for bug 15446
[koha.git] / t / db_dependent / Patrons.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 => 13;
21 use Test::Warn;
22
23 use C4::Context;
24 use Koha::Database;
25
26 BEGIN {
27     use_ok('Koha::Objects');
28     use_ok('Koha::Patrons');
29 }
30
31 # Start transaction
32 my $dbh = C4::Context->dbh;
33 $dbh->{AutoCommit} = 0;
34 $dbh->{RaiseError} = 1;
35 $dbh->do("DELETE FROM issues");
36 $dbh->do("DELETE FROM borrowers");
37
38 my $categorycode =
39   Koha::Database->new()->schema()->resultset('Category')->first()
40   ->categorycode();
41 my $branchcode =
42   Koha::Database->new()->schema()->resultset('Branch')->first()->branchcode();
43
44 my $b1 = Koha::Patron->new(
45     {
46         surname      => 'Test 1',
47         branchcode   => $branchcode,
48         categorycode => $categorycode
49     }
50 );
51 $b1->store();
52 my $b2 = Koha::Patron->new(
53     {
54         surname      => 'Test 2',
55         branchcode   => $branchcode,
56         categorycode => $categorycode
57     }
58 );
59 $b2->store();
60 my $b3 = Koha::Patron->new(
61     {
62         surname      => 'Test 3',
63         branchcode   => $branchcode,
64         categorycode => $categorycode
65     }
66 );
67 $b3->store();
68
69 my $b1_new = Koha::Patrons->find( $b1->borrowernumber() );
70 is( $b1->surname(), $b1_new->surname(), "Found matching patron" );
71
72 my @patrons = Koha::Patrons->search( { branchcode => $branchcode } );
73 is( @patrons, 3, "Found 3 patrons with Search" );
74
75 my $unexistent = Koha::Patrons->find( '1234567890' );
76 is( $unexistent, undef, 'Koha::Objects->Find should return undef if the record does not exist' );
77
78 my $patrons = Koha::Patrons->search( { branchcode => $branchcode } );
79 is( $patrons->count( { branchcode => $branchcode } ), 3, "Counted 3 patrons with Count" );
80
81 my $b = $patrons->next();
82 is( $b->surname(), 'Test 1', "Next returns first patron" );
83 $b = $patrons->next();
84 is( $b->surname(), 'Test 2', "Next returns second patron" );
85 $b = $patrons->next();
86 is( $b->surname(), 'Test 3', "Next returns third patron" );
87 $b = $patrons->next();
88 is( $b, undef, "Next returns undef" );
89
90 # Test Reset and iteration in concert
91 $patrons->reset();
92 foreach my $b ( $patrons->as_list() ) {
93     is( $b->categorycode(), $categorycode, "Iteration returns a patron object" );
94 }
95
96 1;