Koha/t/db_dependent/SIP/Patron.t
Nick Clemens 41b5db64b3
Bug 20816: Add ability to define custom templated fields in SIP patron responses
To test:
 1 - You will need to enable SIP on your testing instance
    cp etc/SIPconfig.xml /etc/koha/sites/kohadev/
    sudo koha-start-sip
    add a user listed in the SIPconfig to your system and give them permissions (superlibrarian works)
    on koha-testing-docker you should be able to start sip with user koha/koha without any adjustments
 2 - If you copied the above file you should be set to get custom field DE with dateexpiry
     Otherwise edit the sip login for the user to have a custom section like:
		  <login id="koha"   password="koha"  delimiter="|" error-detect="enabled" institution="kohalibrary" encoding="utf8" >
			  <custom_patron_field field="DE" template="[% patron.dateexpiry %]" />
		  </login>
 3 - send a status test using the sip cli tester:
     perl misc/sip_cli_emulator.pl -a 127.0.0.1 -p 6001 -su koha -sp koha -l kohalibrary --patron 23529001000463 -m patron_status_request
 4 - send an information test using the sip cli tester:
     perl misc/sip_cli_emulator.pl -a 127.0.0.1 -p 6001 -su koha -sp koha -l kohalibrary --patron 23529001000463 -m patron_information
 5 - confirm you receive the DE field with a dateexpiry
 6 - Add your own custom fields and confirm it works with several
         <custom_patron_field field="EW" template="Phone: [% patron.phone %] Email: [% patron.email %]" />
 7 - prove -v t/db_dependent/SIP/Patron.t
 8 - prove -v t/db_dependent/SIP/

Signed-off-by: Andrew Fuerste-Henry <andrew@bywatersolutions.com>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
2020-04-22 13:31:59 +01:00

185 lines
7.9 KiB
Perl
Executable file

#!/usr/bin/perl
# Some tests for SIP::ILS::Patron
# This needs to be extended! Your help is appreciated..
use Modern::Perl;
use Test::More tests => 6;
use Koha::Database;
use Koha::Patrons;
use Koha::DateUtils;
use t::lib::TestBuilder;
use t::lib::Mocks;
use C4::SIP::ILS::Patron;
use Koha::Patron::Attributes;
my $schema = Koha::Database->new->schema;
$schema->storage->txn_begin;
my $builder = t::lib::TestBuilder->new();
my $patron1 = $builder->build({ source => 'Borrower' });
my $card = $patron1->{cardnumber};
# Check existing card number
my $sip_patron = C4::SIP::ILS::Patron->new( $card );
is( defined $sip_patron, 1, "Patron is valid" );
# Check invalid cardnumber by deleting patron
$schema->resultset('Borrower')->search({ cardnumber => $card })->delete;
my $sip_patron2 = C4::SIP::ILS::Patron->new( $card );
is( $sip_patron2, undef, "Patron is not valid (anymore)" );
subtest "OverduesBlockCirc tests" => sub {
plan tests => 6;
my $odue_patron = $builder->build(
{
source => 'Borrower',
value => {
dateexpiry => "3000-01-01",
}
}
);
my $good_patron = $builder->build(
{
source => 'Borrower',
value => {
dateexpiry => "3000-01-01",
}
}
);
my $odue = $builder->build({ source => 'Issue', value => {
borrowernumber => $odue_patron->{borrowernumber},
date_due => '2017-01-01',
}
});
t::lib::Mocks::mock_preference( 'OverduesBlockCirc', 'noblock' );
my $odue_sip_patron = C4::SIP::ILS::Patron->new( $odue_patron->{cardnumber} );
is( $odue_sip_patron->{charge_ok}, 1, "Not blocked with overdues when set to 'Don't block'");
$odue_sip_patron = C4::SIP::ILS::Patron->new( $good_patron->{cardnumber} );
is( $odue_sip_patron->{charge_ok}, 1, "Not blocked without overdues when set to 'Don't block'");
t::lib::Mocks::mock_preference( 'OverduesBlockCirc', 'confirmation' );
$odue_sip_patron = C4::SIP::ILS::Patron->new( $odue_patron->{cardnumber} );
is( $odue_sip_patron->{charge_ok}, '', "Blocked with overdues when set to 'Ask for confirmation'");
$odue_sip_patron = C4::SIP::ILS::Patron->new( $good_patron->{cardnumber} );
is( $odue_sip_patron->{charge_ok}, 1, "Not blocked without overdues when set to 'confirmation'");
t::lib::Mocks::mock_preference( 'OverduesBlockCirc', 'block' );
$odue_sip_patron = C4::SIP::ILS::Patron->new( $odue_patron->{cardnumber} );
is( $odue_sip_patron->{charge_ok}, '', "Blocked with overdues when set to 'Block'");
$odue_sip_patron = C4::SIP::ILS::Patron->new( $good_patron->{cardnumber} );
is( $odue_sip_patron->{charge_ok}, 1, "Not blocked without overdues when set to 'Block'");
};
subtest "Test build_patron_attribute_string" => sub {
plan tests => 2;
my $patron = $builder->build( { source => 'Borrower' } );
my $attribute_type = $builder->build( { source => 'BorrowerAttributeType' } );
my $attribute = Koha::Patron::Attribute->new(
{
borrowernumber => $patron->{borrowernumber},
code => $attribute_type->{code},
attribute => 'Test Attribute'
}
)->store();
my $attribute_type2 = $builder->build( { source => 'BorrowerAttributeType' } );
my $attribute2 = Koha::Patron::Attribute->new(
{
borrowernumber => $patron->{borrowernumber},
code => $attribute_type2->{code},
attribute => 'Another Test Attribute'
}
)->store();
my $ils_patron = C4::SIP::ILS::Patron->new( $patron->{cardnumber} );
my $server = {};
$server->{account}->{patron_attribute}->{code} = $attribute->code;
$server->{account}->{patron_attribute}->{field} = 'XY';
my $attribute_string = $ils_patron->build_patron_attributes_string( $server );
is( $attribute_string, "XYTest Attribute|", 'Attribute field generated correctly with single param' );
$server = {};
$server->{account}->{patron_attribute}->[0]->{code} = $attribute->code;
$server->{account}->{patron_attribute}->[0]->{field} = 'XY';
$server->{account}->{patron_attribute}->[1]->{code} = $attribute2->code;
$server->{account}->{patron_attribute}->[1]->{field} = 'YZ';
$attribute_string = $ils_patron->build_patron_attributes_string( $server );
is( $attribute_string, "XYTest Attribute|YZAnother Test Attribute|", 'Attribute field generated correctly with multiple params' );
};
subtest "Test build_custom_field_string" => sub {
plan tests => 5;
my $patron = $builder->build_object( { class => 'Koha::Patrons',value=>{surname => "Duck", firstname => "Darkwing"} } );
my $ils_patron = C4::SIP::ILS::Patron->new( $patron->cardnumber );
my $server = {};
$server->{account}->{custom_patron_field}->{field} = "DW";
my $attribute_string = $ils_patron->build_custom_field_string( $server );
is( $attribute_string, "", 'Custom field not generated if no value passed' );
$server = {};
$server->{account}->{custom_patron_field}->{template} = "[% patron.surname %]";
$attribute_string = $ils_patron->build_custom_field_string( $server );
is( $attribute_string, "", 'Custom field not generated if no field passed' );
$server = {};
$server->{account}->{custom_patron_field}->{field} = "DW";
$server->{account}->{custom_patron_field}->{template} = "[% patron.firstname %] [% patron.surname %], let's get dangerous!";
$attribute_string = $ils_patron->build_custom_field_string( $server );
is( $attribute_string, "DWDarkwing Duck, let's get dangerous!|", 'Custom field processed correctly' );
$server = {};
$server->{account}->{custom_patron_field}->[0]->{field} = "DW";
$server->{account}->{custom_patron_field}->[0]->{template} = "[% patron.firstname %] [% patron.surname %], let's get dangerous!";
$server->{account}->{custom_patron_field}->[1]->{field} = "LM";
$server->{account}->{custom_patron_field}->[1]->{template} = "Launchpad McQuack crashed on [% patron.dateexpiry %]";
$attribute_string = $ils_patron->build_custom_field_string( $server );
is( $attribute_string, "DWDarkwing Duck, let's get dangerous!|LMLaunchpad McQuack crashed on ".$patron->dateexpiry."|", 'Custom fields processed correctly when multiple exist' );
$server = {};
$server->{account}->{custom_patron_field}->[0]->{field} = "DW";
$server->{account}->{custom_patron_field}->[0]->{template} = "[% IF (patron.firstname) %] patron.surname, let's get dangerous!";
$server->{account}->{custom_patron_field}->[1]->{field} = "LM";
$server->{account}->{custom_patron_field}->[1]->{template} = "Launchpad McQuack crashed on [% patron.dateexpiry %]";
$attribute_string = $ils_patron->build_custom_field_string( $server );
is( $attribute_string, "LMLaunchpad McQuack crashed on ".$patron->dateexpiry."|", 'Custom fields processed correctly, bad template generate no text' );
};
subtest "update_lastseen tests" => sub {
plan tests => 2;
my $seen_patron = $builder->build(
{
source => 'Borrower',
value => {
lastseen => "2001-01-01",
}
}
);
my $sip_patron = C4::SIP::ILS::Patron->new( $seen_patron->{cardnumber} );
t::lib::Mocks::mock_preference( 'TrackLastPatronActivity', '' );
$sip_patron->update_lastseen();
$seen_patron = Koha::Patrons->find({ cardnumber => $seen_patron->{cardnumber} });
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');
t::lib::Mocks::mock_preference( 'TrackLastPatronActivity', '1' );
$sip_patron->update_lastseen();
$seen_patron = Koha::Patrons->find({ cardnumber => $seen_patron->cardnumber() });
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');
};
$schema->storage->txn_rollback;