3 # This file is part of Koha.
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.
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.
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>.
20 use Test::More tests => 18;
25 use Koha::DateUtils qw( dt_from_string );
28 use t::lib::TestBuilder;
32 use_ok('Koha::Objects');
33 use_ok('Koha::Patrons');
37 my $database = Koha::Database->new();
38 my $schema = $database->schema();
39 $schema->storage->txn_begin();
40 my $builder = t::lib::TestBuilder->new;
42 my $categorycode = $builder->build({ source => 'Category' })->{categorycode};
43 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
45 my $b1 = Koha::Patron->new(
48 branchcode => $branchcode,
49 categorycode => $categorycode
53 my $now = dt_from_string;
54 my $b2 = Koha::Patron->new(
57 branchcode => $branchcode,
58 categorycode => $categorycode
62 my $three_days_ago = dt_from_string->add( days => -3 );
63 my $b3 = Koha::Patron->new(
66 branchcode => $branchcode,
67 categorycode => $categorycode,
68 updated_on => $three_days_ago,
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" );
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" );
84 my @patrons = Koha::Patrons->search( { branchcode => $branchcode } )->as_list;
85 is( @patrons, 3, "Found 3 patrons with Search" );
87 my $unexistent = Koha::Patrons->find( '1234567890' );
88 is( $unexistent, undef, 'Koha::Objects->Find should return undef if the record does not exist' );
90 my $patrons = Koha::Patrons->search( { branchcode => $branchcode } );
91 is( $patrons->count( { branchcode => $branchcode } ), 3, "Counted 3 patrons with Count" );
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" );
102 # Test Reset and iteration in concert
104 foreach my $b ( $patrons->as_list() ) {
105 is( $b->categorycode(), $categorycode, "Iteration returns a patron object" );
108 subtest "Update patron categories" => sub {
110 t::lib::Mocks::mock_preference( 'borrowerRelationship', 'test' );
111 my $c_categorycode = $builder->build({ source => 'Category', value => {
114 dateofbirthrequired=>5,
116 } })->{categorycode};
117 my $c_categorycode_2 = $builder->build({ source => 'Category', value => {
120 dateofbirthrequired=>5,
122 } })->{categorycode};
123 my $a_categorycode = $builder->build({ source => 'Category', value => {category_type=>'A', can_be_guarantee=>0} })->{categorycode};
124 my $a_categorycode_2 = $builder->build({ source => 'Category', value => {category_type=>'A', can_be_guarantee=>1} })->{categorycode};
125 my $p_categorycode = $builder->build({ source => 'Category', value => {category_type=>'P'} })->{categorycode};
126 my $i_categorycode = $builder->build({ source => 'Category', value => {category_type=>'I'} })->{categorycode};
127 my $branchcode1 = $builder->build({ source => 'Branch' })->{branchcode};
128 my $branchcode2 = $builder->build({ source => 'Branch' })->{branchcode};
129 my $adult1 = $builder->build_object({class => 'Koha::Patrons', value => {
130 categorycode=>$a_categorycode,
131 branchcode=>$branchcode1,
132 dateenrolled=>'2018-01-01',
136 my $adult2 = $builder->build_object({class => 'Koha::Patrons', value => {
137 categorycode=>$a_categorycode,
138 branchcode=>$branchcode2,
139 dateenrolled=>'2017-01-01',
142 my $inst = $builder->build_object({class => 'Koha::Patrons', value => {
143 categorycode=>$i_categorycode,
144 branchcode=>$branchcode2,
147 my $prof = $builder->build_object({class => 'Koha::Patrons', value => {
148 categorycode=>$p_categorycode,
149 branchcode=>$branchcode2,
152 $prof->add_guarantor({guarantor_id => $inst->borrowernumber, relationship => 'test'});
153 my $child1 = $builder->build_object({class => 'Koha::Patrons', value => {
154 dateofbirth => dt_from_string->add(years=>-4),
155 categorycode=>$c_categorycode,
156 branchcode=>$branchcode1,
159 $child1->add_guarantor({guarantor_id => $adult1->borrowernumber, relationship => 'test'});
160 my $child2 = $builder->build_object({class => 'Koha::Patrons', value => {
161 dateofbirth => dt_from_string->add(years=>-8),
162 categorycode=>$c_categorycode,
163 branchcode=>$branchcode1,
166 $child2->add_guarantor({guarantor_id => $adult1->borrowernumber, relationship => 'test'});
167 my $child3 = $builder->build_object({class => 'Koha::Patrons', value => {
168 dateofbirth => dt_from_string->add(years=>-18),
169 categorycode=>$c_categorycode,
170 branchcode=>$branchcode1,
173 $child3->add_guarantor({guarantor_id => $adult1->borrowernumber, relationship => 'test'});
174 $builder->build({source=>'Accountline',value => {amountoutstanding=>4.99,borrowernumber=>$adult1->borrowernumber}});
175 $builder->build({source=>'Accountline',value => {amountoutstanding=>5.01,borrowernumber=>$adult2->borrowernumber}});
177 is( Koha::Patrons->search_patrons_to_update_category({from=>$c_categorycode})->count,3,'Three patrons in child category');
178 is( Koha::Patrons->search_patrons_to_update_category({from=>$c_categorycode,too_young=>1})->count,1,'One under age patron in child category');
179 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');
180 is( Koha::Patrons->search_patrons_to_update_category({from=>$c_categorycode,too_old=>1})->count,1,'One over age patron in child category');
181 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');
182 is( Koha::Patrons->search({branchcode=>$branchcode2})->search_patrons_to_update_category({from=>$a_categorycode})->count,1,'One patron in branch 2');
183 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');
184 is( Koha::Patrons->search_patrons_to_update_category({from=>$a_categorycode,fine_min=>5})->count,1,'One patron with fines over $5');
185 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');
186 is( Koha::Patrons->search_patrons_to_update_category({from=>$a_categorycode,fine_max=>5})->count,1,'One patron with fines under $5');
187 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');
189 my $adult3 = $builder->build_object({class => 'Koha::Patrons', value => {
190 categorycode=>$a_categorycode,
191 branchcode=>$branchcode1,
194 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');
196 is( Koha::Patrons->find($adult1->borrowernumber)->guarantee_relationships->guarantees->count,3,'Guarantor has 3 guarantees');
197 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');
199 # FIXME - The script currently allows you to move patrons to a category that is invalid, this test confirms the current behaviour
200 is( Koha::Patrons->find($adult1->borrowernumber)->guarantee_relationships->guarantees->count,3,'Guarantees not removed when made changing child categories');
201 is( Koha::Patrons->search_patrons_to_update_category({from=>$c_categorycode_2,too_young=>1})->next->borrowernumber, $child1->borrowernumber );
202 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');
203 is( Koha::Patrons->find($adult1->borrowernumber)->guarantee_relationships->guarantees->count,2,'Guarantee was removed when made adult');
205 is( Koha::Patrons->search_patrons_to_update_category({from=>$c_categorycode_2,too_old=>1})->next->borrowernumber, $child3->borrowernumber );
206 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');
207 is( Koha::Patrons->find($adult1->borrowernumber)->guarantee_relationships->guarantees->count,1,'Guarantee was removed when made adult');
208 is( Koha::Patrons->search_patrons_to_update_category({from=>$c_categorycode_2})->update_category_to({category=>$a_categorycode_2}),1,'Two child patrons updated to adult category');
209 is( Koha::Patrons->find($adult1->borrowernumber)->guarantee_relationships->guarantees->count,1,'Guarantees were not removed when made adult which can be guarantee');
211 is( Koha::Patrons->find($inst->borrowernumber)->guarantee_relationships->guarantees->count,1,'Guarantor has 1 guarantees');
212 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');
213 is( Koha::Patrons->find($inst->borrowernumber)->guarantee_relationships->guarantees->count,0,'Guarantee was removed when made adult');
216 Koha::Patrons->search_patrons_to_update_category( { from => $a_categorycode, fine_max => 5 } )
217 ->search( { "me.borrowernumber" => $adult1->borrowernumber } )->next->borrowernumber,
218 $adult1->borrowernumber, 'Expected patron is in the list of patrons to update'
220 my $userid_before = $adult1->userid;
222 Koha::Patrons->search_patrons_to_update_category( { from => $a_categorycode, fine_max => 5 } )
223 ->update_category_to( { category => $a_categorycode_2 } ),
224 'Update of patrons is successful including adult1'
226 $adult1->discard_changes;
227 is( $adult1->userid, $userid_before, "Patron userid not changed by updated" );
232 $schema->storage->txn_rollback();