Bug 35086: Add chunk_size option to elasticsearch configuration
[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 qw( dt_from_string );
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 } )->as_list;
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 => 26;
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             can_be_guarantee=>1,
116         } })->{categorycode};
117     my $c_categorycode_2 = $builder->build({ source => 'Category', value => {
118             category_type=>'C',
119             upperagelimit=>17,
120             dateofbirthrequired=>5,
121             can_be_guarantee=>1,
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',
133             sort1 =>'quack',
134         }
135     });
136     my $adult2 = $builder->build_object({class => 'Koha::Patrons', value => {
137             categorycode=>$a_categorycode,
138             branchcode=>$branchcode2,
139             dateenrolled=>'2017-01-01',
140         }
141     });
142     my $inst = $builder->build_object({class => 'Koha::Patrons', value => {
143             categorycode=>$i_categorycode,
144             branchcode=>$branchcode2,
145         }
146     });
147     my $prof = $builder->build_object({class => 'Koha::Patrons', value => {
148             categorycode=>$p_categorycode,
149             branchcode=>$branchcode2,
150         }
151     });
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,
157         }
158     });
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,
164         }
165     });
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,
171         }
172     });
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}});
176
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');
188
189     my $adult3 = $builder->build_object({class => 'Koha::Patrons', value => {
190             categorycode=>$a_categorycode,
191             branchcode=>$branchcode1,
192         }
193     });
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');
195
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');
198
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');
204
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');
210
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');
214
215 };
216
217
218
219 $schema->storage->txn_rollback();
220