Bug 24446: Improve StockRotationItem->advance tests
[koha.git] / t / db_dependent / SIP / Patron.t
1 #!/usr/bin/perl
2
3 # Some tests for SIP::ILS::Patron
4 # This needs to be extended! Your help is appreciated..
5
6 use Modern::Perl;
7 use Test::More tests => 8;
8
9 use t::lib::Mocks;
10 use t::lib::TestBuilder;
11
12 use C4::SIP::ILS::Patron;
13 use Koha::Account::Lines;
14 use Koha::Database;
15 use Koha::DateUtils;
16 use Koha::Patron::Attributes;
17 use Koha::Patrons;
18
19 my $schema = Koha::Database->new->schema;
20 $schema->storage->txn_begin;
21
22 my $builder = t::lib::TestBuilder->new();
23 my $patron1 = $builder->build({ source => 'Borrower' });
24 my $card = $patron1->{cardnumber};
25
26 # Check existing card number
27 my $sip_patron = C4::SIP::ILS::Patron->new( $card );
28 is( defined $sip_patron, 1, "Patron is valid" );
29
30 # Check invalid cardnumber by deleting patron
31 $schema->resultset('Borrower')->search({ cardnumber => $card })->delete;
32 my $sip_patron2 = C4::SIP::ILS::Patron->new( $card );
33 is( $sip_patron2, undef, "Patron is not valid (anymore)" );
34
35 subtest "new tests" => sub {
36
37     plan tests => 5;
38
39     my $patron = $builder->build(
40         {
41             source => 'Borrower'
42         }
43     );
44
45     my $cardnumber      = $patron->{cardnumber};
46     my $userid         = $patron->{userid};
47     my $borrowernumber = $patron->{borrowernumber};
48
49     my $ils_patron = C4::SIP::ILS::Patron->new($cardnumber);
50     is( ref($ils_patron), 'C4::SIP::ILS::Patron', 'Found patron via cardnumber scalar' );
51     $ils_patron = C4::SIP::ILS::Patron->new($userid);
52     is( ref($ils_patron), 'C4::SIP::ILS::Patron', 'Found patron via userid scalar' );
53     $ils_patron = C4::SIP::ILS::Patron->new( { borrowernumber => $borrowernumber } );
54     is( ref($ils_patron), 'C4::SIP::ILS::Patron', 'Found patron via borrowernumber hashref' );
55     $ils_patron = C4::SIP::ILS::Patron->new( { cardnumber => $cardnumber } );
56     is( ref($ils_patron), 'C4::SIP::ILS::Patron', 'Found patron via cardnumber hashref' );
57     $ils_patron = C4::SIP::ILS::Patron->new( { userid => $userid } );
58     is( ref($ils_patron), 'C4::SIP::ILS::Patron', 'Found patron via userid hashref' );
59 };
60
61 subtest "OverduesBlockCirc tests" => sub {
62
63     plan tests => 6;
64
65     my $odue_patron = $builder->build(
66         {
67             source => 'Borrower',
68             value  => {
69                 dateexpiry    => "3000-01-01",
70             }
71         }
72     );
73     my $good_patron = $builder->build(
74         {
75             source => 'Borrower',
76             value  => {
77                 dateexpiry    => "3000-01-01",
78             }
79         }
80     );
81     my $odue = $builder->build({ source => 'Issue', value => {
82             borrowernumber => $odue_patron->{borrowernumber},
83             date_due => '2017-01-01',
84             }
85     });
86     t::lib::Mocks::mock_preference( 'OverduesBlockCirc', 'noblock' );
87     my $odue_sip_patron = C4::SIP::ILS::Patron->new( $odue_patron->{cardnumber} );
88     is( $odue_sip_patron->{charge_ok}, 1, "Not blocked with overdues when set to 'Don't block'");
89     $odue_sip_patron = C4::SIP::ILS::Patron->new( $good_patron->{cardnumber} );
90     is( $odue_sip_patron->{charge_ok}, 1, "Not blocked without overdues when set to 'Don't block'");
91
92     t::lib::Mocks::mock_preference( 'OverduesBlockCirc', 'confirmation' );
93     $odue_sip_patron = C4::SIP::ILS::Patron->new( $odue_patron->{cardnumber} );
94     is( $odue_sip_patron->{charge_ok}, '', "Blocked with overdues when set to 'Ask for confirmation'");
95     $odue_sip_patron = C4::SIP::ILS::Patron->new( $good_patron->{cardnumber} );
96     is( $odue_sip_patron->{charge_ok}, 1, "Not blocked without overdues when set to 'confirmation'");
97
98     t::lib::Mocks::mock_preference( 'OverduesBlockCirc', 'block' );
99     $odue_sip_patron = C4::SIP::ILS::Patron->new( $odue_patron->{cardnumber} );
100     is( $odue_sip_patron->{charge_ok}, '', "Blocked with overdues when set to 'Block'");
101     $odue_sip_patron = C4::SIP::ILS::Patron->new( $good_patron->{cardnumber} );
102     is( $odue_sip_patron->{charge_ok}, 1, "Not blocked without overdues when set to 'Block'");
103
104 };
105
106 subtest "Test build_patron_attribute_string" => sub {
107
108     plan tests => 2;
109
110     my $patron = $builder->build( { source => 'Borrower' } );
111
112     my $attribute_type = $builder->build( { source => 'BorrowerAttributeType' } );
113     my $attribute = Koha::Patron::Attribute->new(
114         {
115             borrowernumber => $patron->{borrowernumber},
116             code           => $attribute_type->{code},
117             attribute      => 'Test Attribute'
118         }
119     )->store();
120
121     my $attribute_type2 = $builder->build( { source => 'BorrowerAttributeType' } );
122     my $attribute2 = Koha::Patron::Attribute->new(
123         {
124             borrowernumber => $patron->{borrowernumber},
125             code           => $attribute_type2->{code},
126             attribute      => 'Another Test Attribute'
127         }
128     )->store();
129
130     my $ils_patron = C4::SIP::ILS::Patron->new( $patron->{cardnumber} );
131
132     my $server = {};
133     $server->{account}->{patron_attribute}->{code} = $attribute->code;
134     $server->{account}->{patron_attribute}->{field} = 'XY';
135     my $attribute_string = $ils_patron->build_patron_attributes_string( $server );
136     is( $attribute_string, "XYTest Attribute|", 'Attribute field generated correctly with single param' );
137
138     $server = {};
139     $server->{account}->{patron_attribute}->[0]->{code} = $attribute->code;
140     $server->{account}->{patron_attribute}->[0]->{field} = 'XY';
141     $server->{account}->{patron_attribute}->[1]->{code} = $attribute2->code;
142     $server->{account}->{patron_attribute}->[1]->{field} = 'YZ';
143     $attribute_string = $ils_patron->build_patron_attributes_string( $server );
144     is( $attribute_string, "XYTest Attribute|YZAnother Test Attribute|", 'Attribute field generated correctly with multiple params' );
145 };
146
147 subtest "Test build_custom_field_string" => sub {
148
149     plan tests => 5;
150
151     my $patron = $builder->build_object( { class => 'Koha::Patrons',value=>{surname => "Duck", firstname => "Darkwing"} } );
152
153
154     my $ils_patron = C4::SIP::ILS::Patron->new( $patron->cardnumber );
155
156     my $server = {};
157     $server->{account}->{custom_patron_field}->{field} = "DW";
158     my $attribute_string = $ils_patron->build_custom_field_string( $server );
159     is( $attribute_string, "", 'Custom field not generated if no value passed' );
160
161     $server = {};
162     $server->{account}->{custom_patron_field}->{template} = "[% patron.surname %]";
163     $attribute_string = $ils_patron->build_custom_field_string( $server );
164     is( $attribute_string, "", 'Custom field not generated if no field passed' );
165
166
167     $server = {};
168     $server->{account}->{custom_patron_field}->{field} = "DW";
169     $server->{account}->{custom_patron_field}->{template} = "[% patron.firstname %] [% patron.surname %], let's get dangerous!";
170     $attribute_string = $ils_patron->build_custom_field_string( $server );
171     is( $attribute_string, "DWDarkwing Duck, let's get dangerous!|", 'Custom field processed correctly' );
172
173     $server = {};
174     $server->{account}->{custom_patron_field}->[0]->{field} = "DW";
175     $server->{account}->{custom_patron_field}->[0]->{template} = "[% patron.firstname %] [% patron.surname %], let's get dangerous!";
176     $server->{account}->{custom_patron_field}->[1]->{field} = "LM";
177     $server->{account}->{custom_patron_field}->[1]->{template} = "Launchpad McQuack crashed on [% patron.dateexpiry %]";
178     $attribute_string = $ils_patron->build_custom_field_string( $server );
179     is( $attribute_string, "DWDarkwing Duck, let's get dangerous!|LMLaunchpad McQuack crashed on ".$patron->dateexpiry."|", 'Custom fields processed correctly when multiple exist' );
180
181     $server = {};
182     $server->{account}->{custom_patron_field}->[0]->{field} = "DW";
183     $server->{account}->{custom_patron_field}->[0]->{template} = "[% IF (patron.firstname) %] patron.surname, let's get dangerous!";
184     $server->{account}->{custom_patron_field}->[1]->{field} = "LM";
185     $server->{account}->{custom_patron_field}->[1]->{template} = "Launchpad McQuack crashed on [% patron.dateexpiry %]";
186     $attribute_string = $ils_patron->build_custom_field_string( $server );
187     is( $attribute_string, "LMLaunchpad McQuack crashed on ".$patron->dateexpiry."|", 'Custom fields processed correctly, bad template generate no text' );
188
189 };
190
191 subtest "update_lastseen tests" => sub {
192     plan tests => 2;
193
194     my $seen_patron = $builder->build(
195         {
196             source => 'Borrower',
197             value  => {
198                 lastseen    => "2001-01-01",
199             }
200         }
201     );
202     my $sip_patron = C4::SIP::ILS::Patron->new( $seen_patron->{cardnumber} );
203     t::lib::Mocks::mock_preference( 'TrackLastPatronActivity', '' );
204     $sip_patron->update_lastseen();
205     $seen_patron = Koha::Patrons->find({ cardnumber => $seen_patron->{cardnumber} });
206     is( output_pref({str => $seen_patron->lastseen(), dateonly => 1}), output_pref({str => '2001-01-01', dateonly => 1}),'Last seen not updated if not tracking patrons');
207     t::lib::Mocks::mock_preference( 'TrackLastPatronActivity', '1' );
208     $sip_patron->update_lastseen();
209     $seen_patron = Koha::Patrons->find({ cardnumber => $seen_patron->cardnumber() });
210     is( output_pref({str => $seen_patron->lastseen(), dateonly => 1}), output_pref({dt => dt_from_string(), dateonly => 1}),'Last seen updated to today if tracking patrons');
211 };
212
213 subtest "fine_items tests" => sub {
214
215     plan tests => 12;
216
217     my $patron = $builder->build(
218         {
219             source => 'Borrower',
220         }
221     );
222
223     my $fee1 = $builder->build(
224         {
225             source => 'Accountline',
226             value  => {
227                 borrowernumber => $patron->{borrowernumber},
228                 amountoutstanding => 1,
229             }
230         }
231     );
232
233     my $fee2 = $builder->build(
234         {
235             source => 'Accountline',
236             value  => {
237                 borrowernumber => $patron->{borrowernumber},
238                 amountoutstanding => 1,
239             }
240         }
241     );
242
243     my $sip_patron = C4::SIP::ILS::Patron->new( $patron->{cardnumber} );
244
245     my $all_fine_items = $sip_patron->fine_items;
246     is( @$all_fine_items, 2, "Got all fine items" );
247
248     # Should return only the first fine item
249     my $fine_items = $sip_patron->fine_items(1,1);
250     is( @$fine_items, 1, "Got one fine item" );
251     is( $fine_items->[0]->{barcode}, $all_fine_items->[0]->{barcode}, "Got correct fine item");
252
253     # Should return only the second fine item
254     $fine_items = $sip_patron->fine_items(2,2);
255     is( @$fine_items, 1, "Got one fine item" );
256     is( $fine_items->[0]->{barcode}, $all_fine_items->[1]->{barcode}, "Got correct fine item");
257
258     # Should return all fine items
259     $fine_items = $sip_patron->fine_items(1,2);
260     is( @$fine_items, 2, "Got two fine items" );
261     is( $fine_items->[0]->{barcode}, $all_fine_items->[0]->{barcode}, "Got correct first fine item");
262     is( $fine_items->[1]->{barcode}, $all_fine_items->[1]->{barcode}, "Got correct second fine item");
263
264     # Check an invalid end boundary
265     $fine_items = $sip_patron->fine_items(1,99);
266     is( @$fine_items, 2, "Got two fine items" );
267     is( $fine_items->[0]->{barcode}, $all_fine_items->[0]->{barcode}, "Got correct first fine item");
268     is( $fine_items->[1]->{barcode}, $all_fine_items->[1]->{barcode}, "Got correct second fine item");
269
270     # Check an invalid start boundary
271     $fine_items = $sip_patron->fine_items(98,99);
272     is( @$fine_items, 0, "Got zero fine items" );
273 };
274
275 $schema->storage->txn_rollback;