Line 0
Link Here
|
0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
|
3 |
use Modern::Perl; |
4 |
use Test::More tests => 31; |
5 |
use C4::Context; |
6 |
|
7 |
BEGIN { |
8 |
use_ok('C4::Branch'); |
9 |
} |
10 |
|
11 |
can_ok( |
12 |
'C4::Branch', qw( |
13 |
GetBranchCategory |
14 |
GetBranchName |
15 |
GetBranch |
16 |
GetBranches |
17 |
GetBranchesLoop |
18 |
GetBranchDetail |
19 |
get_branchinfos_of |
20 |
ModBranch |
21 |
CheckBranchCategorycode |
22 |
GetBranchInfo |
23 |
GetCategoryTypes |
24 |
GetBranchCategories |
25 |
GetBranchesInCategory |
26 |
ModBranchCategoryInfo |
27 |
DelBranch |
28 |
DelBranchCategory |
29 |
CheckCategoryUnique |
30 |
mybranch |
31 |
GetBranchesCount) |
32 |
); |
33 |
|
34 |
# Start transaction |
35 |
my $dbh = C4::Context->dbh; |
36 |
$dbh->{RaiseError} = 1; |
37 |
$dbh->{AutoCommit} = 0; |
38 |
|
39 |
# Start test |
40 |
|
41 |
my $count = GetBranchesCount; |
42 |
like( $count, '/^\d+$/', "the count is a number" ); |
43 |
|
44 |
#add 2 branches |
45 |
my $b1 = { |
46 |
add => 1, |
47 |
branchcode => 'BRA', |
48 |
branchname => 'BranchA', |
49 |
branchaddress1 => 'adr1A', |
50 |
branchaddress2 => 'adr2A', |
51 |
branchaddress3 => 'adr3A', |
52 |
branchzip => 'zipA', |
53 |
branchcity => 'cityA', |
54 |
branchstate => 'stateA', |
55 |
branchcountry => 'countryA', |
56 |
branchphone => 'phoneA', |
57 |
branchfax => 'faxA', |
58 |
branchemail => 'emailA', |
59 |
branchurl => 'urlA', |
60 |
branchip => 'ipA', |
61 |
branchprinter => 'printerA', |
62 |
branchnotes => 'noteA', |
63 |
opac_info => 'opacA' |
64 |
}; |
65 |
my $b2 = { |
66 |
branchcode => 'BRB', |
67 |
branchname => 'BranchB', |
68 |
branchaddress1 => 'adr1B', |
69 |
branchaddress2 => 'adr2B', |
70 |
branchaddress3 => 'adr3B', |
71 |
branchzip => 'zipB', |
72 |
branchcity => 'cityB', |
73 |
branchstate => 'stateB', |
74 |
branchcountry => 'countryB', |
75 |
branchphone => 'phoneB', |
76 |
branchfax => 'faxB', |
77 |
branchemail => 'emailB', |
78 |
branchurl => 'urlB', |
79 |
branchip => 'ipB', |
80 |
branchprinter => 'printerB', |
81 |
branchnotes => 'noteB', |
82 |
opac_info => 'opacB', |
83 |
}; |
84 |
ModBranch($b1); |
85 |
is( ModBranch($b2), undef, 'the field add is missing' ); |
86 |
|
87 |
$b2->{add} = 1; |
88 |
ModBranch($b2); |
89 |
is( GetBranchesCount, $count + 2, "two branches added" ); |
90 |
|
91 |
#Test DelBranch |
92 |
|
93 |
is( DelBranch( $b2->{branchcode} ), 1, "One row affected" ); |
94 |
is( GetBranchesCount, $count + 1, "branch BRB deleted" ); |
95 |
|
96 |
#Test GetBranchName |
97 |
is( GetBranchName( $b1->{branchcode} ), |
98 |
$b1->{branchname}, "GetBranchName returns the right name" ); |
99 |
|
100 |
#Test GetBranchDetail |
101 |
my $branchdetail = GetBranchDetail( $b1->{branchcode} ); |
102 |
$branchdetail->{add} = 1; |
103 |
$b1->{issuing} = undef; # Not used in DB |
104 |
is_deeply( $branchdetail, $b1, 'branchdetail is right' ); |
105 |
|
106 |
#Test Getbranches |
107 |
my $branches = GetBranches; |
108 |
is( scalar( keys %$branches ), |
109 |
GetBranchesCount, "GetBranches returns the right number of branches" ); |
110 |
|
111 |
#Test ModBranch |
112 |
|
113 |
$b1 = { |
114 |
branchcode => 'BRA', |
115 |
branchname => 'BranchA modified', |
116 |
branchaddress1 => 'adr1A modified', |
117 |
branchaddress2 => 'adr2A modified', |
118 |
branchaddress3 => 'adr3A modified', |
119 |
branchzip => 'zipA modified', |
120 |
branchcity => 'cityA modified', |
121 |
branchstate => 'stateA modified', |
122 |
branchcountry => 'countryA modified', |
123 |
branchphone => 'phoneA modified', |
124 |
branchfax => 'faxA modified', |
125 |
branchemail => 'emailA modified', |
126 |
branchurl => 'urlA modified', |
127 |
branchip => 'ipA modified', |
128 |
branchprinter => 'printerA modified', |
129 |
branchnotes => 'notesA modified', |
130 |
opac_info => 'opacA modified' |
131 |
}; |
132 |
|
133 |
ModBranch($b1); |
134 |
is( GetBranchesCount, $count + 1, |
135 |
"A branch has been modified, no new branch added" ); |
136 |
$branchdetail = GetBranchDetail( $b1->{branchcode} ); |
137 |
$b1->{issuing} = undef; |
138 |
is_deeply( $branchdetail, $b1 ); |
139 |
|
140 |
#Test categories |
141 |
my $categories = GetBranchCategories; |
142 |
my $count_cat = scalar( keys $categories ); |
143 |
|
144 |
my $cat1 = { |
145 |
add => 1, |
146 |
categorycode => 'CAT1', |
147 |
categoryname => 'catname1', |
148 |
codedescription => 'catdesc1', |
149 |
categorytype => 'cattype1', |
150 |
show_in_pulldown => 1 |
151 |
}; |
152 |
my $cat2 = { |
153 |
add => 1, |
154 |
categorycode => 'CAT2', |
155 |
categoryname => 'catname2', |
156 |
categorytype => 'catype2', |
157 |
codedescription => 'catdesc2', |
158 |
show_in_pulldown => 1 |
159 |
}; |
160 |
|
161 |
ModBranchCategoryInfo($cat1); |
162 |
ModBranchCategoryInfo($cat2); |
163 |
|
164 |
$categories = GetBranchCategories; |
165 |
is( scalar( keys $categories ), $count_cat + 2, "Two categories added" ); |
166 |
|
167 |
#test GetBranchCategory |
168 |
my $cat1detail = GetBranchCategory( $cat1->{categorycode} ); |
169 |
delete $cat1->{add}; |
170 |
is_deeply( $cat1detail, $cat1, 'CAT1 details are right' ); |
171 |
|
172 |
#Test DelBranchCategory |
173 |
my $del = DelBranchCategory( $cat2->{categorycode} ); |
174 |
is( $del, 1, 'One row affected' ); |
175 |
$categories = GetBranchCategories; |
176 |
is( scalar( keys $categories ), $count_cat + 1, "Category CAT2 deleted" ); |
177 |
|
178 |
my $cat2detail = GetBranchCategory( $cat2->{categorycode} ); |
179 |
is( $cat2detail, undef, 'CAT2 doesnt exist' ); |
180 |
|
181 |
#Test CheckBranchCategoryCode |
182 |
my $check1 = CheckBranchCategorycode( $cat1->{categorycode} ); |
183 |
my $check2 = CheckBranchCategorycode( $cat2->{categorycode} ); |
184 |
like( $check1, '/^\d+$/', "CheckBranchCategorycode returns a number" ); |
185 |
|
186 |
$b2->{CAT1} = 1; |
187 |
ModBranch($b2); |
188 |
is( GetBranchesCount, $count + 2, 'BRB added' ); |
189 |
is( |
190 |
CheckBranchCategorycode( $cat1->{categorycode} ), |
191 |
$check1 + 1, |
192 |
'BRB added to CAT1' |
193 |
); |
194 |
|
195 |
#Test GetBranchInfo |
196 |
my $b1info = GetBranchInfo( $b1->{branchcode} ); |
197 |
$b1->{categories} = []; |
198 |
is_deeply( @$b1info[0], $b1, 'BRA has no categories' ); |
199 |
|
200 |
my $b2info = GetBranchInfo( $b2->{branchcode} ); |
201 |
my @cat = ( $cat1->{categorycode} ); |
202 |
delete $b2->{add}; |
203 |
delete $b2->{CAT1}; |
204 |
$b2->{issuing} = undef; |
205 |
$b2->{categories} = \@cat; |
206 |
is_deeply( @$b2info[0], $b2, 'BRB has the category CAT1' ); |
207 |
|
208 |
ModBranchCategoryInfo($cat2); |
209 |
$b2 = { |
210 |
branchcode => 'BRB', |
211 |
branchname => 'BranchB', |
212 |
branchaddress1 => 'adr1B', |
213 |
branchaddress2 => 'adr2B', |
214 |
branchaddress3 => 'adr3B', |
215 |
branchzip => 'zipB', |
216 |
branchcity => 'cityB', |
217 |
branchstate => 'stateB', |
218 |
branchcountry => 'countryB', |
219 |
branchphone => 'phoneB', |
220 |
branchfax => 'faxB', |
221 |
branchemail => 'emailB', |
222 |
branchurl => 'urlB', |
223 |
branchip => 'ipB', |
224 |
branchprinter => 'printerB', |
225 |
branchnotes => 'noteB', |
226 |
opac_info => 'opacB', |
227 |
CAT1 => 1, |
228 |
CAT2 => 1 |
229 |
}; |
230 |
ModBranch($b2); |
231 |
$b2info = GetBranchInfo( $b2->{branchcode} ); |
232 |
is( |
233 |
CheckBranchCategorycode( $cat2->{categorycode} ), |
234 |
$check2 + 1, |
235 |
'BRB added to CAT2' |
236 |
); |
237 |
push( @cat, $cat2->{categorycode} ); |
238 |
delete $b2->{CAT1}; |
239 |
delete $b2->{CAT2}; |
240 |
$b2->{issuing} = undef; |
241 |
$b2->{categories} = \@cat; |
242 |
is_deeply( @$b2info[0], $b2, 'BRB has the category CAT1 and CAT2' ); |
243 |
|
244 |
#Test GetBranchesInCategory |
245 |
my $brCat1 = GetBranchesInCategory( $cat1->{categorycode} ); |
246 |
my @b = ( $b2->{branchcode} ); |
247 |
is_deeply( $brCat1, \@b, 'CAT1 has branch BRB' ); |
248 |
|
249 |
my $b3 = { |
250 |
add => 1, |
251 |
branchcode => 'BRC', |
252 |
branchname => 'BranchC', |
253 |
branchaddress1 => 'adr1C', |
254 |
branchaddress2 => 'adr2C', |
255 |
branchaddress3 => 'adr3C', |
256 |
branchzip => 'zipC', |
257 |
branchcity => 'cityC', |
258 |
branchstate => 'stateC', |
259 |
branchcountry => 'countryC', |
260 |
branchphone => 'phoneC', |
261 |
branchfax => 'faxC', |
262 |
branchemail => 'emailC', |
263 |
branchurl => 'urlC', |
264 |
branchip => 'ipC', |
265 |
branchprinter => 'printerC', |
266 |
branchnotes => 'noteC', |
267 |
opac_info => 'opacC', |
268 |
CAT1 => 1, |
269 |
CAT2 => 1 |
270 |
}; |
271 |
ModBranch($b3); |
272 |
$brCat1 = GetBranchesInCategory( $cat1->{categorycode} ); |
273 |
push( @b, $b3->{branchcode} ); |
274 |
is_deeply( $brCat1, \@b, 'CAT1 has branch BRB and BRC' ); |
275 |
is( |
276 |
CheckBranchCategorycode( $cat1->{categorycode} ), |
277 |
$check1 + 2, |
278 |
'BRC has been added to CAT1' |
279 |
); |
280 |
|
281 |
#Test CheckCategoryUnique |
282 |
is( CheckCategoryUnique('CAT2'), 0, 'CAT2 exists' ); |
283 |
is( CheckCategoryUnique('CAT_NO_EXISTS'), 1, 'CAT_NO_EXISTS doesnt exist' ); |
284 |
|
285 |
#Test GetCategoryTypes |
286 |
my @Types = GetCategoryTypes; |
287 |
is_deeply( |
288 |
\@Types, |
289 |
[ 'searchdomain', 'properties' ], |
290 |
"the type is properties and searchdomain" |
291 |
); |
292 |
|
293 |
#TODO later: test mybranchine and onlymine |
294 |
# Actually we cannot mock C4::Context->userenv in unit tests |
295 |
|
296 |
#Test GetBranchesLoop |
297 |
my $loop = GetBranchesLoop; |
298 |
is( scalar(@$loop), GetBranchesCount, 'There is the right number of branches' ); |
299 |
|
300 |
# End transaction |
301 |
$dbh->rollback; |
302 |
|