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;
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 } );
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,
115 } })->{categorycode
};
116 my $c_categorycode_2 = $builder->build({ source
=> 'Category', value
=> {
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',
133 my $adult2 = $builder->build_object({class => 'Koha::Patrons', value
=> {
134 categorycode
=>$a_categorycode,
135 branchcode
=>$branchcode2,
136 dateenrolled
=>'2017-01-01',
139 my $inst = $builder->build_object({class => 'Koha::Patrons', value
=> {
140 categorycode
=>$i_categorycode,
141 branchcode
=>$branchcode2,
144 my $prof = $builder->build_object({class => 'Koha::Patrons', value
=> {
145 categorycode
=>$p_categorycode,
146 branchcode
=>$branchcode2,
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,
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,
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,
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}});
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');
186 is
( Koha
::Patrons
->find($adult1->borrowernumber)->guarantee_relationships->guarantees->count,3,'Guarantor has 3 guarantees');
187 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');
188 is
( Koha
::Patrons
->find($adult1->borrowernumber)->guarantee_relationships->guarantees->count,3,'Guarantees not removed when made changing child categories');
189 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');
190 is
( Koha
::Patrons
->find($adult1->borrowernumber)->guarantee_relationships->guarantees->count,2,'Guarantee was removed when made adult');
192 is
( Koha
::Patrons
->find($inst->borrowernumber)->guarantee_relationships->guarantees->count,1,'Guarantor has 1 guarantees');
193 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');
194 is
( Koha
::Patrons
->find($inst->borrowernumber)->guarantee_relationships->guarantees->count,0,'Guarantee was removed when made adult');
200 $schema->storage->txn_rollback();