Bug 14544: [QA Follow-up] Fix warning about transaction
[koha.git] / t / db_dependent / Virtualshelves.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4 use Test::More tests => 5;
5 use DateTime::Duration;
6
7 use C4::Context;
8 use Koha::DateUtils;
9 use Koha::Virtualshelves;
10 use Koha::Virtualshelfshares;
11 use Koha::Virtualshelfcontents;
12
13 use t::lib::TestBuilder;
14
15 my $builder = t::lib::TestBuilder->new;
16
17 my $dbh = C4::Context->dbh;
18 $dbh->{AutoCommit} = 0;
19 teardown();
20
21 subtest 'CRUD' => sub {
22     plan tests => 13;
23     my $patron = $builder->build({
24         source => 'Borrower',
25     });
26
27     my $number_of_shelves = Koha::Virtualshelves->search->count;
28
29     is( $number_of_shelves, 0, 'No shelves should exist' );
30
31     my $shelf = Koha::Virtualshelf->new({
32             shelfname => "my first shelf",
33             owner => $patron->{borrowernumber},
34             category => 1,
35         }
36     )->store;
37
38     is( ref( $shelf ), 'Koha::Virtualshelf', 'The constructor should return a valid object' );
39
40     $number_of_shelves = Koha::Virtualshelves->search->count;
41     is( $number_of_shelves, 1, '1 shelf should have been inserted' );
42     is( $shelf->allow_add, 0, 'The default value for allow_add should be 1' );
43     is( $shelf->allow_delete_own, 1, 'The default value for allow_delete_own should be 0' );
44     is( $shelf->allow_delete_other, 0, 'The default value for allow_delete_other should be 0' );
45     is( output_pref($shelf->created_on), output_pref(dt_from_string), 'The creation time should have been set to today' );
46
47     my $retrieved_shelf = Koha::Virtualshelves->find( $shelf->shelfnumber );
48
49     is( $retrieved_shelf->shelfname, $shelf->shelfname, 'Find should correctly return the shelfname' );
50
51     # Insert with the same name
52     eval {
53         $shelf = Koha::Virtualshelf->new({
54                 shelfname => "my first shelf",
55                 owner => $patron->{borrowernumber},
56                 category => 1,
57             }
58         )->store;
59     };
60     is( ref($@), 'Koha::Exceptions::Virtualshelves::DuplicateObject' );
61     $number_of_shelves = Koha::Virtualshelves->search->count;
62     is( $number_of_shelves, 1, 'To be sure the number of shelves is still 1' );
63
64     my $another_patron = $builder->build({
65         source => 'Borrower',
66     });
67
68     $shelf = Koha::Virtualshelf->new({
69             shelfname => "my first shelf",
70             owner => $another_patron->{borrowernumber},
71             category => 1,
72         }
73     )->store;
74     $number_of_shelves = Koha::Virtualshelves->search->count;
75     is( $number_of_shelves, 2, 'Another patron should be able to create a shelf with an existing shelfname');
76
77     my $is_deleted = Koha::Virtualshelves->find( $shelf->shelfnumber )->delete;
78     is( $is_deleted, 1, 'The shelf has been deleted correctly' );
79     $number_of_shelves = Koha::Virtualshelves->search->count;
80     is( $number_of_shelves, 1, 'To be sure the shelf has been deleted' );
81
82     teardown();
83 };
84
85 subtest 'Sharing' => sub {
86     plan tests => 18;
87     my $patron_wants_to_share = $builder->build({
88         source => 'Borrower',
89     });
90     my $share_with_me = $builder->build({
91         source => 'Borrower',
92     });
93     my $just_another_patron = $builder->build({
94         source => 'Borrower',
95     });
96
97     my $number_of_shelves_shared = Koha::Virtualshelfshares->search->count;
98     is( $number_of_shelves_shared, 0, 'No shelves should exist' );
99
100     my $shelf_to_share = Koha::Virtualshelf->new({
101             shelfname => "my first shelf",
102             owner => $patron_wants_to_share->{borrowernumber},
103             category => 1,
104         }
105     )->store;
106
107     my $shelf_not_to_share = Koha::Virtualshelf->new({
108             shelfname => "my second shelf",
109             owner => $patron_wants_to_share->{borrowernumber},
110             category => 1,
111         }
112     )->store;
113
114     my $shared_shelf = eval { $shelf_to_share->share };
115     is ( ref( $@ ), 'Koha::Exceptions::Virtualshelves::InvalidKeyOnSharing', 'Do not share if no key given' );
116     $shared_shelf = eval { $shelf_to_share->share('this is a valid key') };
117     is( ref( $shared_shelf ), 'Koha::Virtualshelfshare', 'On sharing, the method should return a valid Koha::Virtualshelfshare object' );
118
119     my $another_shared_shelf = eval { $shelf_to_share->share('this is another valid key') }; # Just to have 2 shares in DB
120
121     $number_of_shelves_shared = Koha::Virtualshelfshares->search->count;
122     is( $number_of_shelves_shared, 2, '2 shares should have been inserted' );
123
124     my $is_accepted = eval {
125         $shared_shelf->accept( 'this is an invalid key', $share_with_me->{borrowernumber} );
126     };
127     is( $is_accepted, undef, 'The share should have not been accepted if the key is invalid' );
128     is( ref( $@ ), 'Koha::Exceptions::Virtualshelves::InvalidInviteKey', 'accept with an invalid key should raise an exception' );
129
130     $is_accepted = $shared_shelf->accept( 'this is a valid key', $share_with_me->{borrowernumber} );
131     ok( defined($is_accepted), 'The share should have been accepted if the key valid' );
132
133     is( $shelf_to_share->is_shared, 1 );
134     is( $shelf_not_to_share->is_shared, 0 );
135
136     is( $shelf_to_share->is_shared_with( $patron_wants_to_share->{borrowernumber} ), 0 , "The shelf should not be shared with the owner" );
137     is( $shelf_to_share->is_shared_with( $share_with_me->{borrowernumber} ), 1 , "The shelf should be shared with share_with_me" );
138     is( $shelf_to_share->is_shared_with( $just_another_patron->{borrowernumber} ), 0, "The shelf should not be shared with just_another_patron" );
139
140     is( $shelf_to_share->remove_share( $just_another_patron->{borrowernumber} ), 0, 'No share should be removed if the share has not been done with this patron' );
141     $number_of_shelves_shared = Koha::Virtualshelfshares->search->count;
142     is( $number_of_shelves_shared, 2, 'To be sure no shares have been removed' );
143
144     is( $shelf_not_to_share->remove_share( $share_with_me->{borrowernumber} ), 0, '0 share should have been removed if the shelf is not share' );
145     $number_of_shelves_shared = Koha::Virtualshelfshares->search->count;
146     is( $number_of_shelves_shared, 2, 'To be sure no shares have been removed' );
147
148     is( $shelf_to_share->remove_share( $share_with_me->{borrowernumber} ), 1, '1 share should have been removed if the shelf was shared with this patron' );
149     $number_of_shelves_shared = Koha::Virtualshelfshares->search->count;
150     is( $number_of_shelves_shared, 1, 'To be sure the share has been removed' );
151
152     teardown();
153 };
154
155 subtest 'Shelf content' => sub {
156
157     plan tests => 18;
158     my $patron1 = $builder->build( { source => 'Borrower', } );
159     my $patron2 = $builder->build( { source => 'Borrower', } );
160     my $biblio1 = $builder->build( { source => 'Biblio', } );
161     my $biblio2 = $builder->build( { source => 'Biblio', } );
162     my $biblio3 = $builder->build( { source => 'Biblio', } );
163     my $biblio4 = $builder->build( { source => 'Biblio', } );
164     my $number_of_contents = Koha::Virtualshelfcontents->search->count;
165
166     is( $number_of_contents, 0, 'No content should exist' );
167
168     my $dt_yesterday = dt_from_string->subtract_duration( DateTime::Duration->new( days => 1 ) );
169     my $shelf = Koha::Virtualshelf->new(
170         {   shelfname    => "my first shelf",
171             owner        => $patron1->{borrowernumber},
172             category     => 1,
173             lastmodified => $dt_yesterday,
174         }
175     )->store;
176
177     $shelf = Koha::Virtualshelves->find( $shelf->shelfnumber );
178     is( output_pref( dt_from_string $shelf->lastmodified ), output_pref($dt_yesterday), 'The lastmodified has been set to yesterday, will be useful for another test later' );
179     my $content1 = $shelf->add_biblio( $biblio1->{biblionumber}, $patron1->{borrowernumber} );
180     is( ref($content1), 'Koha::Virtualshelfcontent', 'add_biblio to a shelf should return a Koha::Virtualshelfcontent object if inserted' );
181     $shelf = Koha::Virtualshelves->find( $shelf->shelfnumber );
182     is( output_pref( dt_from_string( $shelf->lastmodified ) ), output_pref(dt_from_string), 'Adding a biblio to a shelf should update the lastmodified for the shelf' );
183     my $content2 = $shelf->add_biblio( $biblio2->{biblionumber}, $patron1->{borrowernumber} );
184     $number_of_contents = Koha::Virtualshelfcontents->search->count;
185     is( $number_of_contents, 2, '2 biblio should have been inserted' );
186
187     my $content1_bis = $shelf->add_biblio( $biblio1->{biblionumber}, $patron1->{borrowernumber} );
188     is( $content1_bis, undef, 'add_biblio should return undef on duplicate' );    # Or an exception ?
189     $number_of_contents = Koha::Virtualshelfcontents->search->count;
190     is( $number_of_contents, 2, 'The biblio should not have been duplicated' );
191
192     $shelf = Koha::Virtualshelves->find( $shelf->shelfnumber );
193     my $contents = $shelf->get_contents;
194     is( $contents->count, 2, 'There are 2 biblios on this shelf' );
195
196     # Patron 2 will try to remove a content
197     # allow_add = 0, allow_delete_own = 1, allow_delete_other = 0 => Default values
198     my $number_of_deleted_biblios = $shelf->remove_biblios( { biblionumbers => [ $biblio1->{biblionumber} ], borrowernumber => $patron2->{borrowernumber} } );
199     is( $number_of_deleted_biblios, 0, );
200     $number_of_deleted_biblios = $shelf->remove_biblios( { biblionumbers => [ $biblio1->{biblionumber} ], borrowernumber => $patron1->{borrowernumber} } );
201     is( $number_of_deleted_biblios, 1, );
202
203     $number_of_contents = Koha::Virtualshelfcontents->search->count;
204     is( $number_of_contents, 1, 'To be sure the content has been deleted' );
205
206     # allow_delete_own = 0
207     $shelf->allow_delete_own(0);
208     $shelf->store;
209     $number_of_deleted_biblios = $shelf->remove_biblios( { biblionumbers => [ $biblio2->{biblionumber} ], borrowernumber => $patron1->{borrowernumber} } );
210     is( $number_of_deleted_biblios, 1, );
211     $number_of_contents = Koha::Virtualshelfcontents->search->count;
212     is( $number_of_contents, 0, 'The biblio should have been deleted to the shelf by the patron, it is his own content (allow_delete_own=0)' );
213     $shelf->add_biblio( $biblio2->{biblionumber}, $patron1->{borrowernumber} );
214
215     # allow_add = 1, allow_delete_own = 1
216     $shelf->allow_add(1);
217     $shelf->allow_delete_own(1);
218     $shelf->store;
219
220     my $content3 = $shelf->add_biblio( $biblio3->{biblionumber}, $patron2->{borrowernumber} );
221     my $content4 = $shelf->add_biblio( $biblio4->{biblionumber}, $patron2->{borrowernumber} );
222
223     $number_of_contents = Koha::Virtualshelfcontents->search->count;
224     is( $number_of_contents, 3, 'The biblio should have been added to the shelf by the patron 2' );
225
226     $number_of_deleted_biblios = $shelf->remove_biblios( { biblionumbers => [ $biblio3->{biblionumber} ], borrowernumber => $patron2->{borrowernumber} } );
227     is( $number_of_deleted_biblios, 1, );
228     $number_of_contents = Koha::Virtualshelfcontents->search->count;
229     is( $number_of_contents, 2, 'The biblio should have been deleted to the shelf by the patron, it is his own content (allow_delete_own=1) ' );
230
231     # allow_add = 1, allow_delete_own = 1, allow_delete_other = 1
232     $shelf->allow_delete_other(1);
233     $shelf->store;
234
235     $number_of_deleted_biblios = $shelf->remove_biblios( { biblionumbers => [ $biblio2->{biblionumber} ], borrowernumber => $patron2->{borrowernumber} } );
236     is( $number_of_deleted_biblios, 1, );
237     $number_of_contents = Koha::Virtualshelfcontents->search->count;
238     is( $number_of_contents, 1, 'The biblio should have been deleted to the shelf by the patron 2, even if it is not his own content (allow_delete_other=1)' );
239
240     teardown();
241 };
242
243 subtest 'Shelf permissions' => sub {
244
245     plan tests => 40;
246     my $patron1 = $builder->build( { source => 'Borrower', value => { flags => '2096766' } } ); # 2096766 is everything checked but not superlibrarian
247     my $patron2 = $builder->build( { source => 'Borrower', value => { flags => '1048190' } } ); # 1048190 is everything checked but not superlibrarian and delete_public_lists
248     my $biblio1 = $builder->build( { source => 'Biblio', } );
249     my $biblio2 = $builder->build( { source => 'Biblio', } );
250     my $biblio3 = $builder->build( { source => 'Biblio', } );
251     my $biblio4 = $builder->build( { source => 'Biblio', } );
252
253
254     my $public_shelf = Koha::Virtualshelf->new(
255         {   shelfname    => "my first shelf",
256             owner        => $patron1->{borrowernumber},
257             category     => 2,
258             allow_add          => 0,
259             allow_delete_own   => 0,
260             allow_delete_other => 0,
261         }
262     )->store;
263
264     is( $public_shelf->can_be_viewed( $patron1->{borrowernumber} ), 1, 'The owner should be able to view his public list' );
265     is( $public_shelf->can_be_viewed( $patron2->{borrowernumber} ), 1, 'Public list should be viewed by someone else' );
266
267     is( $public_shelf->can_be_deleted( $patron1->{borrowernumber} ), 1, 'The owner should be able to delete his list' );
268     is( $public_shelf->can_be_deleted( $patron2->{borrowernumber} ), 0, 'Public list should not be deleted by someone else' );
269
270     is( $public_shelf->can_be_managed( $patron1->{borrowernumber} ), 1, 'The owner should be able to manage his list' );
271     is( $public_shelf->can_be_managed( $patron2->{borrowernumber} ), 0, 'Public list should not be managed by someone else' );
272
273     is( $public_shelf->can_biblios_be_added( $patron1->{borrowernumber} ), 1, 'The owner should be able to add biblios to his list' );
274     is( $public_shelf->can_biblios_be_added( $patron2->{borrowernumber} ), 0, 'Public list should not be modified (add) by someone else' );
275
276     is( $public_shelf->can_biblios_be_removed( $patron1->{borrowernumber} ), 1, 'The owner should be able to remove biblios to his list' );
277     is( $public_shelf->can_biblios_be_removed( $patron2->{borrowernumber} ), 0, 'Public list should not be modified (remove) by someone else' );
278
279
280     $public_shelf->allow_add(1);
281     $public_shelf->allow_delete_own(1);
282     $public_shelf->allow_delete_other(1);
283     $public_shelf->store;
284
285     is( $public_shelf->can_be_viewed( $patron1->{borrowernumber} ), 1, 'The owner should be able to view his public list' );
286     is( $public_shelf->can_be_viewed( $patron2->{borrowernumber} ), 1, 'Public list should be viewed by someone else' );
287
288     is( $public_shelf->can_be_deleted( $patron1->{borrowernumber} ), 1, 'The owner should be able to delete his list' );
289     is( $public_shelf->can_be_deleted( $patron2->{borrowernumber} ), 0, 'Public list should not be deleted by someone else' );
290
291     is( $public_shelf->can_be_managed( $patron1->{borrowernumber} ), 1, 'The owner should be able to manage his list' );
292     is( $public_shelf->can_be_managed( $patron2->{borrowernumber} ), 0, 'Public list should not be managed by someone else' );
293
294     is( $public_shelf->can_biblios_be_added( $patron1->{borrowernumber} ), 1, 'The owner should be able to add biblios to his list' );
295     is( $public_shelf->can_biblios_be_added( $patron2->{borrowernumber} ), 1, 'Public list should not be modified (add) by someone else' );
296
297     is( $public_shelf->can_biblios_be_removed( $patron1->{borrowernumber} ), 1, 'The owner should be able to remove biblios to his list' );
298     is( $public_shelf->can_biblios_be_removed( $patron2->{borrowernumber} ), 1, 'Public list should not be modified (remove) by someone else' );
299
300
301     my $private_shelf = Koha::Virtualshelf->new(
302         {   shelfname    => "my first shelf",
303             owner        => $patron1->{borrowernumber},
304             category     => 1,
305             allow_add          => 0,
306             allow_delete_own   => 0,
307             allow_delete_other => 0,
308         }
309     )->store;
310
311     is( $private_shelf->can_be_viewed( $patron1->{borrowernumber} ), 1, 'The owner should be able to view his list' );
312     is( $private_shelf->can_be_viewed( $patron2->{borrowernumber} ), 0, 'Private list should not be viewed by someone else' );
313
314     is( $private_shelf->can_be_deleted( $patron1->{borrowernumber} ), 1, 'The owner should be able to delete his list' );
315     is( $private_shelf->can_be_deleted( $patron2->{borrowernumber} ), 0, 'Private list should not be deleted by someone else' );
316
317     is( $private_shelf->can_be_managed( $patron1->{borrowernumber} ), 1, 'The owner should be able to manage his list' );
318     is( $private_shelf->can_be_managed( $patron2->{borrowernumber} ), 0, 'Private list should not be managed by someone else' );
319
320     is( $private_shelf->can_biblios_be_added( $patron1->{borrowernumber} ), 1, 'The owner should be able to add biblios to his list' );
321     is( $private_shelf->can_biblios_be_added( $patron2->{borrowernumber} ), 0, 'Private list should not be modified (add) by someone else' );
322
323     is( $private_shelf->can_biblios_be_removed( $patron1->{borrowernumber} ), 1, 'The owner should be able to remove biblios to his list' );
324     is( $private_shelf->can_biblios_be_removed( $patron2->{borrowernumber} ), 0, 'Private list should not be modified (remove) by someone else' );
325
326
327     $private_shelf->allow_add(1);
328     $private_shelf->allow_delete_own(1);
329     $private_shelf->allow_delete_other(1);
330     $private_shelf->store;
331
332     is( $private_shelf->can_be_viewed( $patron1->{borrowernumber} ), 1, 'The owner should be able to view his list' );
333     is( $private_shelf->can_be_viewed( $patron2->{borrowernumber} ), 0, 'Private list should not be viewed by someone else' );
334
335     is( $private_shelf->can_be_deleted( $patron1->{borrowernumber} ), 1, 'The owner should be able to delete his list' );
336     is( $private_shelf->can_be_deleted( $patron2->{borrowernumber} ), 0, 'Private list should not be deleted by someone else' );
337
338     is( $private_shelf->can_be_managed( $patron1->{borrowernumber} ), 1, 'The owner should be able to manage his list' );
339     is( $private_shelf->can_be_managed( $patron2->{borrowernumber} ), 0, 'Private list should not be managed by someone else' );
340
341     is( $private_shelf->can_biblios_be_added( $patron1->{borrowernumber} ), 1, 'The owner should be able to add biblios to his list' );
342     is( $private_shelf->can_biblios_be_added( $patron2->{borrowernumber} ), 1, 'Private list could be modified (add) by someone else # individual check done later' );
343
344     is( $private_shelf->can_biblios_be_removed( $patron1->{borrowernumber} ), 1, 'The owner should be able to remove biblios to his list' );
345     is( $private_shelf->can_biblios_be_removed( $patron2->{borrowernumber} ), 1, 'Private list could be modified (remove) by someone else # individual check done later' );
346
347     teardown();
348 };
349
350 subtest 'Get shelves' => sub {
351     plan tests => 4;
352     my $patron1 = $builder->build({
353         source => 'Borrower',
354     });
355     my $patron2 = $builder->build({
356         source => 'Borrower',
357     });
358
359     my $private_shelf1_1 = Koha::Virtualshelf->new({
360             shelfname => "private shelf 1 for patron 1",
361             owner => $patron1->{borrowernumber},
362             category => 1,
363         }
364     )->store;
365     my $private_shelf1_2 = Koha::Virtualshelf->new({
366             shelfname => "private shelf 2 for patron 1",
367             owner => $patron1->{borrowernumber},
368             category => 1,
369         }
370     )->store;
371     my $private_shelf2_1 = Koha::Virtualshelf->new({
372             shelfname => "private shelf 1 for patron 2",
373             owner => $patron2->{borrowernumber},
374             category => 1,
375         }
376     )->store;
377     my $public_shelf1_1 = Koha::Virtualshelf->new({
378             shelfname => "public shelf 1 for patron 1",
379             owner => $patron1->{borrowernumber},
380             category => 2,
381         }
382     )->store;
383     my $public_shelf1_2 = Koha::Virtualshelf->new({
384             shelfname => "public shelf 2 for patron 1",
385             owner => $patron1->{borrowernumber},
386             category => 2,
387         }
388     )->store;
389
390     my $private_shelves = Koha::Virtualshelves->get_private_shelves;
391     is( $private_shelves->count, 0, 'Without borrowernumber given, get_private_shelves should not return any shelf' );
392     $private_shelves = Koha::Virtualshelves->get_private_shelves({ borrowernumber => $patron1->{borrowernumber} });
393     is( $private_shelves->count, 2, 'get_private_shelves should return all shelves for a given patron' );
394
395     $private_shelf2_1->share('a key')->accept('a key', $patron1->{borrowernumber});
396     $private_shelves = Koha::Virtualshelves->get_private_shelves({ borrowernumber => $patron1->{borrowernumber} });
397     is( $private_shelves->count, 3, 'get_private_shelves should return all shelves for a given patron, even the shared ones' );
398
399     my $public_shelves = Koha::Virtualshelves->get_public_shelves;
400     is( $public_shelves->count, 2, 'get_public_shelves should return all public shelves, no matter who is the owner' );
401
402     teardown();
403 };
404
405 sub teardown {
406     $dbh->do(q|DELETE FROM virtualshelfshares|);
407     $dbh->do(q|DELETE FROM virtualshelfcontents|);
408     $dbh->do(q|DELETE FROM virtualshelves|);
409 }