Bug 21832: add unit test
[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 => 5;
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 "update_lastseen tests" => sub {
120     plan tests => 2;
121
122     my $seen_patron = $builder->build(
123         {
124             source => 'Borrower',
125             value  => {
126                 lastseen    => "2001-01-01",
127             }
128         }
129     );
130     my $sip_patron = C4::SIP::ILS::Patron->new( $seen_patron->{cardnumber} );
131     t::lib::Mocks::mock_preference( 'TrackLastPatronActivity', '' );
132     $sip_patron->update_lastseen();
133     $seen_patron = Koha::Patrons->find({ cardnumber => $seen_patron->{cardnumber} });
134     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');
135     t::lib::Mocks::mock_preference( 'TrackLastPatronActivity', '1' );
136     $sip_patron->update_lastseen();
137     $seen_patron = Koha::Patrons->find({ cardnumber => $seen_patron->cardnumber() });
138     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');
139 };
140
141 $schema->storage->txn_rollback;