Bug 17168: Add a command line script for updating patron category based on status
[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
30 BEGIN {
31     use_ok('Koha::Objects');
32     use_ok('Koha::Patrons');
33 }
34
35 # Start transaction
36 my $database = Koha::Database->new();
37 my $schema = $database->schema();
38 $schema->storage->txn_begin();
39 my $builder = t::lib::TestBuilder->new;
40
41 my $categorycode = $builder->build({ source => 'Category' })->{categorycode};
42 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
43
44 my $b1 = Koha::Patron->new(
45     {
46         surname      => 'Test 1',
47         branchcode   => $branchcode,
48         categorycode => $categorycode
49     }
50 );
51 $b1->store();
52 my $now = dt_from_string;
53 my $b2 = Koha::Patron->new(
54     {
55         surname      => 'Test 2',
56         branchcode   => $branchcode,
57         categorycode => $categorycode
58     }
59 );
60 $b2->store();
61 my $three_days_ago = dt_from_string->add( days => -3 );
62 my $b3 = Koha::Patron->new(
63     {
64         surname      => 'Test 3',
65         branchcode   => $branchcode,
66         categorycode => $categorycode,
67         updated_on   => $three_days_ago,
68     }
69 );
70 $b3->store();
71
72 my $b1_new = Koha::Patrons->find( $b1->borrowernumber() );
73 is( $b1->surname(), $b1_new->surname(), "Found matching patron" );
74 isnt( $b1_new->updated_on, undef, "borrowers.updated_on should be set" );
75 is( t::lib::Dates::compare( $b1_new->updated_on, $now), 0, "borrowers.updated_on should have been set to now on creating" );
76
77 my $b3_new = Koha::Patrons->find( $b3->borrowernumber() );
78 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" );
79 $b3_new->set({ firstname => 'Some first name for Test 3' })->store();
80 $b3_new = Koha::Patrons->find( $b3->borrowernumber() );
81 is( t::lib::Dates::compare( $b3_new->updated_on, $now), 0, "borrowers.updated_on should have been set to now on updating" );
82
83 my @patrons = Koha::Patrons->search( { branchcode => $branchcode } );
84 is( @patrons, 3, "Found 3 patrons with Search" );
85
86 my $unexistent = Koha::Patrons->find( '1234567890' );
87 is( $unexistent, undef, 'Koha::Objects->Find should return undef if the record does not exist' );
88
89 my $patrons = Koha::Patrons->search( { branchcode => $branchcode } );
90 is( $patrons->count( { branchcode => $branchcode } ), 3, "Counted 3 patrons with Count" );
91
92 my $b = $patrons->next();
93 is( $b->surname(), 'Test 1', "Next returns first patron" );
94 $b = $patrons->next();
95 is( $b->surname(), 'Test 2', "Next returns second patron" );
96 $b = $patrons->next();
97 is( $b->surname(), 'Test 3', "Next returns third patron" );
98 $b = $patrons->next();
99 is( $b, undef, "Next returns undef" );
100
101 # Test Reset and iteration in concert
102 $patrons->reset();
103 foreach my $b ( $patrons->as_list() ) {
104     is( $b->categorycode(), $categorycode, "Iteration returns a patron object" );
105 }
106
107 subtest "Update patron categories" => sub {
108     plan tests => 23;
109     $builder->schema->resultset( 'Issue' )->delete_all;
110     $builder->schema->resultset( 'Borrower' )->delete_all;
111     $builder->schema->resultset( 'Category' )->delete_all;
112     my $c_categorycode = $builder->build({ source => 'Category', value => {
113             category_type=>'C',
114             upperagelimit=>17,
115             dateofbirthrequired=>5,
116         } })->{categorycode};
117     my $a_categorycode = $builder->build({ source => 'Category', value => {category_type=>'A'} })->{categorycode};
118     my $p_categorycode = $builder->build({ source => 'Category', value => {category_type=>'P'} })->{categorycode};
119     my $i_categorycode = $builder->build({ source => 'Category', value => {category_type=>'I'} })->{categorycode};
120     my $branchcode1 = $builder->build({ source => 'Branch' })->{branchcode};
121     my $branchcode2 = $builder->build({ source => 'Branch' })->{branchcode};
122     my $adult1 = $builder->build({source => 'Borrower', value => {
123             categorycode=>$a_categorycode,
124             branchcode=>$branchcode1,
125             dateenrolled=>'2018-01-01',
126             sort1 =>'quack',
127         }
128     });
129     my $adult2 = $builder->build({source => 'Borrower', value => {
130             categorycode=>$a_categorycode,
131             branchcode=>$branchcode2,
132             dateenrolled=>'2017-01-01',
133         }
134     });
135     my $inst = $builder->build({source => 'Borrower', value => {
136             categorycode=>$i_categorycode,
137             branchcode=>$branchcode2,
138         }
139     });
140     my $prof = $builder->build({source => 'Borrower', value => {
141             categorycode=>$p_categorycode,
142             branchcode=>$branchcode2,
143             guarantorid=>$inst->{borrowernumber},
144         }
145     });
146     my $child1 = $builder->build({source => 'Borrower', value => {
147             dateofbirth => dt_from_string->add(years=>-4),
148             categorycode=>$c_categorycode,
149             guarantorid=>$adult1->{borrowernumber},
150             branchcode=>$branchcode1,
151         }
152     });
153     my $child2 = $builder->build({source => 'Borrower', value => {
154             dateofbirth => dt_from_string->add(years=>-8),
155             categorycode=>$c_categorycode,
156             guarantorid=>$adult1->{borrowernumber},
157             branchcode=>$branchcode1,
158         }
159     });
160     my $child3 = $builder->build({source => 'Borrower', value => {
161             dateofbirth => dt_from_string->add(years=>-18),
162             categorycode=>$c_categorycode,
163             guarantorid=>$adult1->{borrowernumber},
164             branchcode=>$branchcode1,
165         }
166     });
167     $builder->build({source=>'Accountline',value => {amountoutstanding=>4.99,borrowernumber=>$adult1->{borrowernumber}}});
168     $builder->build({source=>'Accountline',value => {amountoutstanding=>5.01,borrowernumber=>$adult2->{borrowernumber}}});
169
170     is( Koha::Patrons->search_patrons_to_update({from=>$c_categorycode})->count,3,'Three patrons in child category');
171     is( Koha::Patrons->search_patrons_to_update({from=>$c_categorycode,au=>1})->count,1,'One under age patron in child category');
172     is( Koha::Patrons->search_patrons_to_update({from=>$c_categorycode,au=>1})->next->borrowernumber,$child1->{borrowernumber},'Under age patron in child category is expected one');
173     is( Koha::Patrons->search_patrons_to_update({from=>$c_categorycode,ao=>1})->count,1,'One over age patron in child category');
174     is( Koha::Patrons->search_patrons_to_update({from=>$c_categorycode,ao=>1})->next->borrowernumber,$child3->{borrowernumber},'Over age patron in child category is expected one');
175     is( Koha::Patrons->search_patrons_to_update({from=>$a_categorycode,search_params=>{branchcode=>$branchcode2}})->count,1,'One patron in branch 2');
176     is( Koha::Patrons->search_patrons_to_update({from=>$a_categorycode,search_params=>{branchcode=>$branchcode2}})->next->borrowernumber,$adult2->{borrowernumber},'Adult patron in branch 2 is expected one');
177     is( Koha::Patrons->search_patrons_to_update({from=>$a_categorycode,fine_min=>5})->count,1,'One patron with fines over $5');
178     is( Koha::Patrons->search_patrons_to_update({from=>$a_categorycode,fine_min=>5})->next->borrowernumber,$adult2->{borrowernumber},'One patron with fines over $5 is expected one');
179     is( Koha::Patrons->search_patrons_to_update({from=>$a_categorycode,fine_max=>5})->count,1,'One patron with fines under $5');
180     is( Koha::Patrons->search_patrons_to_update({from=>$a_categorycode,fine_max=>5})->next->borrowernumber,$adult1->{borrowernumber},'One patron with fines under $5 is expected one');
181
182     is( Koha::Patrons->search_patrons_to_update({from=>$a_categorycode,search_params=>{dateenrolled=>{'<'=>'2018-01-01'}}})->count,1,'One adult patron enrolled before date');
183     is( Koha::Patrons->search_patrons_to_update({from=>$a_categorycode,search_params=>{dateenrolled=>{'<'=>'2018-01-01'}}})->next->borrowernumber,$adult2->{borrowernumber},'One adult patron enrolled before date is expected one');
184     is( Koha::Patrons->search_patrons_to_update({from=>$a_categorycode,search_params=>{dateenrolled=>{'>'=>'2017-01-01'}}})->count,1,'One adult patron enrolled after date');
185     is( Koha::Patrons->search_patrons_to_update({from=>$a_categorycode,search_params=>{dateenrolled=>{'>'=>'2017-01-01'}}})->next->borrowernumber,$adult1->{borrowernumber},'One adult patron enrolled after date is expected one');
186     is( Koha::Patrons->search_patrons_to_update({from=>$a_categorycode,search_params=>{'sort1'=>'quack'}})->count,1,'One adult patron has a quack');
187     is( Koha::Patrons->search_patrons_to_update({from=>$a_categorycode,search_params=>{'sort1'=>'quack'}})->next->borrowernumber,$adult1->{borrowernumber},'One adult patron with a quack is expected one');
188
189     is( Koha::Patrons->find($adult1->{borrowernumber})->guarantees->count,3,'Guarantor has 3 guarantees');
190     is( Koha::Patrons->search_patrons_to_update({from=>$c_categorycode,au=>1})->update_category({to=>$a_categorycode}),1,'One child patron updated to adult category');
191     is( Koha::Patrons->find($adult1->{borrowernumber})->guarantees->count,2,'Guarantee was removed when made adult');
192
193     is( Koha::Patrons->find($inst->{borrowernumber})->guarantees->count,1,'Guarantor has 1 guarantees');
194     is( Koha::Patrons->search_patrons_to_update({from=>$p_categorycode})->update_category({to=>$a_categorycode}),1,'One professional patron updated to adult category');
195     is( Koha::Patrons->find($inst->{borrowernumber})->guarantees->count,0,'Guarantee was removed when made adult');
196
197 };
198
199
200
201 $schema->storage->txn_rollback();
202