From 39cd3bbfc73cef6d57a31f37f745ca696d159b02 Mon Sep 17 00:00:00 2001
From: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Date: Fri, 23 Feb 2018 10:00:13 +0100
Subject: [PATCH] Bug 13287: (QA follow-up) Extend Suggestions.t for
 DelSuggestionsOlderThan
Content-Type: text/plain; charset=utf-8

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>
---
 C4/Suggestions.pm            |  3 ++-
 t/db_dependent/Suggestions.t | 37 +++++++++++++++++++++++++++++++++++--
 2 files changed, 37 insertions(+), 3 deletions(-)

diff --git a/C4/Suggestions.pm b/C4/Suggestions.pm
index 1ea2339..70b7b0d 100644
--- a/C4/Suggestions.pm
+++ b/C4/Suggestions.pm
@@ -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{
diff --git a/t/db_dependent/Suggestions.t b/t/db_dependent/Suggestions.t
index 5e9e5ed..6604cd1 100644
--- a/t/db_dependent/Suggestions.t
+++ b/t/db_dependent/Suggestions.t
@@ -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;
-- 
2.1.4