Bug 30663: Add Koha::Suggestions helper methods
authorTomas Cohen Arazi <tomascohen@theke.io>
Tue, 3 May 2022 14:18:10 +0000 (11:18 -0300)
committerTomas Cohen Arazi <tomascohen@theke.io>
Thu, 5 May 2022 13:26:41 +0000 (10:26 -0300)
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 <tomascohen@theke.io>
Signed-off-by: Lucas Gass <lucas@bywatersolutions.com>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Koha/Suggestions.pm
t/db_dependent/Koha/Suggestions.t

index 5fadfd1..9b1d266 100644 (file)
@@ -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
index 8dbb93a..ae95ebe 100755 (executable)
 
 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;
+};