4 use Test::More tests => 5;
5 use DateTime::Duration;
9 use Koha::Virtualshelves;
10 use Koha::Virtualshelfshares;
11 use Koha::Virtualshelfcontents;
13 use t::lib::TestBuilder;
15 my $dbh = C4::Context->dbh;
16 $dbh->{AutoCommit} = 0;
19 my $builder = t::lib::TestBuilder->new;
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_add, 0, 'The default value for allow_add should be 1' );
43 is( $shelf->allow_delete_own, 1, 'The default value for allow_delete_own should be 0' );
44 is( $shelf->allow_delete_other, 0, 'The default value for allow_delete_other should be 0' );
45 is( output_pref($shelf->created_on), output_pref(dt_from_string), 'The creation time should have been set to today' );
47 my $retrieved_shelf = Koha::Virtualshelves->find( $shelf->shelfnumber );
49 is( $retrieved_shelf->shelfname, $shelf->shelfname, 'Find should correctly return the shelfname' );
51 # Insert with the same name
53 $shelf = Koha::Virtualshelf->new({
54 shelfname => "my first shelf",
55 owner => $patron->{borrowernumber},
60 is( ref($@), 'Koha::Exceptions::Virtualshelves::DuplicateObject' );
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 );
134 is( $shelf_not_to_share->is_shared, 0 );
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 a content
197 # allow_add = 0, allow_delete_own = 1, allow_delete_other = 0 => Default values
198 my $number_of_deleted_biblios = $shelf->remove_biblios( { biblionumbers => [ $biblio1->{biblionumber} ], borrowernumber => $patron2->{borrowernumber} } );
199 is( $number_of_deleted_biblios, 0, );
200 $number_of_deleted_biblios = $shelf->remove_biblios( { biblionumbers => [ $biblio1->{biblionumber} ], borrowernumber => $patron1->{borrowernumber} } );
201 is( $number_of_deleted_biblios, 1, );
203 $number_of_contents = Koha::Virtualshelfcontents->search->count;
204 is( $number_of_contents, 1, 'To be sure the content has been deleted' );
206 # allow_delete_own = 0
207 $shelf->allow_delete_own(0);
209 $number_of_deleted_biblios = $shelf->remove_biblios( { biblionumbers => [ $biblio2->{biblionumber} ], borrowernumber => $patron1->{borrowernumber} } );
210 is( $number_of_deleted_biblios, 1, );
211 $number_of_contents = Koha::Virtualshelfcontents->search->count;
212 is( $number_of_contents, 0, 'The biblio should have been deleted to the shelf by the patron, it is his own content (allow_delete_own=0)' );
213 $shelf->add_biblio( $biblio2->{biblionumber}, $patron1->{borrowernumber} );
215 # allow_add = 1, allow_delete_own = 1
216 $shelf->allow_add(1);
217 $shelf->allow_delete_own(1);
220 my $content3 = $shelf->add_biblio( $biblio3->{biblionumber}, $patron2->{borrowernumber} );
221 my $content4 = $shelf->add_biblio( $biblio4->{biblionumber}, $patron2->{borrowernumber} );
223 $number_of_contents = Koha::Virtualshelfcontents->search->count;
224 is( $number_of_contents, 3, 'The biblio should have been added to the shelf by the patron 2' );
226 $number_of_deleted_biblios = $shelf->remove_biblios( { biblionumbers => [ $biblio3->{biblionumber} ], borrowernumber => $patron2->{borrowernumber} } );
227 is( $number_of_deleted_biblios, 1, );
228 $number_of_contents = Koha::Virtualshelfcontents->search->count;
229 is( $number_of_contents, 2, 'The biblio should have been deleted to the shelf by the patron, it is his own content (allow_delete_own=1) ' );
231 # allow_add = 1, allow_delete_own = 1, allow_delete_other = 1
232 $shelf->allow_delete_other(1);
235 $number_of_deleted_biblios = $shelf->remove_biblios( { biblionumbers => [ $biblio2->{biblionumber} ], borrowernumber => $patron2->{borrowernumber} } );
236 is( $number_of_deleted_biblios, 1, );
237 $number_of_contents = Koha::Virtualshelfcontents->search->count;
238 is( $number_of_contents, 1, 'The biblio should have been deleted to the shelf by the patron 2, even if it is not his own content (allow_delete_other=1)' );
243 subtest 'Shelf permissions' => sub {
246 my $patron1 = $builder->build( { source => 'Borrower', value => { flags => '2096766' } } ); # 2096766 is everything checked but not superlibrarian
247 my $patron2 = $builder->build( { source => 'Borrower', value => { flags => '1048190' } } ); # 1048190 is everything checked but not superlibrarian and delete_public_lists
248 my $biblio1 = $builder->build( { source => 'Biblio', } );
249 my $biblio2 = $builder->build( { source => 'Biblio', } );
250 my $biblio3 = $builder->build( { source => 'Biblio', } );
251 my $biblio4 = $builder->build( { source => 'Biblio', } );
254 my $public_shelf = Koha::Virtualshelf->new(
255 { shelfname => "my first shelf",
256 owner => $patron1->{borrowernumber},
259 allow_delete_own => 0,
260 allow_delete_other => 0,
264 is( $public_shelf->can_be_viewed( $patron1->{borrowernumber} ), 1, 'The owner should be able to view his public list' );
265 is( $public_shelf->can_be_viewed( $patron2->{borrowernumber} ), 1, 'Public list should be viewed by someone else' );
267 is( $public_shelf->can_be_deleted( $patron1->{borrowernumber} ), 1, 'The owner should be able to delete his list' );
268 is( $public_shelf->can_be_deleted( $patron2->{borrowernumber} ), 0, 'Public list should not be deleted by someone else' );
270 is( $public_shelf->can_be_managed( $patron1->{borrowernumber} ), 1, 'The owner should be able to manage his list' );
271 is( $public_shelf->can_be_managed( $patron2->{borrowernumber} ), 0, 'Public list should not be managed by someone else' );
273 is( $public_shelf->can_biblios_be_added( $patron1->{borrowernumber} ), 1, 'The owner should be able to add biblios to his list' );
274 is( $public_shelf->can_biblios_be_added( $patron2->{borrowernumber} ), 0, 'Public list should not be modified (add) by someone else' );
276 is( $public_shelf->can_biblios_be_removed( $patron1->{borrowernumber} ), 1, 'The owner should be able to remove biblios to his list' );
277 is( $public_shelf->can_biblios_be_removed( $patron2->{borrowernumber} ), 0, 'Public list should not be modified (remove) by someone else' );
280 $public_shelf->allow_add(1);
281 $public_shelf->allow_delete_own(1);
282 $public_shelf->allow_delete_other(1);
283 $public_shelf->store;
285 is( $public_shelf->can_be_viewed( $patron1->{borrowernumber} ), 1, 'The owner should be able to view his public list' );
286 is( $public_shelf->can_be_viewed( $patron2->{borrowernumber} ), 1, 'Public list should be viewed by someone else' );
288 is( $public_shelf->can_be_deleted( $patron1->{borrowernumber} ), 1, 'The owner should be able to delete his list' );
289 is( $public_shelf->can_be_deleted( $patron2->{borrowernumber} ), 0, 'Public list should not be deleted by someone else' );
291 is( $public_shelf->can_be_managed( $patron1->{borrowernumber} ), 1, 'The owner should be able to manage his list' );
292 is( $public_shelf->can_be_managed( $patron2->{borrowernumber} ), 0, 'Public list should not be managed by someone else' );
294 is( $public_shelf->can_biblios_be_added( $patron1->{borrowernumber} ), 1, 'The owner should be able to add biblios to his list' );
295 is( $public_shelf->can_biblios_be_added( $patron2->{borrowernumber} ), 1, 'Public list should not be modified (add) by someone else' );
297 is( $public_shelf->can_biblios_be_removed( $patron1->{borrowernumber} ), 1, 'The owner should be able to remove biblios to his list' );
298 is( $public_shelf->can_biblios_be_removed( $patron2->{borrowernumber} ), 1, 'Public list should not be modified (remove) by someone else' );
301 my $private_shelf = Koha::Virtualshelf->new(
302 { shelfname => "my first shelf",
303 owner => $patron1->{borrowernumber},
306 allow_delete_own => 0,
307 allow_delete_other => 0,
311 is( $private_shelf->can_be_viewed( $patron1->{borrowernumber} ), 1, 'The owner should be able to view his list' );
312 is( $private_shelf->can_be_viewed( $patron2->{borrowernumber} ), 0, 'Private list should not be viewed by someone else' );
314 is( $private_shelf->can_be_deleted( $patron1->{borrowernumber} ), 1, 'The owner should be able to delete his list' );
315 is( $private_shelf->can_be_deleted( $patron2->{borrowernumber} ), 0, 'Private list should not be deleted by someone else' );
317 is( $private_shelf->can_be_managed( $patron1->{borrowernumber} ), 1, 'The owner should be able to manage his list' );
318 is( $private_shelf->can_be_managed( $patron2->{borrowernumber} ), 0, 'Private list should not be managed by someone else' );
320 is( $private_shelf->can_biblios_be_added( $patron1->{borrowernumber} ), 1, 'The owner should be able to add biblios to his list' );
321 is( $private_shelf->can_biblios_be_added( $patron2->{borrowernumber} ), 0, 'Private list should not be modified (add) by someone else' );
323 is( $private_shelf->can_biblios_be_removed( $patron1->{borrowernumber} ), 1, 'The owner should be able to remove biblios to his list' );
324 is( $private_shelf->can_biblios_be_removed( $patron2->{borrowernumber} ), 0, 'Private list should not be modified (remove) by someone else' );
327 $private_shelf->allow_add(1);
328 $private_shelf->allow_delete_own(1);
329 $private_shelf->allow_delete_other(1);
330 $private_shelf->store;
332 is( $private_shelf->can_be_viewed( $patron1->{borrowernumber} ), 1, 'The owner should be able to view his list' );
333 is( $private_shelf->can_be_viewed( $patron2->{borrowernumber} ), 0, 'Private list should not be viewed by someone else' );
335 is( $private_shelf->can_be_deleted( $patron1->{borrowernumber} ), 1, 'The owner should be able to delete his list' );
336 is( $private_shelf->can_be_deleted( $patron2->{borrowernumber} ), 0, 'Private list should not be deleted by someone else' );
338 is( $private_shelf->can_be_managed( $patron1->{borrowernumber} ), 1, 'The owner should be able to manage his list' );
339 is( $private_shelf->can_be_managed( $patron2->{borrowernumber} ), 0, 'Private list should not be managed by someone else' );
341 is( $private_shelf->can_biblios_be_added( $patron1->{borrowernumber} ), 1, 'The owner should be able to add biblios to his list' );
342 is( $private_shelf->can_biblios_be_added( $patron2->{borrowernumber} ), 1, 'Private list could be modified (add) by someone else # individual check done later' );
344 is( $private_shelf->can_biblios_be_removed( $patron1->{borrowernumber} ), 1, 'The owner should be able to remove biblios to his list' );
345 is( $private_shelf->can_biblios_be_removed( $patron2->{borrowernumber} ), 1, 'Private list could be modified (remove) by someone else # individual check done later' );
350 subtest 'Get shelves' => sub {
352 my $patron1 = $builder->build({
353 source => 'Borrower',
355 my $patron2 = $builder->build({
356 source => 'Borrower',
359 my $private_shelf1_1 = Koha::Virtualshelf->new({
360 shelfname => "private shelf 1 for patron 1",
361 owner => $patron1->{borrowernumber},
365 my $private_shelf1_2 = Koha::Virtualshelf->new({
366 shelfname => "private shelf 2 for patron 1",
367 owner => $patron1->{borrowernumber},
371 my $private_shelf2_1 = Koha::Virtualshelf->new({
372 shelfname => "private shelf 1 for patron 2",
373 owner => $patron2->{borrowernumber},
377 my $public_shelf1_1 = Koha::Virtualshelf->new({
378 shelfname => "public shelf 1 for patron 1",
379 owner => $patron1->{borrowernumber},
383 my $public_shelf1_2 = Koha::Virtualshelf->new({
384 shelfname => "public shelf 2 for patron 1",
385 owner => $patron1->{borrowernumber},
390 my $private_shelves = Koha::Virtualshelves->get_private_shelves;
391 is( $private_shelves->count, 0, 'Without borrowernumber given, get_private_shelves should not return any shelf' );
392 $private_shelves = Koha::Virtualshelves->get_private_shelves({ borrowernumber => $patron1->{borrowernumber} });
393 is( $private_shelves->count, 2, 'get_private_shelves should return all shelves for a given patron' );
395 $private_shelf2_1->share('a key')->accept('a key', $patron1->{borrowernumber});
396 $private_shelves = Koha::Virtualshelves->get_private_shelves({ borrowernumber => $patron1->{borrowernumber} });
397 is( $private_shelves->count, 3, 'get_private_shelves should return all shelves for a given patron, even the shared ones' );
399 my $public_shelves = Koha::Virtualshelves->get_public_shelves;
400 is( $public_shelves->count, 2, 'get_public_shelves should return all public shelves, no matter who is the owner' );
406 $dbh->do(q|DELETE FROM virtualshelfshares|);
407 $dbh->do(q|DELETE FROM virtualshelfcontents|);
408 $dbh->do(q|DELETE FROM virtualshelves|);