Bug 14544: Make the intranet side independent of Page.pm
[koha.git] / t / db_dependent / Virtualshelves.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4 use Test::More tests => 4;
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 $dbh = C4::Context->dbh;
16 $dbh->{AutoCommit} = 0;
17
18 $dbh->do(q|DELETE FROM virtualshelfshares|);
19 $dbh->do(q|DELETE FROM virtualshelfcontents|);
20 $dbh->do(q|DELETE FROM virtualshelves|);
21
22 my $builder = t::lib::TestBuilder->new;
23
24 subtest 'CRUD' => sub {
25     plan tests => 13;
26     my $patron = $builder->build({
27         source => 'Borrower',
28     });
29
30     my $number_of_shelves = Koha::Virtualshelves->search->count;
31
32     is( $number_of_shelves, 0, 'No shelves should exist' );
33
34     my $shelf = Koha::Virtualshelf->new({
35             shelfname => "my first shelf",
36             owner => $patron->{borrowernumber},
37             category => 1,
38         }
39     )->store;
40
41     is( ref( $shelf ), 'Koha::Virtualshelf', 'The constructor should return a valid object' );
42
43     $number_of_shelves = Koha::Virtualshelves->search->count;
44     is( $number_of_shelves, 1, '1 shelf should have been inserted' );
45     is( $shelf->allow_add, 0, 'The default value for allow_add should be 1' );
46     is( $shelf->allow_delete_own, 1, 'The default value for allow_delete_own should be 0' );
47     is( $shelf->allow_delete_other, 0, 'The default value for allow_delete_other should be 0' );
48     is( output_pref($shelf->created_on), output_pref(dt_from_string), 'The creation time should have been set to today' );
49
50     my $retrieved_shelf = Koha::Virtualshelves->find( $shelf->shelfnumber );
51
52     is( $retrieved_shelf->shelfname, $shelf->shelfname, 'Find should correctly return the shelfname' );
53
54     # Insert with the same name
55     eval {
56         $shelf = Koha::Virtualshelf->new({
57                 shelfname => "my first shelf",
58                 owner => $patron->{borrowernumber},
59                 category => 1,
60             }
61         )->store;
62     };
63     is( ref($@), 'Koha::Exceptions::Virtualshelves::DuplicateObject' );
64     $number_of_shelves = Koha::Virtualshelves->search->count;
65     is( $number_of_shelves, 1, 'To be sure the number of shelves is still 1' );
66
67     my $another_patron = $builder->build({
68         source => 'Borrower',
69     });
70
71     $shelf = Koha::Virtualshelf->new({
72             shelfname => "my first shelf",
73             owner => $another_patron->{borrowernumber},
74             category => 1,
75         }
76     )->store;
77     $number_of_shelves = Koha::Virtualshelves->search->count;
78     is( $number_of_shelves, 2, 'Another patron should be able to create a shelf with an existing shelfname');
79
80     my $is_deleted = Koha::Virtualshelves->find( $shelf->shelfnumber )->delete;
81     is( $is_deleted, 1, 'The shelf has been deleted correctly' );
82     $number_of_shelves = Koha::Virtualshelves->search->count;
83     is( $number_of_shelves, 1, 'To be sure the shelf has been deleted' );
84 };
85
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     });
97
98     my $number_of_shelves_shared = Koha::Virtualshelfshares->search->count;
99     is( $number_of_shelves_shared, 0, 'No shelves should exist' );
100
101     my $shelf_to_share = Koha::Virtualshelf->new({
102             shelfname => "my first shelf",
103             owner => $patron_wants_to_share->{borrowernumber},
104             category => 1,
105         }
106     )->store;
107
108     my $shelf_not_to_share = Koha::Virtualshelf->new({
109             shelfname => "my second shelf",
110             owner => $patron_wants_to_share->{borrowernumber},
111             category => 1,
112         }
113     )->store;
114
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' );
119
120     my $another_shared_shelf = eval { $shelf_to_share->share('this is another valid key') }; # Just to have 2 shares in DB
121
122     $number_of_shelves_shared = Koha::Virtualshelfshares->search->count;
123     is( $number_of_shelves_shared, 2, '2 shares should have been inserted' );
124
125     my $is_accepted = eval {
126         $shared_shelf->accept( 'this is an invalid key', $share_with_me->{borrowernumber} );
127     };
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' );
130
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' );
133
134     is( $shelf_to_share->is_shared, 1 );
135     is( $shelf_not_to_share->is_shared, 0 );
136
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" );
140
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' );
144
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' );
148
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' );
152 };
153
154 subtest 'Shelf content' => sub {
155
156     plan tests => 18;
157     my $patron1 = $builder->build( { source => 'Borrower', } );
158     my $patron2 = $builder->build( { source => 'Borrower', } );
159     my $biblio1 = $builder->build( { source => 'Biblio', } );
160     my $biblio2 = $builder->build( { source => 'Biblio', } );
161     my $biblio3 = $builder->build( { source => 'Biblio', } );
162     my $biblio4 = $builder->build( { source => 'Biblio', } );
163     my $number_of_contents = Koha::Virtualshelfcontents->search->count;
164
165     is( $number_of_contents, 0, 'No content should exist' );
166
167     my $dt_yesterday = dt_from_string->subtract_duration( DateTime::Duration->new( days => 1 ) );
168     my $shelf = Koha::Virtualshelf->new(
169         {   shelfname    => "my first shelf",
170             owner        => $patron1->{borrowernumber},
171             category     => 1,
172             lastmodified => $dt_yesterday,
173         }
174     )->store;
175
176     $shelf = Koha::Virtualshelves->find( $shelf->shelfnumber );
177     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' );
178     my $content1 = $shelf->add_biblio( $biblio1->{biblionumber}, $patron1->{borrowernumber} );
179     is( ref($content1), 'Koha::Virtualshelfcontent', 'add_biblio to a shelf should return a Koha::Virtualshelfcontent object if inserted' );
180     $shelf = Koha::Virtualshelves->find( $shelf->shelfnumber );
181     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' );
182     my $content2 = $shelf->add_biblio( $biblio2->{biblionumber}, $patron1->{borrowernumber} );
183     $number_of_contents = Koha::Virtualshelfcontents->search->count;
184     is( $number_of_contents, 2, '2 biblio should have been inserted' );
185
186     my $content1_bis = $shelf->add_biblio( $biblio1->{biblionumber}, $patron1->{borrowernumber} );
187     is( $content1_bis, undef, 'add_biblio should return undef on duplicate' );    # Or an exception ?
188     $number_of_contents = Koha::Virtualshelfcontents->search->count;
189     is( $number_of_contents, 2, 'The biblio should not have been duplicated' );
190
191     $shelf = Koha::Virtualshelves->find( $shelf->shelfnumber );
192     my $contents = $shelf->get_contents;
193     is( $contents->count, 2, 'There are 2 biblios on this shelf' );
194
195     # Patron 2 will try to remove a content
196     # allow_add = 0, allow_delete_own = 1, allow_delete_other = 0 => Default values
197     my $number_of_deleted_biblios = $shelf->remove_biblios( { biblionumbers => [ $biblio1->{biblionumber} ], borrowernumber => $patron2->{borrowernumber} } );
198     is( $number_of_deleted_biblios, 0, );
199     $number_of_deleted_biblios = $shelf->remove_biblios( { biblionumbers => [ $biblio1->{biblionumber} ], borrowernumber => $patron1->{borrowernumber} } );
200     is( $number_of_deleted_biblios, 1, );
201
202     $number_of_contents = Koha::Virtualshelfcontents->search->count;
203     is( $number_of_contents, 1, 'To be sure the content has been deleted' );
204
205     # allow_delete_own = 0
206     $shelf->allow_delete_own(0);
207     $shelf->store;
208     $number_of_deleted_biblios = $shelf->remove_biblios( { biblionumbers => [ $biblio2->{biblionumber} ], borrowernumber => $patron1->{borrowernumber} } );
209     is( $number_of_deleted_biblios, 1, );
210     $number_of_contents = Koha::Virtualshelfcontents->search->count;
211     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)' );
212     $shelf->add_biblio( $biblio2->{biblionumber}, $patron1->{borrowernumber} );
213
214     # allow_add = 1, allow_delete_own = 1
215     $shelf->allow_add(1);
216     $shelf->allow_delete_own(1);
217     $shelf->store;
218
219     my $content3 = $shelf->add_biblio( $biblio3->{biblionumber}, $patron2->{borrowernumber} );
220     my $content4 = $shelf->add_biblio( $biblio4->{biblionumber}, $patron2->{borrowernumber} );
221
222     $number_of_contents = Koha::Virtualshelfcontents->search->count;
223     is( $number_of_contents, 3, 'The biblio should have been added to the shelf by the patron 2' );
224
225     $number_of_deleted_biblios = $shelf->remove_biblios( { biblionumbers => [ $biblio3->{biblionumber} ], borrowernumber => $patron2->{borrowernumber} } );
226     is( $number_of_deleted_biblios, 1, );
227     $number_of_contents = Koha::Virtualshelfcontents->search->count;
228     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) ' );
229
230     # allow_add = 1, allow_delete_own = 1, allow_delete_other = 1
231     $shelf->allow_delete_other(1);
232     $shelf->store;
233
234     $number_of_deleted_biblios = $shelf->remove_biblios( { biblionumbers => [ $biblio2->{biblionumber} ], borrowernumber => $patron2->{borrowernumber} } );
235     is( $number_of_deleted_biblios, 1, );
236     $number_of_contents = Koha::Virtualshelfcontents->search->count;
237     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)' );
238 };
239
240 subtest 'Shelf permissions' => sub {
241
242     plan tests => 40;
243     my $patron1 = $builder->build( { source => 'Borrower', value => { flags => '2096766' } } ); # 2096766 is everything checked but not superlibrarian
244     my $patron2 = $builder->build( { source => 'Borrower', value => { flags => '1048190' } } ); # 1048190 is everything checked but not superlibrarian and delete_public_lists
245     my $biblio1 = $builder->build( { source => 'Biblio', } );
246     my $biblio2 = $builder->build( { source => 'Biblio', } );
247     my $biblio3 = $builder->build( { source => 'Biblio', } );
248     my $biblio4 = $builder->build( { source => 'Biblio', } );
249
250
251     my $public_shelf = Koha::Virtualshelf->new(
252         {   shelfname    => "my first shelf",
253             owner        => $patron1->{borrowernumber},
254             category     => 2,
255             allow_add          => 0,
256             allow_delete_own   => 0,
257             allow_delete_other => 0,
258         }
259     )->store;
260
261     is( $public_shelf->can_be_viewed( $patron1->{borrowernumber} ), 1, 'The owner should be able to view his public list' );
262     is( $public_shelf->can_be_viewed( $patron2->{borrowernumber} ), 1, 'Public list should be viewed by someone else' );
263
264     is( $public_shelf->can_be_deleted( $patron1->{borrowernumber} ), 1, 'The owner should be able to delete his list' );
265     is( $public_shelf->can_be_deleted( $patron2->{borrowernumber} ), 0, 'Public list should not be deleted by someone else' );
266
267     is( $public_shelf->can_be_managed( $patron1->{borrowernumber} ), 1, 'The owner should be able to manage his list' );
268     is( $public_shelf->can_be_managed( $patron2->{borrowernumber} ), 0, 'Public list should not be managed by someone else' );
269
270     is( $public_shelf->can_biblios_be_added( $patron1->{borrowernumber} ), 1, 'The owner should be able to add biblios to his list' );
271     is( $public_shelf->can_biblios_be_added( $patron2->{borrowernumber} ), 0, 'Public list should not be modified (add) by someone else' );
272
273     is( $public_shelf->can_biblios_be_removed( $patron1->{borrowernumber} ), 1, 'The owner should be able to remove biblios to his list' );
274     is( $public_shelf->can_biblios_be_removed( $patron2->{borrowernumber} ), 0, 'Public list should not be modified (remove) by someone else' );
275
276
277     $public_shelf->allow_add(1);
278     $public_shelf->allow_delete_own(1);
279     $public_shelf->allow_delete_other(1);
280     $public_shelf->store;
281
282     is( $public_shelf->can_be_viewed( $patron1->{borrowernumber} ), 1, 'The owner should be able to view his public list' );
283     is( $public_shelf->can_be_viewed( $patron2->{borrowernumber} ), 1, 'Public list should be viewed by someone else' );
284
285     is( $public_shelf->can_be_deleted( $patron1->{borrowernumber} ), 1, 'The owner should be able to delete his list' );
286     is( $public_shelf->can_be_deleted( $patron2->{borrowernumber} ), 0, 'Public list should not be deleted by someone else' );
287
288     is( $public_shelf->can_be_managed( $patron1->{borrowernumber} ), 1, 'The owner should be able to manage his list' );
289     is( $public_shelf->can_be_managed( $patron2->{borrowernumber} ), 0, 'Public list should not be managed by someone else' );
290
291     is( $public_shelf->can_biblios_be_added( $patron1->{borrowernumber} ), 1, 'The owner should be able to add biblios to his list' );
292     is( $public_shelf->can_biblios_be_added( $patron2->{borrowernumber} ), 1, 'Public list should not be modified (add) by someone else' );
293
294     is( $public_shelf->can_biblios_be_removed( $patron1->{borrowernumber} ), 1, 'The owner should be able to remove biblios to his list' );
295     is( $public_shelf->can_biblios_be_removed( $patron2->{borrowernumber} ), 1, 'Public list should not be modified (remove) by someone else' );
296
297
298     my $private_shelf = Koha::Virtualshelf->new(
299         {   shelfname    => "my first shelf",
300             owner        => $patron1->{borrowernumber},
301             category     => 1,
302             allow_add          => 0,
303             allow_delete_own   => 0,
304             allow_delete_other => 0,
305         }
306     )->store;
307
308     is( $private_shelf->can_be_viewed( $patron1->{borrowernumber} ), 1, 'The owner should be able to view his list' );
309     is( $private_shelf->can_be_viewed( $patron2->{borrowernumber} ), 0, 'Private list should not be viewed by someone else' );
310
311     is( $private_shelf->can_be_deleted( $patron1->{borrowernumber} ), 1, 'The owner should be able to delete his list' );
312     is( $private_shelf->can_be_deleted( $patron2->{borrowernumber} ), 0, 'Private list should not be deleted by someone else' );
313
314     is( $private_shelf->can_be_managed( $patron1->{borrowernumber} ), 1, 'The owner should be able to manage his list' );
315     is( $private_shelf->can_be_managed( $patron2->{borrowernumber} ), 0, 'Private list should not be managed by someone else' );
316
317     is( $private_shelf->can_biblios_be_added( $patron1->{borrowernumber} ), 1, 'The owner should be able to add biblios to his list' );
318     is( $private_shelf->can_biblios_be_added( $patron2->{borrowernumber} ), 0, 'Private list should not be modified (add) by someone else' );
319
320     is( $private_shelf->can_biblios_be_removed( $patron1->{borrowernumber} ), 1, 'The owner should be able to remove biblios to his list' );
321     is( $private_shelf->can_biblios_be_removed( $patron2->{borrowernumber} ), 0, 'Private list should not be modified (remove) by someone else' );
322
323
324     $private_shelf->allow_add(1);
325     $private_shelf->allow_delete_own(1);
326     $private_shelf->allow_delete_other(1);
327     $private_shelf->store;
328
329     is( $private_shelf->can_be_viewed( $patron1->{borrowernumber} ), 1, 'The owner should be able to view his list' );
330     is( $private_shelf->can_be_viewed( $patron2->{borrowernumber} ), 0, 'Private list should not be viewed by someone else' );
331
332     is( $private_shelf->can_be_deleted( $patron1->{borrowernumber} ), 1, 'The owner should be able to delete his list' );
333     is( $private_shelf->can_be_deleted( $patron2->{borrowernumber} ), 0, 'Private list should not be deleted by someone else' );
334
335     is( $private_shelf->can_be_managed( $patron1->{borrowernumber} ), 1, 'The owner should be able to manage his list' );
336     is( $private_shelf->can_be_managed( $patron2->{borrowernumber} ), 0, 'Private list should not be managed by someone else' );
337
338     is( $private_shelf->can_biblios_be_added( $patron1->{borrowernumber} ), 1, 'The owner should be able to add biblios to his list' );
339     is( $private_shelf->can_biblios_be_added( $patron2->{borrowernumber} ), 1, 'Private list could be modified (add) by someone else # individual check done later' );
340
341     is( $private_shelf->can_biblios_be_removed( $patron1->{borrowernumber} ), 1, 'The owner should be able to remove biblios to his list' );
342     is( $private_shelf->can_biblios_be_removed( $patron2->{borrowernumber} ), 1, 'Private list could be modified (remove) by someone else # individual check done later' );
343 };