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