From 30cf6649623c07c471fdaaf5b32d8e4dfa9703e5 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Mon, 31 Aug 2020 15:25:05 -0300 Subject: [PATCH] Bug 26327: Add ->library method to Koha::*Checkout This patch adds a 'library' relationship to the Issue and OldIssue resultsets, to be used in the Koha::*Checkout classes corresponding accessors. This way we will be able to eventually embed and filter on library fields on the API. The ->library method is then added to the classes- To test: 1. Apply this patch 2. Run: $ kshell k$ prove t/db_dependent/Koha/Old.t t/db_dependent/Koha/Checkout.t => SUCCESS: Tests pass! 3. Sign off :-D Signed-off-by: Kyle M Hall --- Koha/Checkout.pm | 15 ++++++++++ Koha/Old/Checkout.pm | 16 +++++++++++ Koha/Schema/Result/Issue.pm | 12 ++++++++ Koha/Schema/Result/OldIssue.pm | 10 +++++++ t/db_dependent/Koha/Checkout.t | 50 ++++++++++++++++++++++++++++++++++ t/db_dependent/Koha/Old.t | 40 +++++++++++++++++++++++---- 6 files changed, 138 insertions(+), 5 deletions(-) create mode 100644 t/db_dependent/Koha/Checkout.t diff --git a/Koha/Checkout.pm b/Koha/Checkout.pm index 9180bf4df7..8fd7b6ad03 100644 --- a/Koha/Checkout.pm +++ b/Koha/Checkout.pm @@ -28,6 +28,7 @@ use Koha::Checkouts::ReturnClaims; use Koha::Database; use Koha::DateUtils; use Koha::Items; +use Koha::Libraries; use base qw(Koha::Object); @@ -77,6 +78,20 @@ sub item { return Koha::Item->_new_from_dbic( $item_rs ); } +=head3 library + +my $library = $checkout->library; + +Return the library in which the transaction took place + +=cut + +sub library { + my ( $self ) = @_; + my $library_rs = $self->_result->library; + return Koha::Library->_new_from_dbic( $library_rs ); +} + =head3 patron my $patron = $checkout->patron diff --git a/Koha/Old/Checkout.pm b/Koha/Old/Checkout.pm index 9dbc58e3a8..7b62111f0c 100644 --- a/Koha/Old/Checkout.pm +++ b/Koha/Old/Checkout.pm @@ -18,6 +18,7 @@ package Koha::Old::Checkout; use Modern::Perl; use Koha::Database; +use Koha::Libraries; use base qw(Koha::Object); @@ -43,6 +44,21 @@ sub item { return Koha::Item->_new_from_dbic( $item_rs ); } +=head3 library + +my $library = $checkout->library; + +Return the library in which the transaction took place. Might return I. + +=cut + +sub library { + my ( $self ) = @_; + my $library_rs = $self->_result->library; + return unless $library_rs; + return Koha::Library->_new_from_dbic( $library_rs ); +} + =head3 patron my $patron = $checkout->patron diff --git a/Koha/Schema/Result/Issue.pm b/Koha/Schema/Result/Issue.pm index 8bb16b6b92..5ffba8af90 100644 --- a/Koha/Schema/Result/Issue.pm +++ b/Koha/Schema/Result/Issue.pm @@ -303,6 +303,18 @@ __PACKAGE__->belongs_to( }, ); +__PACKAGE__->belongs_to( + "library", + "Koha::Schema::Result::Branch", + { "foreign.branchcode" => "self.branchcode" }, + { + is_deferrable => 1, + join_type => "LEFT", + on_delete => "CASCADE", + on_update => "CASCADE", + }, +); + sub koha_object_class { 'Koha::Checkout'; } diff --git a/Koha/Schema/Result/OldIssue.pm b/Koha/Schema/Result/OldIssue.pm index f1ff5c2fa6..6e5b6e81b3 100644 --- a/Koha/Schema/Result/OldIssue.pm +++ b/Koha/Schema/Result/OldIssue.pm @@ -261,6 +261,16 @@ __PACKAGE__->belongs_to( }, ); +__PACKAGE__->belongs_to( + "library", + "Koha::Schema::Result::Branch", + { "foreign.branchcode" => "self.branchcode" }, + { + is_deferrable => 1, + join_type => "LEFT", + }, +); + sub koha_object_class { 'Koha::Old::Checkout'; } diff --git a/t/db_dependent/Koha/Checkout.t b/t/db_dependent/Koha/Checkout.t new file mode 100644 index 0000000000..f8288ea02a --- /dev/null +++ b/t/db_dependent/Koha/Checkout.t @@ -0,0 +1,50 @@ +#!/usr/bin/perl + +# Copyright 2020 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 => 1; +use t::lib::TestBuilder; + +use Koha::Database; + +my $builder = t::lib::TestBuilder->new; +my $schema = Koha::Database->new->schema; + +subtest 'library() tests' => sub { + + plan tests => 2; + + $schema->storage->txn_begin; + + my $library = $builder->build_object({ class => 'Koha::Libraries' }); + my $checkout = $builder->build_object( + { + class => 'Koha::Checkouts', + value => { + branchcode => $library->branchcode + } + } + ); + + is( ref($checkout->library), 'Koha::Library', 'Object type is correct' ); + is( $checkout->library->branchcode, $library->branchcode, 'Right library linked' ); + + $schema->storage->txn_rollback; +}; diff --git a/t/db_dependent/Koha/Old.t b/t/db_dependent/Koha/Old.t index de2b0ba69f..42bdbe4d65 100644 --- a/t/db_dependent/Koha/Old.t +++ b/t/db_dependent/Koha/Old.t @@ -19,23 +19,25 @@ use Modern::Perl; -use Test::More tests => 2; +use Test::More tests => 3; use Koha::Database; use Koha::Old::Patrons; use Koha::Old::Biblios; +use Koha::Old::Checkouts; use Koha::Old::Items; use t::lib::TestBuilder; -my $schema = Koha::Database->new->schema; -$schema->storage->txn_begin; - +my $schema = Koha::Database->new->schema; my $builder = t::lib::TestBuilder->new; subtest 'Koha::Old::Patrons' => sub { + plan tests => 1; + $schema->storage->txn_begin; + my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); my $patron_unblessed = $patron->unblessed; $patron->move_to_deleted; @@ -48,10 +50,38 @@ subtest 'Koha::Old::Patrons' => sub { delete $deleted_patron->{updated_on}; delete $patron_unblessed->{updated_on}; is_deeply( $deleted_patron, $patron_unblessed ); + + $schema->storage->txn_rollback; }; subtest 'Koha::Old::Biblios and Koha::Old::Items' => sub { # Cannot be tested in a meaningful way so far ok(1); }; -$schema->storage->txn_rollback; + +subtest 'Koha::Old::Checkout->library() tests' => sub { + + plan tests => 3; + + $schema->storage->txn_begin; + + my $library = $builder->build_object({ class => 'Koha::Libraries' }); + my $checkout = $builder->build_object( + { + class => 'Koha::Old::Checkouts', + value => { + branchcode => $library->branchcode + } + } + ); + + is( ref($checkout->library), 'Koha::Library', 'Object type is correct' ); + is( $checkout->library->branchcode, $library->branchcode, 'Right library linked' ); + + $library->delete; + $checkout->discard_changes; + + is( $checkout->library, undef, 'If the library has been deleted, undef is returned' ); + + $schema->storage->txn_rollback; +}; -- 2.24.1 (Apple Git-126)