Bug 14334: Remove AutoCommit from tests
[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     is( $is_deleted, 1, '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     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' );
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( { source => 'Biblio', } );
178     my $biblio2 = $builder->build( { source => 'Biblio', } );
179     my $biblio3 = $builder->build( { source => 'Biblio', } );
180     my $biblio4 = $builder->build( { source => '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( { source => 'Biblio', } );
259     my $biblio2 = $builder->build( { source => 'Biblio', } );
260     my $biblio3 = $builder->build( { source => 'Biblio', } );
261     my $biblio4 = $builder->build( { source => 'Biblio', } );
262
263
264     my $public_shelf = Koha::Virtualshelf->new(
265         {   shelfname    => "my first shelf",
266             owner        => $patron1->{borrowernumber},
267             category     => 2,
268             allow_change_from_owner => 0,
269             allow_change_from_others => 0,
270         }
271     )->store;
272
273     is( $public_shelf->can_be_viewed( $patron1->{borrowernumber} ), 1, 'The owner should be able to view his public list' );
274     is( $public_shelf->can_be_viewed( $patron2->{borrowernumber} ), 1, 'Public list should be viewed by someone else' );
275
276     is( $public_shelf->can_be_deleted( $patron1->{borrowernumber} ), 1, 'The owner should be able to delete his list' );
277     is( $public_shelf->can_be_deleted( $patron2->{borrowernumber} ), 0, 'Public list should not be deleted by someone else' );
278
279     is( $public_shelf->can_be_managed( $patron1->{borrowernumber} ), 1, 'The owner should be able to manage his list' );
280     is( $public_shelf->can_be_managed( $patron2->{borrowernumber} ), 0, 'Public list should not be managed by someone else' );
281
282     is( $public_shelf->can_biblios_be_added( $patron1->{borrowernumber} ), 0, 'The owner should not be able to add biblios to their list' );
283     is( $public_shelf->can_biblios_be_added( $patron2->{borrowernumber} ), 0, 'Public list should not be modified (add) by someone else' );
284
285     is( $public_shelf->can_biblios_be_removed( $patron1->{borrowernumber} ), 0, 'The owner should not be able to remove biblios to their list' );
286     is( $public_shelf->can_biblios_be_removed( $patron2->{borrowernumber} ), 0, 'Public list should not be modified (remove) by someone else' );
287
288
289     $public_shelf->allow_change_from_owner(1);
290     $public_shelf->store;
291
292     is( $public_shelf->can_be_viewed( $patron1->{borrowernumber} ), 1, 'The owner should be able to view his public list' );
293     is( $public_shelf->can_be_viewed( $patron2->{borrowernumber} ), 1, 'Public list should be viewed by someone else' );
294
295     is( $public_shelf->can_be_deleted( $patron1->{borrowernumber} ), 1, 'The owner should be able to delete his list' );
296     is( $public_shelf->can_be_deleted( $patron2->{borrowernumber} ), 0, 'Public list should not be deleted by someone else' );
297
298     is( $public_shelf->can_be_managed( $patron1->{borrowernumber} ), 1, 'The owner should be able to manage his list' );
299     is( $public_shelf->can_be_managed( $patron2->{borrowernumber} ), 0, 'Public list should not be managed by someone else' );
300
301     is( $public_shelf->can_biblios_be_added( $patron1->{borrowernumber} ), 1, 'The owner should be able to add biblios to his list' );
302     is( $public_shelf->can_biblios_be_added( $patron2->{borrowernumber} ), 0, 'Public list should not be modified (add) by someone else' );
303
304     is( $public_shelf->can_biblios_be_removed( $patron1->{borrowernumber} ), 1, 'The owner should be able to remove biblios to his list' );
305     is( $public_shelf->can_biblios_be_removed( $patron2->{borrowernumber} ), 0, 'Public list should not be modified (remove) by someone else' );
306
307
308     my $private_shelf = Koha::Virtualshelf->new(
309         {   shelfname    => "my first shelf",
310             owner        => $patron1->{borrowernumber},
311             category     => 1,
312             allow_change_from_owner => 0,
313             allow_change_from_others => 0,
314         }
315     )->store;
316
317     is( $private_shelf->can_be_viewed( $patron1->{borrowernumber} ), 1, 'The owner should be able to view his list' );
318     is( $private_shelf->can_be_viewed( $patron2->{borrowernumber} ), 0, 'Private list should not be viewed by someone else' );
319
320     is( $private_shelf->can_be_deleted( $patron1->{borrowernumber} ), 1, 'The owner should be able to delete his list' );
321     is( $private_shelf->can_be_deleted( $patron2->{borrowernumber} ), 0, 'Private list should not be deleted by someone else' );
322
323     is( $private_shelf->can_be_managed( $patron1->{borrowernumber} ), 1, 'The owner should be able to manage his list' );
324     is( $private_shelf->can_be_managed( $patron2->{borrowernumber} ), 0, 'Private list should not be managed by someone else' );
325
326     is( $private_shelf->can_biblios_be_added( $patron1->{borrowernumber} ), 0, 'The owner should not be able to add biblios to their list' );
327     is( $private_shelf->can_biblios_be_added( $patron2->{borrowernumber} ), 0, 'Private list should not be modified (add) by someone else' );
328
329     is( $private_shelf->can_biblios_be_removed( $patron1->{borrowernumber} ), 0, 'The owner should not be able to remove biblios to their list' );
330     is( $private_shelf->can_biblios_be_removed( $patron2->{borrowernumber} ), 0, 'Private list should not be modified (remove) by someone else' );
331
332
333     $private_shelf->allow_change_from_owner(1);
334     $private_shelf->allow_change_from_others(1);
335     $private_shelf->store;
336
337     is( $private_shelf->can_be_viewed( $patron1->{borrowernumber} ), 1, 'The owner should be able to view his list' );
338     is( $private_shelf->can_be_viewed( $patron2->{borrowernumber} ), 0, 'Private list should not be viewed by someone else' );
339
340     is( $private_shelf->can_be_deleted( $patron1->{borrowernumber} ), 1, 'The owner should be able to delete his list' );
341     is( $private_shelf->can_be_deleted( $patron2->{borrowernumber} ), 0, 'Private list should not be deleted by someone else' );
342
343     is( $private_shelf->can_be_managed( $patron1->{borrowernumber} ), 1, 'The owner should be able to manage his list' );
344     is( $private_shelf->can_be_managed( $patron2->{borrowernumber} ), 0, 'Private list should not be managed by someone else' );
345
346     is( $private_shelf->can_biblios_be_added( $patron1->{borrowernumber} ), 1, 'The owner should be able to add biblios to his list' );
347     is( $private_shelf->can_biblios_be_added( $patron2->{borrowernumber} ), 1, 'Private list could be modified (add) by someone else # individual check done later' );
348
349     is( $private_shelf->can_biblios_be_removed( $patron1->{borrowernumber} ), 1, 'The owner should be able to remove biblios to his list' );
350     is( $private_shelf->can_biblios_be_removed( $patron2->{borrowernumber} ), 1, 'Private list could be modified (remove) by someone else # individual check done later' );
351
352     teardown();
353 };
354
355 subtest 'Get shelves' => sub {
356     plan tests => 4;
357     my $patron1 = $builder->build({
358         source => 'Borrower',
359     });
360     my $patron2 = $builder->build({
361         source => 'Borrower',
362     });
363
364     my $private_shelf1_1 = Koha::Virtualshelf->new({
365             shelfname => "private shelf 1 for patron 1",
366             owner => $patron1->{borrowernumber},
367             category => 1,
368         }
369     )->store;
370     my $private_shelf1_2 = Koha::Virtualshelf->new({
371             shelfname => "private shelf 2 for patron 1",
372             owner => $patron1->{borrowernumber},
373             category => 1,
374         }
375     )->store;
376     my $private_shelf2_1 = Koha::Virtualshelf->new({
377             shelfname => "private shelf 1 for patron 2",
378             owner => $patron2->{borrowernumber},
379             category => 1,
380         }
381     )->store;
382     my $public_shelf1_1 = Koha::Virtualshelf->new({
383             shelfname => "public shelf 1 for patron 1",
384             owner => $patron1->{borrowernumber},
385             category => 2,
386         }
387     )->store;
388     my $public_shelf1_2 = Koha::Virtualshelf->new({
389             shelfname => "public shelf 2 for patron 1",
390             owner => $patron1->{borrowernumber},
391             category => 2,
392         }
393     )->store;
394
395     my $private_shelves = Koha::Virtualshelves->get_private_shelves;
396     is( $private_shelves->count, 0, 'Without borrowernumber given, get_private_shelves should not return any shelf' );
397     $private_shelves = Koha::Virtualshelves->get_private_shelves({ borrowernumber => $patron1->{borrowernumber} });
398     is( $private_shelves->count, 2, 'get_private_shelves should return all shelves for a given patron' );
399
400     $private_shelf2_1->share('a key')->accept('a key', $patron1->{borrowernumber});
401     $private_shelves = Koha::Virtualshelves->get_private_shelves({ borrowernumber => $patron1->{borrowernumber} });
402     is( $private_shelves->count, 3, 'get_private_shelves should return all shelves for a given patron, even the shared ones' );
403
404     my $public_shelves = Koha::Virtualshelves->get_public_shelves;
405     is( $public_shelves->count, 2, 'get_public_shelves should return all public shelves, no matter who is the owner' );
406
407     teardown();
408 };
409
410 subtest 'Get shelves containing biblios' => sub {
411
412     plan tests => 9;
413     my $patron1 = $builder->build( { source => 'Borrower', } );
414     my $patron2 = $builder->build( { source => 'Borrower', } );
415     my $biblio1 = $builder->build( { source => 'Biblio', } );
416     my $biblio2 = $builder->build( { source => 'Biblio', } );
417     my $biblio3 = $builder->build( { source => 'Biblio', } );
418     my $biblio4 = $builder->build( { source => 'Biblio', } );
419
420     my $shelf1 = Koha::Virtualshelf->new(
421         {   shelfname    => "my first shelf",
422             owner        => $patron1->{borrowernumber},
423             category     => 1,
424         }
425     )->store;
426     my $shelf2 = Koha::Virtualshelf->new(
427         {   shelfname    => "my x second shelf", # 'x' to make it sorted after 'third'
428             owner        => $patron2->{borrowernumber},
429             category     => 1,
430         }
431     )->store;
432     my $shelf3 = Koha::Virtualshelf->new(
433         {   shelfname    => "my third shelf",
434             owner        => $patron1->{borrowernumber},
435             category     => 2,
436         }
437     )->store;
438
439     my $content1 = $shelf1->add_biblio( $biblio1->{biblionumber}, $patron1->{borrowernumber} );
440     my $content2 = $shelf1->add_biblio( $biblio2->{biblionumber}, $patron1->{borrowernumber} );
441     my $content3 = $shelf2->add_biblio( $biblio2->{biblionumber}, $patron2->{borrowernumber} );
442     my $content4 = $shelf2->add_biblio( $biblio3->{biblionumber}, $patron2->{borrowernumber} );
443     my $content5 = $shelf2->add_biblio( $biblio4->{biblionumber}, $patron2->{borrowernumber} );
444     my $content6 = $shelf3->add_biblio( $biblio4->{biblionumber}, $patron1->{borrowernumber} );
445
446     my $shelves_with_biblio1_for_any_patrons = Koha::Virtualshelves->get_shelves_containing_record(
447         {
448             biblionumber => $biblio1->{biblionumber},
449         }
450     );
451     is ( $shelves_with_biblio1_for_any_patrons->count, 0, 'shelf1 is private and should not be displayed if patron is not logged in' );
452
453     my $shelves_with_biblio4_for_any_patrons = Koha::Virtualshelves->get_shelves_containing_record(
454         {
455             biblionumber => $biblio4->{biblionumber},
456         }
457     );
458     is ( $shelves_with_biblio4_for_any_patrons->count, 1, 'shelf3 is public and should be displayed for any patrons' );
459     is ( $shelves_with_biblio4_for_any_patrons->next->shelfname, $shelf3->shelfname, 'The correct shelf (3) should be displayed' );
460
461     my $shelves_with_biblio1_for_other_patrons = Koha::Virtualshelves->get_shelves_containing_record(
462         {
463             biblionumber => $biblio1->{biblionumber},
464             borrowernumber => $patron2->{borrowernumber},
465         }
466     );
467     is ( $shelves_with_biblio1_for_other_patrons->count, 0, 'shelf1 is private and should not be displayed for other patrons' );
468
469     my $shelves_with_biblio1_for_owner = Koha::Virtualshelves->get_shelves_containing_record(
470         {
471             biblionumber => $biblio1->{biblionumber},
472             borrowernumber => $patron1->{borrowernumber},
473         }
474     );
475     is ( $shelves_with_biblio1_for_owner->count, 1, 'shelf1 is private and should be displayed for the owner' );
476
477     my $shelves_with_biblio2_for_patron1 = Koha::Virtualshelves->get_shelves_containing_record(
478         {
479             biblionumber => $biblio2->{biblionumber},
480             borrowernumber => $patron1->{borrowernumber},
481         }
482     );
483     is ( $shelves_with_biblio2_for_patron1->count, 1, 'Only shelf1 should be displayed for patron 1 and biblio 1' );
484     is ( $shelves_with_biblio2_for_patron1->next->shelfname, $shelf1->shelfname, 'The correct shelf (1) should be displayed for patron 1' );
485
486     my $shelves_with_biblio4_for_patron2 = Koha::Virtualshelves->get_shelves_containing_record(
487         {
488             biblionumber => $biblio4->{biblionumber},
489             borrowernumber => $patron2->{borrowernumber},
490         }
491     );
492     is ( $shelves_with_biblio4_for_patron2->count, 2, 'Patron should shown private and public lists for a given biblio' );
493     is ( $shelves_with_biblio4_for_patron2->next->shelfname, $shelf3->shelfname, 'The shelves should be sorted by shelfname' );
494
495     teardown();
496 };
497
498 sub teardown {
499     $dbh->do(q|DELETE FROM virtualshelfshares|);
500     $dbh->do(q|DELETE FROM virtualshelfcontents|);
501     $dbh->do(q|DELETE FROM virtualshelves|);
502 }