Bug 27993: Add unit tests
[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 => 18;
21 use Test::Warn;
22
23 use C4::Context;
24 use Koha::Database;
25 use Koha::DateUtils;
26
27 use t::lib::Dates;
28 use t::lib::TestBuilder;
29 use t::lib::Mocks;
30
31 BEGIN {
32     use_ok('Koha::Objects');
33     use_ok('Koha::Patrons');
34 }
35
36 # Start transaction
37 my $database = Koha::Database->new();
38 my $schema = $database->schema();
39 $schema->storage->txn_begin();
40 my $builder = t::lib::TestBuilder->new;
41
42 my $categorycode = $builder->build({ source => 'Category' })->{categorycode};
43 my $branchcode = $builder->build({ source => 'Branch' })->{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 $now = dt_from_string;
54 my $b2 = Koha::Patron->new(
55     {
56         surname      => 'Test 2',
57         branchcode   => $branchcode,
58         categorycode => $categorycode
59     }
60 );
61 $b2->store();
62 my $three_days_ago = dt_from_string->add( days => -3 );
63 my $b3 = Koha::Patron->new(
64     {
65         surname      => 'Test 3',
66         branchcode   => $branchcode,
67         categorycode => $categorycode,
68         updated_on   => $three_days_ago,
69     }
70 );
71 $b3->store();
72
73 my $b1_new = Koha::Patrons->find( $b1->borrowernumber() );
74 is( $b1->surname(), $b1_new->surname(), "Found matching patron" );
75 isnt( $b1_new->updated_on, undef, "borrowers.updated_on should be set" );
76 is( t::lib::Dates::compare( $b1_new->updated_on, $now), 0, "borrowers.updated_on should have been set to now on creating" );
77
78 my $b3_new = Koha::Patrons->find( $b3->borrowernumber() );
79 is( t::lib::Dates::compare( $b3_new->updated_on, $three_days_ago), 0, "borrowers.updated_on should have been kept to what we set on creating" );
80 $b3_new->set({ firstname => 'Some first name for Test 3' })->store();
81 $b3_new = Koha::Patrons->find( $b3->borrowernumber() );
82 is( t::lib::Dates::compare( $b3_new->updated_on, $now), 0, "borrowers.updated_on should have been set to now on updating" );
83
84 my @patrons = Koha::Patrons->search( { branchcode => $branchcode } );
85 is( @patrons, 3, "Found 3 patrons with Search" );
86
87 my $unexistent = Koha::Patrons->find( '1234567890' );
88 is( $unexistent, undef, 'Koha::Objects->Find should return undef if the record does not exist' );
89
90 my $patrons = Koha::Patrons->search( { branchcode => $branchcode } );
91 is( $patrons->count( { branchcode => $branchcode } ), 3, "Counted 3 patrons with Count" );
92
93 my $b = $patrons->next();
94 is( $b->surname(), 'Test 1', "Next returns first patron" );
95 $b = $patrons->next();
96 is( $b->surname(), 'Test 2', "Next returns second patron" );
97 $b = $patrons->next();
98 is( $b->surname(), 'Test 3', "Next returns third patron" );
99 $b = $patrons->next();
100 is( $b, undef, "Next returns undef" );
101
102 # Test Reset and iteration in concert
103 $patrons->reset();
104 foreach my $b ( $patrons->as_list() ) {
105     is( $b->categorycode(), $categorycode, "Iteration returns a patron object" );
106 }
107
108 subtest "Update patron categories" => sub {
109     plan tests => 24;
110     t::lib::Mocks::mock_preference( 'borrowerRelationship', 'test' );
111     my $c_categorycode = $builder->build({ source => 'Category', value => {
112             category_type=>'C',
113             upperagelimit=>17,
114             dateofbirthrequired=>5,
115         } })->{categorycode};
116     my $c_categorycode_2 = $builder->build({ source => 'Category', value => {
117             category_type=>'C',
118             upperagelimit=>17,
119             dateofbirthrequired=>5,
120         } })->{categorycode};
121     my $a_categorycode = $builder->build({ source => 'Category', value => {category_type=>'A'} })->{categorycode};
122     my $p_categorycode = $builder->build({ source => 'Category', value => {category_type=>'P'} })->{categorycode};
123     my $i_categorycode = $builder->build({ source => 'Category', value => {category_type=>'I'} })->{categorycode};
124     my $branchcode1 = $builder->build({ source => 'Branch' })->{branchcode};
125     my $branchcode2 = $builder->build({ source => 'Branch' })->{branchcode};
126     my $adult1 = $builder->build_object({class => 'Koha::Patrons', value => {
127             categorycode=>$a_categorycode,
128             branchcode=>$branchcode1,
129             dateenrolled=>'2018-01-01',
130             sort1 =>'quack',
131         }
132     });
133     my $adult2 = $builder->build_object({class => 'Koha::Patrons', value => {
134             categorycode=>$a_categorycode,
135             branchcode=>$branchcode2,
136             dateenrolled=>'2017-01-01',
137         }
138     });
139     my $inst = $builder->build_object({class => 'Koha::Patrons', value => {
140             categorycode=>$i_categorycode,
141             branchcode=>$branchcode2,
142         }
143     });
144     my $prof = $builder->build_object({class => 'Koha::Patrons', value => {
145             categorycode=>$p_categorycode,
146             branchcode=>$branchcode2,
147         }
148     });
149     $prof->add_guarantor({guarantor_id => $inst->borrowernumber, relationship => 'test'});
150     my $child1 = $builder->build_object({class => 'Koha::Patrons', value => {
151             dateofbirth => dt_from_string->add(years=>-4),
152             categorycode=>$c_categorycode,
153             branchcode=>$branchcode1,
154         }
155     });
156     $child1->add_guarantor({guarantor_id => $adult1->borrowernumber, relationship => 'test'});
157     my $child2 = $builder->build_object({class => 'Koha::Patrons', value => {
158             dateofbirth => dt_from_string->add(years=>-8),
159             categorycode=>$c_categorycode,
160             branchcode=>$branchcode1,
161         }
162     });
163     $child2->add_guarantor({guarantor_id => $adult1->borrowernumber, relationship => 'test'});
164     my $child3 = $builder->build_object({class => 'Koha::Patrons', value => {
165             dateofbirth => dt_from_string->add(years=>-18),
166             categorycode=>$c_categorycode,
167             branchcode=>$branchcode1,
168         }
169     });
170     $child3->add_guarantor({guarantor_id => $adult1->borrowernumber, relationship => 'test'});
171     $builder->build({source=>'Accountline',value => {amountoutstanding=>4.99,borrowernumber=>$adult1->borrowernumber}});
172     $builder->build({source=>'Accountline',value => {amountoutstanding=>5.01,borrowernumber=>$adult2->borrowernumber}});
173
174     is( Koha::Patrons->search_patrons_to_update_category({from=>$c_categorycode})->count,3,'Three patrons in child category');
175     is( Koha::Patrons->search_patrons_to_update_category({from=>$c_categorycode,too_young=>1})->count,1,'One under age patron in child category');
176     is( Koha::Patrons->search_patrons_to_update_category({from=>$c_categorycode,too_young=>1})->next->borrowernumber,$child1->borrowernumber,'Under age patron in child category is expected one');
177     is( Koha::Patrons->search_patrons_to_update_category({from=>$c_categorycode,too_old=>1})->count,1,'One over age patron in child category');
178     is( Koha::Patrons->search_patrons_to_update_category({from=>$c_categorycode,too_old=>1})->next->borrowernumber,$child3->borrowernumber,'Over age patron in child category is expected one');
179     is( Koha::Patrons->search({branchcode=>$branchcode2})->search_patrons_to_update_category({from=>$a_categorycode})->count,1,'One patron in branch 2');
180     is( Koha::Patrons->search({branchcode=>$branchcode2})->search_patrons_to_update_category({from=>$a_categorycode})->next->borrowernumber,$adult2->borrowernumber,'Adult patron in branch 2 is expected one');
181     is( Koha::Patrons->search_patrons_to_update_category({from=>$a_categorycode,fine_min=>5})->count,1,'One patron with fines over $5');
182     is( Koha::Patrons->search_patrons_to_update_category({from=>$a_categorycode,fine_min=>5})->next->borrowernumber,$adult2->borrowernumber,'One patron with fines over $5 is expected one');
183     is( Koha::Patrons->search_patrons_to_update_category({from=>$a_categorycode,fine_max=>5})->count,1,'One patron with fines under $5');
184     is( Koha::Patrons->search_patrons_to_update_category({from=>$a_categorycode,fine_max=>5})->next->borrowernumber,$adult1->borrowernumber,'One patron with fines under $5 is expected one');
185
186     my $adult3 = $builder->build_object({class => 'Koha::Patrons', value => {
187             categorycode=>$a_categorycode,
188             branchcode=>$branchcode1,
189         }
190     });
191     is( Koha::Patrons->search_patrons_to_update_category({from=>$a_categorycode,fine_max=>5})->count,2,'Two patrons with fines under $5, patron with no fine history is found');
192
193     is( Koha::Patrons->find($adult1->borrowernumber)->guarantee_relationships->guarantees->count,3,'Guarantor has 3 guarantees');
194     is( Koha::Patrons->search_patrons_to_update_category({from=>$c_categorycode})->update_category_to({category=>$c_categorycode_2}),3,'Three child patrons updated to another child category with no params passed');
195
196     # FIXME - The script currently allows you to move patrons to a category that is invalid, this test confirms the current behaviour
197     is( Koha::Patrons->find($adult1->borrowernumber)->guarantee_relationships->guarantees->count,3,'Guarantees not removed when made changing child categories');
198     is( Koha::Patrons->search_patrons_to_update_category({from=>$c_categorycode_2,too_young=>1})->next->borrowernumber, $child1->borrowernumber );
199     is( Koha::Patrons->search_patrons_to_update_category({from=>$c_categorycode_2,too_young=>1})->update_category_to({category=>$a_categorycode}),1,'One child patron updated to adult category because too young');
200     is( Koha::Patrons->find($adult1->borrowernumber)->guarantee_relationships->guarantees->count,2,'Guarantee was removed when made adult');
201
202     is( Koha::Patrons->search_patrons_to_update_category({from=>$c_categorycode_2,too_old=>1})->next->borrowernumber, $child3->borrowernumber );
203     is( Koha::Patrons->search_patrons_to_update_category({from=>$c_categorycode_2,too_old=>1})->update_category_to({category=>$a_categorycode}),1,'One child patron updated to adult category because too old');
204     is( Koha::Patrons->find($adult1->borrowernumber)->guarantee_relationships->guarantees->count,1,'Guarantee was removed when made adult');
205
206     is( Koha::Patrons->find($inst->borrowernumber)->guarantee_relationships->guarantees->count,1,'Guarantor has 1 guarantees');
207     is( Koha::Patrons->search_patrons_to_update_category({from=>$p_categorycode})->update_category_to({category=>$a_categorycode}),1,'One professional patron updated to adult category');
208     is( Koha::Patrons->find($inst->borrowernumber)->guarantee_relationships->guarantees->count,0,'Guarantee was removed when made adult');
209
210 };
211
212
213
214 $schema->storage->txn_rollback();
215