Merge remote-tracking branch 'translations/22.05.08-translate-20221222' into security...
[koha.git] / t / db_dependent / Koha / Suggestions.t
1 #!/usr/bin/perl
2
3 # Copyright 2015-2019 Koha Development team
4 #
5 # This file is part of Koha
6 #
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.
11 #
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.
16 #
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>.
19
20 use Modern::Perl;
21
22 use Test::More tests => 11;
23 use Test::Exception;
24
25 use Koha::Suggestions;
26 use Koha::Database;
27 use Koha::DateUtils qw( dt_from_string output_pref );
28
29 use t::lib::Mocks;
30 use t::lib::TestBuilder;
31
32 my $schema = Koha::Database->new->schema;
33 $schema->storage->txn_begin;
34
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,
43     }
44 )->store;
45 my $new_suggestion_2 = Koha::Suggestion->new(
46     {   suggestedby  => $patron->{borrowernumber},
47         biblionumber => $biblio_2->biblionumber,
48     }
49 )->store;
50
51 subtest 'store' => sub {
52     plan tests => 3;
53     my $suggestion  = Koha::Suggestion->new(
54         {   suggestedby  => $patron->{borrowernumber},
55             biblionumber => $biblio_1->biblionumber,
56         }
57     )->store;
58
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' );
68     $suggestion->delete;
69 };
70
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' );
73
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' );
76
77 $retrieved_suggestion_1->delete;
78 is( Koha::Suggestions->search->count, $nb_of_suggestions + 1, 'Delete should have deleted the suggestion' );
79
80 $schema->storage->txn_rollback;
81
82 subtest 'constraints' => sub {
83     plan tests => 11;
84     $schema->storage->txn_begin;
85
86     my $print_error = $schema->storage->dbh->{PrintError};
87     $schema->storage->dbh->{PrintError} = 0;
88
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" } );
92
93     my $suggestion = $builder->build_object(
94         {
95             class => "Koha::Suggestions",
96             value => {
97                 suggestedby  => $patron->borrowernumber,
98                 biblionumber => $biblio->biblionumber,
99                 branchcode   => $branch->branchcode,
100                 managedby    => undef,
101                 acceptedby   => undef,
102                 rejectedby   => undef,
103                 budgetid     => undef,
104             }
105         }
106     );
107
108     my $nonexistent_borrowernumber = $patron->borrowernumber;
109     # suggestedby
110     $patron->delete;
111     $suggestion = $suggestion->get_from_storage;
112     is( $suggestion->suggestedby, undef,
113         "The suggestion is not deleted when the related patron is deleted" );
114
115     # biblionumber
116     $biblio->delete;
117     $suggestion = $suggestion->get_from_storage;
118     is( $suggestion->biblionumber, undef,
119         "The suggestion is not deleted when the related biblio is deleted" );
120
121     # branchcode
122     $branch->delete;
123     $suggestion = $suggestion->get_from_storage;
124     is( $suggestion->branchcode, undef,
125         "The suggestion is not deleted when the related branch is deleted" );
126
127     # managerid
128     {   # hide useless warnings
129         local *STDERR;
130         open STDERR, '>', '/dev/null';
131         throws_ok {
132             $suggestion->managedby($nonexistent_borrowernumber)->store;
133         }
134         'Koha::Exceptions::Object::FKConstraint',
135           'store raises an exception on invalid managerid';
136         close STDERR;
137     }
138     my $manager = $builder->build_object( { class => "Koha::Patrons" } );
139     $suggestion->managedby( $manager->borrowernumber )->store;
140     $manager->delete;
141     $suggestion = $suggestion->get_from_storage;
142     is( $suggestion->managedby, undef,
143         "The suggestion is not deleted when the related manager is deleted" );
144
145     # acceptedby
146     {    # hide useless warnings
147         local *STDERR;
148         open STDERR, '>', '/dev/null';
149         throws_ok {
150             $suggestion->acceptedby($nonexistent_borrowernumber)->store;
151         }
152         'Koha::Exceptions::Object::FKConstraint',
153           'store raises an exception on invalid acceptedby id';
154         close STDERR;
155     }
156     my $acceptor = $builder->build_object( { class => "Koha::Patrons" } );
157     $suggestion->acceptedby( $acceptor->borrowernumber )->store;
158     $acceptor->delete;
159     $suggestion = $suggestion->get_from_storage;
160     is( $suggestion->acceptedby, undef,
161         "The suggestion is not deleted when the related acceptor is deleted" );
162
163     # rejectedby
164     {    # hide useless warnings
165         local *STDERR;
166         open STDERR, '>', '/dev/null';
167         throws_ok {
168             $suggestion->rejectedby($nonexistent_borrowernumber)->store;
169         }
170         'Koha::Exceptions::Object::FKConstraint',
171           'store raises an exception on invalid rejectedby id';
172         close STDERR;
173     }
174     my $rejecter = $builder->build_object( { class => "Koha::Patrons" } );
175     $suggestion->rejectedby( $rejecter->borrowernumber )->store;
176     $rejecter->delete;
177     $suggestion = $suggestion->get_from_storage;
178     is( $suggestion->rejectedby, undef,
179         "The suggestion is not deleted when the related rejecter is deleted" );
180
181     # budgetid
182     {    # hide useless warnings
183         local *STDERR;
184         open STDERR, '>', '/dev/null';
185
186         throws_ok { $suggestion->budgetid($nonexistent_borrowernumber)->store; }
187         'Koha::Exceptions::Object::FKConstraint',
188           'store raises an exception on invalid budgetid';
189         close STDERR;
190     }
191     my $fund = $builder->build_object( { class => "Koha::Acquisition::Funds" } );
192     $suggestion->budgetid( $fund->id )->store;
193     $fund->delete;
194     $suggestion = $suggestion->get_from_storage;
195     is( $suggestion->budgetid, undef,
196         "The suggestion is not deleted when the related budget is deleted" );
197
198     $schema->storage->dbh->{PrintError} = $print_error;
199     $schema->storage->txn_rollback;
200 };
201
202 subtest 'manager, suggester, rejecter, last_modifier' => sub {
203     plan tests => 8;
204     $schema->storage->txn_begin;
205
206     my $suggestion = $builder->build_object( { class => 'Koha::Suggestions' } );
207
208     is( ref( $suggestion->manager ),
209         'Koha::Patron',
210         '->manager should have returned a Koha::Patron object' );
211     is( ref( $suggestion->rejecter ),
212         'Koha::Patron',
213         '->rejecter should have returned a Koha::Patron object' );
214     is( ref( $suggestion->suggester ),
215         'Koha::Patron',
216         '->suggester should have returned a Koha::Patron object' );
217     is( ref( $suggestion->last_modifier ),
218         'Koha::Patron',
219         '->last_modifier should have returned a Koha::Patron object' );
220
221     $suggestion->set(
222         {
223             managedby          => undef,
224             rejectedby         => undef,
225             suggestedby        => undef,
226             lastmodificationby => undef
227         }
228     );
229
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,
237         undef,
238         '->last_modifier should have returned undef if no last_modifier set' );
239
240     $schema->storage->txn_rollback;
241 };
242
243 subtest 'fund' => sub {
244     plan tests => 2;
245
246     $schema->storage->txn_begin;
247
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' );
252
253     $suggestion->set( { budgetid => undef } );
254
255     is( $suggestion->fund, undef,
256         '->fund should have returned undef if not fund set' );
257
258     $schema->storage->txn_rollback;
259 };
260
261 subtest 'search_limited() tests' => sub {
262
263     plan tests => 4;
264
265     $schema->storage->txn_begin;
266
267     # Two libraries
268     my $library_1 = $builder->build_object({ class => 'Koha::Libraries' });
269     my $library_2 = $builder->build_object({ class => 'Koha::Libraries' });
270
271     # A patron from $library_1, that is not superlibrarian at all
272     my $patron = $builder->build_object(
273         {
274             class => 'Koha::Patrons',
275             value => { branchcode => $library_1->id, flags => 0 }
276         }
277     );
278
279     # Add 3 suggestions, to be sorted by author
280     my $suggestion_1 = $builder->build_object(
281         {
282             class => 'Koha::Suggestions',
283             value => { branchcode => $library_1->id, author => 'A' }
284         }
285     );
286     my $suggestion_2 = $builder->build_object(
287         {
288             class => 'Koha::Suggestions',
289             value => { branchcode => $library_2->id, author => 'B' }
290         }
291     );
292     my $suggestion_3 = $builder->build_object(
293         {
294             class => 'Koha::Suggestions',
295             value => { branchcode => $library_2->id, author => 'C' }
296         }
297     );
298
299     my $resultset = Koha::Suggestions->search(
300         { branchcode => [ $library_1->id, $library_2->id ] },
301         { order_by   => { -desc => ['author'] } } );
302
303     is( $resultset->count, 3, 'Only this three suggestions are returned' );
304
305     # Now the tests
306     t::lib::Mocks::mock_userenv({ patron => $patron, branchcode => $library_1->id });
307
308     # Disable IndependentBranches
309     t::lib::Mocks::mock_preference( 'IndependentBranches', 0 );
310
311     my $filtered_rs = $resultset->search_limited;
312     is( $filtered_rs->count, 3, 'No IndependentBranches, all suggestions returned' );
313
314     # Enable IndependentBranches
315     t::lib::Mocks::mock_preference( 'IndependentBranches', 1 );
316
317     $filtered_rs = $resultset->search_limited;
318
319     is( $filtered_rs->count, 1, 'IndependentBranches, only suggestions from own branch returned' );
320
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 });
325
326     $filtered_rs = $resultset->search_limited;
327     is( $filtered_rs->count, 3, 'IndependentBranches but patron is superlibrarian, all suggestions returned' );
328
329     $schema->storage->txn_rollback;
330 };
331
332 subtest 'filter_by_pending() tests' => sub {
333
334     plan tests => 3;
335
336     $schema->storage->txn_begin;
337
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' } } );
341
342     my $suggestions =
343       Koha::Suggestions->search( { suggestionid => [ $suggestion_1->id, $suggestion_2->id, $suggestion_3->id ] },
344         { order_by => ['suggestionid'] } );
345
346     is( $suggestions->count, 3 );
347
348     my $pending     = $suggestions->filter_by_pending;
349     my @pending_ids = $pending->get_column('suggestionid');
350
351     is( $pending->count, 2 );
352     is_deeply( \@pending_ids, [ $suggestion_1->id, $suggestion_3->id ] );
353
354     $schema->storage->txn_rollback;
355 };
356
357 subtest 'filter_by_suggested_days_range() tests' => sub {
358
359     plan tests => 4;
360
361     $schema->storage->txn_begin;
362
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 );
366
367     my $dtf = Koha::Database->new->schema->storage->datetime_parser;
368
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) } } );
375
376     my $suggestions =
377       Koha::Suggestions->search( { suggestionid => [ $suggestion_1->id, $suggestion_2->id, $suggestion_3->id ] },
378         { order_by => ['suggestionid'] } );
379
380     is( $suggestions->count, 3 );
381
382     my $three_days = $suggestions->filter_by_suggested_days_range(3);
383     is( $three_days->count, 3 );
384
385     my $two_days = $suggestions->filter_by_suggested_days_range(2);
386     is( $two_days->count, 2 );
387
388     my $one_days = $suggestions->filter_by_suggested_days_range(1);
389     is( $one_days->count, 1 );
390
391     $schema->storage->txn_rollback;
392 };