Bug 15294: Koha::Libraries - Move existing Koha::Branch[es]
authorJonathan Druart <jonathan.druart@bugs.koha-community.org>
Wed, 2 Dec 2015 16:53:33 +0000 (16:53 +0000)
committerBrendan Gallagher <brendan@bywatersolutions.com>
Thu, 14 Jan 2016 15:22:26 +0000 (15:22 +0000)
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.

Tested both patches together, works as expected.
Signed-off-by: Marc VĂ©ron <veron@veron.ch>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Signed-off-by: Brendan Gallagher brendan@bywatersolutions.com
Koha/Branch.pm [deleted file]
Koha/Branches.pm [deleted file]
Koha/Hold.pm
Koha/Item.pm
Koha/Libraries.pm [new file with mode: 0644]
Koha/Library.pm [new file with mode: 0644]
Koha/LibraryCategories.pm [new file with mode: 0644]
Koha/LibraryCategory.pm [new file with mode: 0644]
t/db_dependent/Items.t
t/db_dependent/Koha/Libraries.t [new file with mode: 0644]

diff --git a/Koha/Branch.pm b/Koha/Branch.pm
deleted file mode 100644 (file)
index cd36c74..0000000
+++ /dev/null
@@ -1,52 +0,0 @@
-package Koha::Branch;
-
-# 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::Branch - Koha Branch object class
-
-=head1 API
-
-=head2 Class Methods
-
-=cut
-
-=head3 type
-
-=cut
-
-sub type {
-    return 'Branch';
-}
-
-=head1 AUTHOR
-
-Kyle M Hall <kyle@bywatersolutions.com>
-
-=cut
-
-1;
diff --git a/Koha/Branches.pm b/Koha/Branches.pm
deleted file mode 100644 (file)
index c1078a2..0000000
+++ /dev/null
@@ -1,62 +0,0 @@
-package Koha::Branches;
-
-# 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 Koha::Branch;
-
-use base qw(Koha::Objects);
-
-=head1 NAME
-
-Koha::Branches - Koha Branch object set class
-
-=head1 API
-
-=head2 Class Methods
-
-=cut
-
-=head3 type
-
-=cut
-
-sub type {
-    return 'Branch';
-}
-
-=head3 object_class
-
-=cut
-
-sub object_class {
-    return 'Koha::Branch';
-}
-
-=head1 AUTHOR
-
-Kyle M Hall <kyle@bywatersolutions.com>
-
-=cut
-
-1;
index 518d0ae..871f3e1 100644 (file)
@@ -22,12 +22,12 @@ use Modern::Perl;
 use Carp;
 
 use C4::Context qw(preference);
-use Koha::DateUtils qw(dt_from_string);
 
+use Koha::DateUtils qw(dt_from_string);
 use Koha::Borrowers;
 use Koha::Biblios;
-use Koha::Branches;
 use Koha::Items;
+use Koha::Libraries;
 
 use base qw(Koha::Object);
 
@@ -168,14 +168,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};
 }
index e8543e1..3aa261d 100644 (file)
@@ -23,8 +23,8 @@ use Carp;
 
 use Koha::Database;
 
-use Koha::Branches;
 use Koha::Borrowers;
+use Koha::Libraries;
 
 use base qw(Koha::Object);
 
@@ -57,7 +57,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};
 }
@@ -69,7 +69,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/Libraries.pm b/Koha/Libraries.pm
new file mode 100644 (file)
index 0000000..eeb3213
--- /dev/null
@@ -0,0 +1,52 @@
+package Koha::Libraries;
+
+# 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 Koha::Library;
+
+use base qw(Koha::Objects);
+
+=head1 NAME
+
+Koha::Libraries - Koha Library Object set class
+
+=head1 API
+
+=head2 Class Methods
+
+=cut
+
+=head3 type
+
+=cut
+
+sub type {
+    return 'Branch';
+}
+
+sub object_class {
+    return 'Koha::Library';
+}
+
+1;
diff --git a/Koha/Library.pm b/Koha/Library.pm
new file mode 100644 (file)
index 0000000..7420c6a
--- /dev/null
@@ -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/LibraryCategories.pm b/Koha/LibraryCategories.pm
new file mode 100644 (file)
index 0000000..bacbbad
--- /dev/null
@@ -0,0 +1,52 @@
+package Koha::LibraryCategories;
+
+# 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 Koha::LibraryCategory;
+
+use base qw(Koha::Objects);
+
+=head1 NAME
+
+Koha::LibraryCategories - Koha Library Category Object set class
+
+=head1 API
+
+=head2 Class Methods
+
+=cut
+
+=head3 type
+
+=cut
+
+sub type {
+    return 'Branchcategory';
+}
+
+sub object_class {
+    return 'Koha::LibraryCategory';
+}
+
+1;
diff --git a/Koha/LibraryCategory.pm b/Koha/LibraryCategory.pm
new file mode 100644 (file)
index 0000000..f675d89
--- /dev/null
@@ -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;
index 35755c6..c0603b9 100755 (executable)
@@ -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 (file)
index 0000000..8e21567
--- /dev/null
@@ -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 <http://www.gnu.org/licenses>.
+
+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;