From 0fdc1021d4477d838c6b38a1172137a5eec08978 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Wed, 10 Feb 2016 15:37:30 +0000 Subject: [PATCH] Bug 15800: Koha::AuthorisedValues - Remove C4::Koha::IsAuthorisedValueCategory C4::Koha::IsAuthorisedValueCategory contains only 2 useful calls, from C4::Reports::Guided and reports/guided_reports.pl It can be replaced with Koha::AuthorisedValues->search({ category => $authorised_value})->count Test plan: 1/ Create a sql report using an authorised value category, something like: SELECT COUNT(*) FROM items where itemlost=<> 2/ Execute the report and confirm that everything works fine. 3/ Create a sql report using a nonexistent authorised value categor, something like: SELECT COUNT(*) FROM items where itemlost=<> 4/ When saving the report, you should get a warning message "lost: The authorized value category (NONEXIST) you selected does not exist." 5/ Save anyway and execute the report, you should get the same warning message. QA: git grep IsAuthorisedValueCategory should not return any results prove t/db_dependent/ReportsGuided.t should return green Signed-off-by: Hector Castro Works as described Signed-off-by: Kyle M Hall Signed-off-by: Brendan Gallagher brendan@bywatersolutions.com --- C4/Koha.pm | 23 ----------------------- C4/Reports/Guided.pm | 4 +++- reports/guided_reports.pl | 5 +++-- t/Koha.t | 22 +++++----------------- t/db_dependent/ReportsGuided.t | 27 +++++---------------------- 5 files changed, 16 insertions(+), 65 deletions(-) diff --git a/C4/Koha.pm b/C4/Koha.pm index b637cd43b0..f334096801 100644 --- a/C4/Koha.pm +++ b/C4/Koha.pm @@ -56,7 +56,6 @@ BEGIN { &getitemtypeimagelocation &GetAuthorisedValues &GetAuthorisedValueCategories - &IsAuthorisedValueCategory &GetKohaAuthorisedValues &GetKohaAuthorisedValuesFromField &GetKohaAuthorisedValuesMapping @@ -1118,28 +1117,6 @@ sub GetAuthorisedValueCategories { return \@results; } -=head2 IsAuthorisedValueCategory - - $is_auth_val_category = IsAuthorisedValueCategory($category); - -Returns whether a given category name is a valid one - -=cut - -sub IsAuthorisedValueCategory { - my $category = shift; - my $query = ' - SELECT category - FROM authorised_values - WHERE category=? - LIMIT 1 - '; - my $sth = C4::Context->dbh->prepare($query); - $sth->execute($category); - $sth->fetchrow ? return 1 - : return 0; -} - =head2 GetAuthorisedValueByCode $authorised_value = GetAuthorisedValueByCode( $category, $authvalcode, $opac ); diff --git a/C4/Reports/Guided.pm b/C4/Reports/Guided.pm index daf810d32d..ffc8afd3f2 100644 --- a/C4/Reports/Guided.pm +++ b/C4/Reports/Guided.pm @@ -34,6 +34,8 @@ use C4::Debug; # use Data::Dumper; use C4::Log; +use Koha::AuthorisedValues; + BEGIN { # set the version for version checking $VERSION = 3.07.00.049; @@ -932,7 +934,7 @@ sub IsAuthorisedValueValid { my $reserved_authorised_values = GetReservedAuthorisedValues(); if ( exists $reserved_authorised_values->{$authorised_value} || - C4::Koha::IsAuthorisedValueCategory($authorised_value) ) { + Koha::AuthorisedValues->search({ category => $authorised_value })->count ) { return 1; } diff --git a/reports/guided_reports.pl b/reports/guided_reports.pl index eed8ab97d7..200260d2e4 100755 --- a/reports/guided_reports.pl +++ b/reports/guided_reports.pl @@ -29,11 +29,12 @@ use C4::Auth qw/:DEFAULT get_session/; use C4::Output; use C4::Debug; use C4::Branch; # XXX subfield_is_koha_internal_p -use C4::Koha qw/IsAuthorisedValueCategory GetFrameworksLoop/; +use C4::Koha qw/GetFrameworksLoop/; use C4::Context; use C4::Log; use Koha::DateUtils qw/dt_from_string output_pref/; use Koha::AuthorisedValue; +use Koha::AuthorisedValues; =head1 NAME @@ -706,7 +707,7 @@ elsif ($phase eq 'Run this report'){ #---- "true" authorised value } else { - if ( IsAuthorisedValueCategory($authorised_value) ) { + if ( Koha::AuthorisedValues->search({ category => $authorised_value })->count ) { my $query = ' SELECT authorised_value,lib FROM authorised_values diff --git a/t/Koha.t b/t/Koha.t index e887632fc7..21cbf68d59 100755 --- a/t/Koha.t +++ b/t/Koha.t @@ -25,7 +25,7 @@ use Module::Load::Conditional qw/check_install/; BEGIN { if ( check_install( module => 'Test::DBIx::Class' ) ) { - plan tests => 34; + plan tests => 31; } else { plan skip_all => "Need Test::DBIx::Class" } @@ -38,15 +38,11 @@ use Test::DBIx::Class { connect_info => ['dbi:SQLite:dbname=:memory:','',''], connect_opts => { name_sep => '.', quote_char => '`', }, fixture_class => '::Populate', -}, 'AuthorisedValue', 'Branch' ; +}, 'Branch' ; sub fixtures { - my ( $av, $libraries ) = @_; + my ( $libraries ) = @_; fixtures_ok [ - AuthorisedValue => [ - [ 'category', 'authorised_value' ], - @$av, - ], Branch => [ ['branchcode', 'branchname'], @$libraries, @@ -57,18 +53,10 @@ sub fixtures { my $db = Test::MockModule->new('Koha::Database'); $db->mock( _new_schema => sub { return Schema(); } ); -my $authorised_values = [ - ['LOC', 'LOC'], - ['RELTERMS', 'RELTERMS'], -]; my $libraries = [ ['XXX_test', 'my branchname XXX'], ]; -fixtures($authorised_values, $libraries); - -is ( IsAuthorisedValueCategory('LOC'), 1, 'LOC is a valid authorized value category'); -is ( IsAuthorisedValueCategory('something'), 0, 'something is not a valid authorized value category'); -is ( IsAuthorisedValueCategory('RELTERMS'), 1, 'RELTERMS is a valid authorized value category'); +fixtures($libraries); my $isbn13 = "9780330356473"; my $isbn13D = "978-0-330-35647-3"; @@ -139,7 +127,7 @@ subtest 'getFacets() tests' => sub { ['YYY_test', 'my branchname YYY'], ['ZZZ_test', 'my branchname XXX'], ]; - fixtures($authorised_values, $libraries); + fixtures($libraries); is ( Koha::Libraries->search->count, 3, 'There should be only more than 1 library (singleBranchMode off)' ); $facets = C4::Koha::getFacets(); diff --git a/t/db_dependent/ReportsGuided.t b/t/db_dependent/ReportsGuided.t index 097722a19f..40e49a8098 100755 --- a/t/db_dependent/ReportsGuided.t +++ b/t/db_dependent/ReportsGuided.t @@ -3,33 +3,16 @@ use Modern::Perl; use Test::More tests => 13; -use Test::MockModule; -use t::lib::Mocks; +use Koha::Database; use_ok('C4::Reports::Guided'); -my $context = new Test::MockModule('C4::Context'); -my $koha = new Test::MockModule('C4::Koha'); +my $schema = Koha::Database->new->schema; +$schema->storage->txn_begin; -BEGIN { - t::lib::Mocks::mock_dbh; -} - -sub MockedIsAuthorisedValueCategory { - my $authorised_value = shift; - - if ( $authorised_value eq 'LOC' ) { - return 1; - } else { - return 0; - } -} - -$koha->mock( - 'IsAuthorisedValueCategory', - \&MockedIsAuthorisedValueCategory -); +$_->delete for Koha::AuthorisedValues->search({ category => 'XXX' }); +Koha::AuthorisedValue->new({category => 'LOC'})->store; { # GetReservedAuthorisedValues tests # This one will catch new reserved words not added -- 2.39.5