Bug 13287: (QA follow-up) Extend Suggestions.t for DelSuggestionsOlderThan
authorMarcel de Rooy <m.de.rooy@rijksmuseum.nl>
Fri, 23 Feb 2018 09:00:13 +0000 (10:00 +0100)
committerJonathan Druart <jonathan.druart@bugs.koha-community.org>
Mon, 26 Feb 2018 16:24:45 +0000 (13:24 -0300)
Adding a days>0 test in the sub with a POD line.
Specific subtest for this sub added in Suggestions.t.

Test plan:
Run t/db_dependent/Suggestions.t again.

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
C4/Suggestions.pm
t/db_dependent/Suggestions.t

index 1ea2339..70b7b0d 100644 (file)
@@ -588,12 +588,13 @@ sub DelSuggestion {
     &DelSuggestionsOlderThan($days)
 
     Delete all suggestions older than TODAY-$days , that have be accepted or rejected.
+    We do now allow a negative number. If you want to delete all suggestions, just use Koha::Suggestions->delete or so.
 
 =cut
 
 sub DelSuggestionsOlderThan {
     my ($days) = @_;
-    return unless $days;
+    return unless $days && $days > 0;
     my $dbh = C4::Context->dbh;
     my $sth = $dbh->prepare(
         q{
index 5e9e5ed..6604cd1 100644 (file)
@@ -18,7 +18,7 @@
 use Modern::Perl;
 
 use DateTime::Duration;
-use Test::More tests => 102;
+use Test::More tests => 103;
 use Test::Warn;
 
 use t::lib::Mocks;
@@ -29,9 +29,10 @@ use C4::Members;
 use C4::Letters;
 use C4::Budgets qw( AddBudgetPeriod AddBudget );
 use Koha::Database;
-use Koha::DateUtils qw( dt_from_string );
+use Koha::DateUtils qw( dt_from_string output_pref );
 use Koha::Library;
 use Koha::Libraries;
+use Koha::Suggestions;
 
 BEGIN {
     use_ok('C4::Suggestions');
@@ -382,4 +383,36 @@ subtest 'GetUnprocessedSuggestions' => sub {
     is( scalar(@$unprocessed_suggestions), 0, 'GetUnprocessedSuggestions should not return the suggestion, it has not been suggested 5 days ago' );
 };
 
+subtest 'DelSuggestionsOlderThan' => sub {
+    plan tests => 6;
+
+    Koha::Suggestions->delete;
+
+    # Add four suggestions; note that STATUS needs uppercase (FIXME)
+    my $d1 = output_pref({ dt => dt_from_string->add(days => -2), dateformat => 'sql' });
+    my $d2 = output_pref({ dt => dt_from_string->add(days => -4), dateformat => 'sql' });
+    my $sugg01 = $builder->build({ source => 'Suggestion', value => { date => $d1, STATUS => 'ASKED' }});
+    my $sugg02 = $builder->build({ source => 'Suggestion', value => { date => $d1, STATUS => 'CHECKED' }});
+    my $sugg03 = $builder->build({ source => 'Suggestion', value => { date => $d2, STATUS => 'ASKED' }});
+    my $sugg04 = $builder->build({ source => 'Suggestion', value => { date => $d2, STATUS => 'ACCEPTED' }});
+
+    # Test no parameter: should do nothing
+    C4::Suggestions::DelSuggestionsOlderThan();
+    is( Koha::Suggestions->count, 4, 'No suggestions deleted' );
+    # Test zero: should do nothing too
+    C4::Suggestions::DelSuggestionsOlderThan(0);
+    is( Koha::Suggestions->count, 4, 'No suggestions deleted again' );
+    # Test negative value
+    C4::Suggestions::DelSuggestionsOlderThan(-1);
+    is( Koha::Suggestions->count, 4, 'No suggestions deleted for -1' );
+
+    # Test positive values
+    C4::Suggestions::DelSuggestionsOlderThan(5);
+    is( Koha::Suggestions->count, 4, 'No suggestions>5d deleted' );
+    C4::Suggestions::DelSuggestionsOlderThan(3);
+    is( Koha::Suggestions->count, 3, '1 suggestions>3d deleted' );
+    C4::Suggestions::DelSuggestionsOlderThan(1);
+    is( Koha::Suggestions->count, 2, '1 suggestions>1d deleted' );
+};
+
 $schema->storage->txn_rollback;