From 57f7b65c53ce5eeb1a215bdc5e3b39d4f03e81f6 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Tue, 14 Jan 2020 12:47:19 -0300 Subject: [PATCH] Bug 24419: Add Koha::Suggestion->suggestor accessor This patch adds a ->suggestor accessor to the Koha::Suggestion class. It will return undef if no suggestor, and a Koha::Patron object otherwise. To test: 1. Apply this patch 2. Run: $ kshell k$ prove t/db_dependent/Koha/Suggestion.t => SUCCESS: Tests pass! 3. Sign off :-D Signed-off-by: Tomas Cohen Arazi Signed-off-by: Kyle M Hall --- Koha/Schema/Result/Suggestion.pm | 12 ++++++++ Koha/Suggestion.pm | 24 +++++++++++++-- t/db_dependent/Koha/Suggestion.t | 50 ++++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 t/db_dependent/Koha/Suggestion.t diff --git a/Koha/Schema/Result/Suggestion.pm b/Koha/Schema/Result/Suggestion.pm index c65b3da7fe..26e5ce8006 100644 --- a/Koha/Schema/Result/Suggestion.pm +++ b/Koha/Schema/Result/Suggestion.pm @@ -443,6 +443,18 @@ __PACKAGE__->belongs_to( # Created by DBIx::Class::Schema::Loader v0.07046 @ 2019-03-11 12:56:41 # DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:UsG/gxLa0HMMbcpbscV29Q +__PACKAGE__->belongs_to( + "suggestor", + "Koha::Schema::Result::Borrower", + { "foreign.borrowernumber" => "self.suggestedby" }, + { + is_deferrable => 1, + join_type => "LEFT", + on_delete => "SET NULL", + on_update => "CASCADE", + }, +); + sub koha_objects_class { 'Koha::Suggestions'; } diff --git a/Koha/Suggestion.pm b/Koha/Suggestion.pm index 2fef425197..61b810aa92 100644 --- a/Koha/Suggestion.pm +++ b/Koha/Suggestion.pm @@ -23,6 +23,7 @@ use Carp; use Koha::Database; use Koha::DateUtils qw(dt_from_string); +use Koha::Patrons; use base qw(Koha::Object); @@ -32,7 +33,7 @@ Koha::Suggestion - Koha Suggestion object class =head1 API -=head2 Class Methods +=head2 Class methods =cut @@ -53,7 +54,26 @@ sub store { return $self->SUPER::store(); } -=head3 type +=head3 suggestor + + my $patron = $suggestion->suggestor + +Returns the I for the suggestion generator. I is +returned if no suggestor is linked. + +=cut + +sub suggestor { + my ($self) = @_; + + my $suggestor_rs = $self->_result->suggestor; + return unless $suggestor_rs; + return Koha::Patron->_new_from_dbic($suggestor_rs); +} + +=head2 Internal methods + +=head3 _type =cut diff --git a/t/db_dependent/Koha/Suggestion.t b/t/db_dependent/Koha/Suggestion.t new file mode 100644 index 0000000000..dbd7f2acbb --- /dev/null +++ b/t/db_dependent/Koha/Suggestion.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 Koha::Database; +use Koha::Suggestions; + +use t::lib::TestBuilder; + +my $schema = Koha::Database->new->schema; +my $builder = t::lib::TestBuilder->new; + +subtest 'suggestor() tests' => sub { + + plan tests => 3; + + $schema->storage->txn_begin; + + my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); + my $suggestion = $builder->build_object( + { class => 'Koha::Suggestions', value => { suggestedby => undef } } ); + + is( $suggestion->suggestor, undef, 'Returns undef if no suggestor' ); + # Set a borrowernumber + $suggestion->suggestedby($patron->borrowernumber)->store->discard_changes; + my $suggestor = $suggestion->suggestor; + is( ref($suggestor), 'Koha::Patron', 'Type is correct for suggestor' ); + is_deeply( $patron->unblessed, $suggestor->unblessed, 'It returns the right patron' ); + + $schema->storage->txn_rollback; +}; -- 2.21.1 (Apple Git-122.3)