From 8760d0a7cc2b9b0d205aa2f3ea3d386167a741ff Mon Sep 17 00:00:00 2001 From: Yohann Dufour Date: Thu, 19 Jun 2014 09:14:24 +0200 Subject: [PATCH] Bug 12445: Improving unit tests for C4::Suggestions.pm Now, the tests used 'is' instead of 'ok', the tests are wrapped in a transaction, adding tests for the routines NewSuggestion, GetSuggestion, ModSuggestion, GetSuggestionFromBiblionumber and GetInfoFromBiblionumber. The tests for the routines DelSuggestionsOlderThan, CountSuggestion, ConnectSuggestionAndBiblio, SearchSuggestion, GetSuggestionInfo, DelSuggestion and GetSuggestionByStatus will be arrived in another patch. Test plan: 1/ Execute the command : prove t/db_dependent/Suggestions.t 2/ The result has to be a success without warning or error : t/db_dependent/Suggestions.t .. ok All tests successful. Files=1, Tests=32, 2 wallclock secs ( 0.03 usr 0.01 sys + 1.49 cusr 0.08 csys = 1.61 CPU) Result: PASS Signed-off-by: Paola Rossi Signed-off-by: Bernardo Gonzalez Kriegel Signed-off-by: Jonathan Druart --- t/db_dependent/Suggestions.t | 119 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 103 insertions(+), 16 deletions(-) diff --git a/t/db_dependent/Suggestions.t b/t/db_dependent/Suggestions.t index f352ff5..cb2fd5b 100644 --- a/t/db_dependent/Suggestions.t +++ b/t/db_dependent/Suggestions.t @@ -18,8 +18,11 @@ use Modern::Perl; use C4::Suggestions; +use C4::Context; +use C4::Members; +use C4::Letters; -use Test::More tests => 14; +use Test::More tests => 34; use Test::Warn; BEGIN { @@ -33,31 +36,115 @@ my $dbh = C4::Context->dbh; $dbh->{AutoCommit} = 0; $dbh->{RaiseError} = 1; -my ($suggestionid, $suggestion, $status, $biblionumber); -$biblionumber = 1; -ok($suggestionid= NewSuggestion( {title=>'Petit traité de philosohpie',author=>'Hubert de Chardassé',publishercode=>'Albin Michel'} ), "NewSuggestion OK"); -ok($suggestion= GetSuggestion( $suggestionid), "GetSuggestion OK"); -ok($status= ModSuggestion( {title=>'test Modif Simple', suggestionid=>$suggestionid} ), "ModSuggestion Simple OK"); -warning_is { $status = ModSuggestion( {STATUS=>'STALLED', suggestionid=>$suggestionid} )} +$dbh->do(q|DELETE FROM suggestions|); +$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')|); + +my $borrowernumber = AddMember( + firstname => 'my firstname', + surname => 'my surname', + categorycode => 'S', + branchcode => 'CPL', +); + +my $my_suggestion = { + title => 'my title', + author => 'my author', + publishercode => 'my publishercode', + suggestedby => $borrowernumber, + biblionumber => 1, +}; +my $my_suggestionid = NewSuggestion($my_suggestion); +isnt( $my_suggestionid, 0, 'NewSuggestion returns an not null id' ); +my $suggestion = GetSuggestion($my_suggestionid); +is( $suggestion->{title}, $my_suggestion->{title}, 'NewSuggestion stores the title correctly' ); +is( $suggestion->{author}, $my_suggestion->{author}, 'NewSuggestion stores the author correctly' ); +is( $suggestion->{publishercode}, $my_suggestion->{publishercode}, 'NewSuggestion stores the publishercode correctly' ); +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' ); +$suggestion = GetSuggestion(); +is( $suggestion, undef, 'GetSuggestion without the suggestion id returns undef' ); + + +my $status = ModSuggestion(); +is( $status, undef, 'ModSuggestion without arguments returns undef' ); + +my $mod_suggestion1 = { + title => 'my modified title', + author => 'my modified author', + publishercode => 'my modified publishercode', +}; +$status = ModSuggestion($mod_suggestion1); +is( $status, '0E0', 'ModSuggestion without the suggestion id returns 0E0' ); + +$mod_suggestion1->{suggestionid} = $my_suggestionid; +$status = ModSuggestion($mod_suggestion1); +is( $status, 1, 'ModSuggestion modifies one entry' ); +$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' ); +my $messages = C4::Letters::GetQueuedMessages({ + borrowernumber => $borrowernumber, +}); +is( @$messages, 0, 'ModSuggestions does not send an email if the status is not updated' ); + +my $mod_suggestion2 = { + STATUS => 'STALLED', + suggestionid => $my_suggestionid, +}; +warning_is { $status = ModSuggestion($mod_suggestion2) } "No suggestions STALLED letter transported by email", "ModSuggestion status warning is correct"; -ok( $status, "ModSuggestion Status OK"); -ok($status= ModSuggestion( {suggestionid => $suggestionid, biblionumber => $biblionumber } ), "ModSuggestion, set biblionumber OK" ); -ok($suggestion= GetSuggestionFromBiblionumber( $biblionumber ), "GetSuggestionFromBiblionumber OK"); -ok($suggestion= GetSuggestionInfoFromBiblionumber( $biblionumber ), "GetSuggestionInfoFromBiblionumber OK"); -ok(@{SearchSuggestion( {STATUS=>'STALLED'} )}>0, "SearchSuggestion Status OK"); +is( $status, 1, "ModSuggestion Status OK"); + +my $mod_suggestion3 = { + STATUS => 'CHECKED', + suggestionid => $my_suggestionid, +}; +$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' ); +$messages = C4::Letters::GetQueuedMessages({ + borrowernumber => $borrowernumber, +}); +is( @$messages, 1, 'ModSuggestion sends an email if the status is updated' ); + +my $biblionumber = 1; +my $suggestionid = GetSuggestionFromBiblionumber($biblionumber); +is( $suggestionid, $my_suggestionid, 'GetSuggestionFromBiblionumber functions correctly' ); + + +$suggestion = GetSuggestionInfoFromBiblionumber($biblionumber); +is( $suggestion->{suggestionid}, $my_suggestionid, 'GetSuggestionInfoFromBiblionumber gets the suggestion id correctly' ); + +is( $suggestion->{title}, $mod_suggestion1->{title}, 'GetSuggestionInfoFromBiblionumber gets the title correctly' ); +is( $suggestion->{author}, $mod_suggestion1->{author}, 'GetSuggestionInfoFromBiblionumber gets the author correctly' ); +is( $suggestion->{publishercode}, $mod_suggestion1->{publishercode}, 'GetSuggestionInfoFromBiblionumber gets the publisher code correctly' ); +is( $suggestion->{suggestedby}, $my_suggestion->{suggestedby}, 'GetSuggestionInfoFromBiblionumber gets the borrower number correctly' ); +is( $suggestion->{biblionumber}, $my_suggestion->{biblionumber}, 'GetSuggestionInfoFromBiblionumber gets the biblio number correctly' ); +is( $suggestion->{STATUS}, $mod_suggestion3->{STATUS}, 'GetSuggestionInfoFromBiblionumber gets the status correctly' ); + + +my $search_suggestion = SearchSuggestion({ + STATUS => $mod_suggestion3->{STATUS}, +}); +is( @$search_suggestion, 1, '' ); ## 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(); -ok(scalar @$itemtypes1, "Purchase suggestion itemtypes collected, multiple AdvancedSearchTypes"); +is(@$itemtypes1, 8, "Purchase suggestion itemtypes collected, multiple AdvancedSearchTypes"); C4::Context->set_preference("AdvancedSearchTypes", 'itemtypes'); my $itemtypes2 = C4::Koha::GetSupportList(); -ok(scalar @$itemtypes2, "Purchase suggestion itemtypes collected, default AdvancedSearchTypes"); +is(@$itemtypes2, 8, "Purchase suggestion itemtypes collected, default AdvancedSearchTypes"); is_deeply($itemtypes1, $itemtypes2, 'same set of purchase suggestion formats retrieved'); $dbh->rollback; - -1; -- 2.0.0.rc2