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