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