Bug 17600: Standardize our EXPORT_OK
[srvgit] / t / db_dependent / Suggestions.t
old mode 100644 (file)
new mode 100755 (executable)
index 4c2760e..44e90a9
 use Modern::Perl;
 
 use DateTime::Duration;
-use Test::More tests => 110;
+use Test::More tests => 106;
 use Test::Warn;
 
 use t::lib::Mocks;
 use t::lib::TestBuilder;
 
 use C4::Context;
-use C4::Letters;
+use C4::Letters qw( GetQueuedMessages GetMessage );
 use C4::Budgets qw( AddBudgetPeriod AddBudget GetBudget );
 use Koha::Database;
 use Koha::DateUtils qw( dt_from_string output_pref );
@@ -34,7 +34,7 @@ use Koha::Patrons;
 use Koha::Suggestions;
 
 BEGIN {
-    use_ok('C4::Suggestions');
+    use_ok('C4::Suggestions', qw( NewSuggestion GetSuggestion ModSuggestion GetSuggestionInfo GetSuggestionFromBiblionumber GetSuggestionInfoFromBiblionumber GetSuggestionByStatus ConnectSuggestionAndBiblio SearchSuggestion DelSuggestion MarcRecordFromNewSuggestion GetUnprocessedSuggestions DelSuggestionsOlderThan ));
 }
 
 my $schema  = Koha::Database->new->schema;
@@ -48,14 +48,14 @@ t::lib::Mocks::mock_preference( "EmailPurchaseSuggestions", "0");
 $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','');|;
+('BK', 'Books',5,0,'bridge/book.png',''),
+('MX', 'Mixed Materials',5,0,'bridge/kit.png',''),
+('CF', 'Computer Files',5,0,'bridge/computer_file.png',''),
+('MP', 'Maps',5,0,'bridge/map.png',''),
+('VM', 'Visual Materials',5,1,'bridge/dvd.png',''),
+('MU', 'Music',5,0,'bridge/sound.png',''),
+('CR', 'Continuing Resources',5,0,'bridge/periodical.png',''),
+('REF', 'Reference',0,1,'bridge/reference.png','');|;
 $dbh->do($sql);
 $dbh->do(q|DELETE FROM suggestions|);
 $dbh->do(q|DELETE FROM issues|);
@@ -91,18 +91,19 @@ my $member2 = {
 my $borrowernumber = Koha::Patron->new($member)->store->borrowernumber;
 my $borrowernumber2 = Koha::Patron->new($member2)->store->borrowernumber;
 
-my $biblionumber1 = 1;
+my $biblio_1 = $builder->build_object({ class => 'Koha::Biblios' });
 my $my_suggestion = {
     title         => 'my title',
     author        => 'my author',
     publishercode => 'my publishercode',
     suggestedby   => $borrowernumber,
-    biblionumber  => $biblionumber1,
+    biblionumber  => $biblio_1->biblionumber,
     branchcode    => 'CPL',
     managedby     => '',
     manageddate   => '',
     accepteddate  => dt_from_string,
     note          => 'my note',
+    quantity      => '', # Insert an empty string into int to catch strict SQL modes errors
 };
 
 my $budgetperiod_id = AddBudgetPeriod({
@@ -124,7 +125,8 @@ my $my_suggestion_with_budget = {
     author        => 'my author 2',
     publishercode => 'my publishercode 2',
     suggestedby   => $borrowernumber,
-    biblionumber  => $biblionumber1,
+    branchcode    => '', # This should not fail be set to undef instead
+    biblionumber  => $biblio_1->biblionumber,
     managedby     => '',
     manageddate   => '',
     accepteddate  => dt_from_string,
@@ -136,19 +138,26 @@ my $my_suggestion_with_budget2 = {
     author        => 'my author 3',
     publishercode => 'my publishercode 3',
     suggestedby   => $borrowernumber2,
-    biblionumber  => $biblionumber1,
+    biblionumber  => $biblio_1->biblionumber,
     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' );
-is( CountSuggestion('CHECKED'), 0, 'CountSuggestion returns the correct number of suggestions' );
-is( CountSuggestion('ACCEPTED'), 0, 'CountSuggestion returns the correct number of suggestions' );
-is( CountSuggestion('REJECTED'), 0, 'CountSuggestion returns the correct number of suggestions' );
+my $my_suggestion_without_suggestedby = {
+    title         => 'my title',
+    author        => 'my author',
+    publishercode => 'my publishercode',
+    suggestedby   => undef,
+    biblionumber  => $biblio_1->biblionumber,
+    branchcode    => 'CPL',
+    managedby     => '',
+    manageddate   => '',
+    accepteddate  => dt_from_string,
+    note          => 'my note',
+    quantity      => '', # Insert an empty string into int to catch strict SQL modes errors
+};
 
 my $my_suggestionid = NewSuggestion($my_suggestion);
 isnt( $my_suggestionid, 0, 'NewSuggestion returns an not null id' );
@@ -166,9 +175,6 @@ is( $suggestion->{managedby}, undef, 'NewSuggestion stores empty string as undef
 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' );
-
-
 is( ModSuggestion(), undef, 'ModSuggestion without the suggestion returns undef' );
 my $mod_suggestion1 = {
     title         => 'my modified title',
@@ -224,7 +230,6 @@ $messages = C4::Letters::GetQueuedMessages({
 });
 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 );
@@ -249,12 +254,21 @@ $messages = C4::Letters::GetQueuedMessages({
 });
 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');
 
-$mod_suggestion4->{manageddate} = 'invalid date!';
-ModSuggestion($mod_suggestion4);
-$messages = C4::Letters::GetQueuedMessages({
-    borrowernumber => $borrowernumber2
-});
-is (scalar(@$messages), 1, 'No new letter should have been generated if the update raised an error');
+{
+    # Hiding the expected warning displayed by DBI
+    # DBD::mysql::st execute failed: Incorrect date value: 'invalid date!' for column 'manageddate'
+    local *STDERR;
+    open STDERR, '>', '/dev/null';
+
+    $mod_suggestion4->{manageddate} = 'invalid date!';
+    ModSuggestion($mod_suggestion4);
+    $messages = C4::Letters::GetQueuedMessages({
+        borrowernumber => $borrowernumber2
+    });
+
+    close STDERR;
+    is (scalar(@$messages), 1, 'No new letter should have been generated if the update raised an error');
+}
 
 is( GetSuggestionInfo(), undef, 'GetSuggestionInfo without the suggestion id returns undef' );
 $suggestion = GetSuggestionInfo($my_suggestionid);
@@ -272,12 +286,12 @@ is( $suggestion->{borrnumsuggestedby}, $my_suggestion->{suggestedby}, 'GetSugges
 
 is( GetSuggestionFromBiblionumber(), undef, 'GetSuggestionFromBiblionumber without the biblio number returns undef' );
 is( GetSuggestionFromBiblionumber(2), undef, 'GetSuggestionFromBiblionumber with an invalid biblio number returns undef' );
-is( GetSuggestionFromBiblionumber($biblionumber1), $my_suggestionid, 'GetSuggestionFromBiblionumber functions correctly' );
+is( GetSuggestionFromBiblionumber($biblio_1->biblionumber), $my_suggestionid, 'GetSuggestionFromBiblionumber functions correctly' );
 
 
 is( GetSuggestionInfoFromBiblionumber(), undef, 'GetSuggestionInfoFromBiblionumber without the biblio number returns undef' );
 is( GetSuggestionInfoFromBiblionumber(2), undef, 'GetSuggestionInfoFromBiblionumber with an invalid biblio number returns undef' );
-$suggestion = GetSuggestionInfoFromBiblionumber($biblionumber1);
+$suggestion = GetSuggestionInfoFromBiblionumber($biblio_1->biblionumber);
 is( $suggestion->{suggestionid}, $my_suggestionid, 'GetSuggestionInfoFromBiblionumber returns the suggestion id correctly' );
 is( $suggestion->{title}, $mod_suggestion1->{title}, 'GetSuggestionInfoFromBiblionumber returns the title correctly' );
 is( $suggestion->{author}, $mod_suggestion1->{author}, 'GetSuggestionInfoFromBiblionumber returns the author correctly' );
@@ -309,11 +323,11 @@ is( $suggestions->[0]->{categorycodesuggestedby}, $member->{categorycode}, 'GetS
 
 
 is( ConnectSuggestionAndBiblio(), '0E0', 'ConnectSuggestionAndBiblio without arguments returns 0E0' );
-my $biblionumber2 = 2;
-my $connect_suggestion_and_biblio = ConnectSuggestionAndBiblio($my_suggestionid, $biblionumber2);
+my $biblio_2 = $builder->build_object({ class => 'Koha::Biblios' });
+my $connect_suggestion_and_biblio = ConnectSuggestionAndBiblio($my_suggestionid, $biblio_2->biblionumber);
 is( $connect_suggestion_and_biblio, '1', 'ConnectSuggestionAndBiblio returns 1' );
 $suggestion = GetSuggestion($my_suggestionid);
-is( $suggestion->{biblionumber}, $biblionumber2, 'ConnectSuggestionAndBiblio updates the biblio number correctly' );
+is( $suggestion->{biblionumber}, $biblio_2->biblionumber, 'ConnectSuggestionAndBiblio updates the biblio number correctly' );
 
 my $search_suggestion = SearchSuggestion();
 is( @$search_suggestion, 3, 'SearchSuggestion without arguments returns all suggestions' );
@@ -389,8 +403,6 @@ my $del_suggestion = {
 };
 my $del_suggestionid = NewSuggestion($del_suggestion);
 
-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' );
 is( DelSuggestion(undef, $my_suggestionid), '', 'DelSuggestion with an invalid borrower number returns an empty string' );
@@ -412,6 +424,24 @@ ModSuggestion( $my_suggestion );
 $suggestion = GetSuggestion($my_suggestionid_test_budgetid);
 is( $suggestion->{budgetid}, undef, 'NewSuggestion Should set budgetid to NULL if equals an empty string' );
 
+my $suggestion2 = {
+    title => "Cuisine d'automne",
+    author => "Catherine",
+    itemtype => "LIV"
+};
+
+my $record = MarcRecordFromNewSuggestion($suggestion2);
+
+is("MARC::Record", ref($record), "MarcRecordFromNewSuggestion should return a MARC::Record object");
+
+my ($title_tag, $title_subfield) = C4::Biblio::GetMarcFromKohaField('biblio.title', '');
+
+is($record->field( $title_tag )->subfield( $title_subfield ), "Cuisine d'automne", "Record from suggestion title should be 'Cuisine d'automne'");
+
+my ($author_tag, $author_subfield) = C4::Biblio::GetMarcFromKohaField('biblio.author', '');
+
+is($record->field( $author_tag )->subfield( $author_subfield ), "Catherine", "Record from suggestion author should be 'Catherine'");
+
 subtest 'GetUnprocessedSuggestions' => sub {
     plan tests => 11;
     $dbh->do(q|DELETE FROM suggestions|);
@@ -598,4 +628,24 @@ subtest 'EmailPurchaseSuggestions' => sub {
         'suggestions@b.c', 'EmailAddressForSuggestions uses EmailAddressForSuggestions when set' );
 };
 
+subtest 'ModSuggestion should work on suggestions without a suggester' => sub {
+    plan tests => 2;
+
+    $dbh->do(q|DELETE FROM suggestions|);
+    my $my_suggestionid = NewSuggestion($my_suggestion_without_suggestedby);
+    $suggestion = GetSuggestion($my_suggestionid);
+    is( $suggestion->{suggestedby}, undef, "Suggestedby is undef" );
+
+    ModSuggestion(
+        {
+            suggestionid => $my_suggestionid,
+            STATUS       => 'CHECKED',
+            note         => "Test note"
+        }
+    );
+    $suggestion = GetSuggestion($my_suggestionid);
+
+    is( $suggestion->{note}, "Test note", "ModSuggestion works on suggestions without a suggester" );
+};
+
 $schema->storage->txn_rollback;