Bug 16699: Remove requirement from borrowernumberQueryParam
[koha.git] / t / db_dependent / Virtualshelves.t
blob58d3fec54c02033d55bb92b88861a68f1f0d616d
1 #!/usr/bin/perl
3 use Modern::Perl;
4 use Test::More tests => 5;
5 use DateTime::Duration;
7 use C4::Context;
8 use Koha::DateUtils;
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;
19 teardown();
21 subtest 'CRUD' => sub {
22 plan tests => 13;
23 my $patron = $builder->build({
24 source => 'Borrower',
25 });
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},
34 category => 1,
36 )->store;
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
52 eval {
53 $shelf = Koha::Virtualshelf->new({
54 shelfname => "my first shelf",
55 owner => $patron->{borrowernumber},
56 category => 1,
58 )->store;
60 is( ref($@), 'Koha::Exceptions::Virtualshelves::DuplicateObject',
61 'Exception on duplicate name' );
62 $number_of_shelves = Koha::Virtualshelves->search->count;
63 is( $number_of_shelves, 1, 'To be sure the number of shelves is still 1' );
65 my $another_patron = $builder->build({
66 source => 'Borrower',
67 });
69 $shelf = Koha::Virtualshelf->new({
70 shelfname => "my first shelf",
71 owner => $another_patron->{borrowernumber},
72 category => 1,
74 )->store;
75 $number_of_shelves = Koha::Virtualshelves->search->count;
76 is( $number_of_shelves, 2, 'Another patron should be able to create a shelf with an existing shelfname');
78 my $is_deleted = Koha::Virtualshelves->find( $shelf->shelfnumber )->delete;
79 is( $is_deleted, 1, 'The shelf has been deleted correctly' );
80 $number_of_shelves = Koha::Virtualshelves->search->count;
81 is( $number_of_shelves, 1, 'To be sure the shelf has been deleted' );
83 teardown();
86 subtest 'Sharing' => sub {
87 plan tests => 18;
88 my $patron_wants_to_share = $builder->build({
89 source => 'Borrower',
90 });
91 my $share_with_me = $builder->build({
92 source => 'Borrower',
93 });
94 my $just_another_patron = $builder->build({
95 source => 'Borrower',
96 });
98 my $number_of_shelves_shared = Koha::Virtualshelfshares->search->count;
99 is( $number_of_shelves_shared, 0, 'No shelves should exist' );
101 my $shelf_to_share = Koha::Virtualshelf->new({
102 shelfname => "my first shelf",
103 owner => $patron_wants_to_share->{borrowernumber},
104 category => 1,
106 )->store;
108 my $shelf_not_to_share = Koha::Virtualshelf->new({
109 shelfname => "my second shelf",
110 owner => $patron_wants_to_share->{borrowernumber},
111 category => 1,
113 )->store;
115 my $shared_shelf = eval { $shelf_to_share->share };
116 is ( ref( $@ ), 'Koha::Exceptions::Virtualshelves::InvalidKeyOnSharing', 'Do not share if no key given' );
117 $shared_shelf = eval { $shelf_to_share->share('this is a valid key') };
118 is( ref( $shared_shelf ), 'Koha::Virtualshelfshare', 'On sharing, the method should return a valid Koha::Virtualshelfshare object' );
120 my $another_shared_shelf = eval { $shelf_to_share->share('this is another valid key') }; # Just to have 2 shares in DB
122 $number_of_shelves_shared = Koha::Virtualshelfshares->search->count;
123 is( $number_of_shelves_shared, 2, '2 shares should have been inserted' );
125 my $is_accepted = eval {
126 $shared_shelf->accept( 'this is an invalid key', $share_with_me->{borrowernumber} );
128 is( $is_accepted, undef, 'The share should have not been accepted if the key is invalid' );
129 is( ref( $@ ), 'Koha::Exceptions::Virtualshelves::InvalidInviteKey', 'accept with an invalid key should raise an exception' );
131 $is_accepted = $shared_shelf->accept( 'this is a valid key', $share_with_me->{borrowernumber} );
132 ok( defined($is_accepted), 'The share should have been accepted if the key valid' );
134 is( $shelf_to_share->is_shared, 1, 'first shelf is shared' );
135 is( $shelf_not_to_share->is_shared, 0, 'second shelf is not shared' );
137 is( $shelf_to_share->is_shared_with( $patron_wants_to_share->{borrowernumber} ), 0 , "The shelf should not be shared with the owner" );
138 is( $shelf_to_share->is_shared_with( $share_with_me->{borrowernumber} ), 1 , "The shelf should be shared with share_with_me" );
139 is( $shelf_to_share->is_shared_with( $just_another_patron->{borrowernumber} ), 0, "The shelf should not be shared with just_another_patron" );
141 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' );
142 $number_of_shelves_shared = Koha::Virtualshelfshares->search->count;
143 is( $number_of_shelves_shared, 2, 'To be sure no shares have been removed' );
145 is( $shelf_not_to_share->remove_share( $share_with_me->{borrowernumber} ), 0, '0 share should have been removed if the shelf is not share' );
146 $number_of_shelves_shared = Koha::Virtualshelfshares->search->count;
147 is( $number_of_shelves_shared, 2, 'To be sure no shares have been removed' );
149 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' );
150 $number_of_shelves_shared = Koha::Virtualshelfshares->search->count;
151 is( $number_of_shelves_shared, 1, 'To be sure the share has been removed' );
153 teardown();
156 subtest 'Shelf content' => sub {
158 plan tests => 18;
159 my $patron1 = $builder->build( { source => 'Borrower', } );
160 my $patron2 = $builder->build( { source => 'Borrower', } );
161 my $biblio1 = $builder->build( { source => 'Biblio', } );
162 my $biblio2 = $builder->build( { source => 'Biblio', } );
163 my $biblio3 = $builder->build( { source => 'Biblio', } );
164 my $biblio4 = $builder->build( { source => 'Biblio', } );
165 my $number_of_contents = Koha::Virtualshelfcontents->search->count;
167 is( $number_of_contents, 0, 'No content should exist' );
169 my $dt_yesterday = dt_from_string->subtract_duration( DateTime::Duration->new( days => 1 ) );
170 my $shelf = Koha::Virtualshelf->new(
171 { shelfname => "my first shelf",
172 owner => $patron1->{borrowernumber},
173 category => 1,
174 lastmodified => $dt_yesterday,
176 )->store;
178 $shelf = Koha::Virtualshelves->find( $shelf->shelfnumber );
179 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' );
180 my $content1 = $shelf->add_biblio( $biblio1->{biblionumber}, $patron1->{borrowernumber} );
181 is( ref($content1), 'Koha::Virtualshelfcontent', 'add_biblio to a shelf should return a Koha::Virtualshelfcontent object if inserted' );
182 $shelf = Koha::Virtualshelves->find( $shelf->shelfnumber );
183 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' );
184 my $content2 = $shelf->add_biblio( $biblio2->{biblionumber}, $patron1->{borrowernumber} );
185 $number_of_contents = Koha::Virtualshelfcontents->search->count;
186 is( $number_of_contents, 2, '2 biblio should have been inserted' );
188 my $content1_bis = $shelf->add_biblio( $biblio1->{biblionumber}, $patron1->{borrowernumber} );
189 is( $content1_bis, undef, 'add_biblio should return undef on duplicate' ); # Or an exception ?
190 $number_of_contents = Koha::Virtualshelfcontents->search->count;
191 is( $number_of_contents, 2, 'The biblio should not have been duplicated' );
193 $shelf = Koha::Virtualshelves->find( $shelf->shelfnumber );
194 my $contents = $shelf->get_contents;
195 is( $contents->count, 2, 'There are 2 biblios on this shelf' );
197 # Patron 2 will try to remove a content
198 # allow_add = 0, allow_delete_own = 1, allow_delete_other = 0 => Default values
199 my $number_of_deleted_biblios = $shelf->remove_biblios( { biblionumbers => [ $biblio1->{biblionumber} ], borrowernumber => $patron2->{borrowernumber} } );
200 is( $number_of_deleted_biblios, 0, 'Patron 2 removed nothing' );
201 # Now try with patron 1
202 $number_of_deleted_biblios = $shelf->remove_biblios( { biblionumbers => [ $biblio1->{biblionumber} ], borrowernumber => $patron1->{borrowernumber} } );
203 is( $number_of_deleted_biblios, 1, 'Patron 1 removed biblio' );
205 $number_of_contents = Koha::Virtualshelfcontents->search->count;
206 is( $number_of_contents, 1, 'To be sure the content has been deleted' );
208 # allow_delete_own = 0
209 $shelf->allow_delete_own(0);
210 $shelf->store;
211 $number_of_deleted_biblios = $shelf->remove_biblios( { biblionumbers => [ $biblio2->{biblionumber} ], borrowernumber => $patron1->{borrowernumber} } );
212 is( $number_of_deleted_biblios, 1, 'Patron 1 removed another biblio' );
213 $number_of_contents = Koha::Virtualshelfcontents->search->count;
214 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)' );
215 $shelf->add_biblio( $biblio2->{biblionumber}, $patron1->{borrowernumber} );
217 # allow_add = 1, allow_delete_own = 1
218 $shelf->allow_add(1);
219 $shelf->allow_delete_own(1);
220 $shelf->store;
222 my $content3 = $shelf->add_biblio( $biblio3->{biblionumber}, $patron2->{borrowernumber} );
223 my $content4 = $shelf->add_biblio( $biblio4->{biblionumber}, $patron2->{borrowernumber} );
225 $number_of_contents = Koha::Virtualshelfcontents->search->count;
226 is( $number_of_contents, 3, '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, 2, 'The biblio should have been deleted to the shelf by the patron, it is his own content (allow_delete_own=1) ' );
233 # allow_add = 1, allow_delete_own = 1, allow_delete_other = 1
234 $shelf->allow_delete_other(1);
235 $shelf->store;
237 $number_of_deleted_biblios = $shelf->remove_biblios( { biblionumbers => [ $biblio2->{biblionumber} ], borrowernumber => $patron2->{borrowernumber} } );
238 is( $number_of_deleted_biblios, 1, 'Biblio 2 deleted by patron 2' );
239 $number_of_contents = Koha::Virtualshelfcontents->search->count;
240 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)' );
242 teardown();
245 subtest 'Shelf permissions' => sub {
247 plan tests => 40;
248 my $patron1 = $builder->build( { source => 'Borrower', value => { flags => '2096766' } } ); # 2096766 is everything checked but not superlibrarian
249 my $patron2 = $builder->build( { source => 'Borrower', value => { flags => '1048190' } } ); # 1048190 is everything checked but not superlibrarian and delete_public_lists
250 my $biblio1 = $builder->build( { source => 'Biblio', } );
251 my $biblio2 = $builder->build( { source => 'Biblio', } );
252 my $biblio3 = $builder->build( { source => 'Biblio', } );
253 my $biblio4 = $builder->build( { source => 'Biblio', } );
256 my $public_shelf = Koha::Virtualshelf->new(
257 { shelfname => "my first shelf",
258 owner => $patron1->{borrowernumber},
259 category => 2,
260 allow_add => 0,
261 allow_delete_own => 0,
262 allow_delete_other => 0,
264 )->store;
266 is( $public_shelf->can_be_viewed( $patron1->{borrowernumber} ), 1, 'The owner should be able to view his public list' );
267 is( $public_shelf->can_be_viewed( $patron2->{borrowernumber} ), 1, 'Public list should be viewed by someone else' );
269 is( $public_shelf->can_be_deleted( $patron1->{borrowernumber} ), 1, 'The owner should be able to delete his list' );
270 is( $public_shelf->can_be_deleted( $patron2->{borrowernumber} ), 0, 'Public list should not be deleted by someone else' );
272 is( $public_shelf->can_be_managed( $patron1->{borrowernumber} ), 1, 'The owner should be able to manage his list' );
273 is( $public_shelf->can_be_managed( $patron2->{borrowernumber} ), 0, 'Public list should not be managed by someone else' );
275 is( $public_shelf->can_biblios_be_added( $patron1->{borrowernumber} ), 1, 'The owner should be able to add biblios to his list' );
276 is( $public_shelf->can_biblios_be_added( $patron2->{borrowernumber} ), 0, 'Public list should not be modified (add) by someone else' );
278 is( $public_shelf->can_biblios_be_removed( $patron1->{borrowernumber} ), 1, 'The owner should be able to remove biblios to his list' );
279 is( $public_shelf->can_biblios_be_removed( $patron2->{borrowernumber} ), 0, 'Public list should not be modified (remove) by someone else' );
282 $public_shelf->allow_add(1);
283 $public_shelf->allow_delete_own(1);
284 $public_shelf->allow_delete_other(1);
285 $public_shelf->store;
287 is( $public_shelf->can_be_viewed( $patron1->{borrowernumber} ), 1, 'The owner should be able to view his public list' );
288 is( $public_shelf->can_be_viewed( $patron2->{borrowernumber} ), 1, 'Public list should be viewed by someone else' );
290 is( $public_shelf->can_be_deleted( $patron1->{borrowernumber} ), 1, 'The owner should be able to delete his list' );
291 is( $public_shelf->can_be_deleted( $patron2->{borrowernumber} ), 0, 'Public list should not be deleted by someone else' );
293 is( $public_shelf->can_be_managed( $patron1->{borrowernumber} ), 1, 'The owner should be able to manage his list' );
294 is( $public_shelf->can_be_managed( $patron2->{borrowernumber} ), 0, 'Public list should not be managed by someone else' );
296 is( $public_shelf->can_biblios_be_added( $patron1->{borrowernumber} ), 1, 'The owner should be able to add biblios to his list' );
297 is( $public_shelf->can_biblios_be_added( $patron2->{borrowernumber} ), 1, 'Public list should not be modified (add) by someone else' );
299 is( $public_shelf->can_biblios_be_removed( $patron1->{borrowernumber} ), 1, 'The owner should be able to remove biblios to his list' );
300 is( $public_shelf->can_biblios_be_removed( $patron2->{borrowernumber} ), 1, 'Public list should not be modified (remove) by someone else' );
303 my $private_shelf = Koha::Virtualshelf->new(
304 { shelfname => "my first shelf",
305 owner => $patron1->{borrowernumber},
306 category => 1,
307 allow_add => 0,
308 allow_delete_own => 0,
309 allow_delete_other => 0,
311 )->store;
313 is( $private_shelf->can_be_viewed( $patron1->{borrowernumber} ), 1, 'The owner should be able to view his list' );
314 is( $private_shelf->can_be_viewed( $patron2->{borrowernumber} ), 0, 'Private list should not be viewed by someone else' );
316 is( $private_shelf->can_be_deleted( $patron1->{borrowernumber} ), 1, 'The owner should be able to delete his list' );
317 is( $private_shelf->can_be_deleted( $patron2->{borrowernumber} ), 0, 'Private list should not be deleted by someone else' );
319 is( $private_shelf->can_be_managed( $patron1->{borrowernumber} ), 1, 'The owner should be able to manage his list' );
320 is( $private_shelf->can_be_managed( $patron2->{borrowernumber} ), 0, 'Private list should not be managed by someone else' );
322 is( $private_shelf->can_biblios_be_added( $patron1->{borrowernumber} ), 1, 'The owner should be able to add biblios to his list' );
323 is( $private_shelf->can_biblios_be_added( $patron2->{borrowernumber} ), 0, 'Private list should not be modified (add) by someone else' );
325 is( $private_shelf->can_biblios_be_removed( $patron1->{borrowernumber} ), 1, 'The owner should be able to remove biblios to his list' );
326 is( $private_shelf->can_biblios_be_removed( $patron2->{borrowernumber} ), 0, 'Private list should not be modified (remove) by someone else' );
329 $private_shelf->allow_add(1);
330 $private_shelf->allow_delete_own(1);
331 $private_shelf->allow_delete_other(1);
332 $private_shelf->store;
334 is( $private_shelf->can_be_viewed( $patron1->{borrowernumber} ), 1, 'The owner should be able to view his list' );
335 is( $private_shelf->can_be_viewed( $patron2->{borrowernumber} ), 0, 'Private list should not be viewed by someone else' );
337 is( $private_shelf->can_be_deleted( $patron1->{borrowernumber} ), 1, 'The owner should be able to delete his list' );
338 is( $private_shelf->can_be_deleted( $patron2->{borrowernumber} ), 0, 'Private list should not be deleted by someone else' );
340 is( $private_shelf->can_be_managed( $patron1->{borrowernumber} ), 1, 'The owner should be able to manage his list' );
341 is( $private_shelf->can_be_managed( $patron2->{borrowernumber} ), 0, 'Private list should not be managed by someone else' );
343 is( $private_shelf->can_biblios_be_added( $patron1->{borrowernumber} ), 1, 'The owner should be able to add biblios to his list' );
344 is( $private_shelf->can_biblios_be_added( $patron2->{borrowernumber} ), 1, 'Private list could be modified (add) by someone else # individual check done later' );
346 is( $private_shelf->can_biblios_be_removed( $patron1->{borrowernumber} ), 1, 'The owner should be able to remove biblios to his list' );
347 is( $private_shelf->can_biblios_be_removed( $patron2->{borrowernumber} ), 1, 'Private list could be modified (remove) by someone else # individual check done later' );
349 teardown();
352 subtest 'Get shelves' => sub {
353 plan tests => 4;
354 my $patron1 = $builder->build({
355 source => 'Borrower',
357 my $patron2 = $builder->build({
358 source => 'Borrower',
361 my $private_shelf1_1 = Koha::Virtualshelf->new({
362 shelfname => "private shelf 1 for patron 1",
363 owner => $patron1->{borrowernumber},
364 category => 1,
366 )->store;
367 my $private_shelf1_2 = Koha::Virtualshelf->new({
368 shelfname => "private shelf 2 for patron 1",
369 owner => $patron1->{borrowernumber},
370 category => 1,
372 )->store;
373 my $private_shelf2_1 = Koha::Virtualshelf->new({
374 shelfname => "private shelf 1 for patron 2",
375 owner => $patron2->{borrowernumber},
376 category => 1,
378 )->store;
379 my $public_shelf1_1 = Koha::Virtualshelf->new({
380 shelfname => "public shelf 1 for patron 1",
381 owner => $patron1->{borrowernumber},
382 category => 2,
384 )->store;
385 my $public_shelf1_2 = Koha::Virtualshelf->new({
386 shelfname => "public shelf 2 for patron 1",
387 owner => $patron1->{borrowernumber},
388 category => 2,
390 )->store;
392 my $private_shelves = Koha::Virtualshelves->get_private_shelves;
393 is( $private_shelves->count, 0, 'Without borrowernumber given, get_private_shelves should not return any shelf' );
394 $private_shelves = Koha::Virtualshelves->get_private_shelves({ borrowernumber => $patron1->{borrowernumber} });
395 is( $private_shelves->count, 2, 'get_private_shelves should return all shelves for a given patron' );
397 $private_shelf2_1->share('a key')->accept('a key', $patron1->{borrowernumber});
398 $private_shelves = Koha::Virtualshelves->get_private_shelves({ borrowernumber => $patron1->{borrowernumber} });
399 is( $private_shelves->count, 3, 'get_private_shelves should return all shelves for a given patron, even the shared ones' );
401 my $public_shelves = Koha::Virtualshelves->get_public_shelves;
402 is( $public_shelves->count, 2, 'get_public_shelves should return all public shelves, no matter who is the owner' );
404 teardown();
407 sub teardown {
408 $dbh->do(q|DELETE FROM virtualshelfshares|);
409 $dbh->do(q|DELETE FROM virtualshelfcontents|);
410 $dbh->do(q|DELETE FROM virtualshelves|);