@@ -, +, @@ $ kshell k$ prove t/db_dependent/Koha/Old.t t/db_dependent/Koha/Checkout.t --- 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 --- a/Koha/Checkout.pm +++ a/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 --- a/Koha/Old/Checkout.pm +++ a/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 --- a/Koha/Schema/Result/Issue.pm +++ a/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'; } --- a/Koha/Schema/Result/OldIssue.pm +++ a/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'; } --- a/t/db_dependent/Koha/Checkout.t +++ a/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; +}; --- a/t/db_dependent/Koha/Old.t +++ a/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; +}; --