Bug 16805: Add test to getalert (no param passed)
[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 => 17;
21 use Test::Warn;
22
23 use C4::Context;
24 use Koha::Database;
25 use Koha::DateUtils;
26
27 BEGIN {
28     use_ok('Koha::Objects');
29     use_ok('Koha::Patrons');
30 }
31
32 # Start transaction
33 my $dbh = C4::Context->dbh;
34 $dbh->{AutoCommit} = 0;
35 $dbh->{RaiseError} = 1;
36 $dbh->do("DELETE FROM issues");
37 $dbh->do("DELETE FROM borrowers");
38
39 my $categorycode =
40   Koha::Database->new()->schema()->resultset('Category')->first()
41   ->categorycode();
42 my $branchcode =
43   Koha::Database->new()->schema()->resultset('Branch')->first()->branchcode();
44
45 my $b1 = Koha::Patron->new(
46     {
47         surname      => 'Test 1',
48         branchcode   => $branchcode,
49         categorycode => $categorycode
50     }
51 );
52 $b1->store();
53 my $b2 = Koha::Patron->new(
54     {
55         surname      => 'Test 2',
56         branchcode   => $branchcode,
57         categorycode => $categorycode
58     }
59 );
60 $b2->store();
61 my $three_days_ago = dt_from_string->add( days => -3 );
62 my $b3 = Koha::Patron->new(
63     {
64         surname      => 'Test 3',
65         branchcode   => $branchcode,
66         categorycode => $categorycode,
67         updated_on   => $three_days_ago,
68     }
69 );
70 $b3->store();
71
72 my $b1_new = Koha::Patrons->find( $b1->borrowernumber() );
73 is( $b1->surname(), $b1_new->surname(), "Found matching patron" );
74 isnt( $b1_new->updated_on, undef, "borrowers.updated_on should be set" );
75 is( dt_from_string($b1_new->updated_on), dt_from_string, "borrowers.updated_on should have been set to now on creating" );
76
77 my $b3_new = Koha::Patrons->find( $b3->borrowernumber() );
78 is( dt_from_string($b3_new->updated_on), $three_days_ago, "borrowers.updated_on should have been kept to what we set on creating" );
79 $b3_new->set({ surname => 'another surname for Test 3' });
80 $b3_new = Koha::Patrons->find( $b3->borrowernumber() );
81 is( dt_from_string($b1_new->updated_on), dt_from_string, "borrowers.updated_on should have been set to now on updating" );
82
83 my @patrons = Koha::Patrons->search( { branchcode => $branchcode } );
84 is( @patrons, 3, "Found 3 patrons with Search" );
85
86 my $unexistent = Koha::Patrons->find( '1234567890' );
87 is( $unexistent, undef, 'Koha::Objects->Find should return undef if the record does not exist' );
88
89 my $patrons = Koha::Patrons->search( { branchcode => $branchcode } );
90 is( $patrons->count( { branchcode => $branchcode } ), 3, "Counted 3 patrons with Count" );
91
92 my $b = $patrons->next();
93 is( $b->surname(), 'Test 1', "Next returns first patron" );
94 $b = $patrons->next();
95 is( $b->surname(), 'Test 2', "Next returns second patron" );
96 $b = $patrons->next();
97 is( $b->surname(), 'Test 3', "Next returns third patron" );
98 $b = $patrons->next();
99 is( $b, undef, "Next returns undef" );
100
101 # Test Reset and iteration in concert
102 $patrons->reset();
103 foreach my $b ( $patrons->as_list() ) {
104     is( $b->categorycode(), $categorycode, "Iteration returns a patron object" );
105 }
106
107 1;