Lines 1-395
Link Here
|
1 |
#!/usr/bin/perl |
|
|
2 |
|
3 |
# Copyright 2015 BibLibre |
4 |
# |
5 |
# This file is part of Koha. |
6 |
# |
7 |
# Koha is free software; you can redistribute it and/or modify it under the |
8 |
# terms of the GNU General Public License as published by the Free Software |
9 |
# Foundation; either version 3 of the License, or (at your option) any later |
10 |
# version. |
11 |
# |
12 |
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY |
13 |
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
14 |
# A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
15 |
# |
16 |
# You should have received a copy of the GNU General Public License along |
17 |
# with Koha; if not, see <http://www.gnu.org/licenses>. |
18 |
|
19 |
use Modern::Perl; |
20 |
|
21 |
use Test::More tests => 117; |
22 |
use t::lib::TestBuilder; |
23 |
|
24 |
use Koha::Database; |
25 |
use Time::Piece; |
26 |
|
27 |
BEGIN { |
28 |
use_ok('C4::Biblio'); |
29 |
use_ok('C4::Review'); |
30 |
use_ok('Koha::Patron'); |
31 |
use_ok('MARC::Record'); |
32 |
} |
33 |
|
34 |
can_ok( |
35 |
'C4::Review', qw( |
36 |
getreview |
37 |
savereview |
38 |
updatereview |
39 |
numberofreviews |
40 |
numberofreviewsbybiblionumber |
41 |
getreviews |
42 |
getallreviews |
43 |
approvereview |
44 |
unapprovereview |
45 |
deletereview ) |
46 |
); |
47 |
|
48 |
my $schema = Koha::Database->new->schema; |
49 |
$schema->storage->txn_begin; |
50 |
our $dbh = C4::Context->dbh; |
51 |
|
52 |
$dbh->do('DELETE FROM reviews'); |
53 |
$dbh->do('DELETE FROM issues'); |
54 |
$dbh->do('DELETE FROM borrowers'); |
55 |
|
56 |
my $builder = t::lib::TestBuilder->new; |
57 |
|
58 |
# ---------- Some borrowers for testing ------------------- |
59 |
my $categorycode = $builder->build({ source => 'Category' })->{ categorycode }; |
60 |
my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode }; |
61 |
|
62 |
my $b1 = Koha::Patron->new( |
63 |
{ surname => 'Borrower 1', |
64 |
branchcode => $branchcode, |
65 |
categorycode => $categorycode |
66 |
} |
67 |
); |
68 |
$b1->store(); |
69 |
|
70 |
my $b2 = Koha::Patron->new( |
71 |
{ surname => 'Borrower 2', |
72 |
branchcode => $branchcode, |
73 |
categorycode => $categorycode |
74 |
} |
75 |
); |
76 |
$b2->store(); |
77 |
|
78 |
my $b3 = Koha::Patron->new( |
79 |
{ surname => 'Borrower 3', |
80 |
branchcode => $branchcode, |
81 |
categorycode => $categorycode |
82 |
} |
83 |
); |
84 |
$b3->store(); |
85 |
|
86 |
# ---------- Some biblios for testing ------------------- |
87 |
my ($biblionumber1) = AddBiblio( MARC::Record->new, '' ); |
88 |
my ($biblionumber2) = AddBiblio( MARC::Record->new, '' ); |
89 |
my ($biblionumber3) = AddBiblio( MARC::Record->new, '' ); |
90 |
|
91 |
# ---------- Some reviews for testing ------------------- |
92 |
my $rev1 = 'Review 1'; |
93 |
my $rev2 = 'Review 2'; |
94 |
my $rev3 = 'Review 3'; |
95 |
|
96 |
# ---------- Testing savereview --------------------------- |
97 |
my $date = Time::Piece::localtime->strftime('%F %T'); |
98 |
|
99 |
savereview( $biblionumber1, $b1->borrowernumber, $rev1 ); |
100 |
|
101 |
my $query = ' |
102 |
SELECT count(*) |
103 |
FROM reviews |
104 |
'; |
105 |
my $count = $dbh->selectrow_array($query); |
106 |
is( $count, 1, 'There is 1 review' ); |
107 |
|
108 |
$query = ' |
109 |
SELECT reviewid, borrowernumber, biblionumber, review, approved, datereviewed |
110 |
FROM reviews |
111 |
'; |
112 |
my ( $reviewid, $borrowernumber, $biblionumber, $review, $approved, $datereviewed ) = $dbh->selectrow_array($query); |
113 |
is( $borrowernumber, $b1->borrowernumber, 'borrowernumber field is good' ); |
114 |
is( $biblionumber, $biblionumber1, 'biblionumber field is good' ); |
115 |
is( $review, $rev1, 'review field is good' ); |
116 |
is( $approved, 0, 'approved field is 0 by default' ); |
117 |
is( $datereviewed, $date, 'datereviewed field is good' ); |
118 |
|
119 |
# We add some others reviews |
120 |
savereview( $biblionumber1, $b2->borrowernumber, $rev2 ); |
121 |
savereview( $biblionumber3, $b2->borrowernumber, $rev3 ); |
122 |
|
123 |
# ---------- Testing getreview ---------------------------- |
124 |
my $review1 = getreview( $biblionumber1, $b1->borrowernumber ); |
125 |
my $review2 = getreview( $biblionumber1, $b2->borrowernumber ); |
126 |
my $review3 = getreview( $biblionumber3, $b2->borrowernumber ); |
127 |
|
128 |
$query = ' |
129 |
SELECT count(*) |
130 |
FROM reviews |
131 |
'; |
132 |
$count = $dbh->selectrow_array($query); |
133 |
is( $count, 3, 'There are 3 reviews' ); |
134 |
|
135 |
isa_ok( $review1, 'HASH', '$review1 is defined as a hash' ); |
136 |
is( $review1->{borrowernumber}, $b1->borrowernumber, 'borrowernumber field is good' ); |
137 |
is( $review1->{biblionumber}, $biblionumber1, 'biblionumber field is good' ); |
138 |
is( $review1->{review}, $rev1, 'review field is good' ); |
139 |
is( $review1->{approved}, 0, 'approved field is 0 by default' ); |
140 |
cmp_ok( $review1->{datereviewed}, 'ge', $date, 'datereviewed field is good' ); |
141 |
|
142 |
isa_ok( $review2, 'HASH', '$review2 is defined as a hash' ); |
143 |
is( $review2->{borrowernumber}, $b2->borrowernumber, 'borrowernumber field is good' ); |
144 |
is( $review2->{biblionumber}, $biblionumber1, 'biblionumber field is good' ); |
145 |
is( $review2->{review}, $rev2, 'review field is good' ); |
146 |
is( $review2->{approved}, 0, 'approved field is 0 by default' ); |
147 |
cmp_ok( $review2->{datereviewed}, 'ge', $date, 'datereviewed field is good' ); |
148 |
|
149 |
isa_ok( $review3, 'HASH', '$review3 is defined as a hash' ); |
150 |
is( $review3->{borrowernumber}, $b2->borrowernumber, 'borrowernumber field is good' ); |
151 |
is( $review3->{biblionumber}, $biblionumber3, 'biblionumber field is good' ); |
152 |
is( $review3->{review}, $rev3, 'review field is good' ); |
153 |
is( $review3->{approved}, 0, 'approved field is 0 by default' ); |
154 |
cmp_ok( $review3->{datereviewed}, 'ge', $date, 'datereviewed field is good' ); |
155 |
|
156 |
# ---------- Testing getreviews --------------------------- |
157 |
my $status = 0; |
158 |
my $reviews = getreviews( $biblionumber1, $status ); |
159 |
|
160 |
$query = ' |
161 |
SELECT count(*) |
162 |
FROM reviews |
163 |
WHERE biblionumber = ? |
164 |
AND approved = ? |
165 |
'; |
166 |
$count = $dbh->selectrow_array( $query, {}, $biblionumber1, $status ); |
167 |
is( $count, 2, 'There are 2 reviews corresponding' ); |
168 |
|
169 |
isa_ok( $reviews, 'ARRAY', '$reviews is defined as an array' ); |
170 |
|
171 |
isa_ok( $reviews->[0], 'HASH', '$reviews->[0] is defined as a hash' ); |
172 |
is( $reviews->[0]->{reviewid}, $review1->{reviewid}, 'reviewid field is good' ); |
173 |
is( $reviews->[0]->{borrowernumber}, $review1->{borrowernumber}, 'borrowernumber field is good' ); |
174 |
is( $reviews->[0]->{biblionumber}, $review1->{biblionumber}, 'biblionumber field is good' ); |
175 |
is( $reviews->[0]->{review}, $review1->{review}, 'review field is good' ); |
176 |
is( $reviews->[0]->{approved}, $review1->{approved}, 'approved field is 0 by default' ); |
177 |
cmp_ok( $reviews->[0]->{datereviewed}, 'ge', $date, 'datereviewed field is good' ); |
178 |
|
179 |
isa_ok( $reviews->[1], 'HASH', '$reviews->[1] is defined as a hash' ); |
180 |
is( $reviews->[1]->{reviewid}, $review2->{reviewid}, 'reviewid field is good' ); |
181 |
is( $reviews->[1]->{borrowernumber}, $review2->{borrowernumber}, 'borrowernumber field is good' ); |
182 |
is( $reviews->[1]->{biblionumber}, $review2->{biblionumber}, 'biblionumber field is good' ); |
183 |
is( $reviews->[1]->{review}, $review2->{review}, 'review field is good' ); |
184 |
is( $reviews->[1]->{approved}, $review2->{approved}, 'approved field is 0 by default' ); |
185 |
cmp_ok( $reviews->[1]->{datereviewed}, 'ge', $date, 'datereviewed field is good' ); |
186 |
|
187 |
$status = 1; |
188 |
$reviews = getreviews( $biblionumber1, $status ); |
189 |
isa_ok( $reviews, 'ARRAY', '$reviews is defined as an array' ); |
190 |
is_deeply( $reviews, [], '$reviews is empty, there is no approved review' ); |
191 |
|
192 |
# ---------- Testing getallreviews ------------------------ |
193 |
$status = 1; |
194 |
$reviews = getallreviews($status); |
195 |
isa_ok( $reviews, 'ARRAY', '$reviews is defined as an array' ); |
196 |
is_deeply( $reviews, [], '$reviews is empty, there is no approved review' ); |
197 |
|
198 |
$status = 0; |
199 |
$reviews = getallreviews($status); |
200 |
|
201 |
$query = ' |
202 |
SELECT count(*) |
203 |
FROM reviews |
204 |
WHERE approved = ? |
205 |
'; |
206 |
$count = $dbh->selectrow_array( $query, {}, $status ); |
207 |
is( $count, 3, 'There are 3 reviews corresponding' ); |
208 |
|
209 |
my $count2 = numberofreviews($status); |
210 |
is( $count2, $count, 'number of reviews returned is good' ); |
211 |
|
212 |
isa_ok( $reviews, 'ARRAY', '$reviews is defined as an array' ); |
213 |
|
214 |
isa_ok( $reviews->[0], 'HASH', '$reviews->[0] is defined as a hash' ); |
215 |
is( $reviews->[0]->{reviewid}, $review1->{reviewid}, 'reviewid field is good' ); |
216 |
is( $reviews->[0]->{borrowernumber}, $review1->{borrowernumber}, 'borrowernumber field is good' ); |
217 |
is( $reviews->[0]->{biblionumber}, $review1->{biblionumber}, 'biblionumber field is good' ); |
218 |
is( $reviews->[0]->{review}, $review1->{review}, 'review field is good' ); |
219 |
is( $reviews->[0]->{approved}, $review1->{approved}, 'approved field is 0 by default' ); |
220 |
cmp_ok( $reviews->[0]->{datereviewed}, 'ge', $date, 'datereviewed field is good' ); |
221 |
|
222 |
isa_ok( $reviews->[1], 'HASH', '$reviews->[1] is defined as a hash' ); |
223 |
is( $reviews->[1]->{reviewid}, $review2->{reviewid}, 'reviewid field is good' ); |
224 |
is( $reviews->[1]->{borrowernumber}, $review2->{borrowernumber}, 'borrowernumber field is good' ); |
225 |
is( $reviews->[1]->{biblionumber}, $review2->{biblionumber}, 'biblionumber field is good' ); |
226 |
is( $reviews->[1]->{review}, $review2->{review}, 'review field is good' ); |
227 |
is( $reviews->[1]->{approved}, $review2->{approved}, 'approved field is 0 by default' ); |
228 |
cmp_ok( $reviews->[1]->{datereviewed}, 'ge', $date, 'datereviewed field is good' ); |
229 |
|
230 |
isa_ok( $reviews->[2], 'HASH', '$reviews->[2] is defined as a hash' ); |
231 |
is( $reviews->[2]->{reviewid}, $review3->{reviewid}, 'reviewid field is good' ); |
232 |
is( $reviews->[2]->{borrowernumber}, $review3->{borrowernumber}, 'borrowernumber field is good' ); |
233 |
is( $reviews->[2]->{biblionumber}, $review3->{biblionumber}, 'biblionumber field is good' ); |
234 |
is( $reviews->[2]->{review}, $review3->{review}, 'review field is good' ); |
235 |
is( $reviews->[2]->{approved}, $review3->{approved}, 'approved field is 0 by default' ); |
236 |
cmp_ok( $reviews->[2]->{datereviewed}, 'ge', $date, 'datereviewed field is good' ); |
237 |
|
238 |
my $offset = 1; |
239 |
my $row_count = 1; |
240 |
$reviews = getallreviews( $status, $offset ); |
241 |
is( @$reviews, 2, 'There are only 2 Reviews here' ); |
242 |
is_deeply( $reviews->[0], $review2, 'We have Review2...' ); |
243 |
is_deeply( $reviews->[1], $review3, '...and Review3' ); |
244 |
|
245 |
$reviews = getallreviews( $status, $offset, $row_count ); |
246 |
is( @$reviews, 1, 'There is only 1 Review here' ); |
247 |
is_deeply( $reviews->[0], $review2, 'We have only Review2' ); |
248 |
|
249 |
# ---------- Testing numberofreviews ---------------------- |
250 |
$status = 0; |
251 |
$count = numberofreviews($status); |
252 |
is( $count, 3, 'There are 3 reviews where approved = 0' ); |
253 |
|
254 |
$status = 1; |
255 |
$count = numberofreviews($status); |
256 |
is( $count, 0, 'There is no review where approved = 0' ); |
257 |
|
258 |
$count = numberofreviews(); |
259 |
is( $count, 0, 'There is no review where approved = 0 (Default)' ); |
260 |
|
261 |
# ---------- Testing approvereview ------------------------ |
262 |
is( $review1->{approved}, 0, 'review1 is not approved' ); |
263 |
approvereview( $review1->{reviewid} ); |
264 |
$review1 = getreview( $biblionumber1, $b1->borrowernumber ); |
265 |
is( $review1->{approved}, 1, 'review1 is approved' ); |
266 |
|
267 |
is( $review2->{approved}, 0, 'review2 is not approved' ); |
268 |
approvereview( $review2->{reviewid} ); |
269 |
$review2 = getreview( $biblionumber1, $b2->borrowernumber ); |
270 |
is( $review2->{approved}, 1, 'review2 is approved' ); |
271 |
|
272 |
is( $review3->{approved}, 0, 'review3 is not approved' ); |
273 |
approvereview( $review3->{reviewid} ); |
274 |
$review3 = getreview( $biblionumber3, $b2->borrowernumber ); |
275 |
is( $review3->{approved}, 1, 'review3 is approved' ); |
276 |
|
277 |
$status = 1; |
278 |
$reviews = getallreviews($status); |
279 |
|
280 |
$count = numberofreviews($status); |
281 |
is( $count, 3, '3 reviews are approved' ); |
282 |
|
283 |
$status = 0; |
284 |
$count = numberofreviews($status); |
285 |
is( $count, 0, 'No review are not approved' ); |
286 |
|
287 |
# ---------- Testing unapprovereview ---------------------- |
288 |
is( $review1->{approved}, 1, 'review1 is approved' ); |
289 |
unapprovereview( $review1->{reviewid} ); |
290 |
$review1 = getreview( $biblionumber1, $b1->borrowernumber ); |
291 |
is( $review1->{approved}, 0, 'review1 is not approved' ); |
292 |
|
293 |
is( $review2->{approved}, 1, 'review2 is approved' ); |
294 |
unapprovereview( $review2->{reviewid} ); |
295 |
$review2 = getreview( $biblionumber1, $b2->borrowernumber ); |
296 |
is( $review2->{approved}, 0, 'review2 is not approved' ); |
297 |
|
298 |
is( $review3->{approved}, 1, 'review3 is approved' ); |
299 |
unapprovereview( $review3->{reviewid} ); |
300 |
$review3 = getreview( $biblionumber3, $b2->borrowernumber ); |
301 |
is( $review3->{approved}, 0, 'review3 is not approved' ); |
302 |
|
303 |
$status = 0; |
304 |
$reviews = getallreviews($status); |
305 |
|
306 |
$count = numberofreviews($status); |
307 |
is( $count, 3, '3 reviews are not approved' ); |
308 |
|
309 |
$status = 1; |
310 |
$count = numberofreviews($status); |
311 |
is( $count, 0, 'No review are approved' ); |
312 |
|
313 |
# ---------- Testing numberofreviewsbybiblionumber -------- |
314 |
approvereview( $review1->{reviewid} ); |
315 |
approvereview( $review2->{reviewid} ); |
316 |
approvereview( $review3->{reviewid} ); |
317 |
|
318 |
$biblionumber = $biblionumber1; |
319 |
$count = numberofreviewsbybiblionumber($biblionumber); |
320 |
is( $count, 2, 'There are 2 reviews for biblionumber1 and approved = 1' ); |
321 |
|
322 |
$biblionumber = $biblionumber2; |
323 |
$count = numberofreviewsbybiblionumber($biblionumber); |
324 |
is( $count, 0, 'There is no review for biblionumber2 and approved = 1' ); |
325 |
|
326 |
$biblionumber = $biblionumber3; |
327 |
$count = numberofreviewsbybiblionumber($biblionumber); |
328 |
is( $count, 1, 'There 1 review for biblionumber3 and approved = 1' ); |
329 |
|
330 |
unapprovereview( $review1->{reviewid} ); |
331 |
unapprovereview( $review3->{reviewid} ); |
332 |
|
333 |
$biblionumber = $biblionumber1; |
334 |
$count = numberofreviewsbybiblionumber($biblionumber); |
335 |
is( $count, 1, 'There is 1 review for biblionumber1 and approved = 1' ); |
336 |
|
337 |
$biblionumber = $biblionumber2; |
338 |
$count = numberofreviewsbybiblionumber($biblionumber); |
339 |
is( $count, 0, 'There is no review for biblionumber2 and approved = 1' ); |
340 |
|
341 |
$biblionumber = $biblionumber3; |
342 |
$count = numberofreviewsbybiblionumber($biblionumber); |
343 |
is( $count, 0, 'There is no review for biblionumber3 and approved = 1' ); |
344 |
|
345 |
# ---------- Testing updatereview ------------------------- |
346 |
my $rev1b = 'Review 1 bis'; |
347 |
my $rev2b = 'Review 2 bis'; |
348 |
my $rev3b = 'Review 3 bis'; |
349 |
|
350 |
is( $review1->{review}, $rev1, 'review field is "Review 1"' ); |
351 |
updatereview( $biblionumber1, $b1->borrowernumber, $rev1b ); |
352 |
$review1 = getreview( $biblionumber1, $b1->borrowernumber ); |
353 |
is( $review1->{review}, $rev1b, 'review field is "Review 1 bis"' ); |
354 |
|
355 |
is( $review2->{review}, $rev2, 'review field is "Review 2"' ); |
356 |
updatereview( $biblionumber1, $b2->borrowernumber, $rev2b ); |
357 |
$review2 = getreview( $biblionumber1, $b2->borrowernumber ); |
358 |
is( $review2->{review}, $rev2b, 'review field is "Review 2 bis"' ); |
359 |
|
360 |
is( $review3->{review}, $rev3, 'review field is "Review 3"' ); |
361 |
updatereview( $biblionumber3, $b2->borrowernumber, $rev3b ); |
362 |
$review3 = getreview( $biblionumber3, $b2->borrowernumber ); |
363 |
is( $review3->{review}, $rev3b, 'review field is "Review 3 bis"' ); |
364 |
|
365 |
# ---------- Testing deletereview ------------------------- |
366 |
my $status0 = 0; |
367 |
my $status1 = 1; |
368 |
|
369 |
my $numberOfReviews = numberofreviews($status0) + numberofreviews($status1); |
370 |
is( $numberOfReviews, 3, 'There are 3 reviews in database' ); |
371 |
|
372 |
deletereview( $review1->{reviewid} ); |
373 |
$review1 = getreview( $biblionumber1, $b3->borrowernumber ); |
374 |
ok( !defined($review1), 'Review1 is no longer defined' ); |
375 |
|
376 |
$numberOfReviews = numberofreviews($status0) + numberofreviews($status1); |
377 |
is( $numberOfReviews, 2, 'There are 2 reviews left in database' ); |
378 |
|
379 |
deletereview( $review2->{reviewid} ); |
380 |
$review2 = getreview( $biblionumber2, $b2->borrowernumber ); |
381 |
ok( !defined($review2), 'Review2 is no longer defined' ); |
382 |
|
383 |
$numberOfReviews = numberofreviews($status0) + numberofreviews($status1); |
384 |
is( $numberOfReviews, 1, 'There is 1 review left in database' ); |
385 |
|
386 |
deletereview( $review3->{reviewid} ); |
387 |
$review3 = getreview( $biblionumber3, $b2->borrowernumber ); |
388 |
ok( !defined($review3), 'Review3 is no longer defined' ); |
389 |
|
390 |
$numberOfReviews = numberofreviews($status0) + numberofreviews($status1); |
391 |
is( $numberOfReviews, 0, 'There is no review left in database' ); |
392 |
|
393 |
$schema->storage->txn_rollback; |
394 |
|
395 |
1; |
396 |
- |