Bug 21597: Data too long for column 'invitekey'
[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 => 21;
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     # Test double accept (BZ 11943) before removing the accepted share
156     my $third_share = $shelf_to_share->share('valid key3');
157     is( Koha::Virtualshelfshares->search->count, 3, 'Three shares' );
158     $is_accepted = $third_share->accept( 'valid key3', $share_with_me->{borrowernumber} );
159     is( $is_accepted->shelfnumber, $shelf_to_share->shelfnumber, 'Accept returned the existing share' );
160     is( Koha::Virtualshelfshares->search->count, 2, 'Check that number of shares went down again' );
161
162     # Remove the first accept
163     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' );
164     $number_of_shelves_shared = Koha::Virtualshelfshares->search->count;
165     is( $number_of_shelves_shared, 1, 'To be sure the share has been removed' );
166
167     teardown();
168 };
169
170 subtest 'Shelf content' => sub {
171
172     plan tests => 18;
173     my $patron1 = $builder->build( { source => 'Borrower', } );
174     my $patron2 = $builder->build( { source => 'Borrower', } );
175     my $biblio1 = $builder->build( { source => 'Biblio', } );
176     my $biblio2 = $builder->build( { source => 'Biblio', } );
177     my $biblio3 = $builder->build( { source => 'Biblio', } );
178     my $biblio4 = $builder->build( { source => 'Biblio', } );
179     my $number_of_contents = Koha::Virtualshelfcontents->search->count;
180
181     is( $number_of_contents, 0, 'No content should exist' );
182
183     my $dt_yesterday = dt_from_string->subtract_duration( DateTime::Duration->new( days => 1 ) );
184     my $shelf = Koha::Virtualshelf->new(
185         {   shelfname    => "my first shelf",
186             owner        => $patron1->{borrowernumber},
187             category     => 1,
188             lastmodified => $dt_yesterday,
189         }
190     )->store;
191
192     $shelf = Koha::Virtualshelves->find( $shelf->shelfnumber );
193     is( t::lib::Dates::compare( $shelf->lastmodified, $dt_yesterday), 0, 'The lastmodified has been set to yesterday, will be useful for another test later' );
194     my $content1 = $shelf->add_biblio( $biblio1->{biblionumber}, $patron1->{borrowernumber} );
195     is( ref($content1), 'Koha::Virtualshelfcontent', 'add_biblio to a shelf should return a Koha::Virtualshelfcontent object if inserted' );
196     $shelf = Koha::Virtualshelves->find( $shelf->shelfnumber );
197     is( t::lib::Dates::compare( $shelf->lastmodified, dt_from_string), 0, 'Adding a biblio to a shelf should update the lastmodified for the shelf' );
198     my $content2 = $shelf->add_biblio( $biblio2->{biblionumber}, $patron1->{borrowernumber} );
199     $number_of_contents = Koha::Virtualshelfcontents->search->count;
200     is( $number_of_contents, 2, '2 biblio should have been inserted' );
201
202     my $content1_bis = $shelf->add_biblio( $biblio1->{biblionumber}, $patron1->{borrowernumber} );
203     is( $content1_bis, undef, 'add_biblio should return undef on duplicate' );    # Or an exception ?
204     $number_of_contents = Koha::Virtualshelfcontents->search->count;
205     is( $number_of_contents, 2, 'The biblio should not have been duplicated' );
206
207     $shelf = Koha::Virtualshelves->find( $shelf->shelfnumber );
208     my $contents = $shelf->get_contents;
209     is( $contents->count, 2, 'There are 2 biblios on this shelf' );
210
211     # Patron 2 will try to remove biblios
212     # allow_change_from_owner = 1, allow_change_from_others = 0 (defaults)
213     my $number_of_deleted_biblios = $shelf->remove_biblios( { biblionumbers => [ $biblio1->{biblionumber} ], borrowernumber => $patron2->{borrowernumber} } );
214     is( $number_of_deleted_biblios, 0, 'Patron 2 removed nothing' );
215     # Now try with patron 1
216     $number_of_deleted_biblios = $shelf->remove_biblios( { biblionumbers => [ $biblio1->{biblionumber} ], borrowernumber => $patron1->{borrowernumber} } );
217     is( $number_of_deleted_biblios, 1, 'Patron 1 removed biblio' );
218     $number_of_contents = Koha::Virtualshelfcontents->search->count;
219     is( $number_of_contents, 1, 'To be sure the content has been deleted' );
220
221     # allow_change_from_owner == 0 (readonly)
222     $shelf->allow_change_from_owner( 0 );
223     $shelf->store;
224     $number_of_deleted_biblios = $shelf->remove_biblios( { biblionumbers => [ $biblio2->{biblionumber} ], borrowernumber => $patron1->{borrowernumber} } );
225     is( $number_of_deleted_biblios, 0, 'Owner could not delete' );
226     $number_of_contents = Koha::Virtualshelfcontents->search->count;
227     is( $number_of_contents, 1, 'Number of entries still equal to 1' );
228     $shelf->add_biblio( $biblio2->{biblionumber}, $patron1->{borrowernumber} );
229     $number_of_contents = Koha::Virtualshelfcontents->search->count;
230     is( $number_of_contents, 1, 'Biblio not added to the list' );
231     # Add back biblio1
232     $shelf->allow_change_from_owner( 1 );
233     $shelf->add_biblio( $biblio1->{biblionumber}, $patron1->{borrowernumber} );
234     $number_of_contents = Koha::Virtualshelfcontents->search->count;
235     is( $number_of_contents, 2, 'Biblio added to the list' );
236
237     # allow_change_from_others == 1
238     $shelf->allow_change_from_others( 1 );
239     my $content3 = $shelf->add_biblio( $biblio3->{biblionumber}, $patron2->{borrowernumber} );
240     my $content4 = $shelf->add_biblio( $biblio4->{biblionumber}, $patron2->{borrowernumber} );
241     $number_of_contents = Koha::Virtualshelfcontents->search->count;
242     is( $number_of_contents, 4, 'The biblio should have been added to the shelf by the patron 2' );
243     $number_of_deleted_biblios = $shelf->remove_biblios( { biblionumbers => [ $biblio3->{biblionumber} ], borrowernumber => $patron2->{borrowernumber} } );
244     is( $number_of_deleted_biblios, 1, 'Biblio 3 deleted by patron 2' );
245     $number_of_contents = Koha::Virtualshelfcontents->search->count;
246     is( $number_of_contents, 3, 'Back to three entries' );
247
248     teardown();
249 };
250
251 subtest 'Shelf permissions' => sub {
252
253     plan tests => 40;
254     my $patron1 = $builder->build( { source => 'Borrower', value => { flags => '2096766' } } ); # 2096766 is everything checked but not superlibrarian
255     my $patron2 = $builder->build( { source => 'Borrower', value => { flags => '1048190' } } ); # 1048190 is everything checked but not superlibrarian and delete_public_lists
256     my $biblio1 = $builder->build( { source => 'Biblio', } );
257     my $biblio2 = $builder->build( { source => 'Biblio', } );
258     my $biblio3 = $builder->build( { source => 'Biblio', } );
259     my $biblio4 = $builder->build( { source => 'Biblio', } );
260
261
262     my $public_shelf = Koha::Virtualshelf->new(
263         {   shelfname    => "my first shelf",
264             owner        => $patron1->{borrowernumber},
265             category     => 2,
266             allow_change_from_owner => 0,
267             allow_change_from_others => 0,
268         }
269     )->store;
270
271     is( $public_shelf->can_be_viewed( $patron1->{borrowernumber} ), 1, 'The owner should be able to view his public list' );
272     is( $public_shelf->can_be_viewed( $patron2->{borrowernumber} ), 1, 'Public list should be viewed by someone else' );
273
274     is( $public_shelf->can_be_deleted( $patron1->{borrowernumber} ), 1, 'The owner should be able to delete his list' );
275     is( $public_shelf->can_be_deleted( $patron2->{borrowernumber} ), 0, 'Public list should not be deleted by someone else' );
276
277     is( $public_shelf->can_be_managed( $patron1->{borrowernumber} ), 1, 'The owner should be able to manage his list' );
278     is( $public_shelf->can_be_managed( $patron2->{borrowernumber} ), 0, 'Public list should not be managed by someone else' );
279
280     is( $public_shelf->can_biblios_be_added( $patron1->{borrowernumber} ), 0, 'The owner should not be able to add biblios to their list' );
281     is( $public_shelf->can_biblios_be_added( $patron2->{borrowernumber} ), 0, 'Public list should not be modified (add) by someone else' );
282
283     is( $public_shelf->can_biblios_be_removed( $patron1->{borrowernumber} ), 0, 'The owner should not be able to remove biblios to their list' );
284     is( $public_shelf->can_biblios_be_removed( $patron2->{borrowernumber} ), 0, 'Public list should not be modified (remove) by someone else' );
285
286
287     $public_shelf->allow_change_from_owner(1);
288     $public_shelf->store;
289
290     is( $public_shelf->can_be_viewed( $patron1->{borrowernumber} ), 1, 'The owner should be able to view his public list' );
291     is( $public_shelf->can_be_viewed( $patron2->{borrowernumber} ), 1, 'Public list should be viewed by someone else' );
292
293     is( $public_shelf->can_be_deleted( $patron1->{borrowernumber} ), 1, 'The owner should be able to delete his list' );
294     is( $public_shelf->can_be_deleted( $patron2->{borrowernumber} ), 0, 'Public list should not be deleted by someone else' );
295
296     is( $public_shelf->can_be_managed( $patron1->{borrowernumber} ), 1, 'The owner should be able to manage his list' );
297     is( $public_shelf->can_be_managed( $patron2->{borrowernumber} ), 0, 'Public list should not be managed by someone else' );
298
299     is( $public_shelf->can_biblios_be_added( $patron1->{borrowernumber} ), 1, 'The owner should be able to add biblios to his list' );
300     is( $public_shelf->can_biblios_be_added( $patron2->{borrowernumber} ), 0, 'Public list should not be modified (add) by someone else' );
301
302     is( $public_shelf->can_biblios_be_removed( $patron1->{borrowernumber} ), 1, 'The owner should be able to remove biblios to his list' );
303     is( $public_shelf->can_biblios_be_removed( $patron2->{borrowernumber} ), 0, 'Public list should not be modified (remove) by someone else' );
304
305
306     my $private_shelf = Koha::Virtualshelf->new(
307         {   shelfname    => "my first shelf",
308             owner        => $patron1->{borrowernumber},
309             category     => 1,
310             allow_change_from_owner => 0,
311             allow_change_from_others => 0,
312         }
313     )->store;
314
315     is( $private_shelf->can_be_viewed( $patron1->{borrowernumber} ), 1, 'The owner should be able to view his list' );
316     is( $private_shelf->can_be_viewed( $patron2->{borrowernumber} ), 0, 'Private list should not be viewed by someone else' );
317
318     is( $private_shelf->can_be_deleted( $patron1->{borrowernumber} ), 1, 'The owner should be able to delete his list' );
319     is( $private_shelf->can_be_deleted( $patron2->{borrowernumber} ), 0, 'Private list should not be deleted by someone else' );
320
321     is( $private_shelf->can_be_managed( $patron1->{borrowernumber} ), 1, 'The owner should be able to manage his list' );
322     is( $private_shelf->can_be_managed( $patron2->{borrowernumber} ), 0, 'Private list should not be managed by someone else' );
323
324     is( $private_shelf->can_biblios_be_added( $patron1->{borrowernumber} ), 0, 'The owner should not be able to add biblios to their list' );
325     is( $private_shelf->can_biblios_be_added( $patron2->{borrowernumber} ), 0, 'Private list should not be modified (add) by someone else' );
326
327     is( $private_shelf->can_biblios_be_removed( $patron1->{borrowernumber} ), 0, 'The owner should not be able to remove biblios to their list' );
328     is( $private_shelf->can_biblios_be_removed( $patron2->{borrowernumber} ), 0, 'Private list should not be modified (remove) by someone else' );
329
330
331     $private_shelf->allow_change_from_owner(1);
332     $private_shelf->allow_change_from_others(1);
333     $private_shelf->store;
334
335     is( $private_shelf->can_be_viewed( $patron1->{borrowernumber} ), 1, 'The owner should be able to view his list' );
336     is( $private_shelf->can_be_viewed( $patron2->{borrowernumber} ), 0, 'Private list should not be viewed by someone else' );
337
338     is( $private_shelf->can_be_deleted( $patron1->{borrowernumber} ), 1, 'The owner should be able to delete his list' );
339     is( $private_shelf->can_be_deleted( $patron2->{borrowernumber} ), 0, 'Private list should not be deleted by someone else' );
340
341     is( $private_shelf->can_be_managed( $patron1->{borrowernumber} ), 1, 'The owner should be able to manage his list' );
342     is( $private_shelf->can_be_managed( $patron2->{borrowernumber} ), 0, 'Private list should not be managed by someone else' );
343
344     is( $private_shelf->can_biblios_be_added( $patron1->{borrowernumber} ), 1, 'The owner should be able to add biblios to his list' );
345     is( $private_shelf->can_biblios_be_added( $patron2->{borrowernumber} ), 1, 'Private list could be modified (add) by someone else # individual check done later' );
346
347     is( $private_shelf->can_biblios_be_removed( $patron1->{borrowernumber} ), 1, 'The owner should be able to remove biblios to his list' );
348     is( $private_shelf->can_biblios_be_removed( $patron2->{borrowernumber} ), 1, 'Private list could be modified (remove) by someone else # individual check done later' );
349
350     teardown();
351 };
352
353 subtest 'Get shelves' => sub {
354     plan tests => 4;
355     my $patron1 = $builder->build({
356         source => 'Borrower',
357     });
358     my $patron2 = $builder->build({
359         source => 'Borrower',
360     });
361
362     my $private_shelf1_1 = Koha::Virtualshelf->new({
363             shelfname => "private shelf 1 for patron 1",
364             owner => $patron1->{borrowernumber},
365             category => 1,
366         }
367     )->store;
368     my $private_shelf1_2 = Koha::Virtualshelf->new({
369             shelfname => "private shelf 2 for patron 1",
370             owner => $patron1->{borrowernumber},
371             category => 1,
372         }
373     )->store;
374     my $private_shelf2_1 = Koha::Virtualshelf->new({
375             shelfname => "private shelf 1 for patron 2",
376             owner => $patron2->{borrowernumber},
377             category => 1,
378         }
379     )->store;
380     my $public_shelf1_1 = Koha::Virtualshelf->new({
381             shelfname => "public shelf 1 for patron 1",
382             owner => $patron1->{borrowernumber},
383             category => 2,
384         }
385     )->store;
386     my $public_shelf1_2 = Koha::Virtualshelf->new({
387             shelfname => "public shelf 2 for patron 1",
388             owner => $patron1->{borrowernumber},
389             category => 2,
390         }
391     )->store;
392
393     my $private_shelves = Koha::Virtualshelves->get_private_shelves;
394     is( $private_shelves->count, 0, 'Without borrowernumber given, get_private_shelves should not return any shelf' );
395     $private_shelves = Koha::Virtualshelves->get_private_shelves({ borrowernumber => $patron1->{borrowernumber} });
396     is( $private_shelves->count, 2, 'get_private_shelves should return all shelves for a given patron' );
397
398     $private_shelf2_1->share('a key')->accept('a key', $patron1->{borrowernumber});
399     $private_shelves = Koha::Virtualshelves->get_private_shelves({ borrowernumber => $patron1->{borrowernumber} });
400     is( $private_shelves->count, 3, 'get_private_shelves should return all shelves for a given patron, even the shared ones' );
401
402     my $public_shelves = Koha::Virtualshelves->get_public_shelves;
403     is( $public_shelves->count, 2, 'get_public_shelves should return all public shelves, no matter who is the owner' );
404
405     teardown();
406 };
407
408 subtest 'Get shelves containing biblios' => sub {
409
410     plan tests => 9;
411     my $patron1 = $builder->build( { source => 'Borrower', } );
412     my $patron2 = $builder->build( { source => 'Borrower', } );
413     my $biblio1 = $builder->build( { source => 'Biblio', } );
414     my $biblio2 = $builder->build( { source => 'Biblio', } );
415     my $biblio3 = $builder->build( { source => 'Biblio', } );
416     my $biblio4 = $builder->build( { source => 'Biblio', } );
417
418     my $shelf1 = Koha::Virtualshelf->new(
419         {   shelfname    => "my first shelf",
420             owner        => $patron1->{borrowernumber},
421             category     => 1,
422         }
423     )->store;
424     my $shelf2 = Koha::Virtualshelf->new(
425         {   shelfname    => "my x second shelf", # 'x' to make it sorted after 'third'
426             owner        => $patron2->{borrowernumber},
427             category     => 1,
428         }
429     )->store;
430     my $shelf3 = Koha::Virtualshelf->new(
431         {   shelfname    => "my third shelf",
432             owner        => $patron1->{borrowernumber},
433             category     => 2,
434         }
435     )->store;
436
437     my $content1 = $shelf1->add_biblio( $biblio1->{biblionumber}, $patron1->{borrowernumber} );
438     my $content2 = $shelf1->add_biblio( $biblio2->{biblionumber}, $patron1->{borrowernumber} );
439     my $content3 = $shelf2->add_biblio( $biblio2->{biblionumber}, $patron2->{borrowernumber} );
440     my $content4 = $shelf2->add_biblio( $biblio3->{biblionumber}, $patron2->{borrowernumber} );
441     my $content5 = $shelf2->add_biblio( $biblio4->{biblionumber}, $patron2->{borrowernumber} );
442     my $content6 = $shelf3->add_biblio( $biblio4->{biblionumber}, $patron1->{borrowernumber} );
443
444     my $shelves_with_biblio1_for_any_patrons = Koha::Virtualshelves->get_shelves_containing_record(
445         {
446             biblionumber => $biblio1->{biblionumber},
447         }
448     );
449     is ( $shelves_with_biblio1_for_any_patrons->count, 0, 'shelf1 is private and should not be displayed if patron is not logged in' );
450
451     my $shelves_with_biblio4_for_any_patrons = Koha::Virtualshelves->get_shelves_containing_record(
452         {
453             biblionumber => $biblio4->{biblionumber},
454         }
455     );
456     is ( $shelves_with_biblio4_for_any_patrons->count, 1, 'shelf3 is public and should be displayed for any patrons' );
457     is ( $shelves_with_biblio4_for_any_patrons->next->shelfname, $shelf3->shelfname, 'The correct shelf (3) should be displayed' );
458
459     my $shelves_with_biblio1_for_other_patrons = Koha::Virtualshelves->get_shelves_containing_record(
460         {
461             biblionumber => $biblio1->{biblionumber},
462             borrowernumber => $patron2->{borrowernumber},
463         }
464     );
465     is ( $shelves_with_biblio1_for_other_patrons->count, 0, 'shelf1 is private and should not be displayed for other patrons' );
466
467     my $shelves_with_biblio1_for_owner = Koha::Virtualshelves->get_shelves_containing_record(
468         {
469             biblionumber => $biblio1->{biblionumber},
470             borrowernumber => $patron1->{borrowernumber},
471         }
472     );
473     is ( $shelves_with_biblio1_for_owner->count, 1, 'shelf1 is private and should be displayed for the owner' );
474
475     my $shelves_with_biblio2_for_patron1 = Koha::Virtualshelves->get_shelves_containing_record(
476         {
477             biblionumber => $biblio2->{biblionumber},
478             borrowernumber => $patron1->{borrowernumber},
479         }
480     );
481     is ( $shelves_with_biblio2_for_patron1->count, 1, 'Only shelf1 should be displayed for patron 1 and biblio 1' );
482     is ( $shelves_with_biblio2_for_patron1->next->shelfname, $shelf1->shelfname, 'The correct shelf (1) should be displayed for patron 1' );
483
484     my $shelves_with_biblio4_for_patron2 = Koha::Virtualshelves->get_shelves_containing_record(
485         {
486             biblionumber => $biblio4->{biblionumber},
487             borrowernumber => $patron2->{borrowernumber},
488         }
489     );
490     is ( $shelves_with_biblio4_for_patron2->count, 2, 'Patron should shown private and public lists for a given biblio' );
491     is ( $shelves_with_biblio4_for_patron2->next->shelfname, $shelf3->shelfname, 'The shelves should be sorted by shelfname' );
492
493     teardown();
494 };
495
496 sub teardown {
497     $dbh->do(q|DELETE FROM virtualshelfshares|);
498     $dbh->do(q|DELETE FROM virtualshelfcontents|);
499     $dbh->do(q|DELETE FROM virtualshelves|);
500 }