Bug 16330: Move patches to OpenAPI
[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 => 4;
8
9 use Koha::Database;
10 use t::lib::TestBuilder;
11 use t::lib::Mocks;
12 use C4::SIP::ILS::Patron;
13 use Koha::Patron::Attributes;
14
15 my $schema = Koha::Database->new->schema;
16 $schema->storage->txn_begin;
17
18 my $builder = t::lib::TestBuilder->new();
19 my $patron1 = $builder->build({ source => 'Borrower' });
20 my $card = $patron1->{cardnumber};
21
22 # Check existing card number
23 my $sip_patron = C4::SIP::ILS::Patron->new( $card );
24 is( defined $sip_patron, 1, "Patron is valid" );
25
26 # Check invalid cardnumber by deleting patron
27 $schema->resultset('Borrower')->search({ cardnumber => $card })->delete;
28 my $sip_patron2 = C4::SIP::ILS::Patron->new( $card );
29 is( $sip_patron2, undef, "Patron is not valid (anymore)" );
30
31 subtest "OverduesBlockCirc tests" => sub {
32
33     plan tests => 6;
34
35     my $odue_patron = $builder->build(
36         {
37             source => 'Borrower',
38             value  => {
39                 dateexpiry    => "3000-01-01",
40             }
41         }
42     );
43     my $good_patron = $builder->build(
44         {
45             source => 'Borrower',
46             value  => {
47                 dateexpiry    => "3000-01-01",
48             }
49         }
50     );
51     my $odue = $builder->build({ source => 'Issue', value => {
52             borrowernumber => $odue_patron->{borrowernumber},
53             date_due => '2017-01-01',
54             }
55     });
56     t::lib::Mocks::mock_preference( 'OverduesBlockCirc', 'noblock' );
57     my $odue_sip_patron = C4::SIP::ILS::Patron->new( $odue_patron->{cardnumber} );
58     is( $odue_sip_patron->{charge_ok}, 1, "Not blocked with overdues when set to 'Don't block'");
59     $odue_sip_patron = C4::SIP::ILS::Patron->new( $good_patron->{cardnumber} );
60     is( $odue_sip_patron->{charge_ok}, 1, "Not blocked without overdues when set to 'Don't block'");
61
62     t::lib::Mocks::mock_preference( 'OverduesBlockCirc', 'confirmation' );
63     $odue_sip_patron = C4::SIP::ILS::Patron->new( $odue_patron->{cardnumber} );
64     is( $odue_sip_patron->{charge_ok}, '', "Blocked with overdues when set to 'Ask for confirmation'");
65     $odue_sip_patron = C4::SIP::ILS::Patron->new( $good_patron->{cardnumber} );
66     is( $odue_sip_patron->{charge_ok}, 1, "Not blocked without overdues when set to 'confirmation'");
67
68     t::lib::Mocks::mock_preference( 'OverduesBlockCirc', 'block' );
69     $odue_sip_patron = C4::SIP::ILS::Patron->new( $odue_patron->{cardnumber} );
70     is( $odue_sip_patron->{charge_ok}, '', "Blocked with overdues when set to 'Block'");
71     $odue_sip_patron = C4::SIP::ILS::Patron->new( $good_patron->{cardnumber} );
72     is( $odue_sip_patron->{charge_ok}, 1, "Not blocked without overdues when set to 'Block'");
73
74 };
75
76 subtest "Test build_patron_attribute_string" => sub {
77
78     plan tests => 2;
79
80     my $patron = $builder->build( { source => 'Borrower' } );
81
82     my $attribute_type = $builder->build( { source => 'BorrowerAttributeType' } );
83     my $attribute = Koha::Patron::Attribute->new(
84         {
85             borrowernumber => $patron->{borrowernumber},
86             code           => $attribute_type->{code},
87             attribute      => 'Test Attribute'
88         }
89     )->store();
90
91     my $attribute_type2 = $builder->build( { source => 'BorrowerAttributeType' } );
92     my $attribute2 = Koha::Patron::Attribute->new(
93         {
94             borrowernumber => $patron->{borrowernumber},
95             code           => $attribute_type2->{code},
96             attribute      => 'Another Test Attribute'
97         }
98     )->store();
99
100     my $ils_patron = C4::SIP::ILS::Patron->new( $patron->{cardnumber} );
101
102     my $server = {};
103     $server->{account}->{patron_attribute}->{code} = $attribute->code;
104     $server->{account}->{patron_attribute}->{field} = 'XY';
105     my $attribute_string = $ils_patron->build_patron_attributes_string( $server );
106     is( $attribute_string, "XYTest Attribute|", 'Attribute field generated correctly with single param' );
107
108     $server = {};
109     $server->{account}->{patron_attribute}->[0]->{code} = $attribute->code;
110     $server->{account}->{patron_attribute}->[0]->{field} = 'XY';
111     $server->{account}->{patron_attribute}->[1]->{code} = $attribute2->code;
112     $server->{account}->{patron_attribute}->[1]->{field} = 'YZ';
113     $attribute_string = $ils_patron->build_patron_attributes_string( $server );
114     is( $attribute_string, "XYTest Attribute|YZAnother Test Attribute|", 'Attribute field generated correctly with multiple params' );
115 };
116
117 $schema->storage->txn_rollback;