From a333f8217f254ca41f158689d81ab586acf84dcf Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Wed, 2 Dec 2015 16:53:33 +0000 Subject: [PATCH] Bug 15294: Koha::Libraries - Move existing Koha::Branch[es] There was already 2 Koha::Branch[es] using Koha::Object[s] before. For this new rewrite, it seems preferable to start with good basis and name the new modules Koha::Library and Koha::Libraries. --- Koha/Hold.pm | 6 +- Koha/Item.pm | 6 +- Koha/{Branch.pm => Libraries.pm} | 18 +++--- Koha/Library.pm | 65 +++++++++++++++++++++ Koha/{Branches.pm => LibraryCategories.pm} | 22 ++------ Koha/LibraryCategory.pm | 58 +++++++++++++++++++ t/db_dependent/Items.t | 4 +- t/db_dependent/Koha/Libraries.t | 90 ++++++++++++++++++++++++++++++ 8 files changed, 236 insertions(+), 33 deletions(-) rename Koha/{Branch.pm => Libraries.pm} (80%) create mode 100644 Koha/Library.pm rename Koha/{Branches.pm => LibraryCategories.pm} (77%) create mode 100644 Koha/LibraryCategory.pm create mode 100644 t/db_dependent/Koha/Libraries.t diff --git a/Koha/Hold.pm b/Koha/Hold.pm index ad84af3..2b1c2cf 100644 --- a/Koha/Hold.pm +++ b/Koha/Hold.pm @@ -22,7 +22,7 @@ use Modern::Perl; use Carp; use C4::Context qw(preference); -use Koha::Branches; +use Koha::Libraries; use Koha::Biblios; use Koha::Items; use Koha::DateUtils qw(dt_from_string); @@ -106,14 +106,14 @@ sub item { =head3 branch -Returns the related Koha::Branch object for this Hold +Returns the related Koha::Library object for this Hold =cut sub branch { my ($self) = @_; - $self->{_branch} ||= Koha::Branches->find( $self->branchcode() ); + $self->{_branch} ||= Koha::Libraries->find( $self->branchcode() ); return $self->{_branch}; } diff --git a/Koha/Item.pm b/Koha/Item.pm index 2dcccfb..c6ac28d 100644 --- a/Koha/Item.pm +++ b/Koha/Item.pm @@ -23,7 +23,7 @@ use Carp; use Koha::Database; -use Koha::Branches; +use Koha::Libraries; use base qw(Koha::Object); @@ -56,7 +56,7 @@ sub effective_itemtype { sub home_branch { my ($self) = @_; - $self->{_home_branch} ||= Koha::Branches->find( $self->homebranch() ); + $self->{_home_branch} ||= Koha::Libraries->find( $self->homebranch() ); return $self->{_home_branch}; } @@ -68,7 +68,7 @@ sub home_branch { sub holding_branch { my ($self) = @_; - $self->{_holding_branch} ||= Koha::Branches->find( $self->holdingbranch() ); + $self->{_holding_branch} ||= Koha::Libraries->find( $self->holdingbranch() ); return $self->{_holding_branch}; } diff --git a/Koha/Branch.pm b/Koha/Libraries.pm similarity index 80% rename from Koha/Branch.pm rename to Koha/Libraries.pm index cd36c74..eeb3213 100644 --- a/Koha/Branch.pm +++ b/Koha/Libraries.pm @@ -1,6 +1,6 @@ -package Koha::Branch; +package Koha::Libraries; -# Copyright ByWater Solutions 2014 +# Copyright 2015 Koha Development team # # This file is part of Koha. # @@ -23,11 +23,13 @@ use Carp; use Koha::Database; -use base qw(Koha::Object); +use Koha::Library; + +use base qw(Koha::Objects); =head1 NAME -Koha::Branch - Koha Branch object class +Koha::Libraries - Koha Library Object set class =head1 API @@ -43,10 +45,8 @@ sub type { return 'Branch'; } -=head1 AUTHOR - -Kyle M Hall - -=cut +sub object_class { + return 'Koha::Library'; +} 1; diff --git a/Koha/Library.pm b/Koha/Library.pm new file mode 100644 index 0000000..7420c6a --- /dev/null +++ b/Koha/Library.pm @@ -0,0 +1,65 @@ +package Koha::Library; + +# Copyright 2015 Koha Development team +# +# 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::Library - Koha Library Object class + +=head1 API + +=head2 Class Methods + +=cut + +sub get_categories { + my ( $self, $params ) = @_; + # TODO This should return Koha::LibraryCategories + return $self->{_result}->categorycodes( $params ); +} + +sub update_categories { + my ( $self, $categories ) = @_; + $self->_result->delete_related( 'branchrelations' ); + $self->add_to_categories( $categories ); +} + +sub add_to_categories { + my ( $self, $categories ) = @_; + for my $category ( @$categories ) { + $self->_result->add_to_categorycodes( $category->_result ); + } +} + +=head3 type + +=cut + +sub type { + return 'Branch'; +} + +1; diff --git a/Koha/Branches.pm b/Koha/LibraryCategories.pm similarity index 77% rename from Koha/Branches.pm rename to Koha/LibraryCategories.pm index c1078a2..bacbbad 100644 --- a/Koha/Branches.pm +++ b/Koha/LibraryCategories.pm @@ -1,6 +1,6 @@ -package Koha::Branches; +package Koha::LibraryCategories; -# Copyright ByWater Solutions 2014 +# Copyright 2015 Koha Development team # # This file is part of Koha. # @@ -23,13 +23,13 @@ use Carp; use Koha::Database; -use Koha::Branch; +use Koha::LibraryCategory; use base qw(Koha::Objects); =head1 NAME -Koha::Branches - Koha Branch object set class +Koha::LibraryCategories - Koha Library Category Object set class =head1 API @@ -42,21 +42,11 @@ Koha::Branches - Koha Branch object set class =cut sub type { - return 'Branch'; + return 'Branchcategory'; } -=head3 object_class - -=cut - sub object_class { - return 'Koha::Branch'; + return 'Koha::LibraryCategory'; } -=head1 AUTHOR - -Kyle M Hall - -=cut - 1; diff --git a/Koha/LibraryCategory.pm b/Koha/LibraryCategory.pm new file mode 100644 index 0000000..f675d89 --- /dev/null +++ b/Koha/LibraryCategory.pm @@ -0,0 +1,58 @@ +package Koha::LibraryCategory; + +# Copyright 2015 Koha Development team +# +# 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::LibraryCategory - Koha Library Category Object class + +=head1 API + +=head2 Class Methods + +=cut + +sub new { + my ( $self, $params ) = @_; + $params->{categorycode} = uc( $params->{categorycode} ); + return $self->SUPER::new( $params ); +} + +sub branchcodes{ + my ( $self, $params ) = @_; + # TODO This should return Koha::Libraries + return $self->{_result}->branchcodes( $params ); +} + +=head3 type + +=cut + +sub type { + return 'Branchcategory'; +} + +1; diff --git a/t/db_dependent/Items.t b/t/db_dependent/Items.t index 35755c6..c0603b9 100755 --- a/t/db_dependent/Items.t +++ b/t/db_dependent/Items.t @@ -453,11 +453,11 @@ subtest 'Koha::Item(s) tests' => sub { is( ref($item), 'Koha::Item', "Got Koha::Item" ); my $homebranch = $item->home_branch(); - is( ref($homebranch), 'Koha::Branch', "Got Koha::Branch from home_branch method" ); + is( ref($homebranch), 'Koha::Library', "Got Koha::Library from home_branch method" ); is( $homebranch->branchcode(), $library1->{branchcode}, "Home branch code matches homebranch" ); my $holdingbranch = $item->holding_branch(); - is( ref($holdingbranch), 'Koha::Branch', "Got Koha::Branch from holding_branch method" ); + is( ref($holdingbranch), 'Koha::Library', "Got Koha::Library from holding_branch method" ); is( $holdingbranch->branchcode(), $library2->{branchcode}, "Home branch code matches holdingbranch" ); $schema->storage->txn_rollback; diff --git a/t/db_dependent/Koha/Libraries.t b/t/db_dependent/Koha/Libraries.t new file mode 100644 index 0000000..8e21567 --- /dev/null +++ b/t/db_dependent/Koha/Libraries.t @@ -0,0 +1,90 @@ +#!/usr/bin/perl + +# Copyright 2015 Koha Development team +# +# 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, see . + +use Modern::Perl; + +use Test::More tests => 9; + +use Koha::Library; +use Koha::Libraries; +use Koha::LibraryCategory; +use Koha::LibraryCategories; +use Koha::Database; + +use t::lib::TestBuilder; + +my $schema = Koha::Database->new->schema; +$schema->storage->txn_begin; + +my $builder = t::lib::TestBuilder->new; +my $nb_of_libraries = Koha::Libraries->search->count; +my $nb_of_categories = Koha::LibraryCategories->search->count; +my $new_library_1 = Koha::Library->new({ + branchcode => 'my_bc_1', + branchname => 'my_branchname_1', + branchnotes => 'my_branchnotes_1', +})->store; +my $new_library_2 = Koha::Library->new({ + branchcode => 'my_bc_2', + branchname => 'my_branchname_2', + branchnotes => 'my_branchnotes_2', +})->store; +my $new_category_1 = Koha::LibraryCategory->new({ + categorycode => 'my_cc_1', + categoryname => 'my_categoryname_1', + codedescription => 'my_codedescription_1', + categorytype => 'properties', +} )->store; +my $new_category_2 = Koha::LibraryCategory->new( { + categorycode => 'my_cc_2', + categoryname => 'my_categoryname_2', + codedescription => 'my_codedescription_2', + categorytype => 'searchdomain', +} )->store; +my $new_category_3 = Koha::LibraryCategory->new( { + categorycode => 'my_cc_3', + categoryname => 'my_categoryname_3', + codedescription => 'my_codedescription_3', + categorytype => 'searchdomain', +} )->store; + +is( Koha::Libraries->search->count, $nb_of_libraries + 2, 'The 2 libraries should have been added' ); +is( Koha::LibraryCategories->search->count, $nb_of_categories + 3, 'The 3 library categories should have been added' ); + +$new_library_1->add_to_categories( [$new_category_1] ); +$new_library_2->add_to_categories( [$new_category_2] ); +my $retrieved_library_1 = Koha::Libraries->find( $new_library_1->branchcode ); +is( $retrieved_library_1->branchname, $new_library_1->branchname, 'Find a library by branchcode should return the correct library' ); +is( Koha::Libraries->find( $new_library_1->branchcode )->get_categories->count, 1, '1 library should have been linked to the category 1' ); + +$retrieved_library_1->update_categories( [ $new_category_2, $new_category_3 ] ); +is( Koha::Libraries->find( $new_library_1->branchcode )->get_categories->count, 2, '2 libraries should have been linked to the category 2' ); + +my $retrieved_category_2 = Koha::LibraryCategories->find( $new_category_2->categorycode ); +is( $retrieved_category_2->branchcodes->count, 2, '2 libraries should have been linked to the category_2' ); +is( $retrieved_category_2->categorycode, uc('my_cc_2'), 'The Koha::LibraryCategory constructor should have upercased the categorycode' ); + +$retrieved_library_1->delete; +is( Koha::Libraries->search->count, $nb_of_libraries + 1, 'Delete should have deleted the library' ); + +$retrieved_category_2->delete; +is( Koha::LibraryCategories->search->count, $nb_of_categories + 2, 'Delete should have deleted the library category' ); + +$schema->storage->txn_rollback; +1; -- 2.1.0