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