From 962515ead41654a1e63be3bc058ab8ff23439b2d Mon Sep 17 00:00:00 2001 From: Alex Buckley Date: Sun, 19 Feb 2017 03:07:03 +0000 Subject: [PATCH] Bug 18212 - Moved all sql queries out aqplan.pl into Koha::Libraries.pm, Koha::ItemType.pm, Koha::AuthorisedValue.pm files The new subroutines in the perl modules contain POD Test plan: 1. Apply all the patches attached to this bug report 2. Restart memcached: sudo service memcached restart 3. Drop and recreate the Koha instance database 4. Go through the web installer, selecting all data to be installed in step 3 5. After the web installer is finished log in with the Koha database administrator credentials 6. Create yourself a patron account 7. Set the patron account to have superlibrarian privileges 8. Log out and back in as your newly created patron 9. Navigate to the Acquisitions interface 10. Create a budget named 'testbudget' 11. Create a fund named 'testfund' 12. Navigate to the 'Administrator' interface 13. Write in the URL 'cgi-bin/koha/admin/aqplan.pl' 14. There should be no records showing 15. If you click on Planning->Plan by Months testfund will be displayed 16. Repeat step 14 but click on Planning->Plan by libraries and Planning->Plan by Itemtypes, Both of these pages should also show 'testfund' 17. In your terminal navigate from your Koha instance root directory to t/db_dependent 18. Enter koha shell: sudo koha-shell 19. Run AuthorisedValues unit tests: prove -v AuthorisedValues.t 20. The tests should run successful 21. Exit the koha shell 22. cd into the 'koha' directory from where you are 23. Enter the koha shell and run the ItemTypes.t and Libraries.t unit tests by writing in: prove -v filename.t (both of which should be successful) Sponsored-by: Catalyst IT --- C4/Acquisition.pm | 18 +++++++++++++ Koha/AuthorisedValue.pm | 32 ++++++++++++++++++++++ Koha/ItemType.pm | 17 ++++++++++-- Koha/Libraries.pm | 15 +++++++++++ admin/aqplan.pl | 57 ++++++++++++++++----------------------- t/db_dependent/AuthorisedValues.t | 28 +++++++++++++++++-- t/db_dependent/Koha/ItemTypes.t | 13 +++++++++ t/db_dependent/Koha/Libraries.t | 13 ++++++++- 8 files changed, 154 insertions(+), 39 deletions(-) diff --git a/C4/Acquisition.pm b/C4/Acquisition.pm index ebbe44b..9074504 100644 --- a/C4/Acquisition.pm +++ b/C4/Acquisition.pm @@ -3040,6 +3040,24 @@ sub GetOrderUsers { return @borrowernumbers; } +=head3 GetAuthorisedValues + + + +=cut + +sub GetAuthValuesForCategory{ + my ($self, $category) = @_; + my $schema = Koha::Database->new()->schema(); + my $authvalues = Koha::AuthorisedValues->search({ + 'category' => $category, + order_by => { -asc => 'lib'} + }); + return $authvalues; +} + + + =head3 ModOrderUsers my @order_users_ids = (1, 2, 3); diff --git a/Koha/AuthorisedValue.pm b/Koha/AuthorisedValue.pm index 898b630..c617e67 100644 --- a/Koha/AuthorisedValue.pm +++ b/Koha/AuthorisedValue.pm @@ -158,6 +158,38 @@ sub _avb_resultset { $self->{_avb_resultset}; } +=head3 GetAuthValues +my @AuthValues = Koha::AuthorisedValue->GetAuthValues($authcat); + +s subroutine retrieves and returns all fields from the authorised_values table where the category is equal to the parameter $authcat, and orders the records by the value of the lib field +=cut + +sub GetAuthValues { + my ($self, $authcat) = @_; + my @Getauthorised_values = + Koha::AuthorisedValues->new()->search({ + category => $authcat, + }, + { + order_by => [qw/ lib /] + }); + return @Getauthorised_values; +}; + +=head3 GetDistinctCat +my @DistinctAuthValues = Koha::AuthorisedValue->GetDistinctCat(); + +This subroutine retrieves and returns all the different category values starting with 'a' in the authorised_values table +=cut + +sub GetDistinctCat { + my @distinctauthorised_values = + Koha::AuthorisedValues->new()->search({ + category => { 'like','%a%'}, + }); + return @distinctauthorised_values; +}; + =head3 type =cut diff --git a/Koha/ItemType.pm b/Koha/ItemType.pm index 24be0d2..06ed695 100644 --- a/Koha/ItemType.pm +++ b/Koha/ItemType.pm @@ -89,8 +89,6 @@ sub translated_descriptions { } @translated_descriptions ]; } - - =head3 can_be_deleted my $can_be_deleted = Koha::ItemType->can_be_deleted(); @@ -106,10 +104,25 @@ sub can_be_deleted { return $nb_items + $nb_biblioitems == 0 ? 1 : 0; } +=head3 GetItemTypes +my $sth = Koha::ItemType->GetItemTypes($dbh); + +This subroutine retrieves and returns all the values for the fields: itemtype and description from the itemtypes table. +=cut + +sub GetItemTypes{ + my ($self, $dbh) = @_; + my $query = qq| SELECT itemtype, description FROM itemtypes |; + my $sth = $dbh->prepare($query); + $sth->execute(); + return $sth; +} + =head3 type =cut + sub _type { return 'Itemtype'; } diff --git a/Koha/Libraries.pm b/Koha/Libraries.pm index fa2bf17..06933a8 100644 --- a/Koha/Libraries.pm +++ b/Koha/Libraries.pm @@ -52,6 +52,21 @@ sub search_filtered { return $self->SUPER::search( $params, $attributes ); } +=head3 +my $sth = Koha::Libraries->GetBranches($dbh); + +This subroutine retrieves and returns all the values for the fields: branchcode and branchname from the branches table. + +=cut + +sub GetBranches { + my ($self, $dbh) = @_; + my $query = qq| SELECT branchcode, branchname FROM branches |; + my $sth = $dbh->prepare($query); + $sth->execute(); + return $sth; +} + =head3 type =cut diff --git a/admin/aqplan.pl b/admin/aqplan.pl index a27fb0a..15a9a0c 100755 --- a/admin/aqplan.pl +++ b/admin/aqplan.pl @@ -110,15 +110,14 @@ my $op = $input->param("op"); my $budgets_ref = GetBudgetHierarchy( $budget_period_id, $show_mine?$template->{VARS}->{'USER_INFO'}->{'branchcode'}:'', $show_mine?$template->{VARS}->{'USER_INFO'}->{'borrowernumber'}:'' ); # build categories list -my $sth = $dbh->prepare("select distinct category from authorised_values where category like 'A%' "); -$sth->execute; +my @DistinctAuthValues = Koha::AuthorisedValue->GetDistinctCat(); # the list my @category_list; # a hash, to check that some hardcoded categories exist. my %categories; -while ( my ($category) = $sth->fetchrow_array ) { +while ( my ($category) = each @DistinctAuthValues ) { push( @category_list, $category ); $categories{$category} = 1; } @@ -192,17 +191,14 @@ HideCols($authcat, @hide_cols); } # ------------------------------------------------------------ if ( $authcat =~ m/^Asort/ ) { - my $query = qq{ SELECT * FROM authorised_values WHERE category=? order by lib }; - my $sth = $dbh->prepare($query); - $sth->execute($authcat ); - if ( $sth->rows > 0 ) { - for ( my $i = 0 ; $i < $sth->rows ; $i++ ) { - my $results = $sth->fetchrow_hashref; + my @AuthValues = Koha::AuthorisedValue->GetAuthValues($authcat); + if ( @AuthValues > 0 ) { + while ( my ($AuthValue) = each @AuthValues) { + my $results = $AuthValue; push @authvals, $results->{authorised_value}; $labels{ $results->{authorised_value} } = $results->{lib}; } } - $sth->finish; @authvals = sort { $a <=> $b } @authvals; } elsif ( $authcat eq 'MONTHS' && $budget_period_startdate && $budget_period_enddate ) { @@ -229,37 +225,30 @@ elsif ( $authcat eq 'MONTHS' && $budget_period_startdate && $budget_period_endda } elsif ( $authcat eq 'ITEMTYPES' ) { - my $query = qq| SELECT itemtype, description FROM itemtypes |; - my $sth = $dbh->prepare($query); - $sth->execute( ); + my $sth = Koha::ItemType->GetItemTypes($dbh); - if ( $sth->rows > 0 ) { - for ( my $i = 0 ; $i < $sth->rows ; $i++ ) { - my $results = $sth->fetchrow_hashref; - push @authvals, $results->{itemtype}; - $labels{ $results->{itemtype} } = $results->{description}; - } - } - $sth->finish; + if ( $sth->rows > 0 ) { + for ( my $i = 0 ; $i < $sth->rows ; $i++ ) { + my $results = $sth->fetchrow_hashref; + push @authvals, $results->{itemtype}; + $labels{ $results->{itemtype} } = $results->{description}; + } + } } elsif ( $authcat eq 'BRANCHES' ) { - my $query = qq| SELECT branchcode, branchname FROM branches |; - my $sth = $dbh->prepare($query); - $sth->execute(); + my $sth = Koha::Libraries->GetBranches($dbh); if ( $sth->rows > 0 ) { - for ( my $i = 0 ; $i < $sth->rows ; $i++ ) { - my $results = $sth->fetchrow_hashref; - push @authvals, $results->{branchcode}; - $labels{ $results->{branchcode} } = $results->{branchname}; - } - } - $sth->finish; + for ( my $i = 0 ; $i < $sth->rows ; $i++ ) { + my $results = $sth->fetchrow_hashref; + push @authvals, $results->{branchcode}; + $labels{ $results->{branchcode} } = $results->{branchname}; + } + } } elsif ($authcat) { - my $query = qq{ SELECT * FROM authorised_values WHERE category=? order by lib }; - my $sth = $dbh->prepare($query); - $sth->execute($authcat); + + my $sth = Koha::AuthorisedValue->GetAuthValues($authcat,$dbh); if ( $sth->rows > 0 ) { for ( my $i = 0 ; $i < $sth->rows ; $i++ ) { my $results = $sth->fetchrow_hashref; diff --git a/t/db_dependent/AuthorisedValues.t b/t/db_dependent/AuthorisedValues.t index f7da7a5..f7cd527 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; @@ -92,6 +92,31 @@ my @authorised_values = Koha::AuthorisedValues->new()->search( { category => 'av_for_testing' } ); is( @authorised_values, 3, "Get correct number of values" ); +sub GetAuthValues { + require Test::More; + my @Getauthorised_values = Koha::AuthorisedValues->new()->search({ + category => 'av_for_testing', + }, + { + order_by => [qw/ lib /] + }); +}; + +my @Getauthorised_values = GetAuthValues(); +is( @Getauthorised_values, 3, "Get correct number of values" ); + +sub GetDistinctCat { + require Test::More; + my @distinctauthorised_values = + Koha::AuthorisedValues->new()->search({ + category => { 'like','%a%'}, + }); +} + +my @distinctauthorised_values = GetDistinctCat(); +is( @distinctauthorised_values, 4, "Correct number of distinct values" ); + + my $branchcode1 = $builder->build({ source => 'Branch' })->{branchcode}; my $branchcode2 = $builder->build({ source => 'Branch' })->{branchcode}; @@ -227,5 +252,4 @@ subtest 'search_by_*_field + find_by_koha_field + get_description' => sub { ); }; }; - $schema->storage->txn_rollback; diff --git a/t/db_dependent/Koha/ItemTypes.t b/t/db_dependent/Koha/ItemTypes.t index ac239b3..4932950 100755 --- a/t/db_dependent/Koha/ItemTypes.t +++ b/t/db_dependent/Koha/ItemTypes.t @@ -26,6 +26,7 @@ use t::lib::Mocks; use Koha::Items; use Koha::Biblioitems; use t::lib::TestBuilder; +use Koha::ItemType; BEGIN { use_ok('Koha::ItemType'); @@ -118,6 +119,18 @@ is( $type->summary, 'summary', 'summary' ); is( $type->checkinmsg, 'checkinmsg', 'checkinmsg' ); is( $type->checkinmsgtype, 'checkinmsgtype', 'checkinmsgtype' ); + +sub GetItemTypes { + my $dbh = C4::Context->dbh; + my $query = qq| SELECT itemtype, description FROM itemtypes |; + my $sth = $dbh->prepare($query); + $sth->execute(); + return $sth->rows; +} + +my $ItemTypes = GetItemTypes(); +is($ItemTypes, 3, "Correct number of item types" ); + t::lib::Mocks::mock_preference('language', 'en'); t::lib::Mocks::mock_preference('opaclanguages', 'en'); my $itemtypes = Koha::ItemTypes->search_with_localization; diff --git a/t/db_dependent/Koha/Libraries.t b/t/db_dependent/Koha/Libraries.t index 483c7c8..aa23beb 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,4 +86,15 @@ 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 GetBranches { + my $dbh = C4::Context->dbh; + my $query = qq| SELECT branchcode, branchname FROM branches |; + my $sth = $dbh->prepare($query); + $sth->execute(); + return $sth->rows; +} + +my $branches = GetBranches(); +is($branches, 13, "correcct number of branches" ); + $schema->storage->txn_rollback; -- 2.1.4