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