Bug 19893: Support for joined subfields in mappings
[koha.git] / t / db_dependent / SIP / Patron.t
blob0f20ac3e5c76b6b423b006bb29c7df97bcdfbf87
1 #!/usr/bin/perl
3 # Some tests for SIP::ILS::Patron
4 # This needs to be extended! Your help is appreciated..
6 use Modern::Perl;
7 use Test::More tests => 5;
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;
17 my $schema = Koha::Database->new->schema;
18 $schema->storage->txn_begin;
20 my $builder = t::lib::TestBuilder->new();
21 my $patron1 = $builder->build({ source => 'Borrower' });
22 my $card = $patron1->{cardnumber};
24 # Check existing card number
25 my $sip_patron = C4::SIP::ILS::Patron->new( $card );
26 is( defined $sip_patron, 1, "Patron is valid" );
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)" );
33 subtest "OverduesBlockCirc tests" => sub {
35 plan tests => 6;
37 my $odue_patron = $builder->build(
39 source => 'Borrower',
40 value => {
41 dateexpiry => "3000-01-01",
45 my $good_patron = $builder->build(
47 source => 'Borrower',
48 value => {
49 dateexpiry => "3000-01-01",
53 my $odue = $builder->build({ source => 'Issue', value => {
54 borrowernumber => $odue_patron->{borrowernumber},
55 date_due => '2017-01-01',
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'");
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'");
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'");
78 subtest "Test build_patron_attribute_string" => sub {
80 plan tests => 2;
82 my $patron = $builder->build( { source => 'Borrower' } );
84 my $attribute_type = $builder->build( { source => 'BorrowerAttributeType' } );
85 my $attribute = Koha::Patron::Attribute->new(
87 borrowernumber => $patron->{borrowernumber},
88 code => $attribute_type->{code},
89 attribute => 'Test Attribute'
91 )->store();
93 my $attribute_type2 = $builder->build( { source => 'BorrowerAttributeType' } );
94 my $attribute2 = Koha::Patron::Attribute->new(
96 borrowernumber => $patron->{borrowernumber},
97 code => $attribute_type2->{code},
98 attribute => 'Another Test Attribute'
100 )->store();
102 my $ils_patron = C4::SIP::ILS::Patron->new( $patron->{cardnumber} );
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' );
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' );
119 subtest "update_lastseen tests" => sub {
120 plan tests => 2;
122 my $seen_patron = $builder->build(
124 source => 'Borrower',
125 value => {
126 lastseen => "2001-01-01",
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');
141 $schema->storage->txn_rollback;