View | Details | Raw Unified | Return to bug 30663
Collapse All | Expand All

(-)a/Koha/Suggestions.pm (+32 lines)
Lines 21-26 use Modern::Perl; Link Here
21
21
22
22
23
use Koha::Database;
23
use Koha::Database;
24
use Koha::DateUtils qw(dt_from_string);
24
use Koha::Suggestion;
25
use Koha::Suggestion;
25
26
26
use base qw(Koha::Objects);
27
use base qw(Koha::Objects);
Lines 62-67 sub search_limited { Link Here
62
    return $resultset->search( $params, $attributes);
63
    return $resultset->search( $params, $attributes);
63
}
64
}
64
65
66
=head3 filter_by_pending
67
68
    my $open = $suggestions->filter_by_pending;
69
70
Filters the resultset on those that are considered pending (i.e. STATUS = ASKED).
71
72
=cut
73
74
sub filter_by_pending {
75
    my ($self) = @_;
76
77
    return $self->search( { STATUS => 'ASKED' } );
78
}
79
80
=head3 filter_by_suggested_days_range
81
82
    my $suggestions = $suggestions->filter_by_suggested_days_range( $days );
83
84
Filters the resultset on those placed within some I<$days> range.
85
86
=cut
87
88
sub filter_by_suggested_days_range {
89
    my ( $self, $days ) = @_;
90
91
    my $dtf = Koha::Database->new->schema->storage->datetime_parser;
92
93
    return $self->search(
94
        { suggesteddate => { '>=' => $dtf->format_date( dt_from_string->subtract( days => $days ) ) } } );
95
}
96
65
=head2 Internal methods
97
=head2 Internal methods
66
98
67
=head3 _type
99
=head3 _type
(-)a/t/db_dependent/Koha/Suggestions.t (-3 / +63 lines)
Lines 19-28 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 9;
22
use Test::More tests => 11;
23
use Test::Exception;
23
use Test::Exception;
24
24
25
use Koha::Suggestion;
26
use Koha::Suggestions;
25
use Koha::Suggestions;
27
use Koha::Database;
26
use Koha::Database;
28
use Koha::DateUtils qw( dt_from_string output_pref );
27
use Koha::DateUtils qw( dt_from_string output_pref );
Lines 329-331 subtest 'search_limited() tests' => sub { Link Here
329
328
330
    $schema->storage->txn_rollback;
329
    $schema->storage->txn_rollback;
331
};
330
};
332
- 
331
332
subtest 'filter_by_pending() tests' => sub {
333
334
    plan tests => 3;
335
336
    $schema->storage->txn_begin;
337
338
    my $suggestion_1 = $builder->build_object( { class => 'Koha::Suggestions', value => { STATUS => 'ASKED' } } );
339
    my $suggestion_2 = $builder->build_object( { class => 'Koha::Suggestions', value => { STATUS => 'ACCEPTED' } } );
340
    my $suggestion_3 = $builder->build_object( { class => 'Koha::Suggestions', value => { STATUS => 'ASKED' } } );
341
342
    my $suggestions =
343
      Koha::Suggestions->search( { suggestionid => [ $suggestion_1->id, $suggestion_2->id, $suggestion_3->id ] },
344
        { order_by => ['suggestionid'] } );
345
346
    is( $suggestions->count, 3 );
347
348
    my $pending     = $suggestions->filter_by_pending;
349
    my @pending_ids = $pending->get_column('suggestionid');
350
351
    is( $pending->count, 2 );
352
    is_deeply( \@pending_ids, [ $suggestion_1->id, $suggestion_3->id ] );
353
354
    $schema->storage->txn_rollback;
355
};
356
357
subtest 'filter_by_suggested_days_range() tests' => sub {
358
359
    plan tests => 4;
360
361
    $schema->storage->txn_begin;
362
363
    my $today             = dt_from_string;
364
    my $today_minus_two   = dt_from_string->subtract( days => 2 );
365
    my $today_minus_three = dt_from_string->subtract( days => 3 );
366
367
    my $dtf = Koha::Database->new->schema->storage->datetime_parser;
368
369
    my $suggestion_1 = $builder->build_object(
370
        { class => 'Koha::Suggestions', value => { suggesteddate => $dtf->format_date($today) } } );
371
    my $suggestion_2 = $builder->build_object(
372
        { class => 'Koha::Suggestions', value => { suggesteddate => $dtf->format_date($today_minus_two) } } );
373
    my $suggestion_3 = $builder->build_object(
374
        { class => 'Koha::Suggestions', value => { suggesteddate => $dtf->format_date($today_minus_three) } } );
375
376
    my $suggestions =
377
      Koha::Suggestions->search( { suggestionid => [ $suggestion_1->id, $suggestion_2->id, $suggestion_3->id ] },
378
        { order_by => ['suggestionid'] } );
379
380
    is( $suggestions->count, 3 );
381
382
    my $three_days = $suggestions->filter_by_suggested_days_range(3);
383
    is( $three_days->count, 3 );
384
385
    my $two_days = $suggestions->filter_by_suggested_days_range(2);
386
    is( $two_days->count, 2 );
387
388
    my $one_days = $suggestions->filter_by_suggested_days_range(1);
389
    is( $one_days->count, 1 );
390
391
    $schema->storage->txn_rollback;
392
};

Return to bug 30663