Line 0
Link Here
|
0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
|
3 |
# This file is part of Koha. |
4 |
# |
5 |
# Copyright (C) 2013 Mark Tompsett |
6 |
# |
7 |
# This Koha test module is intended for Patron Category |
8 |
# tweaks to the OpacHiddenItems system preference. |
9 |
# Related to Bug 10859 |
10 |
# |
11 |
# Koha is free software; you can redistribute it and/or modify it |
12 |
# under the terms of the GNU General Public License as published by |
13 |
# the Free Software Foundation; either version 3 of the License, or |
14 |
# (at your option) any later version. |
15 |
# |
16 |
# Koha is distributed in the hope that it will be useful, but |
17 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
18 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
19 |
# GNU General Public License for more details. |
20 |
# |
21 |
# You should have received a copy of the GNU General Public License |
22 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
23 |
# |
24 |
|
25 |
use Modern::Perl; |
26 |
use MARC::Record; |
27 |
use Carp; |
28 |
use C4::Biblio; |
29 |
use C4::Context; |
30 |
use C4::Members; |
31 |
use Test::More tests => 22; |
32 |
|
33 |
BEGIN { |
34 |
use_ok('C4::Items'); |
35 |
} |
36 |
|
37 |
my $dbh = C4::Context->dbh; |
38 |
# Start transaction |
39 |
$dbh->{AutoCommit} = 0; |
40 |
$dbh->{RaiseError} = 1; |
41 |
|
42 |
# Helper biblio. |
43 |
my ($bibnum, $bibitemnum) = get_biblio(); |
44 |
|
45 |
# Add dummy branches. |
46 |
$dbh->do('INSERT INTO branches (branchcode,branchname) VALUES ("LIB1","LIB1");'); |
47 |
$dbh->do('INSERT INTO branches (branchcode,branchname) VALUES ("LIB2","LIB2");'); |
48 |
|
49 |
# Add dummy patron categories |
50 |
my $sql = "SELECT categorycode FROM categories WHERE categorycode='PT';"; |
51 |
my $sth = $dbh->prepare($sql); |
52 |
$sth->execute(); |
53 |
my $categorycode = $sth->fetchrow; |
54 |
if (!$categorycode) { |
55 |
$dbh->do("INSERT INTO categories (categorycode, description) VALUES ('PT','Generic Patron Code');"); |
56 |
} |
57 |
|
58 |
$sql = "SELECT categorycode FROM categories WHERE categorycode='SS';"; |
59 |
$sth = $dbh->prepare($sql); |
60 |
$sth->execute(); |
61 |
$categorycode = $sth->fetchrow; |
62 |
if (!$categorycode) { |
63 |
$dbh->do("INSERT INTO categories (categorycode, description) VALUES ('SS','Sample Staff Code');"); |
64 |
} |
65 |
|
66 |
$sql = "SELECT categorycode FROM categories WHERE categorycode='SPT';"; |
67 |
$sth = $dbh->prepare($sql); |
68 |
$sth->execute(); |
69 |
$categorycode = $sth->fetchrow; |
70 |
if (!$categorycode) { |
71 |
$dbh->do("INSERT INTO categories (categorycode, description) VALUES ('SPT','Sample Patron Code');"); |
72 |
} |
73 |
|
74 |
# Add same item for two branches. |
75 |
my ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => 'LIB1', holdingbranch => 'LIB1' } , $bibnum); |
76 |
cmp_ok($item_bibnum, '==', $bibnum, "LIB1 Item linked to correct biblionumber."); |
77 |
cmp_ok($item_bibitemnum, '==', $bibitemnum, "LIB1 Item linked to correct biblioitemnumber."); |
78 |
($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => 'LIB2', holdingbranch => 'LIB2' } , $bibnum); |
79 |
cmp_ok($item_bibnum, '==', $bibnum, "LIB2 Item linked to correct biblionumber."); |
80 |
cmp_ok($item_bibitemnum, '==', $bibitemnum, "LIB2 Item linked to correct biblioitemnumber."); |
81 |
|
82 |
# Add dummy patron. |
83 |
C4::Context->set_preference('PatronSelfRegistrationDefaultCategory','PT'); |
84 |
my %borrower; |
85 |
$borrower{'surname'} = 'Borrower'; |
86 |
$borrower{'firstname'} = 'Sample'; |
87 |
$borrower{'address'} = '123 Bogus Street'; |
88 |
$borrower{'city'} = 'Bogusville'; |
89 |
$borrower{'branchcode'} = 'LIB1'; |
90 |
$borrower{'categorycode'} = 'PT'; |
91 |
$borrower{'zipcode'} = 'H0H 0H0'; |
92 |
$borrower{'userid'} = 'Sample.Borrower'; |
93 |
$borrower{'email'} = 'santa@northpole.com'; |
94 |
$borrower{'phone'} = '967-1111'; |
95 |
my ( $borrowernumber, $password ) = C4::Members::AddMember_Opac(%borrower) or |
96 |
croak "AddMember_Opac failed.\n"; |
97 |
print STDERR "CHECK: $borrowernumber\n$password\n"; |
98 |
|
99 |
# Get the member's card number. |
100 |
$sql = "SELECT cardnumber FROM borrowers WHERE borrowernumber=?;"; |
101 |
$sth = $dbh->prepare($sql); |
102 |
$sth->execute($borrowernumber); |
103 |
my $cardnumber = $sth->fetchrow; |
104 |
|
105 |
# Get an array of hashes of Item information. |
106 |
my @all_items = C4::Items::GetItemsInfo($bibnum); |
107 |
|
108 |
# Hide everything for this record. |
109 |
C4::Context->set_preference('OpacHiddenItems',"homebranch: [LIB1,LIB2]\n"); |
110 |
|
111 |
# Check what's hidden when a user isn't logged in. |
112 |
my @items2hide = C4::Items::GetHiddenItemnumbers(@all_items); |
113 |
cmp_ok( scalar @items2hide, '==', 2, "OpacHiddenItems hid everything correctly."); |
114 |
|
115 |
# Set up dummy patron as if logged in. |
116 |
my @USERENV = ( |
117 |
$borrowernumber, |
118 |
'Sample.Borrower', |
119 |
$cardnumber, |
120 |
'Sample', |
121 |
'Borrower', |
122 |
'LIB1', |
123 |
'LIB1', |
124 |
0, |
125 |
); |
126 |
C4::Context->_new_userenv('DUMMY_SESSION_ID'); |
127 |
C4::Context->set_userenv( @USERENV ); |
128 |
|
129 |
# 4 blocks of 4 cases: 'SPT','SS','','SS|SPT' |
130 |
C4::Context->set_preference('PatronSingleBranch','SPT'); |
131 |
C4::Context->set_preference('PatronEveryBranch','SPT'); |
132 |
$sql = "UPDATE borrowers SET categorycode='SPT' WHERE borrowernumber=?;"; |
133 |
$sth = $dbh->prepare($sql); |
134 |
$sth->execute($borrowernumber); |
135 |
|
136 |
@items2hide = C4::Items::GetHiddenItemnumbers(@all_items); |
137 |
cmp_ok( scalar @items2hide, '==', 0, "OpacHiddenItems unhid everything correctly. (SPT,SPT)"); |
138 |
|
139 |
# PatronSingleBranch toggle |
140 |
C4::Context->set_preference('PatronSingleBranch','SPT'); |
141 |
C4::Context->set_preference('PatronEveryBranch','SS'); |
142 |
@items2hide = C4::Items::GetHiddenItemnumbers(@all_items); |
143 |
cmp_ok( scalar @items2hide, '==', 1, "OpacHiddenItems hid LIB2 correctly. (SPT,SS)"); |
144 |
|
145 |
# PatronSingleBranch toggle |
146 |
C4::Context->set_preference('PatronSingleBranch','SPT'); |
147 |
C4::Context->set_preference('PatronEveryBranch',''); |
148 |
@items2hide = C4::Items::GetHiddenItemnumbers(@all_items); |
149 |
cmp_ok( scalar @items2hide, '==', 1, "OpacHiddenItems hid LIB2 correctly. (SPT,'')"); |
150 |
|
151 |
# PatronSingleBranch toggle |
152 |
C4::Context->set_preference('PatronSingleBranch','SPT'); |
153 |
C4::Context->set_preference('PatronEveryBranch','SS|SPT'); |
154 |
@items2hide = C4::Items::GetHiddenItemnumbers(@all_items); |
155 |
cmp_ok( scalar @items2hide, '==', 0, "OpacHiddenItems unhid everything correctly. (SPT,SS|SPT)"); |
156 |
|
157 |
# PatronSingleBranch toggle |
158 |
C4::Context->set_preference('PatronSingleBranch','SS'); |
159 |
C4::Context->set_preference('PatronEveryBranch','SPT'); |
160 |
@items2hide = C4::Items::GetHiddenItemnumbers(@all_items); |
161 |
cmp_ok( scalar @items2hide, '==', 0, "OpacHiddenItems unhid everything correctly. (SS,SPT)"); |
162 |
|
163 |
# PatronSingleBranch toggle |
164 |
C4::Context->set_preference('PatronSingleBranch','SS'); |
165 |
C4::Context->set_preference('PatronEveryBranch','SS'); |
166 |
@items2hide = C4::Items::GetHiddenItemnumbers(@all_items); |
167 |
cmp_ok( scalar @items2hide, '==', 2, "OpacHiddenItems hid everything correctly. (SS,SS)"); |
168 |
|
169 |
# PatronSingleBranch toggle |
170 |
C4::Context->set_preference('PatronSingleBranch','SS'); |
171 |
C4::Context->set_preference('PatronEveryBranch',''); |
172 |
@items2hide = C4::Items::GetHiddenItemnumbers(@all_items); |
173 |
cmp_ok( scalar @items2hide, '==', 2, "OpacHiddenItems hid everything correctly. (SS,'')"); |
174 |
|
175 |
# PatronSingleBranch toggle |
176 |
C4::Context->set_preference('PatronSingleBranch','SS'); |
177 |
C4::Context->set_preference('PatronEveryBranch','SS|SPT'); |
178 |
@items2hide = C4::Items::GetHiddenItemnumbers(@all_items); |
179 |
cmp_ok( scalar @items2hide, '==', 0, "OpacHiddenItems unhid everything correctly. (SS,SS|SPT)"); |
180 |
|
181 |
# PatronSingleBranch toggle |
182 |
C4::Context->set_preference('PatronSingleBranch',''); |
183 |
C4::Context->set_preference('PatronEveryBranch','SPT'); |
184 |
@items2hide = C4::Items::GetHiddenItemnumbers(@all_items); |
185 |
cmp_ok( scalar @items2hide, '==', 0, "OpacHiddenItems unhid everything correctly. ('',SPT)"); |
186 |
|
187 |
# PatronSingleBranch toggle |
188 |
C4::Context->set_preference('PatronSingleBranch',''); |
189 |
C4::Context->set_preference('PatronEveryBranch','SS'); |
190 |
@items2hide = C4::Items::GetHiddenItemnumbers(@all_items); |
191 |
cmp_ok( scalar @items2hide, '==', 2, "OpacHiddenItems hid everything correctly. ('',SS)"); |
192 |
|
193 |
# PatronSingleBranch toggle |
194 |
C4::Context->set_preference('PatronSingleBranch',''); |
195 |
C4::Context->set_preference('PatronEveryBranch',''); |
196 |
@items2hide = C4::Items::GetHiddenItemnumbers(@all_items); |
197 |
cmp_ok( scalar @items2hide, '==', 2, "OpacHiddenItems hid everything correctly. ('','')"); |
198 |
|
199 |
# PatronSingleBranch toggle |
200 |
C4::Context->set_preference('PatronSingleBranch',''); |
201 |
C4::Context->set_preference('PatronEveryBranch','SS|SPT'); |
202 |
@items2hide = C4::Items::GetHiddenItemnumbers(@all_items); |
203 |
cmp_ok( scalar @items2hide, '==', 0, "OpacHiddenItems unhid everything correctly. ('',SS|SPT)"); |
204 |
|
205 |
# PatronSingleBranch toggle |
206 |
C4::Context->set_preference('PatronSingleBranch','SS|SPT'); |
207 |
C4::Context->set_preference('PatronEveryBranch','SPT'); |
208 |
@items2hide = C4::Items::GetHiddenItemnumbers(@all_items); |
209 |
cmp_ok( scalar @items2hide, '==', 0, "OpacHiddenItems unhid everything correctly. (SS|SPT,SPT)"); |
210 |
|
211 |
# PatronSingleBranch toggle |
212 |
C4::Context->set_preference('PatronSingleBranch','SS|SPT'); |
213 |
C4::Context->set_preference('PatronEveryBranch','SS'); |
214 |
@items2hide = C4::Items::GetHiddenItemnumbers(@all_items); |
215 |
cmp_ok( scalar @items2hide, '==', 1, "OpacHiddenItems hid LIB2 correctly. (SS|SPT,SS)"); |
216 |
|
217 |
# PatronSingleBranch toggle |
218 |
C4::Context->set_preference('PatronSingleBranch','SS|SPT'); |
219 |
C4::Context->set_preference('PatronEveryBranch',''); |
220 |
@items2hide = C4::Items::GetHiddenItemnumbers(@all_items); |
221 |
cmp_ok( scalar @items2hide, '==', 1, "OpacHiddenItems hid LIB2 correctly. (SS|SPT,'')"); |
222 |
|
223 |
# PatronSingleBranch toggle |
224 |
C4::Context->set_preference('PatronSingleBranch','SS|SPT'); |
225 |
C4::Context->set_preference('PatronEveryBranch','SS|SPT'); |
226 |
@items2hide = C4::Items::GetHiddenItemnumbers(@all_items); |
227 |
cmp_ok( scalar @items2hide, '==', 0, "OpacHiddenItems unhid everything correctly. (SS|SPT,SS|SPT)"); |
228 |
|
229 |
# Nothing wrong with being explicit. :) |
230 |
$dbh->rollback(); |
231 |
|
232 |
# Helper method to set up a Biblio. |
233 |
sub get_biblio { |
234 |
my $bib = MARC::Record->new(); |
235 |
$bib->append_fields( |
236 |
MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'), |
237 |
MARC::Field->new('245', ' ', ' ', a => 'Silence in the library'), |
238 |
); |
239 |
return ($bibnum, $bibitemnum) = AddBiblio($bib, ''); |
240 |
} |