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' );
70 $suggestion->STATUS('UNKNOWN')->store;
72 'Koha::Exceptions::Suggestion::StatusForbidden',
73 'store raises an exception on invalid STATUS';
75 my $authorised_value = Koha::AuthorisedValue->new(
77 category => 'SUGGEST_STATUS',
78 authorised_value => 'UNKNOWN'
81 $suggestion->STATUS('UNKNOWN')->store;
82 is( $suggestion->STATUS, 'UNKNOWN', "UNKNOWN status stored" );
86 like( $new_suggestion_1->suggestionid, qr|^\d+$|, 'Adding a new suggestion should have set the suggestionid' );
87 is( Koha::Suggestions->search->count, $nb_of_suggestions + 2, 'The 2 suggestions should have been added' );
89 my $retrieved_suggestion_1 = Koha::Suggestions->find( $new_suggestion_1->suggestionid );
90 is( $retrieved_suggestion_1->biblionumber, $new_suggestion_1->biblionumber, 'Find a suggestion by id should return the correct suggestion' );
92 $retrieved_suggestion_1->delete;
93 is( Koha::Suggestions->search->count, $nb_of_suggestions + 1, 'Delete should have deleted the suggestion' );
95 $schema->storage->txn_rollback;
97 subtest 'constraints' => sub {
99 $schema->storage->txn_begin;
101 my $print_error = $schema->storage->dbh->{PrintError};
102 $schema->storage->dbh->{PrintError} = 0;
104 my $patron = $builder->build_object( { class => "Koha::Patrons" } );
105 my $biblio = $builder->build_sample_biblio();
106 my $branch = $builder->build_object( { class => "Koha::Libraries" } );
108 my $suggestion = $builder->build_object(
110 class => "Koha::Suggestions",
112 suggestedby => $patron->borrowernumber,
113 biblionumber => $biblio->biblionumber,
114 branchcode => $branch->branchcode,
123 my $nonexistent_borrowernumber = $patron->borrowernumber;
126 $suggestion = $suggestion->get_from_storage;
127 is( $suggestion->suggestedby, undef,
128 "The suggestion is not deleted when the related patron is deleted" );
132 $suggestion = $suggestion->get_from_storage;
133 is( $suggestion->biblionumber, undef,
134 "The suggestion is not deleted when the related biblio is deleted" );
138 $suggestion = $suggestion->get_from_storage;
139 is( $suggestion->branchcode, undef,
140 "The suggestion is not deleted when the related branch is deleted" );
143 { # hide useless warnings
145 open STDERR, '>', '/dev/null';
147 $suggestion->managedby($nonexistent_borrowernumber)->store;
149 'Koha::Exceptions::Object::FKConstraint',
150 'store raises an exception on invalid managerid';
153 my $manager = $builder->build_object( { class => "Koha::Patrons" } );
154 $suggestion->managedby( $manager->borrowernumber )->store;
156 $suggestion = $suggestion->get_from_storage;
157 is( $suggestion->managedby, undef,
158 "The suggestion is not deleted when the related manager is deleted" );
161 { # hide useless warnings
163 open STDERR, '>', '/dev/null';
165 $suggestion->acceptedby($nonexistent_borrowernumber)->store;
167 'Koha::Exceptions::Object::FKConstraint',
168 'store raises an exception on invalid acceptedby id';
171 my $acceptor = $builder->build_object( { class => "Koha::Patrons" } );
172 $suggestion->acceptedby( $acceptor->borrowernumber )->store;
174 $suggestion = $suggestion->get_from_storage;
175 is( $suggestion->acceptedby, undef,
176 "The suggestion is not deleted when the related acceptor is deleted" );
179 { # hide useless warnings
181 open STDERR, '>', '/dev/null';
183 $suggestion->rejectedby($nonexistent_borrowernumber)->store;
185 'Koha::Exceptions::Object::FKConstraint',
186 'store raises an exception on invalid rejectedby id';
189 my $rejecter = $builder->build_object( { class => "Koha::Patrons" } );
190 $suggestion->rejectedby( $rejecter->borrowernumber )->store;
192 $suggestion = $suggestion->get_from_storage;
193 is( $suggestion->rejectedby, undef,
194 "The suggestion is not deleted when the related rejecter is deleted" );
197 { # hide useless warnings
199 open STDERR, '>', '/dev/null';
201 throws_ok { $suggestion->budgetid($nonexistent_borrowernumber)->store; }
202 'Koha::Exceptions::Object::FKConstraint',
203 'store raises an exception on invalid budgetid';
206 my $fund = $builder->build_object( { class => "Koha::Acquisition::Funds" } );
207 $suggestion->budgetid( $fund->id )->store;
209 $suggestion = $suggestion->get_from_storage;
210 is( $suggestion->budgetid, undef,
211 "The suggestion is not deleted when the related budget is deleted" );
213 $schema->storage->dbh->{PrintError} = $print_error;
214 $schema->storage->txn_rollback;
217 subtest 'manager, suggester, rejecter, last_modifier' => sub {
219 $schema->storage->txn_begin;
221 my $suggestion = $builder->build_object( { class => 'Koha::Suggestions' } );
223 is( ref( $suggestion->manager ),
225 '->manager should have returned a Koha::Patron object' );
226 is( ref( $suggestion->rejecter ),
228 '->rejecter should have returned a Koha::Patron object' );
229 is( ref( $suggestion->suggester ),
231 '->suggester should have returned a Koha::Patron object' );
232 is( ref( $suggestion->last_modifier ),
234 '->last_modifier should have returned a Koha::Patron object' );
240 suggestedby => undef,
241 lastmodificationby => undef
245 is( $suggestion->manager, undef,
246 '->manager should have returned undef if no manager set' );
247 is( $suggestion->rejecter, undef,
248 '->rejecter should have returned undef if no rejecter set' );
249 is( $suggestion->suggester, undef,
250 '->suggester should have returned undef if no suggester set' );
251 is( $suggestion->last_modifier,
253 '->last_modifier should have returned undef if no last_modifier set' );
255 $schema->storage->txn_rollback;
258 subtest 'fund' => sub {
261 $schema->storage->txn_begin;
263 my $suggestion = $builder->build_object( { class => 'Koha::Suggestions' } );
264 is( ref( $suggestion->fund ),
265 'Koha::Acquisition::Fund',
266 '->fund should have returned a Koha::Acquisition::Fund object' );
268 $suggestion->set( { budgetid => undef } );
270 is( $suggestion->fund, undef,
271 '->fund should have returned undef if not fund set' );
273 $schema->storage->txn_rollback;
276 subtest 'search_limited() tests' => sub {
280 $schema->storage->txn_begin;
283 my $library_1 = $builder->build_object({ class => 'Koha::Libraries' });
284 my $library_2 = $builder->build_object({ class => 'Koha::Libraries' });
286 # A patron from $library_1, that is not superlibrarian at all
287 my $patron = $builder->build_object(
289 class => 'Koha::Patrons',
290 value => { branchcode => $library_1->id, flags => 0 }
294 # Add 3 suggestions, to be sorted by author
295 my $suggestion_1 = $builder->build_object(
297 class => 'Koha::Suggestions',
298 value => { branchcode => $library_1->id, author => 'A' }
301 my $suggestion_2 = $builder->build_object(
303 class => 'Koha::Suggestions',
304 value => { branchcode => $library_2->id, author => 'B' }
307 my $suggestion_3 = $builder->build_object(
309 class => 'Koha::Suggestions',
310 value => { branchcode => $library_2->id, author => 'C' }
314 my $resultset = Koha::Suggestions->search(
315 { branchcode => [ $library_1->id, $library_2->id ] },
316 { order_by => { -desc => ['author'] } } );
318 is( $resultset->count, 3, 'Only this three suggestions are returned' );
321 t::lib::Mocks::mock_userenv({ patron => $patron, branchcode => $library_1->id });
323 # Disable IndependentBranches
324 t::lib::Mocks::mock_preference( 'IndependentBranches', 0 );
326 my $filtered_rs = $resultset->search_limited;
327 is( $filtered_rs->count, 3, 'No IndependentBranches, all suggestions returned' );
329 # Enable IndependentBranches
330 t::lib::Mocks::mock_preference( 'IndependentBranches', 1 );
332 $filtered_rs = $resultset->search_limited;
334 is( $filtered_rs->count, 1, 'IndependentBranches, only suggestions from own branch returned' );
336 # Make the patron superlibrarian to override IndependentBranches
337 $patron->flags(1)->store;
338 # So it reloads C4::Context->userenv->{flags}
339 t::lib::Mocks::mock_userenv({ patron => $patron, branchcode => $library_1->id });
341 $filtered_rs = $resultset->search_limited;
342 is( $filtered_rs->count, 3, 'IndependentBranches but patron is superlibrarian, all suggestions returned' );
344 $schema->storage->txn_rollback;
347 subtest 'filter_by_pending() tests' => sub {
351 $schema->storage->txn_begin;
353 my $suggestion_1 = $builder->build_object( { class => 'Koha::Suggestions', value => { STATUS => 'ASKED' } } );
354 my $suggestion_2 = $builder->build_object( { class => 'Koha::Suggestions', value => { STATUS => 'ACCEPTED' } } );
355 my $suggestion_3 = $builder->build_object( { class => 'Koha::Suggestions', value => { STATUS => 'ASKED' } } );
358 Koha::Suggestions->search( { suggestionid => [ $suggestion_1->id, $suggestion_2->id, $suggestion_3->id ] },
359 { order_by => ['suggestionid'] } );
361 is( $suggestions->count, 3 );
363 my $pending = $suggestions->filter_by_pending;
364 my @pending_ids = $pending->get_column('suggestionid');
366 is( $pending->count, 2 );
367 is_deeply( \@pending_ids, [ $suggestion_1->id, $suggestion_3->id ] );
369 $schema->storage->txn_rollback;
372 subtest 'filter_by_suggested_days_range() tests' => sub {
376 $schema->storage->txn_begin;
378 my $today = dt_from_string;
379 my $today_minus_two = dt_from_string->subtract( days => 2 );
380 my $today_minus_three = dt_from_string->subtract( days => 3 );
382 my $dtf = Koha::Database->new->schema->storage->datetime_parser;
384 my $suggestion_1 = $builder->build_object(
385 { class => 'Koha::Suggestions', value => { suggesteddate => $dtf->format_date($today) } } );
386 my $suggestion_2 = $builder->build_object(
387 { class => 'Koha::Suggestions', value => { suggesteddate => $dtf->format_date($today_minus_two) } } );
388 my $suggestion_3 = $builder->build_object(
389 { class => 'Koha::Suggestions', value => { suggesteddate => $dtf->format_date($today_minus_three) } } );
392 Koha::Suggestions->search( { suggestionid => [ $suggestion_1->id, $suggestion_2->id, $suggestion_3->id ] },
393 { order_by => ['suggestionid'] } );
395 is( $suggestions->count, 3 );
397 my $three_days = $suggestions->filter_by_suggested_days_range(3);
398 is( $three_days->count, 3 );
400 my $two_days = $suggestions->filter_by_suggested_days_range(2);
401 is( $two_days->count, 2 );
403 my $one_days = $suggestions->filter_by_suggested_days_range(1);
404 is( $one_days->count, 1 );
406 $schema->storage->txn_rollback;