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 C4::Context; |
21 |
use Data::Dumper; |
22 |
use Test::More tests => 145; |
23 |
use Test::Warn; |
24 |
use Test::MockModule; |
25 |
|
26 |
|
27 |
BEGIN { |
28 |
use_ok('C4::OAI::Sets'); |
29 |
use_ok('MARC::Record'); |
30 |
use_ok('C4::Biblio'); |
31 |
} |
32 |
can_ok( |
33 |
'C4::OAI::Sets', qw( |
34 |
GetOAISets |
35 |
GetOAISet |
36 |
GetOAISetBySpec |
37 |
ModOAISet |
38 |
DelOAISet |
39 |
AddOAISet |
40 |
GetOAISetsMappings |
41 |
GetOAISetMappings |
42 |
ModOAISetMappings |
43 |
GetOAISetsBiblio |
44 |
DelOAISetsBiblio |
45 |
CalcOAISetsBiblio |
46 |
ModOAISetsBiblios |
47 |
UpdateOAISetsBiblio |
48 |
AddOAISetsBiblios ) |
49 |
); |
50 |
|
51 |
|
52 |
my $dbh = C4::Context->dbh; |
53 |
$dbh->{AutoCommit} = 0; |
54 |
$dbh->{RaiseError} = 1; |
55 |
$dbh->do('DELETE FROM oai_sets'); |
56 |
$dbh->do('DELETE FROM oai_sets_descriptions'); |
57 |
$dbh->do('DELETE FROM oai_sets_mappings'); |
58 |
$dbh->do('DELETE FROM oai_sets_biblios'); |
59 |
|
60 |
|
61 |
# ---------- Testing AddOAISet ------------------ |
62 |
ok (!defined(AddOAISet), 'AddOAISet without argument is undef'); |
63 |
|
64 |
my $set_without_spec_and_name_and_desc = {}; |
65 |
ok (!defined(AddOAISet($set_without_spec_and_name_and_desc)), 'AddOAISet without "field", "name" and "descriptions" fields is undef'); |
66 |
|
67 |
my $set_without_spec_and_name = { |
68 |
'descriptions' => ['descNoSpecNoName'], |
69 |
}; |
70 |
ok (!defined(AddOAISet($set_without_spec_and_name)), 'AddOAISet without "field" and "name" fields is undef'); |
71 |
|
72 |
my $set_without_spec = { |
73 |
'name' => 'nameNoSpec', |
74 |
'descriptions' => ['descNoSpec'], |
75 |
}; |
76 |
ok (!defined(AddOAISet($set_without_spec)), 'AddOAISet without "field" field is undef'); |
77 |
|
78 |
my $set_without_name = { |
79 |
'spec' => 'specNoName', |
80 |
'descriptions' => ['descNoName'], |
81 |
}; |
82 |
ok (!defined(AddOAISet($set_without_name)), 'AddOAISet without "name" field is undef'); |
83 |
|
84 |
#Test to enter in the 'else' case of 'AddOAISet' line 280 |
85 |
{ |
86 |
my $dbi_st = Test::MockModule->new('DBI::st', no_auto => 1); # ref($sth) == 'DBI::st' |
87 |
$dbi_st->mock('execute', sub { return 0; }); |
88 |
|
89 |
my $setWrong = { |
90 |
'spec' => 'specWrong', |
91 |
'name' => 'nameWrong', |
92 |
}; |
93 |
warn '> AddOAISet should failed'; |
94 |
my $setWrong_id = AddOAISet($setWrong); |
95 |
|
96 |
DelOAISet($setWrong_id); |
97 |
} |
98 |
|
99 |
#Adding a Set without description |
100 |
my $set1 = { |
101 |
'spec' => 'specSet1', |
102 |
'name' => 'nameSet1', |
103 |
}; |
104 |
my $set1_id = AddOAISet($set1); |
105 |
isa_ok(\$set1_id, 'SCALAR', '$set1_id is a SCALAR'); |
106 |
|
107 |
my $sth = $dbh->prepare("SELECT count(*) FROM oai_sets"); |
108 |
$sth->execute; |
109 |
my $setsCount = $sth->fetchrow_array; |
110 |
is ($setsCount, 1, 'There is 1 set'); |
111 |
|
112 |
$sth = $dbh->prepare("SELECT spec, name FROM oai_sets"); |
113 |
$sth->execute; |
114 |
my ($spec, $name) = $sth->fetchrow_array; |
115 |
is ($spec, 'specSet1', 'spec field is "specSet1"'); |
116 |
is ($name, 'nameSet1', 'name field is "nameSet1"'); |
117 |
|
118 |
$sth = $dbh->prepare("SELECT description FROM oai_sets_descriptions"); |
119 |
$sth->execute; |
120 |
my $desc = $sth -> rows; |
121 |
is ($desc, 0, 'There is NO set description'); |
122 |
|
123 |
#Adding a Set with a description |
124 |
my $set2 = { |
125 |
'spec' => 'specSet2', |
126 |
'name' => 'nameSet2', |
127 |
'descriptions' => ['descSet2'], |
128 |
}; |
129 |
my $set2_id = AddOAISet($set2); |
130 |
isa_ok(\$set2_id, 'SCALAR', '$set2_id is a SCALAR'); |
131 |
|
132 |
$sth = $dbh->prepare("SELECT count(*) FROM oai_sets"); |
133 |
$sth->execute; |
134 |
$setsCount = $sth->fetchrow_array; |
135 |
is ($setsCount, 2, 'There is 2 sets'); |
136 |
|
137 |
$sth = $dbh->prepare("SELECT spec, name FROM oai_sets ORDER BY id DESC"); |
138 |
$sth->execute; |
139 |
($spec, $name) = $sth->fetchrow_array; |
140 |
is ($spec, 'specSet2', 'spec field is "specSet2"'); |
141 |
is ($name, 'nameSet2', 'name field is "nameSet2"'); |
142 |
|
143 |
$sth = $dbh->prepare("SELECT description FROM oai_sets_descriptions"); |
144 |
$sth->execute; |
145 |
$desc = $sth->fetchrow_array; |
146 |
is ($desc, 'descSet2', 'description field is "descSet2"'); |
147 |
|
148 |
|
149 |
# ---------- Testing GetOAISets ----------------- |
150 |
my $oai_sets = GetOAISets; |
151 |
isa_ok($oai_sets, 'ARRAY', '$oai_sets is an array reference of hash reference describing the sets'); |
152 |
|
153 |
isa_ok($oai_sets->[0], 'HASH', '$set1 is defined as a hash'); |
154 |
is ($oai_sets->[0]->{spec}, 'specSet1', 'spec field is "specSet1"'); |
155 |
is ($oai_sets->[0]->{name}, 'nameSet1', 'name field is "nameSet1"'); |
156 |
|
157 |
isa_ok($oai_sets->[1], 'HASH', '$set2 is defined as a hash'); |
158 |
is ($oai_sets->[1]->{spec}, 'specSet2', 'spec field is "specSet2"'); |
159 |
is ($oai_sets->[1]->{name}, 'nameSet2', 'name field is "nameSet2"'); |
160 |
is_deeply ($oai_sets->[1]->{descriptions}, ['descSet2'], 'description field is "descSet2"'); |
161 |
|
162 |
ok(!defined($oai_sets->[2]), 'There are only 2 sets'); |
163 |
|
164 |
|
165 |
# ---------- Testing GetOAISet ------------------ |
166 |
ok (!defined(GetOAISet), 'GetOAISet without argument is undef'); |
167 |
|
168 |
my $set = GetOAISet($set1_id); |
169 |
isa_ok($set, 'HASH', '$set is a hash reference describing the set with the given set_id'); |
170 |
is ($set->{spec}, 'specSet1', 'spec field is "specSet1"'); |
171 |
is ($set->{name}, 'nameSet1', 'name field is "nameSet1"'); |
172 |
|
173 |
$set = GetOAISet($set2_id); |
174 |
isa_ok($set, 'HASH', '$set is a hash reference describing the set with the given set_id'); |
175 |
is ($set->{spec}, 'specSet2', 'spec field is "specSet2"'); |
176 |
is ($set->{name}, 'nameSet2', 'name field is "nameSet2"'); |
177 |
is_deeply ($set->{descriptions}, ['descSet2'], 'description field is "descSet2"'); |
178 |
|
179 |
|
180 |
# ---------- Testing GetOAISetBySpec ------------ |
181 |
ok (!defined(GetOAISetBySpec), 'GetOAISetBySpec without argument is undef'); |
182 |
|
183 |
$set = GetOAISetBySpec($set1->{spec}); |
184 |
isa_ok($set, 'HASH', '$set is a hash describing the set whose spec is $oai_sets->[0]->{spec}'); |
185 |
is ($set->{spec}, 'specSet1', 'spec field is "specSet1"'); |
186 |
is ($set->{name}, 'nameSet1', 'name field is "nameSet1"'); |
187 |
|
188 |
$set = GetOAISetBySpec($set2->{spec}); |
189 |
isa_ok($set, 'HASH', '$set is a hash describing the set whose spec is $oai_sets->[1]->{spec}'); |
190 |
is ($set->{spec}, 'specSet2', 'spec field is "specSet2"'); |
191 |
is ($set->{name}, 'nameSet2', 'name field is "nameSet2"'); |
192 |
#GetOAISetBySpec does't return the description field. |
193 |
|
194 |
|
195 |
# ---------- Testing ModOAISet ------------------ |
196 |
ok (!defined(ModOAISet), 'ModOAISet without argument is undef'); |
197 |
|
198 |
my $new_set_without_id = { |
199 |
'spec' => 'specNoName', |
200 |
'name' => 'nameNoSpec', |
201 |
'descriptions' => ['descNoSpecNoName'], |
202 |
}; |
203 |
warn '> Set ID is not defined on purpose'; |
204 |
ok (!defined(ModOAISet($new_set_without_id)), 'ModOAISet without "id" field is undef'); |
205 |
|
206 |
|
207 |
my $new_set_without_spec_and_name = { |
208 |
'id' => $set1_id, |
209 |
'descriptions' => ['descNoSpecNoName'], |
210 |
}; |
211 |
ok (!defined(ModOAISet($new_set_without_spec_and_name)), 'ModOAISet without "field" and "name" fields is undef'); |
212 |
|
213 |
my $new_set_without_spec = { |
214 |
'id' => $set1_id, |
215 |
'name' => 'nameNoSpec', |
216 |
'descriptions' => ['descNoSpec'], |
217 |
}; |
218 |
ok (!defined(ModOAISet($new_set_without_spec)), 'ModOAISet without "field" field is undef'); |
219 |
|
220 |
my $new_set_without_name = { |
221 |
'id' => $set1_id, |
222 |
'spec' => 'specNoName', |
223 |
'descriptions' => ['descNoName'], |
224 |
}; |
225 |
ok (!defined(ModOAISet($new_set_without_name)), 'ModOAISet without "name" field is undef'); |
226 |
|
227 |
my $new_set1 = { |
228 |
'id' => $set1_id, |
229 |
'spec' => 'new_specSet1', |
230 |
'name' => 'new_nameSet1', |
231 |
'descriptions' => ['new_descSet1'], |
232 |
}; |
233 |
ModOAISet($new_set1); |
234 |
|
235 |
my $new_set2 = { |
236 |
'id' => $set2_id, |
237 |
'spec' => 'new_specSet2', |
238 |
'name' => 'new_nameSet2', |
239 |
}; |
240 |
ModOAISet($new_set2); |
241 |
|
242 |
$set1 = GetOAISet($set1_id); |
243 |
isa_ok($set1, 'HASH', '$set1 is defined as a hash'); |
244 |
is ($set1->{spec}, 'new_specSet1', 'spec field is "new_specSet1"'); |
245 |
is ($set1->{name}, 'new_nameSet1', 'name field is "new_nameSet1"'); |
246 |
is_deeply ($set1->{descriptions}, ['new_descSet1'], 'description field is "new_descSet1"'); |
247 |
|
248 |
$set2 = GetOAISet($set2_id); |
249 |
isa_ok($set2, 'HASH', '$new_set2 is defined as a hash'); |
250 |
is ($set2->{spec}, 'new_specSet2', 'spec field is "new_specSet2"'); |
251 |
is ($set2->{name}, 'new_nameSet2', 'name field is "new_nameSet2"'); |
252 |
|
253 |
|
254 |
# ---------- Testing ModOAISetMappings ---------- |
255 |
ok (!defined(ModOAISetMappings), 'ModOAISetMappings without argument is undef'); |
256 |
#Add 1st mapping for set1 |
257 |
my $mapping1 = [ |
258 |
{ |
259 |
marcfield => '206', |
260 |
marcsubfield => 'a', |
261 |
operator => 'equal', |
262 |
marcvalue => 'myMarcValue' |
263 |
}, |
264 |
]; |
265 |
ModOAISetMappings($set1_id, $mapping1); |
266 |
|
267 |
$sth = $dbh->prepare("SELECT count(*) FROM oai_sets_mappings"); |
268 |
$sth->execute; |
269 |
my $mappingsCount = $sth->fetchrow_array; |
270 |
is ($mappingsCount, 1, 'There is 1 mapping'); |
271 |
|
272 |
$sth = $dbh->prepare("SELECT marcfield, marcsubfield, operator, marcvalue FROM oai_sets_mappings"); |
273 |
$sth->execute; |
274 |
my ($marcfield, $marcsubfield, $operator, $marcvalue) = $sth->fetchrow_array; |
275 |
is ($marcfield, '206', 'marcfield field is "206"'); |
276 |
is ($marcsubfield, 'a', 'marcsubfield field is "a"'); |
277 |
is ($operator, 'equal', 'operator field is "equal"'); |
278 |
is ($marcvalue, 'myMarcValue', 'marcvalue field is "myMarcValue"'); |
279 |
|
280 |
#Mod 1st mapping of set1 |
281 |
my $mapping1_bis = [ |
282 |
{ |
283 |
marcfield => '256', |
284 |
marcsubfield => 'b', |
285 |
operator => 'notequal', |
286 |
marcvalue => 'myMarcValueBis' |
287 |
}, |
288 |
]; |
289 |
ModOAISetMappings($set1_id, $mapping1_bis); |
290 |
|
291 |
$sth = $dbh->prepare("SELECT count(*) FROM oai_sets_mappings"); |
292 |
$sth->execute; |
293 |
$mappingsCount = $sth->fetchrow_array; |
294 |
is ($mappingsCount, 1, 'There is 1 mapping'); |
295 |
|
296 |
$sth = $dbh->prepare("SELECT marcfield, marcsubfield, operator, marcvalue FROM oai_sets_mappings"); |
297 |
$sth->execute; |
298 |
($marcfield, $marcsubfield, $operator, $marcvalue) = $sth->fetchrow_array; |
299 |
is ($marcfield, '256', 'marcfield field is "256"'); |
300 |
is ($marcsubfield, 'b', 'marcsubfield field is "b"'); |
301 |
is ($operator, 'notequal', 'operator field is "notequal"'); |
302 |
is ($marcvalue, 'myMarcValueBis', 'marcvalue field is "myMarcValueBis"'); |
303 |
|
304 |
#Add 1st mapping of set2 |
305 |
my $mapping2 = [ |
306 |
{ |
307 |
marcfield => '306', |
308 |
marcsubfield => 'c', |
309 |
operator => 'equal', |
310 |
marcvalue => 'myOtherMarcValue' |
311 |
}, |
312 |
]; |
313 |
ModOAISetMappings($set2_id, $mapping2); |
314 |
|
315 |
$sth = $dbh->prepare("SELECT count(*) FROM oai_sets_mappings"); |
316 |
$sth->execute; |
317 |
$mappingsCount = $sth->fetchrow_array; |
318 |
is ($mappingsCount, 2, 'There is 2 mappings'); |
319 |
|
320 |
$sth = $dbh->prepare("SELECT marcfield, marcsubfield, operator, marcvalue FROM oai_sets_mappings ORDER BY set_id DESC LIMIT 1"); |
321 |
$sth->execute; |
322 |
($marcfield, $marcsubfield, $operator, $marcvalue) = $sth->fetchrow_array; |
323 |
is ($marcfield, '306', 'marcfield field is "306"'); |
324 |
is ($marcsubfield, 'c', 'marcsubfield field is "c"'); |
325 |
is ($operator, 'equal', 'operator field is "equal"'); |
326 |
is ($marcvalue, 'myOtherMarcValue', 'marcvalue field is "myOtherMarcValue"'); |
327 |
|
328 |
|
329 |
# ---------- Testing GetOAISetsMappings --------- |
330 |
my $mappings = GetOAISetsMappings; |
331 |
|
332 |
isa_ok($mappings, 'HASH', '$mappings is a hashref of arrayrefs of hashrefs'); |
333 |
isa_ok($mappings->{$set1_id}, 'ARRAY', '$mappings->{$set1_id} is a arrayrefs of hashrefs'); |
334 |
isa_ok($mappings->{$set1_id}->[0], 'HASH', '$mappings->{$set1_id}->[0] is a hashrefs'); |
335 |
is ($mappings->{$set1_id}->[0]->{marcfield}, '256', 'marcfield field is "256"'); |
336 |
is ($mappings->{$set1_id}->[0]->{marcsubfield}, 'b', 'marcsubfield field is "b"'); |
337 |
is ($mappings->{$set1_id}->[0]->{operator}, 'notequal', 'operator field is "notequal"'); |
338 |
is ($mappings->{$set1_id}->[0]->{marcvalue}, 'myMarcValueBis', 'marcvalue field is "myMarcValueBis"'); |
339 |
|
340 |
isa_ok($mappings->{$set2_id}, 'ARRAY', '$mappings->{$set2_id} is a arrayrefs of hashrefs'); |
341 |
isa_ok($mappings->{$set2_id}, 'ARRAY', '$mappings->{$set2_id} is a arrayrefs of hashrefs'); |
342 |
isa_ok($mappings->{$set2_id}->[0], 'HASH', '$mappings->{$set2_id}->[0] is a hashrefs'); |
343 |
is ($mappings->{$set2_id}->[0]->{marcfield}, '306', 'marcfield field is "306"'); |
344 |
is ($mappings->{$set2_id}->[0]->{marcsubfield}, 'c', 'marcsubfield field is "c"'); |
345 |
is ($mappings->{$set2_id}->[0]->{operator}, 'equal', 'operator field is "equal"'); |
346 |
is ($mappings->{$set2_id}->[0]->{marcvalue}, 'myOtherMarcValue', 'marcvalue field is "myOtherMarcValue"'); |
347 |
|
348 |
|
349 |
# ---------- Testing GetOAISetMappings ---------- |
350 |
ok (!defined(GetOAISetMappings), 'GetOAISetMappings without argument is undef'); |
351 |
|
352 |
my $set_mappings1 = GetOAISetMappings($set1_id); |
353 |
isa_ok($set_mappings1->[0], 'HASH', '$set_mappings1->[0] is a hashref'); |
354 |
is ($set_mappings1->[0]->{marcfield}, '256', 'marcfield field is "256"'); |
355 |
is ($set_mappings1->[0]->{marcsubfield}, 'b', 'marcsubfield field is "b"'); |
356 |
is ($set_mappings1->[0]->{operator}, 'notequal', 'operator field is "notequal"'); |
357 |
is ($set_mappings1->[0]->{marcvalue}, 'myMarcValueBis', 'marcvalue field is "myMarcValueBis"'); |
358 |
|
359 |
my $set_mappings2 = GetOAISetMappings($set2_id); |
360 |
isa_ok($mappings->{$set2_id}->[0], 'HASH', '$mappings->{$set2_id}->[0] is a hashref'); |
361 |
is ($mappings->{$set2_id}->[0]->{marcfield}, '306', 'marcfield field is "306"'); |
362 |
is ($mappings->{$set2_id}->[0]->{marcsubfield}, 'c', 'marcsubfield field is "c"'); |
363 |
is ($mappings->{$set2_id}->[0]->{operator}, 'equal', 'operator field is "equal"'); |
364 |
is ($mappings->{$set2_id}->[0]->{marcvalue}, 'myOtherMarcValue', 'marcvalue field is "myOtherMarcValue"'); |
365 |
|
366 |
|
367 |
# ---------- Testing AddOAISetsBiblios ---------- |
368 |
ok (!defined(AddOAISetsBiblios), 'AddOAISetsBiblios without argument is undef'); |
369 |
ok (!defined(AddOAISetsBiblios(my $arg=[])), 'AddOAISetsBiblios with a no HASH argument is undef'); |
370 |
ok (defined(AddOAISetsBiblios($arg={})), 'AddOAISetsBiblios with a HASH argument is def'); |
371 |
|
372 |
# Create a biblio instance for testing |
373 |
my $biblionumber1 = create_helper_biblio('Moffat, Steven'); |
374 |
isa_ok(\$biblionumber1, 'SCALAR', '$biblionumber1 is a SCALAR'); |
375 |
my $biblionumber2 = create_helper_biblio('Moffat, Steven'); |
376 |
isa_ok(\$biblionumber2, 'SCALAR', '$biblionumber2 is a SCALAR'); |
377 |
|
378 |
my $oai_sets_biblios = { |
379 |
$set1_id => [$biblionumber1, $biblionumber2], # key is the set_id, and value is an array ref of biblionumbers |
380 |
$set2_id => [], |
381 |
}; |
382 |
AddOAISetsBiblios($oai_sets_biblios); |
383 |
|
384 |
$sth = $dbh->prepare("SELECT count(*) FROM oai_sets_biblios"); |
385 |
$sth->execute; |
386 |
my $bibliosCount = $sth->fetchrow_array; |
387 |
is ($bibliosCount, 2, 'There are 2 biblios in oai_sets_biblios'); |
388 |
|
389 |
#testing biblio for set1_id |
390 |
$sth = $dbh->prepare("SELECT * FROM oai_sets_biblios WHERE set_id = ?"); |
391 |
$sth->execute($set1_id); |
392 |
my $count = $sth->rows; |
393 |
is ($count, '2', '$set_id1 has 2 biblio'); |
394 |
|
395 |
$sth->execute($set1_id); |
396 |
my $line = ${ $sth->fetchall_arrayref( {} ) }[0]; |
397 |
is($line->{set_id}, $set1_id, "set_id is good"); |
398 |
is($line->{biblionumber}, $biblionumber1, "biblionumber is good"); |
399 |
|
400 |
$sth->execute($set1_id); |
401 |
$line = ${ $sth->fetchall_arrayref( {} ) }[1]; |
402 |
is($line->{set_id}, $set1_id, "set_id is good"); |
403 |
is($line->{biblionumber}, $biblionumber2, "biblionumber is good"); |
404 |
|
405 |
#testing biblio for set2_id |
406 |
$sth->execute($set2_id); |
407 |
$count = $sth->rows; |
408 |
is ($count, '0', '$set_id2 has 0 biblio'); |
409 |
|
410 |
|
411 |
# ---------- Testing GetOAISetsBiblio ----------- |
412 |
$oai_sets = GetOAISetsBiblio($biblionumber1); |
413 |
isa_ok($oai_sets, 'ARRAY', '$oai_sets is an arrayref of hashref where each element of the array is a set'); |
414 |
isa_ok($oai_sets->[0], 'HASH', '$oai_sets->[0] is a hashrefs of $set1_id'); |
415 |
is($oai_sets->[0]->{id}, $set1_id, 'id is $set1_id'); |
416 |
is($oai_sets->[0]->{spec}, $set1->{spec}, 'spec is new_specset1'); |
417 |
is($oai_sets->[0]->{name}, $set1->{name}, 'name is new_specname1'); |
418 |
|
419 |
$oai_sets = GetOAISetsBiblio($biblionumber2); |
420 |
isa_ok($oai_sets, 'ARRAY', '$oai_sets is an arrayref of hashref where each element of the array is a set'); |
421 |
isa_ok($oai_sets->[0], 'HASH', '$oai_sets->[0] is a hashrefs of $set2_id'); |
422 |
is($oai_sets->[0]->{id}, $set1_id, 'id is $set1_id'); |
423 |
is($oai_sets->[0]->{spec}, $set1->{spec}, 'spec is new_specset1'); |
424 |
is($oai_sets->[0]->{name}, $set1->{name}, 'name is new_specname1'); |
425 |
|
426 |
|
427 |
# ---------- Testing ModOAISetsBiblios ---------- |
428 |
ok (!defined(ModOAISetsBiblios), 'ModOAISetsBiblios without argument is undef'); |
429 |
ok (!defined(ModOAISetsBiblios($arg=[])), 'ModOAISetsBiblios with a no HASH argument is undef'); |
430 |
ok (defined(ModOAISetsBiblios($arg={})), 'ModOAISetsBiblios with a HASH argument is def'); |
431 |
|
432 |
$oai_sets_biblios = { |
433 |
$set1_id => [$biblionumber1], |
434 |
$set2_id => [$biblionumber2], |
435 |
}; |
436 |
ModOAISetsBiblios($oai_sets_biblios); |
437 |
|
438 |
$sth = $dbh->prepare("SELECT count(*) FROM oai_sets_biblios"); |
439 |
$sth->execute; |
440 |
$bibliosCount = $sth->fetchrow_array; |
441 |
is ($bibliosCount, 2, 'There are 2 biblios in oai_sets_biblios'); |
442 |
|
443 |
#testing biblio for set1_id |
444 |
$sth = $dbh->prepare("SELECT * FROM oai_sets_biblios WHERE set_id = ?"); |
445 |
$sth->execute($set1_id); |
446 |
$count = $sth->rows; |
447 |
is ($count, '1', '$set_id1 has 2 biblio'); |
448 |
|
449 |
$sth->execute($set1_id); |
450 |
$line = ${ $sth->fetchall_arrayref( {} ) }[0]; |
451 |
is($line->{set_id}, $set1_id, "set_id is good"); |
452 |
is($line->{biblionumber}, $biblionumber1, "biblionumber is good"); |
453 |
|
454 |
#testing biblio for set2_id |
455 |
$sth->execute($set2_id); |
456 |
$count = $sth->rows; |
457 |
is ($count, '1', '$set_id2 has 1 biblio'); |
458 |
|
459 |
$sth->execute($set2_id); |
460 |
$line = ${ $sth->fetchall_arrayref( {} ) }[0]; |
461 |
is($line->{set_id}, $set2_id, "set_id is good"); |
462 |
is($line->{biblionumber}, $biblionumber2, "biblionumber is good"); |
463 |
|
464 |
|
465 |
# ---------- Testing DelOAISetsBiblio ----------- |
466 |
ok (!defined(DelOAISetsBiblio), 'DelOAISetsBiblio without argument is undef'); |
467 |
|
468 |
DelOAISetsBiblio($biblionumber1); |
469 |
is_deeply(GetOAISetsBiblio($biblionumber1), [], "no biblio1 appear in any OAI sets"); |
470 |
|
471 |
DelOAISetsBiblio($biblionumber2); |
472 |
is_deeply(GetOAISetsBiblio($biblionumber2), [], "no biblio2 appear in any OAI sets"); |
473 |
|
474 |
|
475 |
# ---------- Testing DelOAISet ------------------ |
476 |
ok (!defined(DelOAISet), 'DelOAISet without argument is undef'); |
477 |
|
478 |
DelOAISet($set1_id); |
479 |
$sth = $dbh->prepare("SELECT count(*) FROM oai_sets"); |
480 |
$sth->execute; |
481 |
$setsCount = $sth->fetchrow_array; |
482 |
is ($setsCount, 1, 'There is 1 set left'); |
483 |
$set1 = GetOAISet($set1_id); |
484 |
is_deeply ($set1, {}, '$set1 is empty'); |
485 |
|
486 |
DelOAISet($set2_id); |
487 |
$sth = $dbh->prepare("SELECT count(*) FROM oai_sets"); |
488 |
$sth->execute; |
489 |
$setsCount = $sth->fetchrow_array; |
490 |
is ($setsCount, 0, 'There is no set anymore'); |
491 |
$set2 = GetOAISet($set2_id); |
492 |
is_deeply ($set2, {}, '$set2 is empty'); |
493 |
|
494 |
$oai_sets=GetOAISets; |
495 |
is_deeply ($oai_sets, [], '$oai_sets is empty'); |
496 |
|
497 |
|
498 |
# ---------- Testing UpdateOAISetsBiblio -------- |
499 |
ok (!defined(UpdateOAISetsBiblio), 'UpdateOAISetsBiblio without argument is undef'); |
500 |
ok (!defined(UpdateOAISetsBiblio($arg)), 'UpdateOAISetsBiblio with only 1 argument is undef'); |
501 |
|
502 |
#Create a set |
503 |
my $setVH = { |
504 |
'spec' => 'Set where Author is Victor Hugo', |
505 |
'name' => 'VH' |
506 |
}; |
507 |
my $setVH_id = AddOAISet($setVH); |
508 |
|
509 |
#Create mappings : 'author' should be 'Victor Hugo' |
510 |
my $marcflavour = C4::Context->preference('marcflavour'); |
511 |
my $mappingsVH; |
512 |
|
513 |
if ($marcflavour eq 'UNIMARC' ){ |
514 |
$mappingsVH = [ |
515 |
{ |
516 |
marcfield => '200', |
517 |
marcsubfield => 'f', |
518 |
operator => 'equal', |
519 |
marcvalue => 'Victor Hugo' |
520 |
} |
521 |
]; |
522 |
} |
523 |
else { |
524 |
$mappingsVH = [ |
525 |
{ |
526 |
marcfield => '100', |
527 |
marcsubfield => 'a', |
528 |
operator => 'equal', |
529 |
marcvalue => 'Victor Hugo' |
530 |
} |
531 |
]; |
532 |
} |
533 |
ModOAISetMappings($setVH_id, $mappingsVH); |
534 |
|
535 |
|
536 |
#Create a biblio notice corresponding at one of mappings |
537 |
my $biblionumberVH = create_helper_biblio('Victor Hugo'); |
538 |
|
539 |
#Update |
540 |
my $record = GetMarcBiblio($biblionumberVH); |
541 |
UpdateOAISetsBiblio($biblionumberVH, $record); |
542 |
|
543 |
#is biblio attached to setVH ? |
544 |
my $oai_setsVH = GetOAISetsBiblio($biblionumberVH); |
545 |
is($oai_setsVH->[0]->{id}, $setVH_id, 'id is ok'); |
546 |
is($oai_setsVH->[0]->{spec}, $setVH->{spec}, 'id is ok'); |
547 |
is($oai_setsVH->[0]->{name}, $setVH->{name}, 'id is ok'); |
548 |
|
549 |
|
550 |
# ---------- Testing CalcOAISetsBiblio ---------- |
551 |
ok (!defined(CalcOAISetsBiblio), 'CalcOAISetsBiblio without argument is undef'); |
552 |
|
553 |
my @setsEq = CalcOAISetsBiblio($record); |
554 |
is_deeply(@setsEq, $setVH_id, 'The $record only belongs to $setVH'); |
555 |
|
556 |
#Testing CalcOAISetsBiblio for a mapping which operator is 'notequal' |
557 |
#Create a set |
558 |
my $setNotVH = { |
559 |
'spec' => 'Set where Author is NOT Victor Hugo', |
560 |
'name' => 'NOT VH' |
561 |
}; |
562 |
my $setNotVH_id = AddOAISet($setNotVH); |
563 |
|
564 |
#Create mappings : 'author' should NOT be 'Victor Hugo' |
565 |
$marcflavour = C4::Context->preference('marcflavour'); |
566 |
my $mappingsNotVH; |
567 |
|
568 |
if ($marcflavour eq 'UNIMARC' ){ |
569 |
$mappingsNotVH = [ |
570 |
{ |
571 |
marcfield => '200', |
572 |
marcsubfield => 'f', |
573 |
operator => 'notequal', |
574 |
marcvalue => 'Victor Hugo' |
575 |
} |
576 |
]; |
577 |
} |
578 |
else { |
579 |
$mappingsNotVH = [ |
580 |
{ |
581 |
marcfield => '100', |
582 |
marcsubfield => 'a', |
583 |
operator => 'notequal', |
584 |
marcvalue => 'Victor Hugo' |
585 |
} |
586 |
]; |
587 |
} |
588 |
ModOAISetMappings($setNotVH_id, $mappingsNotVH); |
589 |
|
590 |
|
591 |
#Create a biblio notice corresponding at one of mappings |
592 |
my $biblionumberNotVH = create_helper_biblio('Sponge, Bob'); |
593 |
|
594 |
#Update |
595 |
$record = GetMarcBiblio($biblionumberNotVH); |
596 |
UpdateOAISetsBiblio($biblionumberNotVH, $record); |
597 |
|
598 |
my @setsNotEq = CalcOAISetsBiblio($record); |
599 |
is_deeply(@setsNotEq, $setNotVH_id, 'The $record only belongs to $setNotVH'); |
600 |
|
601 |
|
602 |
# |
603 |
|
604 |
|
605 |
|
606 |
|
607 |
# ---------- Subs -------------------------------- |
608 |
|
609 |
|
610 |
# Helper method to set up a Biblio. |
611 |
sub create_helper_biblio { |
612 |
my $author = shift; |
613 |
|
614 |
return unless (defined($author)); |
615 |
|
616 |
my $marcflavour = C4::Context->preference('marcflavour'); |
617 |
my $bib = MARC::Record->new(); |
618 |
my $title = 'Silence in the library'; |
619 |
|
620 |
if ($marcflavour eq 'UNIMARC' ){ |
621 |
$bib->append_fields( |
622 |
MARC::Field->new('200', ' ', ' ', f => $author), |
623 |
MARC::Field->new('200', ' ', ' ', a => $title), |
624 |
); |
625 |
} |
626 |
else{ |
627 |
$bib->append_fields( |
628 |
MARC::Field->new('100', ' ', ' ', a => $author), |
629 |
MARC::Field->new('245', ' ', ' ', a => $title), |
630 |
); |
631 |
} |
632 |
my ($biblionumber)= AddBiblio($bib, ''); |
633 |
return $biblionumber; |
634 |
} |
635 |
|
636 |
$dbh->rollback; |