Bug 27317: (Bug 27127 follow-up) fix t/db_dependent/Koha/BackgroundJobs.t
[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 => 6;
8
9 use Koha::Database;
10 use Koha::Patrons;
11 use Koha::DateUtils;
12 use t::lib::TestBuilder;
13 use t::lib::Mocks;
14 use C4::SIP::ILS::Patron;
15 use Koha::Patron::Attributes;
16
17 my $schema = Koha::Database->new->schema;
18 $schema->storage->txn_begin;
19
20 my $builder = t::lib::TestBuilder->new();
21 my $patron1 = $builder->build({ source => 'Borrower' });
22 my $card = $patron1->{cardnumber};
23
24 # Check existing card number
25 my $sip_patron = C4::SIP::ILS::Patron->new( $card );
26 is( defined $sip_patron, 1, "Patron is valid" );
27
28 # Check invalid cardnumber by deleting patron
29 $schema->resultset('Borrower')->search({ cardnumber => $card })->delete;
30 my $sip_patron2 = C4::SIP::ILS::Patron->new( $card );
31 is( $sip_patron2, undef, "Patron is not valid (anymore)" );
32
33 subtest "OverduesBlockCirc tests" => sub {
34
35     plan tests => 6;
36
37     my $odue_patron = $builder->build(
38         {
39             source => 'Borrower',
40             value  => {
41                 dateexpiry    => "3000-01-01",
42             }
43         }
44     );
45     my $good_patron = $builder->build(
46         {
47             source => 'Borrower',
48             value  => {
49                 dateexpiry    => "3000-01-01",
50             }
51         }
52     );
53     my $odue = $builder->build({ source => 'Issue', value => {
54             borrowernumber => $odue_patron->{borrowernumber},
55             date_due => '2017-01-01',
56             }
57     });
58     t::lib::Mocks::mock_preference( 'OverduesBlockCirc', 'noblock' );
59     my $odue_sip_patron = C4::SIP::ILS::Patron->new( $odue_patron->{cardnumber} );
60     is( $odue_sip_patron->{charge_ok}, 1, "Not blocked with overdues when set to 'Don't block'");
61     $odue_sip_patron = C4::SIP::ILS::Patron->new( $good_patron->{cardnumber} );
62     is( $odue_sip_patron->{charge_ok}, 1, "Not blocked without overdues when set to 'Don't block'");
63
64     t::lib::Mocks::mock_preference( 'OverduesBlockCirc', 'confirmation' );
65     $odue_sip_patron = C4::SIP::ILS::Patron->new( $odue_patron->{cardnumber} );
66     is( $odue_sip_patron->{charge_ok}, '', "Blocked with overdues when set to 'Ask for confirmation'");
67     $odue_sip_patron = C4::SIP::ILS::Patron->new( $good_patron->{cardnumber} );
68     is( $odue_sip_patron->{charge_ok}, 1, "Not blocked without overdues when set to 'confirmation'");
69
70     t::lib::Mocks::mock_preference( 'OverduesBlockCirc', 'block' );
71     $odue_sip_patron = C4::SIP::ILS::Patron->new( $odue_patron->{cardnumber} );
72     is( $odue_sip_patron->{charge_ok}, '', "Blocked with overdues when set to 'Block'");
73     $odue_sip_patron = C4::SIP::ILS::Patron->new( $good_patron->{cardnumber} );
74     is( $odue_sip_patron->{charge_ok}, 1, "Not blocked without overdues when set to 'Block'");
75
76 };
77
78 subtest "Test build_patron_attribute_string" => sub {
79
80     plan tests => 2;
81
82     my $patron = $builder->build( { source => 'Borrower' } );
83
84     my $attribute_type = $builder->build( { source => 'BorrowerAttributeType' } );
85     my $attribute = Koha::Patron::Attribute->new(
86         {
87             borrowernumber => $patron->{borrowernumber},
88             code           => $attribute_type->{code},
89             attribute      => 'Test Attribute'
90         }
91     )->store();
92
93     my $attribute_type2 = $builder->build( { source => 'BorrowerAttributeType' } );
94     my $attribute2 = Koha::Patron::Attribute->new(
95         {
96             borrowernumber => $patron->{borrowernumber},
97             code           => $attribute_type2->{code},
98             attribute      => 'Another Test Attribute'
99         }
100     )->store();
101
102     my $ils_patron = C4::SIP::ILS::Patron->new( $patron->{cardnumber} );
103
104     my $server = {};
105     $server->{account}->{patron_attribute}->{code} = $attribute->code;
106     $server->{account}->{patron_attribute}->{field} = 'XY';
107     my $attribute_string = $ils_patron->build_patron_attributes_string( $server );
108     is( $attribute_string, "XYTest Attribute|", 'Attribute field generated correctly with single param' );
109
110     $server = {};
111     $server->{account}->{patron_attribute}->[0]->{code} = $attribute->code;
112     $server->{account}->{patron_attribute}->[0]->{field} = 'XY';
113     $server->{account}->{patron_attribute}->[1]->{code} = $attribute2->code;
114     $server->{account}->{patron_attribute}->[1]->{field} = 'YZ';
115     $attribute_string = $ils_patron->build_patron_attributes_string( $server );
116     is( $attribute_string, "XYTest Attribute|YZAnother Test Attribute|", 'Attribute field generated correctly with multiple params' );
117 };
118
119 subtest "Test build_custom_field_string" => sub {
120
121     plan tests => 5;
122
123     my $patron = $builder->build_object( { class => 'Koha::Patrons',value=>{surname => "Duck", firstname => "Darkwing"} } );
124
125
126     my $ils_patron = C4::SIP::ILS::Patron->new( $patron->cardnumber );
127
128     my $server = {};
129     $server->{account}->{custom_patron_field}->{field} = "DW";
130     my $attribute_string = $ils_patron->build_custom_field_string( $server );
131     is( $attribute_string, "", 'Custom field not generated if no value passed' );
132
133     $server = {};
134     $server->{account}->{custom_patron_field}->{template} = "[% patron.surname %]";
135     $attribute_string = $ils_patron->build_custom_field_string( $server );
136     is( $attribute_string, "", 'Custom field not generated if no field passed' );
137
138
139     $server = {};
140     $server->{account}->{custom_patron_field}->{field} = "DW";
141     $server->{account}->{custom_patron_field}->{template} = "[% patron.firstname %] [% patron.surname %], let's get dangerous!";
142     $attribute_string = $ils_patron->build_custom_field_string( $server );
143     is( $attribute_string, "DWDarkwing Duck, let's get dangerous!|", 'Custom field processed correctly' );
144
145     $server = {};
146     $server->{account}->{custom_patron_field}->[0]->{field} = "DW";
147     $server->{account}->{custom_patron_field}->[0]->{template} = "[% patron.firstname %] [% patron.surname %], let's get dangerous!";
148     $server->{account}->{custom_patron_field}->[1]->{field} = "LM";
149     $server->{account}->{custom_patron_field}->[1]->{template} = "Launchpad McQuack crashed on [% patron.dateexpiry %]";
150     $attribute_string = $ils_patron->build_custom_field_string( $server );
151     is( $attribute_string, "DWDarkwing Duck, let's get dangerous!|LMLaunchpad McQuack crashed on ".$patron->dateexpiry."|", 'Custom fields processed correctly when multiple exist' );
152
153     $server = {};
154     $server->{account}->{custom_patron_field}->[0]->{field} = "DW";
155     $server->{account}->{custom_patron_field}->[0]->{template} = "[% IF (patron.firstname) %] patron.surname, let's get dangerous!";
156     $server->{account}->{custom_patron_field}->[1]->{field} = "LM";
157     $server->{account}->{custom_patron_field}->[1]->{template} = "Launchpad McQuack crashed on [% patron.dateexpiry %]";
158     $attribute_string = $ils_patron->build_custom_field_string( $server );
159     is( $attribute_string, "LMLaunchpad McQuack crashed on ".$patron->dateexpiry."|", 'Custom fields processed correctly, bad template generate no text' );
160
161 };
162
163 subtest "update_lastseen tests" => sub {
164     plan tests => 2;
165
166     my $seen_patron = $builder->build(
167         {
168             source => 'Borrower',
169             value  => {
170                 lastseen    => "2001-01-01",
171             }
172         }
173     );
174     my $sip_patron = C4::SIP::ILS::Patron->new( $seen_patron->{cardnumber} );
175     t::lib::Mocks::mock_preference( 'TrackLastPatronActivity', '' );
176     $sip_patron->update_lastseen();
177     $seen_patron = Koha::Patrons->find({ cardnumber => $seen_patron->{cardnumber} });
178     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');
179     t::lib::Mocks::mock_preference( 'TrackLastPatronActivity', '1' );
180     $sip_patron->update_lastseen();
181     $seen_patron = Koha::Patrons->find({ cardnumber => $seen_patron->cardnumber() });
182     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');
183 };
184
185 $schema->storage->txn_rollback;