From 3fc745690996684bdea6da80fcc4494a74cec9dd Mon Sep 17 00:00:00 2001 From: Alex Buckley Date: Fri, 10 Mar 2017 13:51:40 +0000 Subject: [PATCH] Bug 18247 - Moved the SQL queries out of the branch_transfer_limits.pl script into the Koha::AuthorisedValue, Koha::Libraries, Koha::ItemType perl modules. Also added 3 successfully working unit tests for the new subroutines --- Koha/AuthorisedValue.pm | 13 +++++++++++++ Koha/ItemType.pm | 15 +++++++++++++++ Koha/Libraries.pm | 17 +++++++++++++++++ admin/branch_transfer_limits.pl | 19 +++++++++++-------- t/db_dependent/AuthorisedValues.t | 25 +++++++++++++++++++++++-- t/db_dependent/Koha/ItemTypes.t | 11 ++++++++++- t/db_dependent/Koha/Libraries.t | 13 ++++++++++++- 7 files changed, 101 insertions(+), 12 deletions(-) diff --git a/Koha/AuthorisedValue.pm b/Koha/AuthorisedValue.pm index 898b630..48aa813 100644 --- a/Koha/AuthorisedValue.pm +++ b/Koha/AuthorisedValue.pm @@ -158,6 +158,19 @@ sub _avb_resultset { $self->{_avb_resultset}; } +=head3 GetCatAuthValues +$sth = Koha::AuthorisedValue->GetCatAuthValues($dbh); + +This subroutine retrieves and returns all authorised_values (in the authorised_values table where the category=CCODE) in a column named ccode +=cut + +sub GetCatAuthValues { + my ($self, $dbh) = @_; + my $sth = $dbh->prepare('SELECT authorised_value AS ccode FROM authorised_values WHERE category = "CCODE"'); + $sth->execute(); + return $sth; +} + =head3 type =cut diff --git a/Koha/ItemType.pm b/Koha/ItemType.pm index 8f2279b..a38081b 100644 --- a/Koha/ItemType.pm +++ b/Koha/ItemType.pm @@ -89,6 +89,21 @@ sub translated_descriptions { } @translated_descriptions ]; } +=head3 GetItemTypes +my $sth = Koha::ItemType->GetItemTypes($dbh); + +This subroutine retreives and returns all itemtype values in the itemtypes table. + +=cut + +sub GetItemTypes { + my ($self, $dbh) = @_; + my $sth = $dbh->prepare('SELECT itemtype FROM itemtypes'); + $sth->execute(); + return $sth; +} + + =head3 type =cut diff --git a/Koha/Libraries.pm b/Koha/Libraries.pm index fa2bf17..1cdbc25 100644 --- a/Koha/Libraries.pm +++ b/Koha/Libraries.pm @@ -52,6 +52,23 @@ sub search_filtered { return $self->SUPER::search( $params, $attributes ); } + +=head3 GetBranchcodes +my $sth = Koha::Libraries->GetBranchCodes($dbh); + +This subroutine retrieves and returns all branchcode values in the branches table to the branch_transfer_limits.pl administrative script. + +=cut + +sub GetBranchCodes { + my ($self, $dbh) = @_; + my $query = qq{ SELECT branchcode FROM branches }; + my $sth = $dbh->prepare($query); + $sth->execute(); + return $sth; +} + + =head3 type =cut diff --git a/admin/branch_transfer_limits.pl b/admin/branch_transfer_limits.pl index 84d7210..64fc6ad 100755 --- a/admin/branch_transfer_limits.pl +++ b/admin/branch_transfer_limits.pl @@ -27,6 +27,7 @@ use C4::Context; use C4::Output; use C4::Koha; use C4::Circulation qw{ IsBranchTransferAllowed DeleteBranchTransferLimits CreateBranchTransferLimit }; +use Koha::Libraries; my $input = new CGI; @@ -57,17 +58,19 @@ my @branchcodes; my $sth; if ( $limitType eq 'ccode' ) { - $sth = $dbh->prepare('SELECT authorised_value AS ccode FROM authorised_values WHERE category = "CCODE"'); + $sth = Koha::AuthorisedValue->GetCatAuthValues($dbh); + while ( my $row = $sth->fetchrow_hashref ) { + push( @codes, $row->{ $limitType } ); + } } elsif ( $limitType eq 'itemtype' ) { - $sth = $dbh->prepare('SELECT itemtype FROM itemtypes'); -} -$sth->execute(); -while ( my $row = $sth->fetchrow_hashref ) { - push( @codes, $row->{ $limitType } ); + $sth = Koha::ItemType->GetItemTypes($dbh); + while ( my $row = $sth->fetchrow_hashref ) { + push( @codes, $row->{ $limitType } ); + } } -$sth = $dbh->prepare("SELECT branchcode FROM branches"); -$sth->execute(); + +my $sth = Koha::Libraries->GetBranchCodes($dbh); while ( my $row = $sth->fetchrow_hashref ) { push( @branchcodes, $row->{'branchcode'} ); } diff --git a/t/db_dependent/AuthorisedValues.t b/t/db_dependent/AuthorisedValues.t index ad5ec60..3cd79b5 100644 --- a/t/db_dependent/AuthorisedValues.t +++ b/t/db_dependent/AuthorisedValues.t @@ -1,7 +1,7 @@ #!/usr/bin/perl use Modern::Perl; -use Test::More tests => 15; +use Test::More tests => 17; use t::lib::TestBuilder; @@ -23,6 +23,8 @@ Koha::AuthorisedValueCategories->delete; Koha::AuthorisedValueCategory->new({ category_name => 'av_for_testing' })->store; Koha::AuthorisedValueCategory->new({ category_name => 'aaav_for_testing' })->store; Koha::AuthorisedValueCategory->new({ category_name => 'restricted_for_testing' })->store; +Koha::AuthorisedValueCategory->new({ category_name => 'CCODE' })->store; + my $av1 = Koha::AuthorisedValue->new( { category => 'av_for_testing', @@ -79,6 +81,13 @@ my $av_0 = Koha::AuthorisedValue->new( } )->store(); +my $CatAuthValue = Koha::AuthorisedValue->new( + { + category => 'CCODE', + authorised_value => 'value 5', + } +)->store(); + ok( $av1->id(), 'AV 1 is inserted' ); ok( $av2->id(), 'AV 2 is inserted' ); ok( $av3->id(), 'AV 3 is inserted' ); @@ -114,7 +123,7 @@ my $limits = $av1->branch_limitations; is( @$limits, 2, 'branch_limitations functions correctly both as setter and getter' ); my @categories = Koha::AuthorisedValues->new->categories; -is( @categories, 3, 'There should have 2 categories inserted' ); +is( @categories, 4, 'There should have 4 categories inserted' ); is( $categories[0], $av4->category, 'The first category should be correct (ordered by category name)' ); is( $categories[1], $av1->category, 'The second category should be correct (ordered by category name)' ); @@ -226,4 +235,16 @@ subtest 'search_by_*_field + find_by_koha_field + get_description' => sub { }; }; +ok( $CatAuthValue->id(), 'CatAuthValue is inserted' ); + +sub GetCatAuthValues { + my $dbh = C4::Context->dbh; + my $sth = $dbh->prepare('SELECT authorised_value AS ccode FROM authorised_values WHERE category = "CCODE"'); + $sth->execute(); + return $sth->rows; +} + +my $AuthValues = GetCatAuthValues(); +is($AuthValues, 1, "Correct number of authorised_value's with category=CCODE"); + $schema->storage->txn_rollback; diff --git a/t/db_dependent/Koha/ItemTypes.t b/t/db_dependent/Koha/ItemTypes.t index e1ae330..dd00aa3 100755 --- a/t/db_dependent/Koha/ItemTypes.t +++ b/t/db_dependent/Koha/ItemTypes.t @@ -19,7 +19,7 @@ use Modern::Perl; -use Test::More tests => 20; +use Test::More tests => 21; use Data::Dumper; use Koha::Database; use t::lib::Mocks; @@ -125,5 +125,14 @@ is( 'a translated itemtype desc', 'item types should be sorted by translated description' ); +sub GetItemTypes { + my $dbh = C4::Context->dbh; + my $sth = $dbh->prepare('SELECT itemtype FROM itemtypes'); + $sth->execute(); + return $sth->rows; +} + +my $ItemTypes = GetItemTypes(); +is($ItemTypes,3, "Correct number of itemtypes"); $schema->txn_rollback; diff --git a/t/db_dependent/Koha/Libraries.t b/t/db_dependent/Koha/Libraries.t index 8126b8c..3090735 100644 --- a/t/db_dependent/Koha/Libraries.t +++ b/t/db_dependent/Koha/Libraries.t @@ -19,7 +19,7 @@ use Modern::Perl; -use Test::More tests => 9; +use Test::More tests => 10; use Koha::Library; use Koha::Libraries; @@ -86,5 +86,16 @@ is( Koha::Libraries->search->count, $nb_of_libraries + 1, 'Delete should have de $retrieved_category_2->delete; is( Koha::LibraryCategories->search->count, $nb_of_categories + 2, 'Delete should have deleted the library category' ); +sub GetBranchCodes { + my $dbh = C4::Context->dbh; + my $query = qq{ SELECT branchcode FROM branches }; + my $sth = $dbh->prepare($query); + $sth->execute(); + return $sth->rows; +} + +my $BranchCodes = GetBranchCodes(); +is($BranchCodes, 13, "Correct number of branchcodes"); + $schema->storage->txn_rollback; 1; -- 2.1.4