]> git.koha-community.org Git - koha.git/blob - t/db_dependent/SIP/Patron.t
Bug 27204: Add unit tests for fine_items
[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 => 7;
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 "OverduesBlockCirc tests" => sub {
36
37     plan tests => 6;
38
39     my $odue_patron = $builder->build(
40         {
41             source => 'Borrower',
42             value  => {
43                 dateexpiry    => "3000-01-01",
44             }
45         }
46     );
47     my $good_patron = $builder->build(
48         {
49             source => 'Borrower',
50             value  => {
51                 dateexpiry    => "3000-01-01",
52             }
53         }
54     );
55     my $odue = $builder->build({ source => 'Issue', value => {
56             borrowernumber => $odue_patron->{borrowernumber},
57             date_due => '2017-01-01',
58             }
59     });
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'");
65
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'");
71
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'");
77
78 };
79
80 subtest "Test build_patron_attribute_string" => sub {
81
82     plan tests => 2;
83
84     my $patron = $builder->build( { source => 'Borrower' } );
85
86     my $attribute_type = $builder->build( { source => 'BorrowerAttributeType' } );
87     my $attribute = Koha::Patron::Attribute->new(
88         {
89             borrowernumber => $patron->{borrowernumber},
90             code           => $attribute_type->{code},
91             attribute      => 'Test Attribute'
92         }
93     )->store();
94
95     my $attribute_type2 = $builder->build( { source => 'BorrowerAttributeType' } );
96     my $attribute2 = Koha::Patron::Attribute->new(
97         {
98             borrowernumber => $patron->{borrowernumber},
99             code           => $attribute_type2->{code},
100             attribute      => 'Another Test Attribute'
101         }
102     )->store();
103
104     my $ils_patron = C4::SIP::ILS::Patron->new( $patron->{cardnumber} );
105
106     my $server = {};
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' );
111
112     $server = {};
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' );
119 };
120
121 subtest "Test build_custom_field_string" => sub {
122
123     plan tests => 5;
124
125     my $patron = $builder->build_object( { class => 'Koha::Patrons',value=>{surname => "Duck", firstname => "Darkwing"} } );
126
127
128     my $ils_patron = C4::SIP::ILS::Patron->new( $patron->cardnumber );
129
130     my $server = {};
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' );
134
135     $server = {};
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' );
139
140
141     $server = {};
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' );
146
147     $server = {};
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' );
154
155     $server = {};
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' );
162
163 };
164
165 subtest "update_lastseen tests" => sub {
166     plan tests => 2;
167
168     my $seen_patron = $builder->build(
169         {
170             source => 'Borrower',
171             value  => {
172                 lastseen    => "2001-01-01",
173             }
174         }
175     );
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');
185 };
186
187 subtest "fine_items tests" => sub {
188
189     plan tests => 12;
190
191     my $patron = $builder->build(
192         {
193             source => 'Borrower',
194         }
195     );
196
197     my $fee1 = $builder->build(
198         {
199             source => 'Accountline',
200             value  => {
201                 borrowernumber => $patron->{borrowernumber},
202                 amountoutstanding => 1,
203             }
204         }
205     );
206
207     my $fee2 = $builder->build(
208         {
209             source => 'Accountline',
210             value  => {
211                 borrowernumber => $patron->{borrowernumber},
212                 amountoutstanding => 1,
213             }
214         }
215     );
216
217     my $sip_patron = C4::SIP::ILS::Patron->new( $patron->{cardnumber} );
218
219     my $all_fine_items = $sip_patron->fine_items;
220     is( @$all_fine_items, 2, "Got all fine items" );
221
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");
226
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");
231
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");
237
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");
243
244     # Check an invalid start boundary
245     $fine_items = $sip_patron->fine_items(98,99);
246     is( @$fine_items, 0, "Got zero fine items" );
247 };
248
249 $schema->storage->txn_rollback;