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 => 9;
26 use Koha::Suggestions;
28 use Koha::DateUtils qw( dt_from_string output_pref );
31 use t::lib::TestBuilder;
33 my $schema = Koha::Database->new->schema;
34 $schema->storage->txn_begin;
36 my $builder = t::lib::TestBuilder->new;
37 my $biblio_1 = $builder->build_sample_biblio;
38 my $biblio_2 = $builder->build_sample_biblio;
39 my $patron = $builder->build( { source => 'Borrower' } );
40 my $nb_of_suggestions = Koha::Suggestions->search->count;
41 my $new_suggestion_1 = Koha::Suggestion->new(
42 { suggestedby => $patron->{borrowernumber},
43 biblionumber => $biblio_1->biblionumber,
46 my $new_suggestion_2 = Koha::Suggestion->new(
47 { suggestedby => $patron->{borrowernumber},
48 biblionumber => $biblio_2->biblionumber,
52 subtest 'store' => sub {
54 my $suggestion = Koha::Suggestion->new(
55 { suggestedby => $patron->{borrowernumber},
56 biblionumber => $biblio_1->biblionumber,
60 is( $suggestion->suggesteddate, dt_from_string()->ymd, "If suggesteddate not passed in, it will default to today" );
61 my $two_days_ago = dt_from_string->subtract( days => 2 );
62 my $two_days_ago_sql = output_pref({dt => $two_days_ago, dateformat => 'sql', dateonly => 1 });
63 $suggestion->suggesteddate($two_days_ago)->store;
64 $suggestion = Koha::Suggestions->find( $suggestion->suggestionid );
65 is( $suggestion->suggesteddate, $two_days_ago_sql, 'If suggesteddate passed in, it should be taken into account' );
66 $suggestion->reason('because!')->store;
67 $suggestion = Koha::Suggestions->find( $suggestion->suggestionid );
68 is( $suggestion->suggesteddate, $two_days_ago_sql, 'If suggestion id modified, suggesteddate should not be modified' );
72 like( $new_suggestion_1->suggestionid, qr|^\d+$|, 'Adding a new suggestion should have set the suggestionid' );
73 is( Koha::Suggestions->search->count, $nb_of_suggestions + 2, 'The 2 suggestions should have been added' );
75 my $retrieved_suggestion_1 = Koha::Suggestions->find( $new_suggestion_1->suggestionid );
76 is( $retrieved_suggestion_1->biblionumber, $new_suggestion_1->biblionumber, 'Find a suggestion by id should return the correct suggestion' );
78 $retrieved_suggestion_1->delete;
79 is( Koha::Suggestions->search->count, $nb_of_suggestions + 1, 'Delete should have deleted the suggestion' );
81 $schema->storage->txn_rollback;
83 subtest 'constraints' => sub {
85 $schema->storage->txn_begin;
87 my $print_error = $schema->storage->dbh->{PrintError};
88 $schema->storage->dbh->{PrintError} = 0;
90 my $patron = $builder->build_object( { class => "Koha::Patrons" } );
91 my $biblio = $builder->build_sample_biblio();
92 my $branch = $builder->build_object( { class => "Koha::Libraries" } );
94 my $suggestion = $builder->build_object(
96 class => "Koha::Suggestions",
98 suggestedby => $patron->borrowernumber,
99 biblionumber => $biblio->biblionumber,
100 branchcode => $branch->branchcode,
109 my $nonexistent_borrowernumber = $patron->borrowernumber;
112 $suggestion = $suggestion->get_from_storage;
113 is( $suggestion->suggestedby, undef,
114 "The suggestion is not deleted when the related patron is deleted" );
118 $suggestion = $suggestion->get_from_storage;
119 is( $suggestion->biblionumber, undef,
120 "The suggestion is not deleted when the related biblio is deleted" );
124 $suggestion = $suggestion->get_from_storage;
125 is( $suggestion->branchcode, undef,
126 "The suggestion is not deleted when the related branch is deleted" );
129 { # hide useless warnings
131 open STDERR, '>', '/dev/null';
133 $suggestion->managedby($nonexistent_borrowernumber)->store;
135 'Koha::Exceptions::Object::FKConstraint',
136 'store raises an exception on invalid managerid';
139 my $manager = $builder->build_object( { class => "Koha::Patrons" } );
140 $suggestion->managedby( $manager->borrowernumber )->store;
142 $suggestion = $suggestion->get_from_storage;
143 is( $suggestion->managedby, undef,
144 "The suggestion is not deleted when the related manager is deleted" );
147 { # hide useless warnings
149 open STDERR, '>', '/dev/null';
151 $suggestion->acceptedby($nonexistent_borrowernumber)->store;
153 'Koha::Exceptions::Object::FKConstraint',
154 'store raises an exception on invalid acceptedby id';
157 my $acceptor = $builder->build_object( { class => "Koha::Patrons" } );
158 $suggestion->acceptedby( $acceptor->borrowernumber )->store;
160 $suggestion = $suggestion->get_from_storage;
161 is( $suggestion->acceptedby, undef,
162 "The suggestion is not deleted when the related acceptor is deleted" );
165 { # hide useless warnings
167 open STDERR, '>', '/dev/null';
169 $suggestion->rejectedby($nonexistent_borrowernumber)->store;
171 'Koha::Exceptions::Object::FKConstraint',
172 'store raises an exception on invalid rejectedby id';
175 my $rejecter = $builder->build_object( { class => "Koha::Patrons" } );
176 $suggestion->rejectedby( $rejecter->borrowernumber )->store;
178 $suggestion = $suggestion->get_from_storage;
179 is( $suggestion->rejectedby, undef,
180 "The suggestion is not deleted when the related rejecter is deleted" );
183 { # hide useless warnings
185 open STDERR, '>', '/dev/null';
187 throws_ok { $suggestion->budgetid($nonexistent_borrowernumber)->store; }
188 'Koha::Exceptions::Object::FKConstraint',
189 'store raises an exception on invalid budgetid';
192 my $fund = $builder->build_object( { class => "Koha::Acquisition::Funds" } );
193 $suggestion->budgetid( $fund->id )->store;
195 $suggestion = $suggestion->get_from_storage;
196 is( $suggestion->budgetid, undef,
197 "The suggestion is not deleted when the related budget is deleted" );
199 $schema->storage->dbh->{PrintError} = $print_error;
200 $schema->storage->txn_rollback;
203 subtest 'manager, suggester, rejecter, last_modifier' => sub {
205 $schema->storage->txn_begin;
207 my $suggestion = $builder->build_object( { class => 'Koha::Suggestions' } );
209 is( ref( $suggestion->manager ),
211 '->manager should have returned a Koha::Patron object' );
212 is( ref( $suggestion->rejecter ),
214 '->rejecter should have returned a Koha::Patron object' );
215 is( ref( $suggestion->suggester ),
217 '->suggester should have returned a Koha::Patron object' );
218 is( ref( $suggestion->last_modifier ),
220 '->last_modifier should have returned a Koha::Patron object' );
226 suggestedby => undef,
227 lastmodificationby => undef
231 is( $suggestion->manager, undef,
232 '->manager should have returned undef if no manager set' );
233 is( $suggestion->rejecter, undef,
234 '->rejecter should have returned undef if no rejecter set' );
235 is( $suggestion->suggester, undef,
236 '->suggester should have returned undef if no suggester set' );
237 is( $suggestion->last_modifier,
239 '->last_modifier should have returned undef if no last_modifier set' );
241 $schema->storage->txn_rollback;
244 subtest 'fund' => sub {
247 $schema->storage->txn_begin;
249 my $suggestion = $builder->build_object( { class => 'Koha::Suggestions' } );
250 is( ref( $suggestion->fund ),
251 'Koha::Acquisition::Fund',
252 '->fund should have returned a Koha::Acquisition::Fund object' );
254 $suggestion->set( { budgetid => undef } );
256 is( $suggestion->fund, undef,
257 '->fund should have returned undef if not fund set' );
259 $schema->storage->txn_rollback;
262 subtest 'search_limited() tests' => sub {
266 $schema->storage->txn_begin;
269 my $library_1 = $builder->build_object({ class => 'Koha::Libraries' });
270 my $library_2 = $builder->build_object({ class => 'Koha::Libraries' });
272 # A patron from $library_1, that is not superlibrarian at all
273 my $patron = $builder->build_object(
275 class => 'Koha::Patrons',
276 value => { branchcode => $library_1->id, flags => 0 }
280 # Add 3 suggestions, to be sorted by author
281 my $suggestion_1 = $builder->build_object(
283 class => 'Koha::Suggestions',
284 value => { branchcode => $library_1->id, author => 'A' }
287 my $suggestion_2 = $builder->build_object(
289 class => 'Koha::Suggestions',
290 value => { branchcode => $library_2->id, author => 'B' }
293 my $suggestion_3 = $builder->build_object(
295 class => 'Koha::Suggestions',
296 value => { branchcode => $library_2->id, author => 'C' }
300 my $resultset = Koha::Suggestions->search(
301 { branchcode => [ $library_1->id, $library_2->id ] },
302 { order_by => { -desc => ['author'] } } );
304 is( $resultset->count, 3, 'Only this three suggestions are returned' );
307 t::lib::Mocks::mock_userenv({ patron => $patron, branchcode => $library_1->id });
309 # Disable IndependentBranches
310 t::lib::Mocks::mock_preference( 'IndependentBranches', 0 );
312 my $filtered_rs = $resultset->search_limited;
313 is( $filtered_rs->count, 3, 'No IndependentBranches, all suggestions returned' );
315 # Enable IndependentBranches
316 t::lib::Mocks::mock_preference( 'IndependentBranches', 1 );
318 $filtered_rs = $resultset->search_limited;
320 is( $filtered_rs->count, 1, 'IndependentBranches, only suggestions from own branch returned' );
322 # Make the patron superlibrarian to override IndependentBranches
323 $patron->flags(1)->store;
324 # So it reloads C4::Context->userenv->{flags}
325 t::lib::Mocks::mock_userenv({ patron => $patron, branchcode => $library_1->id });
327 $filtered_rs = $resultset->search_limited;
328 is( $filtered_rs->count, 3, 'IndependentBranches but patron is superlibrarian, all suggestions returned' );
330 $schema->storage->txn_rollback;