3 # Copyright 2015-2019 Koha Development team
5 # This file is part of Koha
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
22 use Test::More tests => 11;
25 use Koha::Suggestions;
27 use Koha::DateUtils qw( dt_from_string output_pref );
30 use t::lib::TestBuilder;
32 my $schema = Koha::Database->new->schema;
33 $schema->storage->txn_begin;
35 my $builder = t::lib::TestBuilder->new;
36 my $biblio_1 = $builder->build_sample_biblio;
37 my $biblio_2 = $builder->build_sample_biblio;
38 my $patron = $builder->build( { source => 'Borrower' } );
39 my $nb_of_suggestions = Koha::Suggestions->search->count;
40 my $new_suggestion_1 = Koha::Suggestion->new(
41 { suggestedby => $patron->{borrowernumber},
42 biblionumber => $biblio_1->biblionumber,
45 my $new_suggestion_2 = Koha::Suggestion->new(
46 { suggestedby => $patron->{borrowernumber},
47 biblionumber => $biblio_2->biblionumber,
51 subtest 'store' => sub {
53 my $suggestion = Koha::Suggestion->new(
54 { suggestedby => $patron->{borrowernumber},
55 biblionumber => $biblio_1->biblionumber,
59 is( $suggestion->suggesteddate, dt_from_string()->ymd, "If suggesteddate not passed in, it will default to today" );
60 my $two_days_ago = dt_from_string->subtract( days => 2 );
61 my $two_days_ago_sql = output_pref({dt => $two_days_ago, dateformat => 'sql', dateonly => 1 });
62 $suggestion->suggesteddate($two_days_ago)->store;
63 $suggestion = Koha::Suggestions->find( $suggestion->suggestionid );
64 is( $suggestion->suggesteddate, $two_days_ago_sql, 'If suggesteddate passed in, it should be taken into account' );
65 $suggestion->reason('because!')->store;
66 $suggestion = Koha::Suggestions->find( $suggestion->suggestionid );
67 is( $suggestion->suggesteddate, $two_days_ago_sql, 'If suggestion id modified, suggesteddate should not be modified' );
71 like( $new_suggestion_1->suggestionid, qr|^\d+$|, 'Adding a new suggestion should have set the suggestionid' );
72 is( Koha::Suggestions->search->count, $nb_of_suggestions + 2, 'The 2 suggestions should have been added' );
74 my $retrieved_suggestion_1 = Koha::Suggestions->find( $new_suggestion_1->suggestionid );
75 is( $retrieved_suggestion_1->biblionumber, $new_suggestion_1->biblionumber, 'Find a suggestion by id should return the correct suggestion' );
77 $retrieved_suggestion_1->delete;
78 is( Koha::Suggestions->search->count, $nb_of_suggestions + 1, 'Delete should have deleted the suggestion' );
80 $schema->storage->txn_rollback;
82 subtest 'constraints' => sub {
84 $schema->storage->txn_begin;
86 my $print_error = $schema->storage->dbh->{PrintError};
87 $schema->storage->dbh->{PrintError} = 0;
89 my $patron = $builder->build_object( { class => "Koha::Patrons" } );
90 my $biblio = $builder->build_sample_biblio();
91 my $branch = $builder->build_object( { class => "Koha::Libraries" } );
93 my $suggestion = $builder->build_object(
95 class => "Koha::Suggestions",
97 suggestedby => $patron->borrowernumber,
98 biblionumber => $biblio->biblionumber,
99 branchcode => $branch->branchcode,
108 my $nonexistent_borrowernumber = $patron->borrowernumber;
111 $suggestion = $suggestion->get_from_storage;
112 is( $suggestion->suggestedby, undef,
113 "The suggestion is not deleted when the related patron is deleted" );
117 $suggestion = $suggestion->get_from_storage;
118 is( $suggestion->biblionumber, undef,
119 "The suggestion is not deleted when the related biblio is deleted" );
123 $suggestion = $suggestion->get_from_storage;
124 is( $suggestion->branchcode, undef,
125 "The suggestion is not deleted when the related branch is deleted" );
128 { # hide useless warnings
130 open STDERR, '>', '/dev/null';
132 $suggestion->managedby($nonexistent_borrowernumber)->store;
134 'Koha::Exceptions::Object::FKConstraint',
135 'store raises an exception on invalid managerid';
138 my $manager = $builder->build_object( { class => "Koha::Patrons" } );
139 $suggestion->managedby( $manager->borrowernumber )->store;
141 $suggestion = $suggestion->get_from_storage;
142 is( $suggestion->managedby, undef,
143 "The suggestion is not deleted when the related manager is deleted" );
146 { # hide useless warnings
148 open STDERR, '>', '/dev/null';
150 $suggestion->acceptedby($nonexistent_borrowernumber)->store;
152 'Koha::Exceptions::Object::FKConstraint',
153 'store raises an exception on invalid acceptedby id';
156 my $acceptor = $builder->build_object( { class => "Koha::Patrons" } );
157 $suggestion->acceptedby( $acceptor->borrowernumber )->store;
159 $suggestion = $suggestion->get_from_storage;
160 is( $suggestion->acceptedby, undef,
161 "The suggestion is not deleted when the related acceptor is deleted" );
164 { # hide useless warnings
166 open STDERR, '>', '/dev/null';
168 $suggestion->rejectedby($nonexistent_borrowernumber)->store;
170 'Koha::Exceptions::Object::FKConstraint',
171 'store raises an exception on invalid rejectedby id';
174 my $rejecter = $builder->build_object( { class => "Koha::Patrons" } );
175 $suggestion->rejectedby( $rejecter->borrowernumber )->store;
177 $suggestion = $suggestion->get_from_storage;
178 is( $suggestion->rejectedby, undef,
179 "The suggestion is not deleted when the related rejecter is deleted" );
182 { # hide useless warnings
184 open STDERR, '>', '/dev/null';
186 throws_ok { $suggestion->budgetid($nonexistent_borrowernumber)->store; }
187 'Koha::Exceptions::Object::FKConstraint',
188 'store raises an exception on invalid budgetid';
191 my $fund = $builder->build_object( { class => "Koha::Acquisition::Funds" } );
192 $suggestion->budgetid( $fund->id )->store;
194 $suggestion = $suggestion->get_from_storage;
195 is( $suggestion->budgetid, undef,
196 "The suggestion is not deleted when the related budget is deleted" );
198 $schema->storage->dbh->{PrintError} = $print_error;
199 $schema->storage->txn_rollback;
202 subtest 'manager, suggester, rejecter, last_modifier' => sub {
204 $schema->storage->txn_begin;
206 my $suggestion = $builder->build_object( { class => 'Koha::Suggestions' } );
208 is( ref( $suggestion->manager ),
210 '->manager should have returned a Koha::Patron object' );
211 is( ref( $suggestion->rejecter ),
213 '->rejecter should have returned a Koha::Patron object' );
214 is( ref( $suggestion->suggester ),
216 '->suggester should have returned a Koha::Patron object' );
217 is( ref( $suggestion->last_modifier ),
219 '->last_modifier should have returned a Koha::Patron object' );
225 suggestedby => undef,
226 lastmodificationby => undef
230 is( $suggestion->manager, undef,
231 '->manager should have returned undef if no manager set' );
232 is( $suggestion->rejecter, undef,
233 '->rejecter should have returned undef if no rejecter set' );
234 is( $suggestion->suggester, undef,
235 '->suggester should have returned undef if no suggester set' );
236 is( $suggestion->last_modifier,
238 '->last_modifier should have returned undef if no last_modifier set' );
240 $schema->storage->txn_rollback;
243 subtest 'fund' => sub {
246 $schema->storage->txn_begin;
248 my $suggestion = $builder->build_object( { class => 'Koha::Suggestions' } );
249 is( ref( $suggestion->fund ),
250 'Koha::Acquisition::Fund',
251 '->fund should have returned a Koha::Acquisition::Fund object' );
253 $suggestion->set( { budgetid => undef } );
255 is( $suggestion->fund, undef,
256 '->fund should have returned undef if not fund set' );
258 $schema->storage->txn_rollback;
261 subtest 'search_limited() tests' => sub {
265 $schema->storage->txn_begin;
268 my $library_1 = $builder->build_object({ class => 'Koha::Libraries' });
269 my $library_2 = $builder->build_object({ class => 'Koha::Libraries' });
271 # A patron from $library_1, that is not superlibrarian at all
272 my $patron = $builder->build_object(
274 class => 'Koha::Patrons',
275 value => { branchcode => $library_1->id, flags => 0 }
279 # Add 3 suggestions, to be sorted by author
280 my $suggestion_1 = $builder->build_object(
282 class => 'Koha::Suggestions',
283 value => { branchcode => $library_1->id, author => 'A' }
286 my $suggestion_2 = $builder->build_object(
288 class => 'Koha::Suggestions',
289 value => { branchcode => $library_2->id, author => 'B' }
292 my $suggestion_3 = $builder->build_object(
294 class => 'Koha::Suggestions',
295 value => { branchcode => $library_2->id, author => 'C' }
299 my $resultset = Koha::Suggestions->search(
300 { branchcode => [ $library_1->id, $library_2->id ] },
301 { order_by => { -desc => ['author'] } } );
303 is( $resultset->count, 3, 'Only this three suggestions are returned' );
306 t::lib::Mocks::mock_userenv({ patron => $patron, branchcode => $library_1->id });
308 # Disable IndependentBranches
309 t::lib::Mocks::mock_preference( 'IndependentBranches', 0 );
311 my $filtered_rs = $resultset->search_limited;
312 is( $filtered_rs->count, 3, 'No IndependentBranches, all suggestions returned' );
314 # Enable IndependentBranches
315 t::lib::Mocks::mock_preference( 'IndependentBranches', 1 );
317 $filtered_rs = $resultset->search_limited;
319 is( $filtered_rs->count, 1, 'IndependentBranches, only suggestions from own branch returned' );
321 # Make the patron superlibrarian to override IndependentBranches
322 $patron->flags(1)->store;
323 # So it reloads C4::Context->userenv->{flags}
324 t::lib::Mocks::mock_userenv({ patron => $patron, branchcode => $library_1->id });
326 $filtered_rs = $resultset->search_limited;
327 is( $filtered_rs->count, 3, 'IndependentBranches but patron is superlibrarian, all suggestions returned' );
329 $schema->storage->txn_rollback;
332 subtest 'filter_by_pending() tests' => sub {
336 $schema->storage->txn_begin;
338 my $suggestion_1 = $builder->build_object( { class => 'Koha::Suggestions', value => { STATUS => 'ASKED' } } );
339 my $suggestion_2 = $builder->build_object( { class => 'Koha::Suggestions', value => { STATUS => 'ACCEPTED' } } );
340 my $suggestion_3 = $builder->build_object( { class => 'Koha::Suggestions', value => { STATUS => 'ASKED' } } );
343 Koha::Suggestions->search( { suggestionid => [ $suggestion_1->id, $suggestion_2->id, $suggestion_3->id ] },
344 { order_by => ['suggestionid'] } );
346 is( $suggestions->count, 3 );
348 my $pending = $suggestions->filter_by_pending;
349 my @pending_ids = $pending->get_column('suggestionid');
351 is( $pending->count, 2 );
352 is_deeply( \@pending_ids, [ $suggestion_1->id, $suggestion_3->id ] );
354 $schema->storage->txn_rollback;
357 subtest 'filter_by_suggested_days_range() tests' => sub {
361 $schema->storage->txn_begin;
363 my $today = dt_from_string;
364 my $today_minus_two = dt_from_string->subtract( days => 2 );
365 my $today_minus_three = dt_from_string->subtract( days => 3 );
367 my $dtf = Koha::Database->new->schema->storage->datetime_parser;
369 my $suggestion_1 = $builder->build_object(
370 { class => 'Koha::Suggestions', value => { suggesteddate => $dtf->format_date($today) } } );
371 my $suggestion_2 = $builder->build_object(
372 { class => 'Koha::Suggestions', value => { suggesteddate => $dtf->format_date($today_minus_two) } } );
373 my $suggestion_3 = $builder->build_object(
374 { class => 'Koha::Suggestions', value => { suggesteddate => $dtf->format_date($today_minus_three) } } );
377 Koha::Suggestions->search( { suggestionid => [ $suggestion_1->id, $suggestion_2->id, $suggestion_3->id ] },
378 { order_by => ['suggestionid'] } );
380 is( $suggestions->count, 3 );
382 my $three_days = $suggestions->filter_by_suggested_days_range(3);
383 is( $three_days->count, 3 );
385 my $two_days = $suggestions->filter_by_suggested_days_range(2);
386 is( $two_days->count, 2 );
388 my $one_days = $suggestions->filter_by_suggested_days_range(1);
389 is( $one_days->count, 1 );
391 $schema->storage->txn_rollback;