Bug 19532: (follow-up) aria-hidden attr on OPAC, and more
[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 => 9;
23 use Test::Exception;
24
25 use Koha::Suggestion;
26 use Koha::Suggestions;
27 use Koha::Database;
28 use Koha::DateUtils qw( dt_from_string output_pref );
29
30 use t::lib::Mocks;
31 use t::lib::TestBuilder;
32
33 my $schema = Koha::Database->new->schema;
34 $schema->storage->txn_begin;
35
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,
44     }
45 )->store;
46 my $new_suggestion_2 = Koha::Suggestion->new(
47     {   suggestedby  => $patron->{borrowernumber},
48         biblionumber => $biblio_2->biblionumber,
49     }
50 )->store;
51
52 subtest 'store' => sub {
53     plan tests => 3;
54     my $suggestion  = Koha::Suggestion->new(
55         {   suggestedby  => $patron->{borrowernumber},
56             biblionumber => $biblio_1->biblionumber,
57         }
58     )->store;
59
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' );
69     $suggestion->delete;
70 };
71
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' );
74
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' );
77
78 $retrieved_suggestion_1->delete;
79 is( Koha::Suggestions->search->count, $nb_of_suggestions + 1, 'Delete should have deleted the suggestion' );
80
81 $schema->storage->txn_rollback;
82
83 subtest 'constraints' => sub {
84     plan tests => 11;
85     $schema->storage->txn_begin;
86
87     my $print_error = $schema->storage->dbh->{PrintError};
88     $schema->storage->dbh->{PrintError} = 0;
89
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" } );
93
94     my $suggestion = $builder->build_object(
95         {
96             class => "Koha::Suggestions",
97             value => {
98                 suggestedby  => $patron->borrowernumber,
99                 biblionumber => $biblio->biblionumber,
100                 branchcode   => $branch->branchcode,
101                 managedby    => undef,
102                 acceptedby   => undef,
103                 rejectedby   => undef,
104                 budgetid     => undef,
105             }
106         }
107     );
108
109     my $nonexistent_borrowernumber = $patron->borrowernumber;
110     # suggestedby
111     $patron->delete;
112     $suggestion = $suggestion->get_from_storage;
113     is( $suggestion->suggestedby, undef,
114         "The suggestion is not deleted when the related patron is deleted" );
115
116     # biblionumber
117     $biblio->delete;
118     $suggestion = $suggestion->get_from_storage;
119     is( $suggestion->biblionumber, undef,
120         "The suggestion is not deleted when the related biblio is deleted" );
121
122     # branchcode
123     $branch->delete;
124     $suggestion = $suggestion->get_from_storage;
125     is( $suggestion->branchcode, undef,
126         "The suggestion is not deleted when the related branch is deleted" );
127
128     # managerid
129     {   # hide useless warnings
130         local *STDERR;
131         open STDERR, '>', '/dev/null';
132         throws_ok {
133             $suggestion->managedby($nonexistent_borrowernumber)->store;
134         }
135         'Koha::Exceptions::Object::FKConstraint',
136           'store raises an exception on invalid managerid';
137         close STDERR;
138     }
139     my $manager = $builder->build_object( { class => "Koha::Patrons" } );
140     $suggestion->managedby( $manager->borrowernumber )->store;
141     $manager->delete;
142     $suggestion = $suggestion->get_from_storage;
143     is( $suggestion->managedby, undef,
144         "The suggestion is not deleted when the related manager is deleted" );
145
146     # acceptedby
147     {    # hide useless warnings
148         local *STDERR;
149         open STDERR, '>', '/dev/null';
150         throws_ok {
151             $suggestion->acceptedby($nonexistent_borrowernumber)->store;
152         }
153         'Koha::Exceptions::Object::FKConstraint',
154           'store raises an exception on invalid acceptedby id';
155         close STDERR;
156     }
157     my $acceptor = $builder->build_object( { class => "Koha::Patrons" } );
158     $suggestion->acceptedby( $acceptor->borrowernumber )->store;
159     $acceptor->delete;
160     $suggestion = $suggestion->get_from_storage;
161     is( $suggestion->acceptedby, undef,
162         "The suggestion is not deleted when the related acceptor is deleted" );
163
164     # rejectedby
165     {    # hide useless warnings
166         local *STDERR;
167         open STDERR, '>', '/dev/null';
168         throws_ok {
169             $suggestion->rejectedby($nonexistent_borrowernumber)->store;
170         }
171         'Koha::Exceptions::Object::FKConstraint',
172           'store raises an exception on invalid rejectedby id';
173         close STDERR;
174     }
175     my $rejecter = $builder->build_object( { class => "Koha::Patrons" } );
176     $suggestion->rejectedby( $rejecter->borrowernumber )->store;
177     $rejecter->delete;
178     $suggestion = $suggestion->get_from_storage;
179     is( $suggestion->rejectedby, undef,
180         "The suggestion is not deleted when the related rejecter is deleted" );
181
182     # budgetid
183     {    # hide useless warnings
184         local *STDERR;
185         open STDERR, '>', '/dev/null';
186
187         throws_ok { $suggestion->budgetid($nonexistent_borrowernumber)->store; }
188         'Koha::Exceptions::Object::FKConstraint',
189           'store raises an exception on invalid budgetid';
190         close STDERR;
191     }
192     my $fund = $builder->build_object( { class => "Koha::Acquisition::Funds" } );
193     $suggestion->budgetid( $fund->id )->store;
194     $fund->delete;
195     $suggestion = $suggestion->get_from_storage;
196     is( $suggestion->budgetid, undef,
197         "The suggestion is not deleted when the related budget is deleted" );
198
199     $schema->storage->dbh->{PrintError} = $print_error;
200     $schema->storage->txn_rollback;
201 };
202
203 subtest 'manager, suggester, rejecter, last_modifier' => sub {
204     plan tests => 8;
205     $schema->storage->txn_begin;
206
207     my $suggestion = $builder->build_object( { class => 'Koha::Suggestions' } );
208
209     is( ref( $suggestion->manager ),
210         'Koha::Patron',
211         '->manager should have returned a Koha::Patron object' );
212     is( ref( $suggestion->rejecter ),
213         'Koha::Patron',
214         '->rejecter should have returned a Koha::Patron object' );
215     is( ref( $suggestion->suggester ),
216         'Koha::Patron',
217         '->suggester should have returned a Koha::Patron object' );
218     is( ref( $suggestion->last_modifier ),
219         'Koha::Patron',
220         '->last_modifier should have returned a Koha::Patron object' );
221
222     $suggestion->set(
223         {
224             managedby          => undef,
225             rejectedby         => undef,
226             suggestedby        => undef,
227             lastmodificationby => undef
228         }
229     );
230
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,
238         undef,
239         '->last_modifier should have returned undef if no last_modifier set' );
240
241     $schema->storage->txn_rollback;
242 };
243
244 subtest 'fund' => sub {
245     plan tests => 2;
246
247     $schema->storage->txn_begin;
248
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' );
253
254     $suggestion->set( { budgetid => undef } );
255
256     is( $suggestion->fund, undef,
257         '->fund should have returned undef if not fund set' );
258
259     $schema->storage->txn_rollback;
260 };
261
262 subtest 'search_limited() tests' => sub {
263
264     plan tests => 4;
265
266     $schema->storage->txn_begin;
267
268     # Two libraries
269     my $library_1 = $builder->build_object({ class => 'Koha::Libraries' });
270     my $library_2 = $builder->build_object({ class => 'Koha::Libraries' });
271
272     # A patron from $library_1, that is not superlibrarian at all
273     my $patron = $builder->build_object(
274         {
275             class => 'Koha::Patrons',
276             value => { branchcode => $library_1->id, flags => 0 }
277         }
278     );
279
280     # Add 3 suggestions, to be sorted by author
281     my $suggestion_1 = $builder->build_object(
282         {
283             class => 'Koha::Suggestions',
284             value => { branchcode => $library_1->id, author => 'A' }
285         }
286     );
287     my $suggestion_2 = $builder->build_object(
288         {
289             class => 'Koha::Suggestions',
290             value => { branchcode => $library_2->id, author => 'B' }
291         }
292     );
293     my $suggestion_3 = $builder->build_object(
294         {
295             class => 'Koha::Suggestions',
296             value => { branchcode => $library_2->id, author => 'C' }
297         }
298     );
299
300     my $resultset = Koha::Suggestions->search(
301         { branchcode => [ $library_1->id, $library_2->id ] },
302         { order_by   => { -desc => ['author'] } } );
303
304     is( $resultset->count, 3, 'Only this three suggestions are returned' );
305
306     # Now the tests
307     t::lib::Mocks::mock_userenv({ patron => $patron, branchcode => $library_1->id });
308
309     # Disable IndependentBranches
310     t::lib::Mocks::mock_preference( 'IndependentBranches', 0 );
311
312     my $filtered_rs = $resultset->search_limited;
313     is( $filtered_rs->count, 3, 'No IndependentBranches, all suggestions returned' );
314
315     # Enable IndependentBranches
316     t::lib::Mocks::mock_preference( 'IndependentBranches', 1 );
317
318     $filtered_rs = $resultset->search_limited;
319
320     is( $filtered_rs->count, 1, 'IndependentBranches, only suggestions from own branch returned' );
321
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 });
326
327     $filtered_rs = $resultset->search_limited;
328     is( $filtered_rs->count, 3, 'IndependentBranches but patron is superlibrarian, all suggestions returned' );
329
330     $schema->storage->txn_rollback;
331 };