From 1fadc6cd7ddb5cf2d67e29a84ffc672476c0843e Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Mon, 8 Oct 2018 16:21:25 -0300 Subject: [PATCH] Bug 21503: Tests for the existing behavior This patch introduces 7 tests for the current behaviour for the AuthorisedValues template plugin. To test: - Apply this patch - Run: $ kshell k$ prove t/db_dependent/Template/Plugin/AuthorisedValues.t => SUCCESS: Tests pass, all green! --- t/db_dependent/Template/Plugin/AuthorisedValues.t | 86 ++++++++++++++++++++++- 1 file changed, 84 insertions(+), 2 deletions(-) diff --git a/t/db_dependent/Template/Plugin/AuthorisedValues.t b/t/db_dependent/Template/Plugin/AuthorisedValues.t index e57dad165d..c5b10de44d 100644 --- a/t/db_dependent/Template/Plugin/AuthorisedValues.t +++ b/t/db_dependent/Template/Plugin/AuthorisedValues.t @@ -16,21 +16,25 @@ use Modern::Perl; -use Test::More tests => 1; +use Test::More tests => 2; use C4::Context; +use Koha::Caches; use Koha::Database; +use Koha::MarcSubfieldStructures; use Koha::Template::Plugin::AuthorisedValues; use t::lib::TestBuilder; use t::lib::Mocks; my $schema = Koha::Database->schema; -$schema->storage->txn_begin; my $builder = t::lib::TestBuilder->new; subtest 'GetByCode' => sub { plan tests => 4; + + $schema->storage->txn_begin; + my $avc = $builder->build_object( { class => 'Koha::AuthorisedValueCategories' } ); my $av_1 = $builder->build_object( @@ -63,4 +67,82 @@ subtest 'GetByCode' => sub { Koha::Template::Plugin::AuthorisedValues->GetByCode( $avc->category_name, 'does_not_exist' ); is( $description, 'does_not_exist', 'GetByCode should return the code passed if the AV does not exist' ); + + $schema->storage->txn_rollback; +}; + +subtest 'GetDescriptionByKohaField' => sub { + + plan tests => 7; + + $schema->storage->txn_begin; + + my $avc = $builder->build_object( { class => 'Koha::AuthorisedValueCategories' } ); + + # Create a framework mapping + Koha::MarcSubfieldStructure->new( + { tagfield => '988', + tagsubfield => 'a', + liblibrarian => 'Dummy field', + libopac => 'Dummy field', + repeatable => 0, + mandatory => 0, + kohafield => 'dummy.field', + tab => '9', + authorised_value => $avc->category_name, + authtypecode => q{}, + value_builder => q{}, + isurl => 0, + hidden => 0, + frameworkcode => q{}, + seealso => q{}, + link => q{}, + defaultvalue => undef + } + )->store; + + # Make sure we are not catch by cache + Koha::Caches->get_instance->flush_all; + my $av_1 = $builder->build_object( + { class => 'Koha::AuthorisedValues', + value => { category => $avc->category_name, lib_opac => 'lib_opac', lib => 'lib' } + } + ); + my $av_2 = $builder->build_object( + { class => 'Koha::AuthorisedValues', + value => { category => $avc->category_name, lib_opac => undef, lib => 'lib' } + } + ); + my $av_3 = $builder->build_object( + { class => 'Koha::AuthorisedValues', + value => { category => $avc->category_name, lib_opac =>undef, lib => undef } + } + ); + my $non_existent_av = $av_3->authorised_value; + $av_3->delete; + + my $av = Koha::Template::Plugin::AuthorisedValues->GetDescriptionByKohaField( + { opac => 1, kohafield => 'dummy.field', authorised_value => $av_1->authorised_value } ); + is( $av, 'lib_opac', 'We requested an existing AV description for the OPAC' ); + $av = Koha::Template::Plugin::AuthorisedValues->GetDescriptionByKohaField( + { opac => 1, kohafield => 'dummy.field', authorised_value => $av_2->authorised_value } ); + is( $av, 'lib', 'We requested an OPAC AV description for the OPAC, return a regular description' ); + $av = Koha::Template::Plugin::AuthorisedValues->GetDescriptionByKohaField( + { opac => 1, kohafield => 'dummy.field', authorised_value => $av_3->authorised_value } ); + is( $av, $av_3->authorised_value, 'We requested an non-existing OPAC AV description for the OPAC, return the authorised_value.' ); + $av = Koha::Template::Plugin::AuthorisedValues->GetDescriptionByKohaField( + { opac => 1, kohafield => 'dummy.field', authorised_value => $non_existent_av } ); + is( $av, '', 'We requested a non existing AV description for the OPAC, return empty string' ); + $av = Koha::Template::Plugin::AuthorisedValues->GetDescriptionByKohaField( + { kohafield => 'dummy.field', authorised_value => $av_1->authorised_value } ); + is( $av, 'lib', 'We requested an existing AV regular description.' ); + $av = Koha::Template::Plugin::AuthorisedValues->GetDescriptionByKohaField( + { kohafield => 'dummy.field', authorised_value => $av_1->authorised_value } ); + is( $av, $av_3->authorised_value, 'We requested an non-existing AV regular description, return the authorised_value.' ); + $av = Koha::Template::Plugin::AuthorisedValues->GetDescriptionByKohaField( + { kohafield => 'dummy.field', authorised_value => $non_existent_av } ); + is( $av, '', + 'We requested a non existing AV description, not for the OPAC, return empty string' ); + + $schema->storage->txn_rollback; }; -- 2.11.0