From 0386f31a9f69449038738800d7ba9320adb23a26 Mon Sep 17 00:00:00 2001 From: Andrew Isherwood Date: Fri, 4 Oct 2019 11:38:19 +0100 Subject: [PATCH] Bug 23681: Add unit tests Signed-off-by: Benjamin Veasey Sponsored-by: Loughborough University --- t/db_dependent/RestrictionType.t | 60 +++++++++++++++++ t/db_dependent/RestrictionTypes.t | 105 ++++++++++++++++++++++++++++++ 2 files changed, 165 insertions(+) create mode 100644 t/db_dependent/RestrictionType.t create mode 100644 t/db_dependent/RestrictionTypes.t diff --git a/t/db_dependent/RestrictionType.t b/t/db_dependent/RestrictionType.t new file mode 100644 index 0000000000..28bc464e19 --- /dev/null +++ b/t/db_dependent/RestrictionType.t @@ -0,0 +1,60 @@ +#!/usr/bin/perl + +use Modern::Perl; + +use C4::Context; +use Koha::Database; +use t::lib::TestBuilder; + +use Test::More tests => 3; + +my $schema = Koha::Database->new->schema; +$schema->storage->txn_begin; +my $dbh = C4::Context->dbh; +my $builder = t::lib::TestBuilder->new; + +use_ok('Koha::RestrictionType'); +use_ok('Koha::RestrictionTypes'); + +$dbh->do(q|DELETE FROM borrower_debarments|); +Koha::RestrictionTypes->search->delete; + +$builder->build({ + source => 'DebarmentType', + value => { + code => 'ONE', + display_text => 'One', + ronly => 1, + dflt => 0 + } +}); +$builder->build({ + source => 'DebarmentType', + value => { + code => 'TWO', + display_text => 'Two', + ronly => 1, + dflt => 1 + } +}); + +# keyed_on_code +my $keyed = Koha::RestrictionTypes->keyed_on_code; +my $expecting = { + ONE => { + code => 'ONE', + display_text => 'One', + ronly => 1, + dflt => 0 + }, + TWO => { + code => 'TWO', + display_text => 'Two', + ronly => 1, + dflt => 1 + } +}; + +is_deeply($keyed, $expecting, 'keyed_on_code returns correctly'); + +$schema->storage->txn_rollback; diff --git a/t/db_dependent/RestrictionTypes.t b/t/db_dependent/RestrictionTypes.t new file mode 100644 index 0000000000..567631a8a7 --- /dev/null +++ b/t/db_dependent/RestrictionTypes.t @@ -0,0 +1,105 @@ +#!/usr/bin/perl + +use Modern::Perl; + +use C4::Context; +use Koha::Database; +use t::lib::TestBuilder; + +use Test::More tests => 6; + +my $schema = Koha::Database->new->schema; +$schema->storage->txn_begin; +my $dbh = C4::Context->dbh; +my $builder = t::lib::TestBuilder->new; + +use_ok('Koha::RestrictionType'); +use_ok('Koha::RestrictionTypes'); + +$dbh->do(q|DELETE FROM borrower_debarments|); +Koha::RestrictionTypes->search->delete; + +$builder->build({ + source => 'DebarmentType', + value => { + code => 'ONE', + display_text => 'One', + ronly => 1, + dflt => 0 + } +}); +$builder->build({ + source => 'DebarmentType', + value => { + code => 'TWO', + display_text => 'Two', + ronly => 1, + dflt => 1 + } +}); +$builder->build({ + source => 'DebarmentType', + value => { + code => 'THREE', + display_text => 'Three', + ronly => 1, + dflt => 0 + } +}); +$builder->build({ + source => 'DebarmentType', + value => { + code => 'FOUR', + display_text => 'Four', + ronly => 0, + dflt => 0 + } +}); +$builder->build({ + source => 'DebarmentType', + value => { + code => 'FIVE', + display_text => 'Five', + ronly => 0, + dflt => 0 + } +}); + +# Can we create RestrictionTypes +my $created = Koha::RestrictionTypes->find({ code => 'ONE' }); +ok( $created->display_text eq 'One', 'Restrictions created'); + +# Can we delete RestrictionTypes, when appropriate +my $deleted = Koha::RestrictionTypes->find({ code => 'FOUR' })->delete; +ok( $deleted == 1, 'Restriction deleted'); +my $not_deleted = Koha::RestrictionTypes->find({ code => 'TWO' })->delete; +ok( $not_deleted == 0, 'Read only restriction not deleted'); + + +# Add a patron with a debarment +my $library = $builder->build({ source => 'Branch' }); + +my $patron_category = $builder->build({ source => 'Category' }); +my $borrowernumber = Koha::Patron->new({ + firstname => 'my firstname', + surname => 'my surname', + categorycode => $patron_category->{categorycode}, + branchcode => $library->{branchcode}, +})->store->borrowernumber; + +Koha::Patron::Debarments::AddDebarment({ + borrowernumber => $borrowernumber, + expiration => '9999-06-10', + type => 'FIVE', + comment => 'Test 1', +}); + +# Now delete a code and check debarments using that code switch to +# using the default +my $to_delete = Koha::RestrictionTypes->find({ code => 'FIVE' })->delete; +my $debarments = Koha::Patron::Debarments::GetDebarments({ + borrowernumber => $borrowernumber +}); +is( $debarments->[0]->{type}, 'TWO', 'Debarments update with restrictions' ); + +$schema->storage->txn_rollback; -- 2.20.1