X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;f=t%2Fdb_dependent%2FSuggestions.t;h=2d2bfb565cd772aaf1c98602a5149b8c32fc3e06;hb=ef410fd62f279a10687636a4f26babb2c91ecadc;hp=a2377f9f94e31aaa7b64995478e39643dbcbec08;hpb=01c7c2a12904fae5076914d839c5e829c54deb6f;p=koha.git diff --git a/t/db_dependent/Suggestions.t b/t/db_dependent/Suggestions.t index a2377f9f94..2d2bfb565c 100644 --- a/t/db_dependent/Suggestions.t +++ b/t/db_dependent/Suggestions.t @@ -17,32 +17,34 @@ use Modern::Perl; -use C4::Context; -use C4::Members; -use C4::Letters; -use C4::Branch; -use C4::Budgets; +use DateTime::Duration; +use Test::More tests => 103; +use Test::Warn; -use Koha::DateUtils qw( dt_from_string ); +use t::lib::Mocks; +use t::lib::TestBuilder; -use Test::More tests => 101; -use Test::Warn; +use C4::Context; +use C4::Letters; +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; -my $sql; - -# 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;|); -$sql = " +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',''), @@ -51,7 +53,7 @@ INSERT INTO itemtypes (itemtype, description, rentalcharge, notforloan, imageurl ('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','');"; +('REF', 'Reference',0,1,'bridge/reference.gif','');|; $dbh->do($sql); $dbh->do(q|DELETE FROM suggestions|); $dbh->do(q|DELETE FROM issues|); @@ -61,31 +63,19 @@ $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 GetBranchDetail('CPL')) { - ModBranch({add => 1, branchcode => 'CPL', branchname => 'Centerville'}); +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 $patron_category = $builder->build({ source => 'Category' }); my $member = { firstname => 'my firstname', surname => 'my surname', - categorycode => 'S', + categorycode => $patron_category->{categorycode}, branchcode => 'CPL', }; -my $borrowernumber = AddMember(%$member); +my $borrowernumber = Koha::Patron->new($member)->store->borrowernumber; my $biblionumber1 = 1; my $my_suggestion = { @@ -148,6 +138,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' ); @@ -298,6 +290,11 @@ $search_suggestion = SearchSuggestion({ STATUS => $mod_suggestion3->{STATUS}, }); is( @$search_suggestion, 1, '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', }); @@ -339,17 +336,82 @@ $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_deeply($itemtypes1, $itemtypes2, 'same set of purchase suggestion formats retrieved'); +# 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' ); +}; -$dbh->rollback; +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' ); +}; -done_testing; +$schema->storage->txn_rollback;