From 9ad6ffa39d8179d2a795d0a2aadb3bd3dc365a7d Mon Sep 17 00:00:00 2001 From: Kyle M Hall Date: Thu, 23 Oct 2014 10:46:47 -0400 Subject: [PATCH] Bug 10363 - There is no package for authorised values. Test Plan: 1) Apply this patch 2) run updatedatabase.pl 3) prove t/db_dependent/AuthorisedValues.t --- Koha/AuthorisedValue.pm | 180 ++++++++++++++++++++++++++ Koha/Object.pm | 6 +- Koha/Schema/Result/AuthorisedValuesBranch.pm | 28 ++--- installer/data/mysql/updatedatabase.pl | 12 ++ t/db_dependent/AuthorisedValues.t | 83 ++++++++++++ 5 files changed, 287 insertions(+), 22 deletions(-) create mode 100644 Koha/AuthorisedValue.pm create mode 100644 t/db_dependent/AuthorisedValues.t diff --git a/Koha/AuthorisedValue.pm b/Koha/AuthorisedValue.pm new file mode 100644 index 0000000..cbc03b3 --- /dev/null +++ b/Koha/AuthorisedValue.pm @@ -0,0 +1,180 @@ +package Koha::AuthorisedValue; + +# Copyright ByWater Solutions 2014 +# +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it under the +# terms of the GNU General Public License as published by the Free Software +# Foundation; either version 3 of the License, or (at your option) any later +# version. +# +# Koha is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with Koha; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +use Modern::Perl; + +use Carp; + +use Koha::Database; + +use base qw(Koha::Object); + +=head1 NAME + +Koha::AuthorisedValue - Koha Authorised Value Object class + +=head1 API + +=head2 Class Methods + +=cut + +=head3 branch_limitations + +my $limitations = $av->branch_limitations(); + +$av->branch_limitations( \@branchcodes ); + +=cut + +sub branch_limitations { + my ( $self, $branchcodes ) = @_; + + if ($branchcodes) { + return $self->replace_branch_limitations($branchcodes); + } + else { + return $self->get_branch_limitations(); + } + +} + +=head3 get_branch_limitations + +my $limitations = $av->get_branch_limitations(); + +=cut + +sub get_branch_limitations { + my ($self) = @_; + + my @branchcodes = + $self->_avb_resultset->search( { av_id => $self->id() } ) + ->get_column('branchcode')->all(); + + return \@branchcodes; +} + +=head3 add_branch_limitation + +$av->add_branch_limitation( $branchcode ); + +=cut + +sub add_branch_limitation { + my ( $self, $branchcode ) = @_; + + croak("No branchcode passed in!") unless $branchcode; + + my $limitation = $self->_avb_resultset->update_or_create( + { av_id => $self->id(), branchcode => $branchcode } ); + + return $limitation ? 1 : undef; +} + +=cut + +=head3 add_branch_limitation + +$av->del_branch_limitation( $branchcode ); + +=cut + +sub del_branch_limitation { + my ( $self, $branchcode ) = @_; + + croak("No branchcode passed in!") unless $branchcode; + + my $limitation = + $self->_avb_resultset->find( + { av_id => $self->id(), branchcode => $branchcode } ); + + unless ($limitation) { + my $id = $self->id(); + carp( +"No branch limit for branch $branchcode found for av_id $id to delete!" + ); + return; + } + + return $limitation->delete(); +} + +=head3 replace_branch_limitations + +$av->replace_branch_limitations( \@branchcodes ); + +=cut + +sub replace_branch_limitations { + my ( $self, $branchcodes ) = @_; + + $self->_avb_resultset->search( { av_id => $self->id() } )->delete(); + + my @return_values = + map { $self->add_branch_limitation($_) } @$branchcodes; + + return \@return_values; +} + +=head3 lib_opac + +my $description = $av->lib_opac(); + +$av->lib_opac( $description ); + +=cut + +sub opac_description { + my ( $self, $value ) = @_; + + return $self->lib_opac() || $self->lib(); +} + +=head3 Koha::Objects->_resultset + +Returns the internal resultset or creates it if undefined + +=cut + +sub _avb_resultset { + my ($self) = @_; + + $self->{_avb_resultset} ||= + Koha::Database->new()->schema()->resultset('AuthorisedValuesBranch'); + + $self->{_avb_resultset}; +} + +=head3 type + +=cut + +sub type { + return 'AuthorisedValue'; +} + +=head1 AUTHOR + +Kyle M Hall + +=cut + +1; +## Please see file perltidy.ERR diff --git a/Koha/Object.pm b/Koha/Object.pm index d1e0455..c198422 100644 --- a/Koha/Object.pm +++ b/Koha/Object.pm @@ -97,15 +97,15 @@ If the object is new, it will be created. If the object previously existed, it will be updated. Returns: - 1 if the store was a success - 0 if the store failed + $self if the store was a success + undef if the store failed =cut sub store { my ($self) = @_; - return $self->_result()->update_or_insert() ? 1 : 0; + return $self->_result()->update_or_insert() ? $self : undef; } =head3 $object->in_storage(); diff --git a/Koha/Schema/Result/AuthorisedValuesBranch.pm b/Koha/Schema/Result/AuthorisedValuesBranch.pm index 35bb493..bff3735 100644 --- a/Koha/Schema/Result/AuthorisedValuesBranch.pm +++ b/Koha/Schema/Result/AuthorisedValuesBranch.pm @@ -27,22 +27,22 @@ __PACKAGE__->table("authorised_values_branches"); data_type: 'integer' is_foreign_key: 1 - is_nullable: 1 + is_nullable: 0 =head2 branchcode data_type: 'varchar' is_foreign_key: 1 - is_nullable: 1 + is_nullable: 0 size: 10 =cut __PACKAGE__->add_columns( "av_id", - { data_type => "integer", is_foreign_key => 1, is_nullable => 1 }, + { data_type => "integer", is_foreign_key => 1, is_nullable => 0 }, "branchcode", - { data_type => "varchar", is_foreign_key => 1, is_nullable => 1, size => 10 }, + { data_type => "varchar", is_foreign_key => 1, is_nullable => 0, size => 10 }, ); =head1 RELATIONS @@ -59,12 +59,7 @@ __PACKAGE__->belongs_to( "av", "Koha::Schema::Result::AuthorisedValue", { id => "av_id" }, - { - is_deferrable => 1, - join_type => "LEFT", - on_delete => "CASCADE", - on_update => "RESTRICT", - }, + { is_deferrable => 1, on_delete => "CASCADE", on_update => "RESTRICT" }, ); =head2 branchcode @@ -79,18 +74,13 @@ __PACKAGE__->belongs_to( "branchcode", "Koha::Schema::Result::Branch", { branchcode => "branchcode" }, - { - is_deferrable => 1, - join_type => "LEFT", - on_delete => "CASCADE", - on_update => "RESTRICT", - }, + { is_deferrable => 1, on_delete => "CASCADE", on_update => "RESTRICT" }, ); -# Created by DBIx::Class::Schema::Loader v0.07039 @ 2014-07-11 09:26:55 -# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:FlVw5Eu4bXF2ygD0QkwwCg +# Created by DBIx::Class::Schema::Loader v0.07040 @ 2014-10-23 10:48:27 +# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:cjPAyQSK7F7sEkiWjCUE7Q +__PACKAGE__->set_primary_key(__PACKAGE__->columns); -# You can replace this text with custom content, and it will be preserved on regeneration 1; diff --git a/installer/data/mysql/updatedatabase.pl b/installer/data/mysql/updatedatabase.pl index e34d214..d0ed487 100755 --- a/installer/data/mysql/updatedatabase.pl +++ b/installer/data/mysql/updatedatabase.pl @@ -8838,6 +8838,18 @@ if ( CheckVersion($DBversion) ) { SetVersion($DBversion); } +$DBversion = "3.17.00.XXX"; +if ( CheckVersion($DBversion) ) { + $dbh->do(q{ + ALTER TABLE authorised_values_branches + CHANGE av_id av_id INT( 11 ) NOT NULL, + CHANGE branchcode branchcode VARCHAR( 10 ) NOT NULL + }); + print "Upgrade to $DBversion done ( Bug 10363 - There is no package for authorised values. )\n"; + SetVersion($DBversion); + SetVersion ($DBversion); +} + =head1 FUNCTIONS =head2 TableExists($table) diff --git a/t/db_dependent/AuthorisedValues.t b/t/db_dependent/AuthorisedValues.t new file mode 100644 index 0000000..da85748 --- /dev/null +++ b/t/db_dependent/AuthorisedValues.t @@ -0,0 +1,83 @@ +#!/usr/bin/perl + +use Modern::Perl; +use Test::More; # tests => 25; + +use C4::Context; +use Koha::AuthorisedValue; +use Koha::AuthorisedValues; + +my $dbh = C4::Context->dbh; +$dbh->{AutoCommit} = 0; +$dbh->{RaiseError} = 1; + +$dbh->do("DELETE FROM authorised_values"); + +# insert +my $av1 = Koha::AuthorisedValue->new( + { + category => 'av_for_testing', + authorised_value => 'value 1', + lib => 'display value 1', + lib_opac => 'opac display value 1', + imageurl => 'image1.png', + } +)->store(); + +my $av2 = Koha::AuthorisedValue->new( + { + category => 'av_for_testing', + authorised_value => 'value 2', + lib => 'display value 2', + lib_opac => 'opac display value 2', + imageurl => 'image2.png', + } +)->store(); + +my $av3 = Koha::AuthorisedValue->new( + { + category => 'av_for_testing', + authorised_value => 'value 3', + lib => 'display value 3', + lib_opac => 'opac display value 3', + imageurl => 'image2.png', + } +)->store(); + +ok( $av1->id(), 'AV 1 is inserted' ); +ok( $av2->id(), 'AV 2 is inserted' ); +ok( $av3->id(), 'AV 3 is inserted' ); + +is( $av3->opac_description, 'opac display value 3', 'Got correction opac description if lib_opac is set' ); +$av3->lib_opac(''); +is( $av3->opac_description, 'display value 3', 'Got correction opac description if lib_opac is *not* set' ); + +my @authorised_values = + Koha::AuthorisedValues->new()->search( { category => 'av_for_testing' } ); +is( @authorised_values, 3, "Get correct number of values" ); + +my $branches_rs = Koha::Database->new()->schema()->resultset('Branch')->search(); +my $branch1 = $branches_rs->next(); +my $branchcode1 = $branch1->branchcode(); +my $branch2 = $branches_rs->next(); +my $branchcode2 = $branch2->branchcode(); + +$av1->add_branch_limitation( $branchcode1 ); + +@authorised_values = Koha::AuthorisedValues->new()->search( { category => 'av_for_testing', branchcode => $branchcode1 } ); +is( @authorised_values, 3, "Search including value with a branch limit ( branch can use the limited value ) gives correct number of results" ); + +@authorised_values = Koha::AuthorisedValues->new()->search( { category => 'av_for_testing', branchcode => $branchcode2 } ); +is( @authorised_values, 2, "Search including value with a branch limit ( branch *cannot* use the limited value ) gives correct number of results" ); + +$av1->del_branch_limitation( $branchcode1 ); +@authorised_values = Koha::AuthorisedValues->new()->search( { category => 'av_for_testing', branchcode => $branchcode2 } ); +is( @authorised_values, 3, "Branch limitation deleted successfully" ); + +$av1->add_branch_limitation( $branchcode1 ); +$av1->branch_limitations( [ $branchcode1, $branchcode2 ] ); + +my $limits = $av1->branch_limitations; +is( @$limits, 2, 'branch_limitations functions correctly both as setter and getter' ); + +done_testing; -- 1.7.2.5