Bug 17742: Fix t/db_dependent/Patrons.t
[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 $database = Koha::Database->new();
34 my $schema = $database->schema();
35 $schema->storage->txn_begin();
36
37 $schema->resultset('Issue')->delete_all();
38 $schema->resultset('Borrower')->delete_all();
39
40 my $categorycode = $schema->resultset('Category')->first()->categorycode();
41 my $branchcode = $schema->resultset('Branch')->first()->branchcode();
42
43 my $b1 = Koha::Patron->new(
44     {
45         surname      => 'Test 1',
46         branchcode   => $branchcode,
47         categorycode => $categorycode
48     }
49 );
50 $b1->store();
51 my $now = dt_from_string;
52 my $b2 = Koha::Patron->new(
53     {
54         surname      => 'Test 2',
55         branchcode   => $branchcode,
56         categorycode => $categorycode
57     }
58 );
59 $b2->store();
60 my $three_days_ago = dt_from_string->add( days => -3 );
61 my $b3 = Koha::Patron->new(
62     {
63         surname      => 'Test 3',
64         branchcode   => $branchcode,
65         categorycode => $categorycode,
66         updated_on   => $three_days_ago,
67     }
68 );
69 $b3->store();
70
71 my $b1_new = Koha::Patrons->find( $b1->borrowernumber() );
72 is( $b1->surname(), $b1_new->surname(), "Found matching patron" );
73 isnt( $b1_new->updated_on, undef, "borrowers.updated_on should be set" );
74 is( dt_from_string($b1_new->updated_on), $now, "borrowers.updated_on should have been set to now on creating" );
75
76 my $b3_new = Koha::Patrons->find( $b3->borrowernumber() );
77 is( dt_from_string($b3_new->updated_on), $three_days_ago, "borrowers.updated_on should have been kept to what we set on creating" );
78 $b3_new->set({ firstname => 'Some first name for Test 3' })->store();
79 $b3_new = Koha::Patrons->find( $b3->borrowernumber() );
80 is( dt_from_string($b3_new->updated_on), dt_from_string, "borrowers.updated_on should have been set to now on updating" );
81
82 my @patrons = Koha::Patrons->search( { branchcode => $branchcode } );
83 is( @patrons, 3, "Found 3 patrons with Search" );
84
85 my $unexistent = Koha::Patrons->find( '1234567890' );
86 is( $unexistent, undef, 'Koha::Objects->Find should return undef if the record does not exist' );
87
88 my $patrons = Koha::Patrons->search( { branchcode => $branchcode } );
89 is( $patrons->count( { branchcode => $branchcode } ), 3, "Counted 3 patrons with Count" );
90
91 my $b = $patrons->next();
92 is( $b->surname(), 'Test 1', "Next returns first patron" );
93 $b = $patrons->next();
94 is( $b->surname(), 'Test 2', "Next returns second patron" );
95 $b = $patrons->next();
96 is( $b->surname(), 'Test 3', "Next returns third patron" );
97 $b = $patrons->next();
98 is( $b, undef, "Next returns undef" );
99
100 # Test Reset and iteration in concert
101 $patrons->reset();
102 foreach my $b ( $patrons->as_list() ) {
103     is( $b->categorycode(), $categorycode, "Iteration returns a patron object" );
104 }
105
106 $schema->storage->txn_rollback();
107
108 1;