Bug 16155: Adjust TestBuilder.t
[koha.git] / t / db_dependent / Suggestions.t
index f6a001c..a960331 100644 (file)
 
 use Modern::Perl;
 
+use t::lib::Mocks;
 use C4::Context;
 use C4::Members;
 use C4::Letters;
-use C4::Budgets;
+use C4::Budgets qw( AddBudgetPeriod AddBudget );
 
 use Koha::DateUtils qw( dt_from_string );
+use Koha::Library;
+use Koha::Libraries;
 
-use Test::More tests => 101;
+use DateTime::Duration;
+use Test::More tests => 105;
 use Test::Warn;
 
 BEGIN {
@@ -33,6 +37,7 @@ BEGIN {
 }
 
 my $dbh = C4::Context->dbh;
+my $sql;
 
 # Start transaction
 $dbh->{AutoCommit} = 0;
@@ -40,7 +45,7 @@ $dbh->{RaiseError} = 1;
 
 # Reset item types to only the default ones
 $dbh->do(q|DELETE FROM itemtypes;|);
-my $sql = "
+$sql = "
 INSERT INTO itemtypes (itemtype, description, rentalcharge, notforloan, imageurl, summary) VALUES
 ('BK', 'Books',5,0,'bridge/book.gif',''),
 ('MX', 'Mixed Materials',5,0,'bridge/kit.gif',''),
@@ -58,6 +63,25 @@ $dbh->do(q|DELETE FROM letter|);
 $dbh->do(q|DELETE FROM message_queue|);
 $dbh->do(q|INSERT INTO letter(module, code, content) VALUES ('suggestions', 'CHECKED', 'my content')|);
 
+# Add CPL if missing.
+if (not defined Koha::Libraries->find('CPL')) {
+    Koha::Library->new({ branchcode => 'CPL', branchname => 'Centerville' })->store;
+}
+
+my $sth = $dbh->prepare("SELECT * FROM categories WHERE categorycode='S';");
+$sth->execute();
+if (!$sth->fetchrow_hashref) {
+    $sql = "INSERT INTO categories
+                (categorycode,description,enrolmentperiod,upperagelimit,
+                 dateofbirthrequired,finetype,bulk,enrolmentfee,
+                 overduenoticerequired,issuelimit,reservefee,category_type)
+            VALUES
+                ('S','Staff',99,999,
+                 18,NULL,NULL,'0.000000',
+                 0,NULL,'0.000000','S');";
+    $dbh->do($sql);
+}
+
 my $member = {
     firstname => 'my firstname',
     surname => 'my surname',
@@ -127,6 +151,8 @@ is( $suggestion->{biblionumber}, $my_suggestion->{biblionumber}, 'NewSuggestion
 is( $suggestion->{STATUS}, 'ASKED', 'NewSuggestion stores a suggestion with the status ASKED by default' );
 is( $suggestion->{managedby}, undef, 'NewSuggestion stores empty string as undef for non existent foreign key (integer)' );
 is( $suggestion->{manageddate}, undef, 'NewSuggestion stores empty string as undef for date' );
+is( $suggestion->{budgetid}, undef, 'NewSuggestion should set budgetid to NULL if not given' );
+
 is( CountSuggestion('ASKED'), 2, 'CountSuggestion returns the correct number of suggestions' );
 
 
@@ -319,16 +345,58 @@ is( @$suggestions, 1, 'DelSuggestion deletes one suggestion' );
 is( $suggestions->[0]->{title}, $del_suggestion->{title}, 'DelSuggestion deletes the correct suggestion' );
 
 ## Bug 11466, making sure GetSupportList() returns itemtypes, even if AdvancedSearchTypes has multiple values
-C4::Context->set_preference("AdvancedSearchTypes", 'itemtypes|loc|ccode');
+t::lib::Mocks::mock_preference("AdvancedSearchTypes", 'itemtypes|loc|ccode');
 my $itemtypes1 = C4::Koha::GetSupportList();
 is(@$itemtypes1, 8, "Purchase suggestion itemtypes collected, multiple AdvancedSearchTypes");
 
-C4::Context->set_preference("AdvancedSearchTypes", 'itemtypes');
+t::lib::Mocks::mock_preference("AdvancedSearchTypes", 'itemtypes');
 my $itemtypes2 = C4::Koha::GetSupportList();
 is(@$itemtypes2, 8, "Purchase suggestion itemtypes collected, default AdvancedSearchTypes");
 
 is_deeply($itemtypes1, $itemtypes2, 'same set of purchase suggestion formats retrieved');
 
-$dbh->rollback;
-
-done_testing;
+# Test budgetid fk
+$my_suggestion->{budgetid} = ''; # If budgetid == '', NULL should be set in DB
+my $my_suggestionid_test_budgetid = NewSuggestion($my_suggestion);
+$suggestion = GetSuggestion($my_suggestionid_test_budgetid);
+is( $suggestion->{budgetid}, undef, 'NewSuggestion Should set budgetid to NULL if equals an empty string' );
+
+$my_suggestion->{budgetid} = ''; # If budgetid == '', NULL should be set in DB
+ModSuggestion( $my_suggestion );
+$suggestion = GetSuggestion($my_suggestionid_test_budgetid);
+is( $suggestion->{budgetid}, undef, 'NewSuggestion Should set budgetid to NULL if equals an empty string' );
+
+subtest 'GetUnprocessedSuggestions' => sub {
+    plan tests => 11;
+    $dbh->do(q|DELETE FROM suggestions|);
+    my $my_suggestionid         = NewSuggestion($my_suggestion);
+    my $unprocessed_suggestions = C4::Suggestions::GetUnprocessedSuggestions;
+    is( scalar(@$unprocessed_suggestions), 0, 'GetUnprocessedSuggestions should return 0 if a suggestion has been processed but not linked to a fund' );
+    my $status     = ModSuggestion($mod_suggestion1);
+    my $suggestion = GetSuggestion($my_suggestionid);
+    is( $suggestion->{budgetid}, undef, 'ModSuggestion should set budgetid to NULL if not given' );
+    ModSuggestion( { suggestionid => $my_suggestionid, budgetid => $budget_id } );
+    $suggestion = GetSuggestion($my_suggestionid);
+    is( $suggestion->{budgetid}, $budget_id, 'ModSuggestion should modify budgetid if given' );
+
+    $unprocessed_suggestions = C4::Suggestions::GetUnprocessedSuggestions;
+    is( scalar(@$unprocessed_suggestions), 1, 'GetUnprocessedSuggestions should return the suggestion if the suggestion is linked to a fund and has not been processed yet' );
+
+    warning_is { ModSuggestion( { suggestionid => $my_suggestionid, STATUS => 'REJECTED' } ) }
+                'No suggestions REJECTED letter transported by email',
+                'Warning raised if no REJECTED letter by email';
+    $unprocessed_suggestions = C4::Suggestions::GetUnprocessedSuggestions;
+    is( scalar(@$unprocessed_suggestions), 0, 'GetUnprocessedSuggestions should return the suggestion if the suggestion is linked to a fund and has not been processed yet' );
+
+    warning_is { ModSuggestion( { suggestionid => $my_suggestionid, STATUS => 'ASKED', suggesteddate => dt_from_string->add_duration( DateTime::Duration->new( days => -4 ) ) } ); }
+                'No suggestions ASKED letter transported by email',
+                'Warning raised if no ASKED letter by email';
+    $unprocessed_suggestions = C4::Suggestions::GetUnprocessedSuggestions;
+    is( scalar(@$unprocessed_suggestions), 0, 'GetUnprocessedSuggestions should use 0 as default value for days' );
+    $unprocessed_suggestions = C4::Suggestions::GetUnprocessedSuggestions(4);
+    is( scalar(@$unprocessed_suggestions), 1, 'GetUnprocessedSuggestions should return the suggestion suggested 4 days ago' );
+    $unprocessed_suggestions = C4::Suggestions::GetUnprocessedSuggestions(3);
+    is( scalar(@$unprocessed_suggestions), 0, 'GetUnprocessedSuggestions should not return the suggestion, it has not been suggested 3 days ago' );
+    $unprocessed_suggestions = C4::Suggestions::GetUnprocessedSuggestions(5);
+    is( scalar(@$unprocessed_suggestions), 0, 'GetUnprocessedSuggestions should not return the suggestion, it has not been suggested 5 days ago' );
+};