From d0446c835c88c44711cc98dba95ccc757bada54b 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->suggester accessor This patch adds a ->suggester accessor to the Koha::Suggestion class. It will return undef if no suggester, 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 Signed-off-by: Martin Renvoize --- 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..1668744771 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( + "suggester", + "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..9fa3e1fde3 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 suggester + + my $patron = $suggestion->suggester + +Returns the I for the suggestion generator. I is +returned if no suggester is linked. + +=cut + +sub suggester { + my ($self) = @_; + + my $suggester_rs = $self->_result->suggester; + return unless $suggester_rs; + return Koha::Patron->_new_from_dbic($suggester_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..1de342436e --- /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 'suggester() 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->suggester, undef, 'Returns undef if no suggester' ); + # Set a borrowernumber + $suggestion->suggestedby($patron->borrowernumber)->store->discard_changes; + my $suggester = $suggestion->suggester; + is( ref($suggester), 'Koha::Patron', 'Type is correct for suggester' ); + is_deeply( $patron->unblessed, $suggester->unblessed, 'It returns the right patron' ); + + $schema->storage->txn_rollback; +}; -- 2.39.5