From 54dbb852486b004a73dc6aabae6dda4fcecb2573 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Tue, 3 May 2022 11:18:10 -0300 Subject: [PATCH] Bug 30663: Add Koha::Suggestions helper methods This patch adds the following helper methods: * filter_by_pending * filter_by_suggested_days_range This methods follow basically what's done in opac-suggestions.pl I chose 'pending' as opposed to 'open' to follow what we use in the UI which might be the case because of being more accurate for end users. To test: 1. Apply this patch 2. Run: $ kshell k$ prove t/db_dependent/Koha/Suggestions.t => SUCCESS: Tests pass! 3. Sign off :-D Signed-off-by: Tomas Cohen Arazi Signed-off-by: Lucas Gass Signed-off-by: Martin Renvoize --- Koha/Suggestions.pm | 32 +++++++++++++++ t/db_dependent/Koha/Suggestions.t | 65 ++++++++++++++++++++++++++++++- 2 files changed, 95 insertions(+), 2 deletions(-) diff --git a/Koha/Suggestions.pm b/Koha/Suggestions.pm index 5fadfd163b..9b1d266586 100644 --- a/Koha/Suggestions.pm +++ b/Koha/Suggestions.pm @@ -21,6 +21,7 @@ use Modern::Perl; use Koha::Database; +use Koha::DateUtils qw(dt_from_string); use Koha::Suggestion; use base qw(Koha::Objects); @@ -62,6 +63,37 @@ sub search_limited { return $resultset->search( $params, $attributes); } +=head3 filter_by_pending + + my $open = $suggestions->filter_by_pending; + +Filters the resultset on those that are considered pending (i.e. STATUS = ASKED). + +=cut + +sub filter_by_pending { + my ($self) = @_; + + return $self->search( { STATUS => 'ASKED' } ); +} + +=head3 filter_by_suggested_days_range + + my $suggestions = $suggestions->filter_by_suggested_days_range( $days ); + +Filters the resultset on those placed within some I<$days> range. + +=cut + +sub filter_by_suggested_days_range { + my ( $self, $days ) = @_; + + my $dtf = Koha::Database->new->schema->storage->datetime_parser; + + return $self->search( + { suggesteddate => { '>=' => $dtf->format_date( dt_from_string->subtract( days => $days ) ) } } ); +} + =head2 Internal methods =head3 _type diff --git a/t/db_dependent/Koha/Suggestions.t b/t/db_dependent/Koha/Suggestions.t index 8dbb93afd3..ae95ebe274 100755 --- a/t/db_dependent/Koha/Suggestions.t +++ b/t/db_dependent/Koha/Suggestions.t @@ -19,10 +19,9 @@ use Modern::Perl; -use Test::More tests => 9; +use Test::More tests => 11; use Test::Exception; -use Koha::Suggestion; use Koha::Suggestions; use Koha::Database; use Koha::DateUtils qw( dt_from_string output_pref ); @@ -329,3 +328,65 @@ subtest 'search_limited() tests' => sub { $schema->storage->txn_rollback; }; + +subtest 'filter_by_pending() tests' => sub { + + plan tests => 3; + + $schema->storage->txn_begin; + + my $suggestion_1 = $builder->build_object( { class => 'Koha::Suggestions', value => { STATUS => 'ASKED' } } ); + my $suggestion_2 = $builder->build_object( { class => 'Koha::Suggestions', value => { STATUS => 'ACCEPTED' } } ); + my $suggestion_3 = $builder->build_object( { class => 'Koha::Suggestions', value => { STATUS => 'ASKED' } } ); + + my $suggestions = + Koha::Suggestions->search( { suggestionid => [ $suggestion_1->id, $suggestion_2->id, $suggestion_3->id ] }, + { order_by => ['suggestionid'] } ); + + is( $suggestions->count, 3 ); + + my $pending = $suggestions->filter_by_pending; + my @pending_ids = $pending->get_column('suggestionid'); + + is( $pending->count, 2 ); + is_deeply( \@pending_ids, [ $suggestion_1->id, $suggestion_3->id ] ); + + $schema->storage->txn_rollback; +}; + +subtest 'filter_by_suggested_days_range() tests' => sub { + + plan tests => 4; + + $schema->storage->txn_begin; + + my $today = dt_from_string; + my $today_minus_two = dt_from_string->subtract( days => 2 ); + my $today_minus_three = dt_from_string->subtract( days => 3 ); + + my $dtf = Koha::Database->new->schema->storage->datetime_parser; + + my $suggestion_1 = $builder->build_object( + { class => 'Koha::Suggestions', value => { suggesteddate => $dtf->format_date($today) } } ); + my $suggestion_2 = $builder->build_object( + { class => 'Koha::Suggestions', value => { suggesteddate => $dtf->format_date($today_minus_two) } } ); + my $suggestion_3 = $builder->build_object( + { class => 'Koha::Suggestions', value => { suggesteddate => $dtf->format_date($today_minus_three) } } ); + + my $suggestions = + Koha::Suggestions->search( { suggestionid => [ $suggestion_1->id, $suggestion_2->id, $suggestion_3->id ] }, + { order_by => ['suggestionid'] } ); + + is( $suggestions->count, 3 ); + + my $three_days = $suggestions->filter_by_suggested_days_range(3); + is( $three_days->count, 3 ); + + my $two_days = $suggestions->filter_by_suggested_days_range(2); + is( $two_days->count, 2 ); + + my $one_days = $suggestions->filter_by_suggested_days_range(1); + is( $one_days->count, 1 ); + + $schema->storage->txn_rollback; +}; -- 2.39.5