4 use Test::More tests => 6;
5 use DateTime::Duration;
9 use Koha::Virtualshelves;
10 use Koha::Virtualshelfshares;
11 use Koha::Virtualshelfcontents;
13 use t::lib::TestBuilder;
15 my $builder = t::lib::TestBuilder->new;
17 my $dbh = C4::Context->dbh;
18 $dbh->{AutoCommit} = 0;
21 subtest 'CRUD' => sub {
23 my $patron = $builder->build({
27 my $number_of_shelves = Koha::Virtualshelves->search->count;
29 is( $number_of_shelves, 0, 'No shelves should exist' );
31 my $shelf = Koha::Virtualshelf->new({
32 shelfname => "my first shelf",
33 owner => $patron->{borrowernumber},
38 is( ref( $shelf ), 'Koha::Virtualshelf', 'The constructor should return a valid object' );
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' );
46 my $retrieved_shelf = Koha::Virtualshelves->find( $shelf->shelfnumber );
48 is( $retrieved_shelf->shelfname, $shelf->shelfname, 'Find should correctly return the shelfname' );
50 # Insert with the same name
52 $shelf = Koha::Virtualshelf->new({
53 shelfname => "my first shelf",
54 owner => $patron->{borrowernumber},
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' );
64 my $another_patron = $builder->build({
68 $shelf = Koha::Virtualshelf->new({
69 shelfname => "my first shelf",
70 owner => $another_patron->{borrowernumber},
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');
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' );
85 subtest 'Sharing' => sub {
87 my $patron_wants_to_share = $builder->build({
90 my $share_with_me = $builder->build({
93 my $just_another_patron = $builder->build({
97 my $number_of_shelves_shared = Koha::Virtualshelfshares->search->count;
98 is( $number_of_shelves_shared, 0, 'No shelves should exist' );
100 my $shelf_to_share = Koha::Virtualshelf->new({
101 shelfname => "my first shelf",
102 owner => $patron_wants_to_share->{borrowernumber},
107 my $shelf_not_to_share = Koha::Virtualshelf->new({
108 shelfname => "my second shelf",
109 owner => $patron_wants_to_share->{borrowernumber},
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' );
119 my $another_shared_shelf = eval { $shelf_to_share->share('this is another valid key') }; # Just to have 2 shares in DB
121 $number_of_shelves_shared = Koha::Virtualshelfshares->search->count;
122 is( $number_of_shelves_shared, 2, '2 shares should have been inserted' );
124 my $is_accepted = eval {
125 $shared_shelf->accept( 'this is an invalid key', $share_with_me->{borrowernumber} );
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' );
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' );
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' );
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" );
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' );
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' );
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' );
155 subtest 'Shelf content' => sub {
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;
166 is( $number_of_contents, 0, 'No content should exist' );
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},
173 lastmodified => $dt_yesterday,
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' );
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' );
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' );
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' );
206 # allow_change_from_owner == 0 (readonly)
207 $shelf->allow_change_from_owner( 0 );
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' );
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' );
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' );
236 subtest 'Shelf permissions' => sub {
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', } );
247 my $public_shelf = Koha::Virtualshelf->new(
248 { shelfname => "my first shelf",
249 owner => $patron1->{borrowernumber},
251 allow_change_from_owner => 0,
252 allow_change_from_others => 0,
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' );
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' );
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' );
265 is( $public_shelf->can_biblios_be_added( $patron1->{borrowernumber} ), 0, 'The owner should not be able to add biblios to their list' );
266 is( $public_shelf->can_biblios_be_added( $patron2->{borrowernumber} ), 0, 'Public list should not be modified (add) by someone else' );
268 is( $public_shelf->can_biblios_be_removed( $patron1->{borrowernumber} ), 0, 'The owner should not be able to remove biblios to their list' );
269 is( $public_shelf->can_biblios_be_removed( $patron2->{borrowernumber} ), 0, 'Public list should not be modified (remove) by someone else' );
272 $public_shelf->allow_change_from_owner(1);
273 $public_shelf->store;
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' );
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' );
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' );
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' );
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' );
291 my $private_shelf = Koha::Virtualshelf->new(
292 { shelfname => "my first shelf",
293 owner => $patron1->{borrowernumber},
295 allow_change_from_owner => 0,
296 allow_change_from_others => 0,
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' );
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' );
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' );
309 is( $private_shelf->can_biblios_be_added( $patron1->{borrowernumber} ), 0, 'The owner should not be able to add biblios to their list' );
310 is( $private_shelf->can_biblios_be_added( $patron2->{borrowernumber} ), 0, 'Private list should not be modified (add) by someone else' );
312 is( $private_shelf->can_biblios_be_removed( $patron1->{borrowernumber} ), 0, 'The owner should not be able to remove biblios to their list' );
313 is( $private_shelf->can_biblios_be_removed( $patron2->{borrowernumber} ), 0, 'Private list should not be modified (remove) by someone else' );
316 $private_shelf->allow_change_from_owner(1);
317 $private_shelf->allow_change_from_others(1);
318 $private_shelf->store;
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' );
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' );
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' );
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' );
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' );
338 subtest 'Get shelves' => sub {
340 my $patron1 = $builder->build({
341 source => 'Borrower',
343 my $patron2 = $builder->build({
344 source => 'Borrower',
347 my $private_shelf1_1 = Koha::Virtualshelf->new({
348 shelfname => "private shelf 1 for patron 1",
349 owner => $patron1->{borrowernumber},
353 my $private_shelf1_2 = Koha::Virtualshelf->new({
354 shelfname => "private shelf 2 for patron 1",
355 owner => $patron1->{borrowernumber},
359 my $private_shelf2_1 = Koha::Virtualshelf->new({
360 shelfname => "private shelf 1 for patron 2",
361 owner => $patron2->{borrowernumber},
365 my $public_shelf1_1 = Koha::Virtualshelf->new({
366 shelfname => "public shelf 1 for patron 1",
367 owner => $patron1->{borrowernumber},
371 my $public_shelf1_2 = Koha::Virtualshelf->new({
372 shelfname => "public shelf 2 for patron 1",
373 owner => $patron1->{borrowernumber},
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' );
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' );
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' );
393 subtest 'Get shelves containing biblios' => sub {
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', } );
403 my $shelf1 = Koha::Virtualshelf->new(
404 { shelfname => "my first shelf",
405 owner => $patron1->{borrowernumber},
409 my $shelf2 = Koha::Virtualshelf->new(
410 { shelfname => "my x second shelf", # 'x' to make it sorted after 'third'
411 owner => $patron2->{borrowernumber},
415 my $shelf3 = Koha::Virtualshelf->new(
416 { shelfname => "my third shelf",
417 owner => $patron1->{borrowernumber},
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} );
429 my $shelves_with_biblio1_for_any_patrons = Koha::Virtualshelves->get_shelves_containing_record(
431 biblionumber => $biblio1->{biblionumber},
434 is ( $shelves_with_biblio1_for_any_patrons->count, 0, 'shelf1 is private and should not be displayed if patron is not logged in' );
436 my $shelves_with_biblio4_for_any_patrons = Koha::Virtualshelves->get_shelves_containing_record(
438 biblionumber => $biblio4->{biblionumber},
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' );
444 my $shelves_with_biblio1_for_other_patrons = Koha::Virtualshelves->get_shelves_containing_record(
446 biblionumber => $biblio1->{biblionumber},
447 borrowernumber => $patron2->{borrowernumber},
450 is ( $shelves_with_biblio1_for_other_patrons->count, 0, 'shelf1 is private and should not be displayed for other patrons' );
452 my $shelves_with_biblio1_for_owner = Koha::Virtualshelves->get_shelves_containing_record(
454 biblionumber => $biblio1->{biblionumber},
455 borrowernumber => $patron1->{borrowernumber},
458 is ( $shelves_with_biblio1_for_owner->count, 1, 'shelf1 is private and should be displayed for the owner' );
460 my $shelves_with_biblio2_for_patron1 = Koha::Virtualshelves->get_shelves_containing_record(
462 biblionumber => $biblio2->{biblionumber},
463 borrowernumber => $patron1->{borrowernumber},
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' );
469 my $shelves_with_biblio4_for_patron2 = Koha::Virtualshelves->get_shelves_containing_record(
471 biblionumber => $biblio4->{biblionumber},
472 borrowernumber => $patron2->{borrowernumber},
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' );
482 $dbh->do(q|DELETE FROM virtualshelfshares|);
483 $dbh->do(q|DELETE FROM virtualshelfcontents|);
484 $dbh->do(q|DELETE FROM virtualshelves|);