3 # Some tests for SIP::ILS::Patron
4 # This needs to be extended! Your help is appreciated..
7 use Test::More tests => 7;
10 use t::lib::TestBuilder;
12 use C4::SIP::ILS::Patron;
13 use Koha::Account::Lines;
16 use Koha::Patron::Attributes;
19 my $schema = Koha::Database->new->schema;
20 $schema->storage->txn_begin;
22 my $builder = t::lib::TestBuilder->new();
23 my $patron1 = $builder->build({ source => 'Borrower' });
24 my $card = $patron1->{cardnumber};
26 # Check existing card number
27 my $sip_patron = C4::SIP::ILS::Patron->new( $card );
28 is( defined $sip_patron, 1, "Patron is valid" );
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)" );
35 subtest "OverduesBlockCirc tests" => sub {
39 my $odue_patron = $builder->build(
43 dateexpiry => "3000-01-01",
47 my $good_patron = $builder->build(
51 dateexpiry => "3000-01-01",
55 my $odue = $builder->build({ source => 'Issue', value => {
56 borrowernumber => $odue_patron->{borrowernumber},
57 date_due => '2017-01-01',
60 t::lib::Mocks::mock_preference( 'OverduesBlockCirc', 'noblock' );
61 my $odue_sip_patron = C4::SIP::ILS::Patron->new( $odue_patron->{cardnumber} );
62 is( $odue_sip_patron->{charge_ok}, 1, "Not blocked with overdues when set to 'Don't block'");
63 $odue_sip_patron = C4::SIP::ILS::Patron->new( $good_patron->{cardnumber} );
64 is( $odue_sip_patron->{charge_ok}, 1, "Not blocked without overdues when set to 'Don't block'");
66 t::lib::Mocks::mock_preference( 'OverduesBlockCirc', 'confirmation' );
67 $odue_sip_patron = C4::SIP::ILS::Patron->new( $odue_patron->{cardnumber} );
68 is( $odue_sip_patron->{charge_ok}, '', "Blocked with overdues when set to 'Ask for confirmation'");
69 $odue_sip_patron = C4::SIP::ILS::Patron->new( $good_patron->{cardnumber} );
70 is( $odue_sip_patron->{charge_ok}, 1, "Not blocked without overdues when set to 'confirmation'");
72 t::lib::Mocks::mock_preference( 'OverduesBlockCirc', 'block' );
73 $odue_sip_patron = C4::SIP::ILS::Patron->new( $odue_patron->{cardnumber} );
74 is( $odue_sip_patron->{charge_ok}, '', "Blocked with overdues when set to 'Block'");
75 $odue_sip_patron = C4::SIP::ILS::Patron->new( $good_patron->{cardnumber} );
76 is( $odue_sip_patron->{charge_ok}, 1, "Not blocked without overdues when set to 'Block'");
80 subtest "Test build_patron_attribute_string" => sub {
84 my $patron = $builder->build( { source => 'Borrower' } );
86 my $attribute_type = $builder->build( { source => 'BorrowerAttributeType' } );
87 my $attribute = Koha::Patron::Attribute->new(
89 borrowernumber => $patron->{borrowernumber},
90 code => $attribute_type->{code},
91 attribute => 'Test Attribute'
95 my $attribute_type2 = $builder->build( { source => 'BorrowerAttributeType' } );
96 my $attribute2 = Koha::Patron::Attribute->new(
98 borrowernumber => $patron->{borrowernumber},
99 code => $attribute_type2->{code},
100 attribute => 'Another Test Attribute'
104 my $ils_patron = C4::SIP::ILS::Patron->new( $patron->{cardnumber} );
107 $server->{account}->{patron_attribute}->{code} = $attribute->code;
108 $server->{account}->{patron_attribute}->{field} = 'XY';
109 my $attribute_string = $ils_patron->build_patron_attributes_string( $server );
110 is( $attribute_string, "XYTest Attribute|", 'Attribute field generated correctly with single param' );
113 $server->{account}->{patron_attribute}->[0]->{code} = $attribute->code;
114 $server->{account}->{patron_attribute}->[0]->{field} = 'XY';
115 $server->{account}->{patron_attribute}->[1]->{code} = $attribute2->code;
116 $server->{account}->{patron_attribute}->[1]->{field} = 'YZ';
117 $attribute_string = $ils_patron->build_patron_attributes_string( $server );
118 is( $attribute_string, "XYTest Attribute|YZAnother Test Attribute|", 'Attribute field generated correctly with multiple params' );
121 subtest "Test build_custom_field_string" => sub {
125 my $patron = $builder->build_object( { class => 'Koha::Patrons',value=>{surname => "Duck", firstname => "Darkwing"} } );
128 my $ils_patron = C4::SIP::ILS::Patron->new( $patron->cardnumber );
131 $server->{account}->{custom_patron_field}->{field} = "DW";
132 my $attribute_string = $ils_patron->build_custom_field_string( $server );
133 is( $attribute_string, "", 'Custom field not generated if no value passed' );
136 $server->{account}->{custom_patron_field}->{template} = "[% patron.surname %]";
137 $attribute_string = $ils_patron->build_custom_field_string( $server );
138 is( $attribute_string, "", 'Custom field not generated if no field passed' );
142 $server->{account}->{custom_patron_field}->{field} = "DW";
143 $server->{account}->{custom_patron_field}->{template} = "[% patron.firstname %] [% patron.surname %], let's get dangerous!";
144 $attribute_string = $ils_patron->build_custom_field_string( $server );
145 is( $attribute_string, "DWDarkwing Duck, let's get dangerous!|", 'Custom field processed correctly' );
148 $server->{account}->{custom_patron_field}->[0]->{field} = "DW";
149 $server->{account}->{custom_patron_field}->[0]->{template} = "[% patron.firstname %] [% patron.surname %], let's get dangerous!";
150 $server->{account}->{custom_patron_field}->[1]->{field} = "LM";
151 $server->{account}->{custom_patron_field}->[1]->{template} = "Launchpad McQuack crashed on [% patron.dateexpiry %]";
152 $attribute_string = $ils_patron->build_custom_field_string( $server );
153 is( $attribute_string, "DWDarkwing Duck, let's get dangerous!|LMLaunchpad McQuack crashed on ".$patron->dateexpiry."|", 'Custom fields processed correctly when multiple exist' );
156 $server->{account}->{custom_patron_field}->[0]->{field} = "DW";
157 $server->{account}->{custom_patron_field}->[0]->{template} = "[% IF (patron.firstname) %] patron.surname, let's get dangerous!";
158 $server->{account}->{custom_patron_field}->[1]->{field} = "LM";
159 $server->{account}->{custom_patron_field}->[1]->{template} = "Launchpad McQuack crashed on [% patron.dateexpiry %]";
160 $attribute_string = $ils_patron->build_custom_field_string( $server );
161 is( $attribute_string, "LMLaunchpad McQuack crashed on ".$patron->dateexpiry."|", 'Custom fields processed correctly, bad template generate no text' );
165 subtest "update_lastseen tests" => sub {
168 my $seen_patron = $builder->build(
170 source => 'Borrower',
172 lastseen => "2001-01-01",
176 my $sip_patron = C4::SIP::ILS::Patron->new( $seen_patron->{cardnumber} );
177 t::lib::Mocks::mock_preference( 'TrackLastPatronActivity', '' );
178 $sip_patron->update_lastseen();
179 $seen_patron = Koha::Patrons->find({ cardnumber => $seen_patron->{cardnumber} });
180 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');
181 t::lib::Mocks::mock_preference( 'TrackLastPatronActivity', '1' );
182 $sip_patron->update_lastseen();
183 $seen_patron = Koha::Patrons->find({ cardnumber => $seen_patron->cardnumber() });
184 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');
187 subtest "fine_items tests" => sub {
191 my $patron = $builder->build(
193 source => 'Borrower',
197 my $fee1 = $builder->build(
199 source => 'Accountline',
201 borrowernumber => $patron->{borrowernumber},
202 amountoutstanding => 1,
207 my $fee2 = $builder->build(
209 source => 'Accountline',
211 borrowernumber => $patron->{borrowernumber},
212 amountoutstanding => 1,
217 my $sip_patron = C4::SIP::ILS::Patron->new( $patron->{cardnumber} );
219 my $all_fine_items = $sip_patron->fine_items;
220 is( @$all_fine_items, 2, "Got all fine items" );
222 # Should return only the first fine item
223 my $fine_items = $sip_patron->fine_items(1,1);
224 is( @$fine_items, 1, "Got one fine item" );
225 is( $fine_items->[0]->{barcode}, $all_fine_items->[0]->{barcode}, "Got correct fine item");
227 # Should return only the second fine item
228 $fine_items = $sip_patron->fine_items(2,2);
229 is( @$fine_items, 1, "Got one fine item" );
230 is( $fine_items->[0]->{barcode}, $all_fine_items->[1]->{barcode}, "Got correct fine item");
232 # Should return all fine items
233 $fine_items = $sip_patron->fine_items(1,2);
234 is( @$fine_items, 2, "Got two fine items" );
235 is( $fine_items->[0]->{barcode}, $all_fine_items->[0]->{barcode}, "Got correct first fine item");
236 is( $fine_items->[1]->{barcode}, $all_fine_items->[1]->{barcode}, "Got correct second fine item");
238 # Check an invalid end boundary
239 $fine_items = $sip_patron->fine_items(1,99);
240 is( @$fine_items, 2, "Got two fine items" );
241 is( $fine_items->[0]->{barcode}, $all_fine_items->[0]->{barcode}, "Got correct first fine item");
242 is( $fine_items->[1]->{barcode}, $all_fine_items->[1]->{barcode}, "Got correct second fine item");
244 # Check an invalid start boundary
245 $fine_items = $sip_patron->fine_items(98,99);
246 is( @$fine_items, 0, "Got zero fine items" );
249 $schema->storage->txn_rollback;