Bug 26384: Fix executable flags
[koha.git] / t / db_dependent / Virtualshelves.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4 use Test::More tests => 6;
5 use DateTime::Duration;
6
7 use C4::Context;
8 use Koha::Database;
9 use Koha::DateUtils;
10 use Koha::Virtualshelves;
11 use Koha::Virtualshelfshares;
12 use Koha::Virtualshelfcontents;
13
14 use t::lib::Dates;
15 use t::lib::TestBuilder;
16
17 my $builder = t::lib::TestBuilder->new;
18
19 my $schema = Koha::Database->new->schema;
20 $schema->storage->txn_begin;
21 my $dbh = C4::Context->dbh;
22 teardown();
23
24 subtest 'CRUD' => sub {
25     plan tests => 13;
26     my $patron = $builder->build({
27         source => 'Borrower',
28     });
29
30     my $number_of_shelves = Koha::Virtualshelves->search->count;
31
32     is( $number_of_shelves, 0, 'No shelves should exist' );
33
34     my $shelf = Koha::Virtualshelf->new({
35             shelfname => "my first shelf",
36             owner => $patron->{borrowernumber},
37             category => 1,
38         }
39     )->store;
40
41     is( ref( $shelf ), 'Koha::Virtualshelf', 'The constructor should return a valid object' );
42
43     $number_of_shelves = Koha::Virtualshelves->search->count;
44     is( $number_of_shelves, 1, '1 shelf should have been inserted' );
45     is( $shelf->allow_change_from_owner, 1, 'The default value for allow_change_from_owner should be 1' );
46     is( $shelf->allow_change_from_others, 0, 'The default value for allow_change_from_others should be 0' );
47     is( t::lib::Dates::compare( $shelf->created_on, dt_from_string), 0, 'The creation time should have been set to today' );
48
49     # Test if creation date will not be overwritten by store
50     my $created = dt_from_string->subtract( hours => 1 );
51     $shelf->created_on( $created );
52     $shelf->store;
53
54     my $retrieved_shelf = Koha::Virtualshelves->find( $shelf->shelfnumber );
55
56     is( $retrieved_shelf->shelfname, $shelf->shelfname, 'Find should correctly return the shelfname' );
57     is( t::lib::Dates::compare( $retrieved_shelf->created_on, $created), 0, 'Creation date is the same after update (Bug 18672)' );
58
59     # Insert with the same name
60     eval {
61         $shelf = Koha::Virtualshelf->new({
62                 shelfname => "my first shelf",
63                 owner => $patron->{borrowernumber},
64                 category => 1,
65             }
66         )->store;
67     };
68     is( ref($@), 'Koha::Exceptions::Virtualshelves::DuplicateObject',
69         'Exception on duplicate name' );
70     $number_of_shelves = Koha::Virtualshelves->search->count;
71     is( $number_of_shelves, 1, 'To be sure the number of shelves is still 1' );
72
73     my $another_patron = $builder->build({
74         source => 'Borrower',
75     });
76
77     $shelf = Koha::Virtualshelf->new({
78             shelfname => "my first shelf",
79             owner => $another_patron->{borrowernumber},
80             category => 1,
81         }
82     )->store;
83     $number_of_shelves = Koha::Virtualshelves->search->count;
84     is( $number_of_shelves, 2, 'Another patron should be able to create a shelf with an existing shelfname');
85
86     my $is_deleted = Koha::Virtualshelves->find( $shelf->shelfnumber )->delete;
87     ok( $is_deleted, 'The shelf has been deleted correctly' );
88     $number_of_shelves = Koha::Virtualshelves->search->count;
89     is( $number_of_shelves, 1, 'To be sure the shelf has been deleted' );
90
91     teardown();
92 };
93
94 subtest 'Sharing' => sub {
95     plan tests => 21;
96     my $patron_wants_to_share = $builder->build({
97         source => 'Borrower',
98     });
99     my $share_with_me = $builder->build({
100         source => 'Borrower',
101     });
102     my $just_another_patron = $builder->build({
103         source => 'Borrower',
104     });
105
106     my $number_of_shelves_shared = Koha::Virtualshelfshares->search->count;
107     is( $number_of_shelves_shared, 0, 'No shelves should exist' );
108
109     my $shelf_to_share = Koha::Virtualshelf->new({
110             shelfname => "my first shelf",
111             owner => $patron_wants_to_share->{borrowernumber},
112             category => 1,
113         }
114     )->store;
115
116     my $shelf_not_to_share = Koha::Virtualshelf->new({
117             shelfname => "my second shelf",
118             owner => $patron_wants_to_share->{borrowernumber},
119             category => 1,
120         }
121     )->store;
122
123     my $shared_shelf = eval { $shelf_to_share->share };
124     is ( ref( $@ ), 'Koha::Exceptions::Virtualshelves::InvalidKeyOnSharing', 'Do not share if no key given' );
125     $shared_shelf = eval { $shelf_to_share->share('valid key') };
126     is( ref( $shared_shelf ), 'Koha::Virtualshelfshare', 'On sharing, the method should return a valid Koha::Virtualshelfshare object' );
127
128     my $another_shared_shelf = eval { $shelf_to_share->share('valid key2') }; # Just to have 2 shares in DB
129
130     $number_of_shelves_shared = Koha::Virtualshelfshares->search->count;
131     is( $number_of_shelves_shared, 2, '2 shares should have been inserted' );
132
133     my $is_accepted = eval {
134         $shared_shelf->accept( 'invalid k', $share_with_me->{borrowernumber} );
135     };
136     is( $is_accepted, undef, 'The share should have not been accepted if the key is invalid' );
137     is( ref( $@ ), 'Koha::Exceptions::Virtualshelves::InvalidInviteKey', 'accept with an invalid key should raise an exception' );
138
139     $is_accepted = $shared_shelf->accept( 'valid key', $share_with_me->{borrowernumber} );
140     ok( defined($is_accepted), 'The share should have been accepted if the key valid' );
141
142     is( $shelf_to_share->is_shared, 1, 'first shelf is shared' );
143     is( $shelf_not_to_share->is_shared, 0, 'second shelf is not shared' );
144
145     is( $shelf_to_share->is_shared_with( $patron_wants_to_share->{borrowernumber} ), 0 , "The shelf should not be shared with the owner" );
146     is( $shelf_to_share->is_shared_with( $share_with_me->{borrowernumber} ), 1 , "The shelf should be shared with share_with_me" );
147     is( $shelf_to_share->is_shared_with( $just_another_patron->{borrowernumber} ), 0, "The shelf should not be shared with just_another_patron" );
148
149     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' );
150     $number_of_shelves_shared = Koha::Virtualshelfshares->search->count;
151     is( $number_of_shelves_shared, 2, 'To be sure no shares have been removed' );
152
153     is( $shelf_not_to_share->remove_share( $share_with_me->{borrowernumber} ), 0, '0 share should have been removed if the shelf is not share' );
154     $number_of_shelves_shared = Koha::Virtualshelfshares->search->count;
155     is( $number_of_shelves_shared, 2, 'To be sure no shares have been removed' );
156
157     # Test double accept (BZ 11943) before removing the accepted share
158     my $third_share = $shelf_to_share->share('valid key3');
159     is( Koha::Virtualshelfshares->search->count, 3, 'Three shares' );
160     $is_accepted = $third_share->accept( 'valid key3', $share_with_me->{borrowernumber} );
161     is( $is_accepted->shelfnumber, $shelf_to_share->shelfnumber, 'Accept returned the existing share' );
162     is( Koha::Virtualshelfshares->search->count, 2, 'Check that number of shares went down again' );
163
164     # Remove the first accept
165     ok( $shelf_to_share->remove_share( $share_with_me->{borrowernumber} ), '1 share should have been removed if the shelf was shared with this patron' );
166     $number_of_shelves_shared = Koha::Virtualshelfshares->search->count;
167     is( $number_of_shelves_shared, 1, 'To be sure the share has been removed' );
168
169     teardown();
170 };
171
172 subtest 'Shelf content' => sub {
173
174     plan tests => 18;
175     my $patron1 = $builder->build( { source => 'Borrower', } );
176     my $patron2 = $builder->build( { source => 'Borrower', } );
177     my $biblio1 = $builder->build_sample_biblio;
178     my $biblio2 = $builder->build_sample_biblio;
179     my $biblio3 = $builder->build_sample_biblio;
180     my $biblio4 = $builder->build_sample_biblio;
181     my $number_of_contents = Koha::Virtualshelfcontents->search->count;
182
183     is( $number_of_contents, 0, 'No content should exist' );
184
185     my $dt_yesterday = dt_from_string->subtract_duration( DateTime::Duration->new( days => 1 ) );
186     my $shelf = Koha::Virtualshelf->new(
187         {   shelfname    => "my first shelf",
188             owner        => $patron1->{borrowernumber},
189             category     => 1,
190             lastmodified => $dt_yesterday,
191         }
192     )->store;
193
194     $shelf = Koha::Virtualshelves->find( $shelf->shelfnumber );
195     is( t::lib::Dates::compare( $shelf->lastmodified, $dt_yesterday), 0, 'The lastmodified has been set to yesterday, will be useful for another test later' );
196     my $content1 = $shelf->add_biblio( $biblio1->biblionumber, $patron1->{borrowernumber} );
197     is( ref($content1), 'Koha::Virtualshelfcontent', 'add_biblio to a shelf should return a Koha::Virtualshelfcontent object if inserted' );
198     $shelf = Koha::Virtualshelves->find( $shelf->shelfnumber );
199     is( t::lib::Dates::compare( $shelf->lastmodified, dt_from_string), 0, 'Adding a biblio to a shelf should update the lastmodified for the shelf' );
200     my $content2 = $shelf->add_biblio( $biblio2->biblionumber, $patron1->{borrowernumber} );
201     $number_of_contents = Koha::Virtualshelfcontents->search->count;
202     is( $number_of_contents, 2, '2 biblio should have been inserted' );
203
204     my $content1_bis = $shelf->add_biblio( $biblio1->biblionumber, $patron1->{borrowernumber} );
205     is( $content1_bis, undef, 'add_biblio should return undef on duplicate' );    # Or an exception ?
206     $number_of_contents = Koha::Virtualshelfcontents->search->count;
207     is( $number_of_contents, 2, 'The biblio should not have been duplicated' );
208
209     $shelf = Koha::Virtualshelves->find( $shelf->shelfnumber );
210     my $contents = $shelf->get_contents;
211     is( $contents->count, 2, 'There are 2 biblios on this shelf' );
212
213     # Patron 2 will try to remove biblios
214     # allow_change_from_owner = 1, allow_change_from_others = 0 (defaults)
215     my $number_of_deleted_biblios = $shelf->remove_biblios( { biblionumbers => [ $biblio1->biblionumber ], borrowernumber => $patron2->{borrowernumber} } );
216     is( $number_of_deleted_biblios, 0, 'Patron 2 removed nothing' );
217     # Now try with patron 1
218     $number_of_deleted_biblios = $shelf->remove_biblios( { biblionumbers => [ $biblio1->biblionumber ], borrowernumber => $patron1->{borrowernumber} } );
219     is( $number_of_deleted_biblios, 1, 'Patron 1 removed biblio' );
220     $number_of_contents = Koha::Virtualshelfcontents->search->count;
221     is( $number_of_contents, 1, 'To be sure the content has been deleted' );
222
223     # allow_change_from_owner == 0 (readonly)
224     $shelf->allow_change_from_owner( 0 );
225     $shelf->store;
226     $number_of_deleted_biblios = $shelf->remove_biblios( { biblionumbers => [ $biblio2->biblionumber ], borrowernumber => $patron1->{borrowernumber} } );
227     is( $number_of_deleted_biblios, 0, 'Owner could not delete' );
228     $number_of_contents = Koha::Virtualshelfcontents->search->count;
229     is( $number_of_contents, 1, 'Number of entries still equal to 1' );
230     $shelf->add_biblio( $biblio2->biblionumber, $patron1->{borrowernumber} );
231     $number_of_contents = Koha::Virtualshelfcontents->search->count;
232     is( $number_of_contents, 1, 'Biblio not added to the list' );
233     # Add back biblio1
234     $shelf->allow_change_from_owner( 1 );
235     $shelf->add_biblio( $biblio1->biblionumber, $patron1->{borrowernumber} );
236     $number_of_contents = Koha::Virtualshelfcontents->search->count;
237     is( $number_of_contents, 2, 'Biblio added to the list' );
238
239     # allow_change_from_others == 1
240     $shelf->allow_change_from_others( 1 );
241     my $content3 = $shelf->add_biblio( $biblio3->biblionumber, $patron2->{borrowernumber} );
242     my $content4 = $shelf->add_biblio( $biblio4->biblionumber, $patron2->{borrowernumber} );
243     $number_of_contents = Koha::Virtualshelfcontents->search->count;
244     is( $number_of_contents, 4, 'The biblio should have been added to the shelf by the patron 2' );
245     $number_of_deleted_biblios = $shelf->remove_biblios( { biblionumbers => [ $biblio3->biblionumber ], borrowernumber => $patron2->{borrowernumber} } );
246     is( $number_of_deleted_biblios, 1, 'Biblio 3 deleted by patron 2' );
247     $number_of_contents = Koha::Virtualshelfcontents->search->count;
248     is( $number_of_contents, 3, 'Back to three entries' );
249
250     teardown();
251 };
252
253 subtest 'Shelf permissions' => sub {
254
255     plan tests => 40;
256     my $patron1 = $builder->build( { source => 'Borrower', value => { flags => '2096766' } } ); # 2096766 is everything checked but not superlibrarian
257     my $patron2 = $builder->build( { source => 'Borrower', value => { flags => '1048190' } } ); # 1048190 is everything checked but not superlibrarian and delete_public_lists
258     my $biblio1 = $builder->build_sample_biblio;
259     my $biblio2 = $builder->build_sample_biblio;
260     my $biblio3 = $builder->build_sample_biblio;
261     my $biblio4 = $builder->build_sample_biblio;
262
263     my $public_shelf = Koha::Virtualshelf->new(
264         {   shelfname    => "my first shelf",
265             owner        => $patron1->{borrowernumber},
266             category     => 2,
267             allow_change_from_owner => 0,
268             allow_change_from_others => 0,
269         }
270     )->store;
271
272     is( $public_shelf->can_be_viewed( $patron1->{borrowernumber} ), 1, 'The owner should be able to view his public list' );
273     is( $public_shelf->can_be_viewed( $patron2->{borrowernumber} ), 1, 'Public list should be viewed by someone else' );
274
275     is( $public_shelf->can_be_deleted( $patron1->{borrowernumber} ), 1, 'The owner should be able to delete his list' );
276     is( $public_shelf->can_be_deleted( $patron2->{borrowernumber} ), 0, 'Public list should not be deleted by someone else' );
277
278     is( $public_shelf->can_be_managed( $patron1->{borrowernumber} ), 1, 'The owner should be able to manage his list' );
279     is( $public_shelf->can_be_managed( $patron2->{borrowernumber} ), 0, 'Public list should not be managed by someone else' );
280
281     is( $public_shelf->can_biblios_be_added( $patron1->{borrowernumber} ), 0, 'The owner should not be able to add biblios to their list' );
282     is( $public_shelf->can_biblios_be_added( $patron2->{borrowernumber} ), 0, 'Public list should not be modified (add) by someone else' );
283
284     is( $public_shelf->can_biblios_be_removed( $patron1->{borrowernumber} ), 0, 'The owner should not be able to remove biblios to their list' );
285     is( $public_shelf->can_biblios_be_removed( $patron2->{borrowernumber} ), 0, 'Public list should not be modified (remove) by someone else' );
286
287
288     $public_shelf->allow_change_from_owner(1);
289     $public_shelf->store;
290
291     is( $public_shelf->can_be_viewed( $patron1->{borrowernumber} ), 1, 'The owner should be able to view his public list' );
292     is( $public_shelf->can_be_viewed( $patron2->{borrowernumber} ), 1, 'Public list should be viewed by someone else' );
293
294     is( $public_shelf->can_be_deleted( $patron1->{borrowernumber} ), 1, 'The owner should be able to delete his list' );
295     is( $public_shelf->can_be_deleted( $patron2->{borrowernumber} ), 0, 'Public list should not be deleted by someone else' );
296
297     is( $public_shelf->can_be_managed( $patron1->{borrowernumber} ), 1, 'The owner should be able to manage his list' );
298     is( $public_shelf->can_be_managed( $patron2->{borrowernumber} ), 0, 'Public list should not be managed by someone else' );
299
300     is( $public_shelf->can_biblios_be_added( $patron1->{borrowernumber} ), 1, 'The owner should be able to add biblios to his list' );
301     is( $public_shelf->can_biblios_be_added( $patron2->{borrowernumber} ), 0, 'Public list should not be modified (add) by someone else' );
302
303     is( $public_shelf->can_biblios_be_removed( $patron1->{borrowernumber} ), 1, 'The owner should be able to remove biblios to his list' );
304     is( $public_shelf->can_biblios_be_removed( $patron2->{borrowernumber} ), 0, 'Public list should not be modified (remove) by someone else' );
305
306
307     my $private_shelf = Koha::Virtualshelf->new(
308         {   shelfname    => "my first shelf",
309             owner        => $patron1->{borrowernumber},
310             category     => 1,
311             allow_change_from_owner => 0,
312             allow_change_from_others => 0,
313         }
314     )->store;
315
316     is( $private_shelf->can_be_viewed( $patron1->{borrowernumber} ), 1, 'The owner should be able to view his list' );
317     is( $private_shelf->can_be_viewed( $patron2->{borrowernumber} ), 0, 'Private list should not be viewed by someone else' );
318
319     is( $private_shelf->can_be_deleted( $patron1->{borrowernumber} ), 1, 'The owner should be able to delete his list' );
320     is( $private_shelf->can_be_deleted( $patron2->{borrowernumber} ), 0, 'Private list should not be deleted by someone else' );
321
322     is( $private_shelf->can_be_managed( $patron1->{borrowernumber} ), 1, 'The owner should be able to manage his list' );
323     is( $private_shelf->can_be_managed( $patron2->{borrowernumber} ), 0, 'Private list should not be managed by someone else' );
324
325     is( $private_shelf->can_biblios_be_added( $patron1->{borrowernumber} ), 0, 'The owner should not be able to add biblios to their list' );
326     is( $private_shelf->can_biblios_be_added( $patron2->{borrowernumber} ), 0, 'Private list should not be modified (add) by someone else' );
327
328     is( $private_shelf->can_biblios_be_removed( $patron1->{borrowernumber} ), 0, 'The owner should not be able to remove biblios to their list' );
329     is( $private_shelf->can_biblios_be_removed( $patron2->{borrowernumber} ), 0, 'Private list should not be modified (remove) by someone else' );
330
331
332     $private_shelf->allow_change_from_owner(1);
333     $private_shelf->allow_change_from_others(1);
334     $private_shelf->store;
335
336     is( $private_shelf->can_be_viewed( $patron1->{borrowernumber} ), 1, 'The owner should be able to view his list' );
337     is( $private_shelf->can_be_viewed( $patron2->{borrowernumber} ), 0, 'Private list should not be viewed by someone else' );
338
339     is( $private_shelf->can_be_deleted( $patron1->{borrowernumber} ), 1, 'The owner should be able to delete his list' );
340     is( $private_shelf->can_be_deleted( $patron2->{borrowernumber} ), 0, 'Private list should not be deleted by someone else' );
341
342     is( $private_shelf->can_be_managed( $patron1->{borrowernumber} ), 1, 'The owner should be able to manage his list' );
343     is( $private_shelf->can_be_managed( $patron2->{borrowernumber} ), 0, 'Private list should not be managed by someone else' );
344
345     is( $private_shelf->can_biblios_be_added( $patron1->{borrowernumber} ), 1, 'The owner should be able to add biblios to his list' );
346     is( $private_shelf->can_biblios_be_added( $patron2->{borrowernumber} ), 1, 'Private list could be modified (add) by someone else # individual check done later' );
347
348     is( $private_shelf->can_biblios_be_removed( $patron1->{borrowernumber} ), 1, 'The owner should be able to remove biblios to his list' );
349     is( $private_shelf->can_biblios_be_removed( $patron2->{borrowernumber} ), 1, 'Private list could be modified (remove) by someone else # individual check done later' );
350
351     teardown();
352 };
353
354 subtest 'Get shelves' => sub {
355     plan tests => 4;
356     my $patron1 = $builder->build({
357         source => 'Borrower',
358     });
359     my $patron2 = $builder->build({
360         source => 'Borrower',
361     });
362
363     my $private_shelf1_1 = Koha::Virtualshelf->new({
364             shelfname => "private shelf 1 for patron 1",
365             owner => $patron1->{borrowernumber},
366             category => 1,
367         }
368     )->store;
369     my $private_shelf1_2 = Koha::Virtualshelf->new({
370             shelfname => "private shelf 2 for patron 1",
371             owner => $patron1->{borrowernumber},
372             category => 1,
373         }
374     )->store;
375     my $private_shelf2_1 = Koha::Virtualshelf->new({
376             shelfname => "private shelf 1 for patron 2",
377             owner => $patron2->{borrowernumber},
378             category => 1,
379         }
380     )->store;
381     my $public_shelf1_1 = Koha::Virtualshelf->new({
382             shelfname => "public shelf 1 for patron 1",
383             owner => $patron1->{borrowernumber},
384             category => 2,
385         }
386     )->store;
387     my $public_shelf1_2 = Koha::Virtualshelf->new({
388             shelfname => "public shelf 2 for patron 1",
389             owner => $patron1->{borrowernumber},
390             category => 2,
391         }
392     )->store;
393
394     my $private_shelves = Koha::Virtualshelves->get_private_shelves;
395     is( $private_shelves->count, 0, 'Without borrowernumber given, get_private_shelves should not return any shelf' );
396     $private_shelves = Koha::Virtualshelves->get_private_shelves({ borrowernumber => $patron1->{borrowernumber} });
397     is( $private_shelves->count, 2, 'get_private_shelves should return all shelves for a given patron' );
398
399     $private_shelf2_1->share('a key')->accept('a key', $patron1->{borrowernumber});
400     $private_shelves = Koha::Virtualshelves->get_private_shelves({ borrowernumber => $patron1->{borrowernumber} });
401     is( $private_shelves->count, 3, 'get_private_shelves should return all shelves for a given patron, even the shared ones' );
402
403     my $public_shelves = Koha::Virtualshelves->get_public_shelves;
404     is( $public_shelves->count, 2, 'get_public_shelves should return all public shelves, no matter who is the owner' );
405
406     teardown();
407 };
408
409 subtest 'Get shelves containing biblios' => sub {
410
411     plan tests => 9;
412     my $patron1 = $builder->build( { source => 'Borrower', } );
413     my $patron2 = $builder->build( { source => 'Borrower', } );
414     my $biblio1 = $builder->build_sample_biblio;
415     my $biblio2 = $builder->build_sample_biblio;
416     my $biblio3 = $builder->build_sample_biblio;
417     my $biblio4 = $builder->build_sample_biblio;
418
419     my $shelf1 = Koha::Virtualshelf->new(
420         {   shelfname    => "my first shelf",
421             owner        => $patron1->{borrowernumber},
422             category     => 1,
423         }
424     )->store;
425     my $shelf2 = Koha::Virtualshelf->new(
426         {   shelfname    => "my x second shelf", # 'x' to make it sorted after 'third'
427             owner        => $patron2->{borrowernumber},
428             category     => 1,
429         }
430     )->store;
431     my $shelf3 = Koha::Virtualshelf->new(
432         {   shelfname    => "my third shelf",
433             owner        => $patron1->{borrowernumber},
434             category     => 2,
435         }
436     )->store;
437
438     my $content1 = $shelf1->add_biblio( $biblio1->biblionumber, $patron1->{borrowernumber} );
439     my $content2 = $shelf1->add_biblio( $biblio2->biblionumber, $patron1->{borrowernumber} );
440     my $content3 = $shelf2->add_biblio( $biblio2->biblionumber, $patron2->{borrowernumber} );
441     my $content4 = $shelf2->add_biblio( $biblio3->biblionumber, $patron2->{borrowernumber} );
442     my $content5 = $shelf2->add_biblio( $biblio4->biblionumber, $patron2->{borrowernumber} );
443     my $content6 = $shelf3->add_biblio( $biblio4->biblionumber, $patron1->{borrowernumber} );
444
445     my $shelves_with_biblio1_for_any_patrons = Koha::Virtualshelves->get_shelves_containing_record(
446         {
447             biblionumber => $biblio1->biblionumber,
448         }
449     );
450     is ( $shelves_with_biblio1_for_any_patrons->count, 0, 'shelf1 is private and should not be displayed if patron is not logged in' );
451
452     my $shelves_with_biblio4_for_any_patrons = Koha::Virtualshelves->get_shelves_containing_record(
453         {
454             biblionumber => $biblio4->biblionumber,
455         }
456     );
457     is ( $shelves_with_biblio4_for_any_patrons->count, 1, 'shelf3 is public and should be displayed for any patrons' );
458     is ( $shelves_with_biblio4_for_any_patrons->next->shelfname, $shelf3->shelfname, 'The correct shelf (3) should be displayed' );
459
460     my $shelves_with_biblio1_for_other_patrons = Koha::Virtualshelves->get_shelves_containing_record(
461         {
462             biblionumber => $biblio1->biblionumber,
463             borrowernumber => $patron2->{borrowernumber},
464         }
465     );
466     is ( $shelves_with_biblio1_for_other_patrons->count, 0, 'shelf1 is private and should not be displayed for other patrons' );
467
468     my $shelves_with_biblio1_for_owner = Koha::Virtualshelves->get_shelves_containing_record(
469         {
470             biblionumber => $biblio1->biblionumber,
471             borrowernumber => $patron1->{borrowernumber},
472         }
473     );
474     is ( $shelves_with_biblio1_for_owner->count, 1, 'shelf1 is private and should be displayed for the owner' );
475
476     my $shelves_with_biblio2_for_patron1 = Koha::Virtualshelves->get_shelves_containing_record(
477         {
478             biblionumber => $biblio2->biblionumber,
479             borrowernumber => $patron1->{borrowernumber},
480         }
481     );
482     is ( $shelves_with_biblio2_for_patron1->count, 1, 'Only shelf1 should be displayed for patron 1 and biblio 1' );
483     is ( $shelves_with_biblio2_for_patron1->next->shelfname, $shelf1->shelfname, 'The correct shelf (1) should be displayed for patron 1' );
484
485     my $shelves_with_biblio4_for_patron2 = Koha::Virtualshelves->get_shelves_containing_record(
486         {
487             biblionumber => $biblio4->biblionumber,
488             borrowernumber => $patron2->{borrowernumber},
489         }
490     );
491     is ( $shelves_with_biblio4_for_patron2->count, 2, 'Patron should shown private and public lists for a given biblio' );
492     is ( $shelves_with_biblio4_for_patron2->next->shelfname, $shelf3->shelfname, 'The shelves should be sorted by shelfname' );
493
494     teardown();
495 };
496
497 sub teardown {
498     $dbh->do(q|DELETE FROM virtualshelfshares|);
499     $dbh->do(q|DELETE FROM virtualshelfcontents|);
500     $dbh->do(q|DELETE FROM virtualshelves|);
501 }