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