Bug 35086: Add chunk_size option to elasticsearch configuration
[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 => 5;
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
69     throws_ok {
70         $suggestion->STATUS('UNKNOWN')->store;
71     }
72     'Koha::Exceptions::Suggestion::StatusForbidden',
73         'store raises an exception on invalid STATUS';
74
75     my $authorised_value = Koha::AuthorisedValue->new(
76         {
77             category         => 'SUGGEST_STATUS',
78             authorised_value => 'UNKNOWN'
79         }
80     )->store;
81     $suggestion->STATUS('UNKNOWN')->store;
82     is( $suggestion->STATUS, 'UNKNOWN', "UNKNOWN status stored" );
83     $suggestion->delete;
84 };
85
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' );
88
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' );
91
92 $retrieved_suggestion_1->delete;
93 is( Koha::Suggestions->search->count, $nb_of_suggestions + 1, 'Delete should have deleted the suggestion' );
94
95 $schema->storage->txn_rollback;
96
97 subtest 'constraints' => sub {
98     plan tests => 11;
99     $schema->storage->txn_begin;
100
101     my $print_error = $schema->storage->dbh->{PrintError};
102     $schema->storage->dbh->{PrintError} = 0;
103
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" } );
107
108     my $suggestion = $builder->build_object(
109         {
110             class => "Koha::Suggestions",
111             value => {
112                 suggestedby  => $patron->borrowernumber,
113                 biblionumber => $biblio->biblionumber,
114                 branchcode   => $branch->branchcode,
115                 managedby    => undef,
116                 acceptedby   => undef,
117                 rejectedby   => undef,
118                 budgetid     => undef,
119             }
120         }
121     );
122
123     my $nonexistent_borrowernumber = $patron->borrowernumber;
124     # suggestedby
125     $patron->delete;
126     $suggestion = $suggestion->get_from_storage;
127     is( $suggestion->suggestedby, undef,
128         "The suggestion is not deleted when the related patron is deleted" );
129
130     # biblionumber
131     $biblio->delete;
132     $suggestion = $suggestion->get_from_storage;
133     is( $suggestion->biblionumber, undef,
134         "The suggestion is not deleted when the related biblio is deleted" );
135
136     # branchcode
137     $branch->delete;
138     $suggestion = $suggestion->get_from_storage;
139     is( $suggestion->branchcode, undef,
140         "The suggestion is not deleted when the related branch is deleted" );
141
142     # managerid
143     {   # hide useless warnings
144         local *STDERR;
145         open STDERR, '>', '/dev/null';
146         throws_ok {
147             $suggestion->managedby($nonexistent_borrowernumber)->store;
148         }
149         'Koha::Exceptions::Object::FKConstraint',
150           'store raises an exception on invalid managerid';
151         close STDERR;
152     }
153     my $manager = $builder->build_object( { class => "Koha::Patrons" } );
154     $suggestion->managedby( $manager->borrowernumber )->store;
155     $manager->delete;
156     $suggestion = $suggestion->get_from_storage;
157     is( $suggestion->managedby, undef,
158         "The suggestion is not deleted when the related manager is deleted" );
159
160     # acceptedby
161     {    # hide useless warnings
162         local *STDERR;
163         open STDERR, '>', '/dev/null';
164         throws_ok {
165             $suggestion->acceptedby($nonexistent_borrowernumber)->store;
166         }
167         'Koha::Exceptions::Object::FKConstraint',
168           'store raises an exception on invalid acceptedby id';
169         close STDERR;
170     }
171     my $acceptor = $builder->build_object( { class => "Koha::Patrons" } );
172     $suggestion->acceptedby( $acceptor->borrowernumber )->store;
173     $acceptor->delete;
174     $suggestion = $suggestion->get_from_storage;
175     is( $suggestion->acceptedby, undef,
176         "The suggestion is not deleted when the related acceptor is deleted" );
177
178     # rejectedby
179     {    # hide useless warnings
180         local *STDERR;
181         open STDERR, '>', '/dev/null';
182         throws_ok {
183             $suggestion->rejectedby($nonexistent_borrowernumber)->store;
184         }
185         'Koha::Exceptions::Object::FKConstraint',
186           'store raises an exception on invalid rejectedby id';
187         close STDERR;
188     }
189     my $rejecter = $builder->build_object( { class => "Koha::Patrons" } );
190     $suggestion->rejectedby( $rejecter->borrowernumber )->store;
191     $rejecter->delete;
192     $suggestion = $suggestion->get_from_storage;
193     is( $suggestion->rejectedby, undef,
194         "The suggestion is not deleted when the related rejecter is deleted" );
195
196     # budgetid
197     {    # hide useless warnings
198         local *STDERR;
199         open STDERR, '>', '/dev/null';
200
201         throws_ok { $suggestion->budgetid($nonexistent_borrowernumber)->store; }
202         'Koha::Exceptions::Object::FKConstraint',
203           'store raises an exception on invalid budgetid';
204         close STDERR;
205     }
206     my $fund = $builder->build_object( { class => "Koha::Acquisition::Funds" } );
207     $suggestion->budgetid( $fund->id )->store;
208     $fund->delete;
209     $suggestion = $suggestion->get_from_storage;
210     is( $suggestion->budgetid, undef,
211         "The suggestion is not deleted when the related budget is deleted" );
212
213     $schema->storage->dbh->{PrintError} = $print_error;
214     $schema->storage->txn_rollback;
215 };
216
217 subtest 'manager, suggester, rejecter, last_modifier' => sub {
218     plan tests => 8;
219     $schema->storage->txn_begin;
220
221     my $suggestion = $builder->build_object( { class => 'Koha::Suggestions' } );
222
223     is( ref( $suggestion->manager ),
224         'Koha::Patron',
225         '->manager should have returned a Koha::Patron object' );
226     is( ref( $suggestion->rejecter ),
227         'Koha::Patron',
228         '->rejecter should have returned a Koha::Patron object' );
229     is( ref( $suggestion->suggester ),
230         'Koha::Patron',
231         '->suggester should have returned a Koha::Patron object' );
232     is( ref( $suggestion->last_modifier ),
233         'Koha::Patron',
234         '->last_modifier should have returned a Koha::Patron object' );
235
236     $suggestion->set(
237         {
238             managedby          => undef,
239             rejectedby         => undef,
240             suggestedby        => undef,
241             lastmodificationby => undef
242         }
243     );
244
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,
252         undef,
253         '->last_modifier should have returned undef if no last_modifier set' );
254
255     $schema->storage->txn_rollback;
256 };
257
258 subtest 'fund' => sub {
259     plan tests => 2;
260
261     $schema->storage->txn_begin;
262
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' );
267
268     $suggestion->set( { budgetid => undef } );
269
270     is( $suggestion->fund, undef,
271         '->fund should have returned undef if not fund set' );
272
273     $schema->storage->txn_rollback;
274 };
275
276 subtest 'search_limited() tests' => sub {
277
278     plan tests => 4;
279
280     $schema->storage->txn_begin;
281
282     # Two libraries
283     my $library_1 = $builder->build_object({ class => 'Koha::Libraries' });
284     my $library_2 = $builder->build_object({ class => 'Koha::Libraries' });
285
286     # A patron from $library_1, that is not superlibrarian at all
287     my $patron = $builder->build_object(
288         {
289             class => 'Koha::Patrons',
290             value => { branchcode => $library_1->id, flags => 0 }
291         }
292     );
293
294     # Add 3 suggestions, to be sorted by author
295     my $suggestion_1 = $builder->build_object(
296         {
297             class => 'Koha::Suggestions',
298             value => { branchcode => $library_1->id, author => 'A' }
299         }
300     );
301     my $suggestion_2 = $builder->build_object(
302         {
303             class => 'Koha::Suggestions',
304             value => { branchcode => $library_2->id, author => 'B' }
305         }
306     );
307     my $suggestion_3 = $builder->build_object(
308         {
309             class => 'Koha::Suggestions',
310             value => { branchcode => $library_2->id, author => 'C' }
311         }
312     );
313
314     my $resultset = Koha::Suggestions->search(
315         { branchcode => [ $library_1->id, $library_2->id ] },
316         { order_by   => { -desc => ['author'] } } );
317
318     is( $resultset->count, 3, 'Only this three suggestions are returned' );
319
320     # Now the tests
321     t::lib::Mocks::mock_userenv({ patron => $patron, branchcode => $library_1->id });
322
323     # Disable IndependentBranches
324     t::lib::Mocks::mock_preference( 'IndependentBranches', 0 );
325
326     my $filtered_rs = $resultset->search_limited;
327     is( $filtered_rs->count, 3, 'No IndependentBranches, all suggestions returned' );
328
329     # Enable IndependentBranches
330     t::lib::Mocks::mock_preference( 'IndependentBranches', 1 );
331
332     $filtered_rs = $resultset->search_limited;
333
334     is( $filtered_rs->count, 1, 'IndependentBranches, only suggestions from own branch returned' );
335
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 });
340
341     $filtered_rs = $resultset->search_limited;
342     is( $filtered_rs->count, 3, 'IndependentBranches but patron is superlibrarian, all suggestions returned' );
343
344     $schema->storage->txn_rollback;
345 };
346
347 subtest 'filter_by_pending() tests' => sub {
348
349     plan tests => 3;
350
351     $schema->storage->txn_begin;
352
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' } } );
356
357     my $suggestions =
358       Koha::Suggestions->search( { suggestionid => [ $suggestion_1->id, $suggestion_2->id, $suggestion_3->id ] },
359         { order_by => ['suggestionid'] } );
360
361     is( $suggestions->count, 3 );
362
363     my $pending     = $suggestions->filter_by_pending;
364     my @pending_ids = $pending->get_column('suggestionid');
365
366     is( $pending->count, 2 );
367     is_deeply( \@pending_ids, [ $suggestion_1->id, $suggestion_3->id ] );
368
369     $schema->storage->txn_rollback;
370 };
371
372 subtest 'filter_by_suggested_days_range() tests' => sub {
373
374     plan tests => 4;
375
376     $schema->storage->txn_begin;
377
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 );
381
382     my $dtf = Koha::Database->new->schema->storage->datetime_parser;
383
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) } } );
390
391     my $suggestions =
392       Koha::Suggestions->search( { suggestionid => [ $suggestion_1->id, $suggestion_2->id, $suggestion_3->id ] },
393         { order_by => ['suggestionid'] } );
394
395     is( $suggestions->count, 3 );
396
397     my $three_days = $suggestions->filter_by_suggested_days_range(3);
398     is( $three_days->count, 3 );
399
400     my $two_days = $suggestions->filter_by_suggested_days_range(2);
401     is( $two_days->count, 2 );
402
403     my $one_days = $suggestions->filter_by_suggested_days_range(1);
404     is( $one_days->count, 1 );
405
406     $schema->storage->txn_rollback;
407 };