Bug 21206: (QA follow-up) Rebase problem and leftover mocked GetItem
[koha.git] / t / db_dependent / Suggestions.t
index 37261eb..16e0dd2 100644 (file)
 
 use Modern::Perl;
 
+use DateTime::Duration;
+use Test::More tests => 106;
+use Test::Warn;
+
+use t::lib::Mocks;
+use t::lib::TestBuilder;
+
 use C4::Context;
-use C4::Members;
 use C4::Letters;
-
-use Test::More tests => 91;
-use Test::Warn;
+use C4::Budgets qw( AddBudgetPeriod AddBudget );
+use Koha::Database;
+use Koha::DateUtils qw( dt_from_string output_pref );
+use Koha::Libraries;
+use Koha::Patrons;
+use Koha::Suggestions;
 
 BEGIN {
     use_ok('C4::Suggestions');
-    use_ok('C4::Koha');
 }
 
+my $schema  = Koha::Database->new->schema;
+$schema->storage->txn_begin;
 my $dbh = C4::Context->dbh;
-
-# Start transaction
-$dbh->{AutoCommit} = 0;
-$dbh->{RaiseError} = 1;
-
+my $builder = t::lib::TestBuilder->new;
+
+# Reset item types to only the default ones
+$dbh->do(q|DELETE FROM itemtypes;|);
+my $sql = qq|
+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',''),
+('CF', 'Computer Files',5,0,'bridge/computer_file.gif',''),
+('MP', 'Maps',5,0,'bridge/map.gif',''),
+('VM', 'Visual Materials',5,1,'bridge/dvd.gif',''),
+('MU', 'Music',5,0,'bridge/sound.gif',''),
+('CR', 'Continuing Resources',5,0,'bridge/periodical.gif',''),
+('REF', 'Reference',0,1,'bridge/reference.gif','');|;
+$dbh->do($sql);
 $dbh->do(q|DELETE FROM suggestions|);
+$dbh->do(q|DELETE FROM issues|);
 $dbh->do(q|DELETE FROM borrowers|);
 $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 $patron_category = $builder->build({ source => 'Category' });
+
 my $member = {
     firstname => 'my firstname',
     surname => 'my surname',
-    categorycode => 'S',
+    categorycode => $patron_category->{categorycode},
+    branchcode => 'CPL',
+    smsalertnumber => 12345,
+};
+
+my $member2 = {
+    firstname => 'my firstname',
+    surname => 'my surname',
+    categorycode => $patron_category->{categorycode},
     branchcode => 'CPL',
+    email => 'to@example.com',
 };
-my $borrowernumber = AddMember(%$member);
+
+my $borrowernumber = Koha::Patron->new($member)->store->borrowernumber;
+my $borrowernumber2 = Koha::Patron->new($member2)->store->borrowernumber;
 
 my $biblionumber1 = 1;
 my $my_suggestion = {
@@ -56,8 +95,50 @@ my $my_suggestion = {
     publishercode => 'my publishercode',
     suggestedby   => $borrowernumber,
     biblionumber  => $biblionumber1,
+    managedby     => '',
+    manageddate   => '',
+    accepteddate  => dt_from_string,
+    note          => 'my note',
 };
 
+my $budgetperiod_id = AddBudgetPeriod({
+    budget_period_startdate   => '2008-01-01',
+    budget_period_enddate     => '2008-12-31',
+    budget_period_description => 'MAPERI',
+    budget_period_active      => 1,
+});
+
+my $budget_id = AddBudget({
+    budget_code      => 'ABCD',
+    budget_amount    => '123.132000',
+    budget_name      => 'ABCD',
+    budget_notes     => 'This is a note',
+    budget_period_id => $budgetperiod_id,
+});
+my $my_suggestion_with_budget = {
+    title         => 'my title 2',
+    author        => 'my author 2',
+    publishercode => 'my publishercode 2',
+    suggestedby   => $borrowernumber,
+    biblionumber  => $biblionumber1,
+    managedby     => '',
+    manageddate   => '',
+    accepteddate  => dt_from_string,
+    note          => 'my note',
+    budgetid      => $budget_id,
+};
+my $my_suggestion_with_budget2 = {
+    title         => 'my title 3',
+    author        => 'my author 3',
+    publishercode => 'my publishercode 3',
+    suggestedby   => $borrowernumber2,
+    biblionumber  => $biblionumber1,
+    managedby     => '',
+    manageddate   => '',
+    accepteddate  => dt_from_string,
+    note          => 'my note',
+    budgetid      => $budget_id,
+};
 
 is( CountSuggestion(), 0, 'CountSuggestion without the status returns 0' );
 is( CountSuggestion('ASKED'), 0, 'CountSuggestion returns the correct number of suggestions' );
@@ -65,9 +146,9 @@ is( CountSuggestion('CHECKED'), 0, 'CountSuggestion returns the correct number o
 is( CountSuggestion('ACCEPTED'), 0, 'CountSuggestion returns the correct number of suggestions' );
 is( CountSuggestion('REJECTED'), 0, 'CountSuggestion returns the correct number of suggestions' );
 
-
 my $my_suggestionid = NewSuggestion($my_suggestion);
 isnt( $my_suggestionid, 0, 'NewSuggestion returns an not null id' );
+my $my_suggestionid_with_budget = NewSuggestion($my_suggestion_with_budget);
 
 is( GetSuggestion(), undef, 'GetSuggestion without the suggestion id returns undef' );
 my $suggestion = GetSuggestion($my_suggestionid);
@@ -77,8 +158,11 @@ is( $suggestion->{publishercode}, $my_suggestion->{publishercode}, 'NewSuggestio
 is( $suggestion->{suggestedby}, $my_suggestion->{suggestedby}, 'NewSuggestion stores the borrower number correctly' );
 is( $suggestion->{biblionumber}, $my_suggestion->{biblionumber}, 'NewSuggestion stores the biblio number correctly' );
 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'), 1, 'CountSuggestion returns the correct number of suggestions' );
+is( CountSuggestion('ASKED'), 2, 'CountSuggestion returns the correct number of suggestions' );
 
 
 is( ModSuggestion(), undef, 'ModSuggestion without the suggestion returns undef' );
@@ -86,9 +170,11 @@ my $mod_suggestion1 = {
     title         => 'my modified title',
     author        => 'my modified author',
     publishercode => 'my modified publishercode',
+    managedby     => '',
+    manageddate   => '',
 };
 my $status = ModSuggestion($mod_suggestion1);
-is( $status, '0E0', 'ModSuggestion without the suggestion id returns 0E0' );
+is( $status, undef, 'ModSuggestion without the suggestion id returns undef' );
 
 $mod_suggestion1->{suggestionid} = $my_suggestionid;
 $status = ModSuggestion($mod_suggestion1);
@@ -97,6 +183,11 @@ $suggestion = GetSuggestion($my_suggestionid);
 is( $suggestion->{title}, $mod_suggestion1->{title}, 'ModSuggestion modifies the title  correctly' );
 is( $suggestion->{author}, $mod_suggestion1->{author}, 'ModSuggestion modifies the author correctly' );
 is( $suggestion->{publishercode}, $mod_suggestion1->{publishercode}, 'ModSuggestion modifies the publishercode correctly' );
+is( $suggestion->{managedby}, undef, 'ModSuggestion stores empty string as undef for non existent foreign key (integer)' );
+is( $suggestion->{manageddate}, undef, 'ModSuggestion stores empty string as undef for date' );
+isnt( $suggestion->{accepteddate}, undef, 'ModSuggestion does not update a non given date value' );
+is( $suggestion->{note}, 'my note', 'ModSuggestion should not erase data if not given' );
+
 my $messages = C4::Letters::GetQueuedMessages({
     borrowernumber => $borrowernumber,
 });
@@ -115,8 +206,12 @@ my $mod_suggestion3 = {
     STATUS       => 'CHECKED',
     suggestionid => $my_suggestionid,
 };
-$status = ModSuggestion($mod_suggestion3);
 
+#Test the message_transport_type of suggestion notices
+
+#Check the message_transport_type when the 'FallbackToSMSIfNoEmail' syspref is disabled
+t::lib::Mocks::mock_preference( 'FallbackToSMSIfNoEmail', 0 );
+$status = ModSuggestion($mod_suggestion3);
 is( $status, 1, 'ModSuggestion modifies one entry' );
 $suggestion = GetSuggestion($my_suggestionid);
 is( $suggestion->{STATUS}, $mod_suggestion3->{STATUS}, 'ModSuggestion modifies the status correctly' );
@@ -124,9 +219,31 @@ $messages = C4::Letters::GetQueuedMessages({
     borrowernumber => $borrowernumber,
 });
 is( @$messages, 1, 'ModSuggestion sends an email if the status is updated' );
-
+is ($messages->[0]->{message_transport_type}, 'email', 'When FallbackToSMSIfNoEmail syspref is disabled the suggestion message_transport_type is always email');
 is( CountSuggestion('CHECKED'), 1, 'CountSuggestion returns the correct number of suggestions' );
 
+#Check the message_transport_type when the 'FallbackToSMSIfNoEmail' syspref is enabled and the borrower has a smsalertnumber and no email
+t::lib::Mocks::mock_preference( 'FallbackToSMSIfNoEmail', 1 );
+ModSuggestion($mod_suggestion3);
+$messages = C4::Letters::GetQueuedMessages({
+    borrowernumber => $borrowernumber,
+});
+is ($messages->[1]->{message_transport_type}, 'sms', 'When FallbackToSMSIfNoEmail syspref is enabled the suggestion message_transport_type is sms if the borrower has no email');
+
+#Make a new suggestion for a borrower with defined email and no smsalertnumber
+my $my_suggestion_2_id = NewSuggestion($my_suggestion_with_budget2);
+
+#Check the message_transport_type when the 'FallbackToSMSIfNoEmail' syspref is enabled and the borrower has a defined email and no smsalertnumber
+t::lib::Mocks::mock_preference( 'FallbackToSMSIfNoEmail', 1 );
+my $mod_suggestion4 = {
+    STATUS       => 'CHECKED',
+    suggestionid => $my_suggestion_2_id,
+};
+ModSuggestion($mod_suggestion4);
+$messages = C4::Letters::GetQueuedMessages({
+    borrowernumber => $borrowernumber2,
+});
+is ($messages->[0]->{message_transport_type}, 'email', 'When FallbackToSMSIfNoEmail syspref is enabled the suggestion message_transport_type is email if the borrower has an email');
 
 is( GetSuggestionInfo(), undef, 'GetSuggestionInfo without the suggestion id returns undef' );
 $suggestion = GetSuggestionInfo($my_suggestionid);
@@ -165,7 +282,7 @@ is( $suggestion->{borrnumsuggestedby}, $my_suggestion->{suggestedby}, 'GetSugges
 my $suggestions = GetSuggestionByStatus();
 is( @$suggestions, 0, 'GetSuggestionByStatus without the status returns an empty array' );
 $suggestions = GetSuggestionByStatus('CHECKED');
-is( @$suggestions, 1, 'GetSuggestionByStatus returns the correct number of suggestions' );
+is( @$suggestions, 2, 'GetSuggestionByStatus returns the correct number of suggestions' );
 is( $suggestions->[0]->{suggestionid}, $my_suggestionid, 'GetSuggestionByStatus returns the suggestion id correctly' );
 is( $suggestions->[0]->{title}, $mod_suggestion1->{title}, 'GetSuggestionByStatus returns the title correctly' );
 is( $suggestions->[0]->{author}, $mod_suggestion1->{author}, 'GetSuggestionByStatus returns the author correctly' );
@@ -187,9 +304,8 @@ is( $connect_suggestion_and_biblio, '1', 'ConnectSuggestionAndBiblio returns 1'
 $suggestion = GetSuggestion($my_suggestionid);
 is( $suggestion->{biblionumber}, $biblionumber2, 'ConnectSuggestionAndBiblio updates the biblio number correctly' );
 
-
 my $search_suggestion = SearchSuggestion();
-is( @$search_suggestion, 1, 'SearchSuggestion without arguments returns all suggestions' );
+is( @$search_suggestion, 3, 'SearchSuggestion without arguments returns all suggestions' );
 
 $search_suggestion = SearchSuggestion({
     title => $mod_suggestion1->{title},
@@ -210,7 +326,7 @@ $search_suggestion = SearchSuggestion({
 is( @$search_suggestion, 0, 'SearchSuggestion returns the correct number of suggestions' );
 
 $search_suggestion = SearchSuggestion({
-    publishercode => $mod_suggestion3->{publishercode},
+    publishercode => $mod_suggestion1->{publishercode},
 });
 is( @$search_suggestion, 1, 'SearchSuggestion returns the correct number of suggestions' );
 $search_suggestion = SearchSuggestion({
@@ -221,12 +337,33 @@ is( @$search_suggestion, 0, 'SearchSuggestion returns the correct number of sugg
 $search_suggestion = SearchSuggestion({
     STATUS => $mod_suggestion3->{STATUS},
 });
-is( @$search_suggestion, 1, 'SearchSuggestion returns the correct number of suggestions' );
+is( @$search_suggestion, 2, 'SearchSuggestion returns the correct number of suggestions' );
+
+$search_suggestion = SearchSuggestion({
+    STATUS => q||
+});
+is( @$search_suggestion, 0, 'SearchSuggestion should not return all suggestions if we want the suggestions with a STATUS=""' );
 $search_suggestion = SearchSuggestion({
     STATUS => 'REJECTED',
 });
 is( @$search_suggestion, 0, 'SearchSuggestion returns the correct number of suggestions' );
 
+$search_suggestion = SearchSuggestion({
+    budgetid => '',
+});
+is( @$search_suggestion, 3, 'SearchSuggestion (budgetid = "") returns the correct number of suggestions' );
+$search_suggestion = SearchSuggestion({
+    budgetid => $budget_id,
+});
+is( @$search_suggestion, 2, 'SearchSuggestion (budgetid = $budgetid) returns the correct number of suggestions' );
+$search_suggestion = SearchSuggestion({
+    budgetid => '__NONE__',
+});
+is( @$search_suggestion, 1, 'SearchSuggestion (budgetid = "__NONE__") returns the correct number of suggestions' );
+$search_suggestion = SearchSuggestion({
+    budgetid => '__ANY__',
+});
+is( @$search_suggestion, 3, 'SearchSuggestion (budgetid = "__ANY__") returns the correct number of suggestions' );
 
 my $del_suggestion = {
     title => 'my deleted title',
@@ -235,7 +372,7 @@ my $del_suggestion = {
 };
 my $del_suggestionid = NewSuggestion($del_suggestion);
 
-is( CountSuggestion('CHECKED'), 2, 'CountSuggestion returns the correct number of suggestions' );
+is( CountSuggestion('CHECKED'), 3, 'CountSuggestion returns the correct number of suggestions' );
 
 is( DelSuggestion(), '0E0', 'DelSuggestion without arguments returns 0E0' );
 is( DelSuggestion($borrowernumber), '', 'DelSuggestion without the suggestion id returns an empty string' );
@@ -244,18 +381,85 @@ $suggestion = DelSuggestion($borrowernumber, $my_suggestionid);
 is( $suggestion, 1, 'DelSuggestion deletes one suggestion' );
 
 $suggestions = GetSuggestionByStatus('CHECKED');
-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');
-my $itemtypes1 = C4::Koha::GetSupportList();
-is(@$itemtypes1, 8, "Purchase suggestion itemtypes collected, multiple AdvancedSearchTypes");
-
-C4::Context->set_preference("AdvancedSearchTypes", 'itemtypes');
-my $itemtypes2 = C4::Koha::GetSupportList();
-is(@$itemtypes2, 8, "Purchase suggestion itemtypes collected, default AdvancedSearchTypes");
+is( @$suggestions, 2, 'DelSuggestion deletes one suggestion' );
+is( $suggestions->[1]->{title}, $del_suggestion->{title}, 'DelSuggestion deletes the correct suggestion' );
+
+# 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' );
+};
 
-is_deeply($itemtypes1, $itemtypes2, 'same set of purchase suggestion formats retrieved');
+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' );
+};
 
-$dbh->rollback;
+$schema->storage->txn_rollback;