Line 0
Link Here
|
0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
use strict; |
3 |
use warnings; |
4 |
|
5 |
use C4::Context; |
6 |
use Test::More tests => 5; |
7 |
use Test::MockModule; |
8 |
use DBD::Mock; |
9 |
|
10 |
use_ok('Koha::Borrower::CheckPrevIssue'); |
11 |
|
12 |
use Koha::Borrower::CheckPrevIssue qw( WantsCheckPrevIssue CheckPrevIssue ); |
13 |
|
14 |
# Setup mock db |
15 |
my $module_context = new Test::MockModule('C4::Context'); |
16 |
$module_context->mock( |
17 |
'_new_dbh', |
18 |
sub { |
19 |
my $dbh = DBI->connect( 'DBI:Mock:', '', '' ) |
20 |
|| die "Cannot create handle: $DBI::errstr\n"; |
21 |
return $dbh; |
22 |
} |
23 |
); |
24 |
|
25 |
my $dbh = C4::Context->dbh(); |
26 |
|
27 |
# convenience variables |
28 |
my $name; |
29 |
# mock_add_resultset vars |
30 |
my ( $sql, $sql2, @bound_params, @keys, @values, %result ) = ( ); |
31 |
# mock_history_verification vars |
32 |
my ( $history, $params, $query ) = ( ); |
33 |
|
34 |
sub clean_vars { |
35 |
( $name, $sql, $sql2, @bound_params, @keys, @values, %result, |
36 |
$history, $params, $query ) = ( ); |
37 |
$dbh->{mock_clear_history} = 1; |
38 |
} |
39 |
|
40 |
# Tests |
41 |
## WantsCheckPrevIssue |
42 |
$name = 'WantsCheckPrevIssue'; |
43 |
$sql = ' |
44 |
SELECT categories.checkprevissue |
45 |
FROM borrowers |
46 |
LEFT JOIN categories ON borrowers.categorycode = categories.categorycode |
47 |
WHERE borrowers.borrowernumber = ? |
48 |
'; |
49 |
|
50 |
# Test if, as brw inherits, result is simply passed from |
51 |
# _WantsCheckPrevIssueByCat back through WantsCheckPrevIssue. |
52 |
my %brw = ( |
53 |
checkprevissue => 'inherit', |
54 |
borrowernumber => '101', |
55 |
); |
56 |
|
57 |
$dbh->{mock_add_resultset} = |
58 |
{ |
59 |
sql => $sql, |
60 |
bound_params => [ '101' ], |
61 |
results => [ [ 'categories.checkprevissue' ], [ 'pass_thru' ] ], |
62 |
}; |
63 |
|
64 |
is( WantsCheckPrevIssue(\%brw), 'pass_thru', |
65 |
$name . ": Return value \"pass_thru\"." ); |
66 |
|
67 |
# Test if, if brw does not inherit, WantsCheckPrevIssue simply returns |
68 |
# its contents. |
69 |
%brw = ( |
70 |
checkprevissue => 'brw_pass_thru', |
71 |
borrowernumber => '101', |
72 |
); |
73 |
|
74 |
is( WantsCheckPrevIssue(\%brw), 'brw_pass_thru', |
75 |
$name . ": Return value \"$brw{checkprevissue}\"." ); |
76 |
|
77 |
clean_vars(); |
78 |
|
79 |
## CheckPrevIssue |
80 |
$name = 'CheckPrevIssue'; |
81 |
$sql = 'select itemnumber from items where biblionumber=?'; |
82 |
$sql2 = ' |
83 |
select count(itemnumber) from old_issues |
84 |
where borrowernumber=? and itemnumber=? |
85 |
'; |
86 |
@keys = qw< borrowernumber biblionumber itemnumber >; |
87 |
@values = qw< 101 3576 5043 >; |
88 |
|
89 |
# 1) Prepop items with itemnumber for result |
90 |
$dbh->{mock_add_resultset} = { |
91 |
sql => $sql, |
92 |
bound_params => $keys[1], |
93 |
results => [ [ ( $keys[2] ) ], [ ( $values[2] ) ] ], |
94 |
}; |
95 |
# 2) Test if never issued before (expect 0) |
96 |
is( CheckPrevIssue( $keys[0], $keys[1] ), 0, |
97 |
$name . ': Return value "no matches".' ); |
98 |
# 3) Prepop old_issues with itemnumber and borrowernumber |
99 |
$dbh->{mock_add_resultset} = { |
100 |
sql => $sql2, |
101 |
bound_params => [ $keys[0], $keys[2] ], |
102 |
results => [ |
103 |
[ ( $keys[0], $keys[2] ) ], |
104 |
[ ( $values[0], $values[2] ) ], |
105 |
[ ( $values[0], $values[2] ) ], |
106 |
[ ( $values[0], $values[2] ) ], |
107 |
[ ( $values[0], $values[2] ) ], |
108 |
], |
109 |
}; |
110 |
# 4) Test if issued before (e.g. 7 times — expect 1) |
111 |
is( CheckPrevIssue( $keys[0], $keys[1] ), 1, |
112 |
$name . ': Return value "> 0 matches".' ); |
113 |
|
114 |
clean_vars(); |