Bug 14686: Remove unused use
[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 => 148;
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 my $res;
205 warning_is { $res = ModOAISet($new_set_without_id) }
206             'Set ID not defined, can\'t modify the set',
207             'ModOAISet raises warning if Set ID is not defined';
208 ok(!defined($res), 'ModOAISet returns undef if Set ID is not defined');
209
210 my $new_set_without_spec_and_name =  {
211     'id' => $set1_id,
212     'descriptions' => ['descNoSpecNoName'],
213 };
214 ok (!defined(ModOAISet($new_set_without_spec_and_name)), 'ModOAISet without "field" and "name" fields is undef');
215
216 my $new_set_without_spec =  {
217     'id' => $set1_id,
218     'name' => 'nameNoSpec',
219     'descriptions' => ['descNoSpec'],
220 };
221 ok (!defined(ModOAISet($new_set_without_spec)), 'ModOAISet without "field" field is undef');
222
223 my $new_set_without_name =  {
224     'id' => $set1_id,
225     'spec' => 'specNoName',
226     'descriptions' => ['descNoName'],
227 };
228 ok (!defined(ModOAISet($new_set_without_name)), 'ModOAISet without "name" field is undef');
229
230 my $new_set1 =  {
231     'id' => $set1_id,
232     'spec' => 'new_specSet1',
233     'name' => 'new_nameSet1',
234     'descriptions' => ['new_descSet1'],
235 };
236 ModOAISet($new_set1);
237
238 my $new_set2 =  {
239     'id' => $set2_id,
240     'spec' => 'new_specSet2',
241     'name' => 'new_nameSet2',
242 };
243 ModOAISet($new_set2);
244
245 $set1 = GetOAISet($set1_id);
246 isa_ok($set1, 'HASH', '$set1 is defined as a hash');
247 is ($set1->{spec}, 'new_specSet1', 'spec field is "new_specSet1"');
248 is ($set1->{name}, 'new_nameSet1', 'name field is "new_nameSet1"');
249 is_deeply ($set1->{descriptions}, ['new_descSet1'], 'description field is "new_descSet1"');
250
251 $set2 = GetOAISet($set2_id);
252 isa_ok($set2, 'HASH', '$new_set2 is defined as a hash');
253 is ($set2->{spec}, 'new_specSet2', 'spec field is "new_specSet2"');
254 is ($set2->{name}, 'new_nameSet2', 'name field is "new_nameSet2"');
255
256
257 # ---------- Testing ModOAISetMappings ----------
258 ok (!defined(ModOAISetMappings), 'ModOAISetMappings without argument is undef');
259 #Add 1st mapping for set1
260 my $mapping1 = [
261     {
262         marcfield => '206',
263         marcsubfield => 'a',
264         operator => 'equal',
265         marcvalue => 'myMarcValue'
266     },
267 ];
268 ModOAISetMappings($set1_id, $mapping1);
269
270 $sth = $dbh->prepare("SELECT count(*) FROM oai_sets_mappings");
271 $sth->execute;
272 my $mappingsCount = $sth->fetchrow_array;
273 is ($mappingsCount, 1, 'There is 1 mapping');
274
275 $sth = $dbh->prepare("SELECT marcfield, marcsubfield, operator, marcvalue FROM oai_sets_mappings");
276 $sth->execute;
277 my ($marcfield, $marcsubfield, $operator, $marcvalue) = $sth->fetchrow_array;
278 is ($marcfield, '206', 'marcfield field is "206"');
279 is ($marcsubfield, 'a', 'marcsubfield field is "a"');
280 is ($operator, 'equal', 'operator field is "equal"');
281 is ($marcvalue, 'myMarcValue', 'marcvalue field is "myMarcValue"');
282
283 #Mod 1st mapping of set1
284 my $mapping1_bis = [
285     {
286         marcfield => '256',
287         marcsubfield => 'b',
288         operator => 'notequal',
289         marcvalue => 'myMarcValueBis'
290     },
291 ];
292 ModOAISetMappings($set1_id, $mapping1_bis);
293
294 $sth = $dbh->prepare("SELECT count(*) FROM oai_sets_mappings");
295 $sth->execute;
296 $mappingsCount = $sth->fetchrow_array;
297 is ($mappingsCount, 1, 'There is 1 mapping');
298
299 $sth = $dbh->prepare("SELECT marcfield, marcsubfield, operator, marcvalue FROM oai_sets_mappings");
300 $sth->execute;
301 ($marcfield, $marcsubfield, $operator, $marcvalue) = $sth->fetchrow_array;
302 is ($marcfield, '256', 'marcfield field is "256"');
303 is ($marcsubfield, 'b', 'marcsubfield field is "b"');
304 is ($operator, 'notequal', 'operator field is "notequal"');
305 is ($marcvalue, 'myMarcValueBis', 'marcvalue field is "myMarcValueBis"');
306
307 #Add 1st mapping of set2
308 my $mapping2 = [
309     {
310         marcfield => '306',
311         marcsubfield => 'c',
312         operator => 'equal',
313         marcvalue => 'myOtherMarcValue'
314     },
315 ];
316 ModOAISetMappings($set2_id, $mapping2);
317
318 $sth = $dbh->prepare("SELECT count(*) FROM oai_sets_mappings");
319 $sth->execute;
320 $mappingsCount = $sth->fetchrow_array;
321 is ($mappingsCount, 2, 'There is 2 mappings');
322
323 $sth = $dbh->prepare("SELECT marcfield, marcsubfield, operator, marcvalue FROM oai_sets_mappings ORDER BY set_id DESC LIMIT 1");
324 $sth->execute;
325 ($marcfield, $marcsubfield, $operator, $marcvalue) = $sth->fetchrow_array;
326 is ($marcfield, '306', 'marcfield field is "306"');
327 is ($marcsubfield, 'c', 'marcsubfield field is "c"');
328 is ($operator, 'equal', 'operator field is "equal"');
329 is ($marcvalue, 'myOtherMarcValue', 'marcvalue field is "myOtherMarcValue"');
330
331
332 # ---------- Testing GetOAISetsMappings ---------
333 my $mappings = GetOAISetsMappings;
334
335 isa_ok($mappings, 'HASH', '$mappings is a hashref of arrayrefs of hashrefs');
336 isa_ok($mappings->{$set1_id}, 'ARRAY', '$mappings->{$set1_id} is a arrayrefs of hashrefs');
337 isa_ok($mappings->{$set1_id}->[0], 'HASH', '$mappings->{$set1_id}->[0] is a hashrefs');
338 is ($mappings->{$set1_id}->[0]->{marcfield}, '256', 'marcfield field is "256"');
339 is ($mappings->{$set1_id}->[0]->{marcsubfield}, 'b', 'marcsubfield field is "b"');
340 is ($mappings->{$set1_id}->[0]->{operator}, 'notequal', 'operator field is "notequal"');
341 is ($mappings->{$set1_id}->[0]->{marcvalue}, 'myMarcValueBis', 'marcvalue field is "myMarcValueBis"');
342
343 isa_ok($mappings->{$set2_id}, 'ARRAY', '$mappings->{$set2_id} is a arrayrefs of hashrefs');
344 isa_ok($mappings->{$set2_id}, 'ARRAY', '$mappings->{$set2_id} is a arrayrefs of hashrefs');
345 isa_ok($mappings->{$set2_id}->[0], 'HASH', '$mappings->{$set2_id}->[0] is a hashrefs');
346 is ($mappings->{$set2_id}->[0]->{marcfield}, '306', 'marcfield field is "306"');
347 is ($mappings->{$set2_id}->[0]->{marcsubfield}, 'c', 'marcsubfield field is "c"');
348 is ($mappings->{$set2_id}->[0]->{operator}, 'equal', 'operator field is "equal"');
349 is ($mappings->{$set2_id}->[0]->{marcvalue}, 'myOtherMarcValue', 'marcvalue field is "myOtherMarcValue"');
350
351
352 # ---------- Testing GetOAISetMappings ----------
353 ok (!defined(GetOAISetMappings), 'GetOAISetMappings without argument is undef');
354
355 my $set_mappings1 = GetOAISetMappings($set1_id);
356 isa_ok($set_mappings1->[0], 'HASH', '$set_mappings1->[0] is a hashref');
357 is ($set_mappings1->[0]->{marcfield}, '256', 'marcfield field is "256"');
358 is ($set_mappings1->[0]->{marcsubfield}, 'b', 'marcsubfield field is "b"');
359 is ($set_mappings1->[0]->{operator}, 'notequal', 'operator field is "notequal"');
360 is ($set_mappings1->[0]->{marcvalue}, 'myMarcValueBis', 'marcvalue field is "myMarcValueBis"');
361
362 my $set_mappings2 = GetOAISetMappings($set2_id);
363 isa_ok($mappings->{$set2_id}->[0], 'HASH', '$mappings->{$set2_id}->[0] is a hashref');
364 is ($mappings->{$set2_id}->[0]->{marcfield}, '306', 'marcfield field is "306"');
365 is ($mappings->{$set2_id}->[0]->{marcsubfield}, 'c', 'marcsubfield field is "c"');
366 is ($mappings->{$set2_id}->[0]->{operator}, 'equal', 'operator field is "equal"');
367 is ($mappings->{$set2_id}->[0]->{marcvalue}, 'myOtherMarcValue', 'marcvalue field is "myOtherMarcValue"');
368
369
370 # ---------- Testing AddOAISetsBiblios ----------
371 ok (!defined(AddOAISetsBiblios), 'AddOAISetsBiblios without argument is undef');
372 ok (!defined(AddOAISetsBiblios(my $arg=[])), 'AddOAISetsBiblios with a no HASH argument is undef');
373 ok (defined(AddOAISetsBiblios($arg={})), 'AddOAISetsBiblios with a HASH argument is def');
374
375 # Create a biblio instance for testing
376 my $biblionumber1 = create_helper_biblio('Moffat, Steven');
377 isa_ok(\$biblionumber1, 'SCALAR', '$biblionumber1 is a SCALAR');
378 my $biblionumber2 = create_helper_biblio('Moffat, Steven');
379 isa_ok(\$biblionumber2, 'SCALAR', '$biblionumber2 is a SCALAR');
380
381 my $oai_sets_biblios = {
382     $set1_id => [$biblionumber1, $biblionumber2],   # key is the set_id, and value is an array ref of biblionumbers
383     $set2_id => [],
384 };
385 AddOAISetsBiblios($oai_sets_biblios);
386
387 $sth = $dbh->prepare("SELECT count(*) FROM oai_sets_biblios");
388 $sth->execute;
389 my $bibliosCount = $sth->fetchrow_array;
390 is ($bibliosCount, 2, 'There are 2 biblios in oai_sets_biblios');
391
392 #testing biblio for set1_id
393 $sth = $dbh->prepare("SELECT * FROM oai_sets_biblios WHERE set_id = ?");
394 $sth->execute($set1_id);
395 my $count = $sth->rows;
396 is ($count, '2', '$set_id1 has 2 biblio');
397
398 $sth->execute($set1_id);
399 my $line = ${ $sth->fetchall_arrayref( {} ) }[0];
400 is($line->{set_id}, $set1_id, "set_id is good");
401 is($line->{biblionumber}, $biblionumber1, "biblionumber is good");
402
403 $sth->execute($set1_id);
404 $line = ${ $sth->fetchall_arrayref( {} ) }[1];
405 is($line->{set_id}, $set1_id, "set_id is good");
406 is($line->{biblionumber}, $biblionumber2, "biblionumber is good");
407
408 #testing biblio for set2_id
409 $sth->execute($set2_id);
410 $count = $sth->rows;
411 is ($count, '0', '$set_id2 has 0 biblio');
412
413
414 # ---------- Testing GetOAISetsBiblio -----------
415 $oai_sets = GetOAISetsBiblio($biblionumber1);
416 isa_ok($oai_sets, 'ARRAY', '$oai_sets is an arrayref of hashref where each element of the array is a set');
417 isa_ok($oai_sets->[0], 'HASH', '$oai_sets->[0] is a hashrefs of $set1_id');
418 is($oai_sets->[0]->{id}, $set1_id, 'id is $set1_id');
419 is($oai_sets->[0]->{spec}, $set1->{spec}, 'spec is new_specset1');
420 is($oai_sets->[0]->{name}, $set1->{name}, 'name is new_specname1');
421
422 $oai_sets = GetOAISetsBiblio($biblionumber2);
423 isa_ok($oai_sets, 'ARRAY', '$oai_sets is an arrayref of hashref where each element of the array is a set');
424 isa_ok($oai_sets->[0], 'HASH', '$oai_sets->[0] is a hashrefs of $set2_id');
425 is($oai_sets->[0]->{id}, $set1_id, 'id is $set1_id');
426 is($oai_sets->[0]->{spec}, $set1->{spec}, 'spec is new_specset1');
427 is($oai_sets->[0]->{name}, $set1->{name}, 'name is new_specname1');
428
429
430 # ---------- Testing ModOAISetsBiblios ----------
431 ok (!defined(ModOAISetsBiblios), 'ModOAISetsBiblios without argument is undef');
432 ok (!defined(ModOAISetsBiblios($arg=[])), 'ModOAISetsBiblios with a no HASH argument is undef');
433 ok (defined(ModOAISetsBiblios($arg={})), 'ModOAISetsBiblios with a HASH argument is def');
434
435 $oai_sets_biblios = {
436     $set1_id => [$biblionumber1],
437     $set2_id => [$biblionumber2],
438 };
439 ModOAISetsBiblios($oai_sets_biblios);
440
441 $sth = $dbh->prepare("SELECT count(*) FROM oai_sets_biblios");
442 $sth->execute;
443 $bibliosCount = $sth->fetchrow_array;
444 is ($bibliosCount, 2, 'There are 2 biblios in oai_sets_biblios');
445
446 #testing biblio for set1_id
447 $sth = $dbh->prepare("SELECT * FROM oai_sets_biblios WHERE set_id = ?");
448 $sth->execute($set1_id);
449 $count = $sth->rows;
450 is ($count, '1', '$set_id1 has 2 biblio');
451
452 $sth->execute($set1_id);
453 $line = ${ $sth->fetchall_arrayref( {} ) }[0];
454 is($line->{set_id}, $set1_id, "set_id is good");
455 is($line->{biblionumber}, $biblionumber1, "biblionumber is good");
456
457 #testing biblio for set2_id
458 $sth->execute($set2_id);
459 $count = $sth->rows;
460 is ($count, '1', '$set_id2 has 1 biblio');
461
462 $sth->execute($set2_id);
463 $line = ${ $sth->fetchall_arrayref( {} ) }[0];
464 is($line->{set_id}, $set2_id, "set_id is good");
465 is($line->{biblionumber}, $biblionumber2, "biblionumber is good");
466
467
468 # ---------- Testing DelOAISetsBiblio -----------
469 ok (!defined(DelOAISetsBiblio), 'DelOAISetsBiblio without argument is undef');
470
471 DelOAISetsBiblio($biblionumber1);
472 is_deeply(GetOAISetsBiblio($biblionumber1), [], "no biblio1 appear in any OAI sets");
473
474 DelOAISetsBiblio($biblionumber2);
475 is_deeply(GetOAISetsBiblio($biblionumber2), [], "no biblio2 appear in any OAI sets");
476
477
478 # ---------- Testing DelOAISet ------------------
479 ok (!defined(DelOAISet), 'DelOAISet without argument is undef');
480
481 DelOAISet($set1_id);
482 $sth = $dbh->prepare("SELECT count(*) FROM oai_sets");
483 $sth->execute;
484 $setsCount = $sth->fetchrow_array;
485 is ($setsCount, 1, 'There is 1 set left');
486 $set1 = GetOAISet($set1_id);
487 is_deeply ($set1, {}, '$set1 is empty');
488
489 DelOAISet($set2_id);
490 $sth = $dbh->prepare("SELECT count(*) FROM oai_sets");
491 $sth->execute;
492 $setsCount = $sth->fetchrow_array;
493 is ($setsCount, 0, 'There is no set anymore');
494 $set2 = GetOAISet($set2_id);
495 is_deeply ($set2, {}, '$set2 is empty');
496
497 $oai_sets=GetOAISets;
498 is_deeply ($oai_sets, [], '$oai_sets is empty');
499
500
501 # ---------- Testing UpdateOAISetsBiblio --------
502 ok (!defined(UpdateOAISetsBiblio), 'UpdateOAISetsBiblio without argument is undef');
503 ok (!defined(UpdateOAISetsBiblio($arg)), 'UpdateOAISetsBiblio with only 1 argument is undef');
504
505 #Create a set
506 my $setVH = {
507     'spec' => 'Set where Author is Victor Hugo',
508     'name' => 'VH'
509 };
510 my $setVH_id = AddOAISet($setVH);
511
512 #Create mappings : 'author' should be 'Victor Hugo'
513 my $marcflavour = C4::Context->preference('marcflavour');
514 my $mappingsVH;
515
516 if ($marcflavour eq 'UNIMARC' ){
517     $mappingsVH = [
518         {
519             marcfield => '200',
520             marcsubfield => 'f',
521             operator => 'equal',
522             marcvalue => 'Victor Hugo'
523         }
524     ];
525 }
526 else {
527     $mappingsVH = [
528             {
529                 marcfield => '100',
530                 marcsubfield => 'a',
531                 operator => 'equal',
532                 marcvalue => 'Victor Hugo'
533             }
534     ];
535 }
536 ModOAISetMappings($setVH_id, $mappingsVH);
537
538
539 #Create a biblio notice corresponding at one of mappings
540 my $biblionumberVH = create_helper_biblio('Victor Hugo');
541
542 #Update
543 my $record = GetMarcBiblio($biblionumberVH);
544 UpdateOAISetsBiblio($biblionumberVH, $record);
545
546 #is biblio attached to setVH ?
547 my $oai_setsVH = GetOAISetsBiblio($biblionumberVH);
548 is($oai_setsVH->[0]->{id}, $setVH_id, 'id is ok');
549 is($oai_setsVH->[0]->{spec}, $setVH->{spec}, 'id is ok');
550 is($oai_setsVH->[0]->{name}, $setVH->{name}, 'id is ok');
551
552
553 # ---------- Testing CalcOAISetsBiblio ----------
554 ok (!defined(CalcOAISetsBiblio), 'CalcOAISetsBiblio without argument is undef');
555
556 my @setsEq = CalcOAISetsBiblio($record);
557 is_deeply(@setsEq, $setVH_id, 'The $record only belongs to $setVH');
558
559 #Testing CalcOAISetsBiblio for a mapping which operator is 'notequal'
560 #Create a set
561 my $setNotVH = {
562     'spec' => 'Set where Author is NOT Victor Hugo',
563     'name' => 'NOT VH'
564 };
565 my $setNotVH_id = AddOAISet($setNotVH);
566
567 #Create mappings : 'author' should NOT be 'Victor Hugo'
568 $marcflavour = C4::Context->preference('marcflavour');
569 my $mappingsNotVH;
570
571 if ($marcflavour eq 'UNIMARC' ){
572     $mappingsNotVH = [
573         {
574             marcfield => '200',
575             marcsubfield => 'f',
576             operator => 'notequal',
577             marcvalue => 'Victor Hugo'
578         }
579     ];
580 }
581 else {
582     $mappingsNotVH = [
583             {
584                 marcfield => '100',
585                 marcsubfield => 'a',
586                 operator => 'notequal',
587                 marcvalue => 'Victor Hugo'
588             }
589     ];
590 }
591 ModOAISetMappings($setNotVH_id, $mappingsNotVH);
592
593
594 #Create a biblio notice corresponding at one of mappings
595 my $biblionumberNotVH = create_helper_biblio('Sponge, Bob');
596
597 #Update
598 $record = GetMarcBiblio($biblionumberNotVH);
599 UpdateOAISetsBiblio($biblionumberNotVH, $record);
600
601 my @setsNotEq = CalcOAISetsBiblio($record);
602 is_deeply(@setsNotEq, $setNotVH_id, 'The $record only belongs to $setNotVH');
603
604
605
606 # ---------- Subs --------------------------------
607
608
609 # Helper method to set up a Biblio.
610 sub create_helper_biblio {
611     my $author = shift;
612
613     return unless (defined($author));
614
615     my $marcflavour = C4::Context->preference('marcflavour');
616     my $bib = MARC::Record->new();
617     my $title = 'Silence in the library';
618
619     if ($marcflavour eq 'UNIMARC' ){
620         $bib->append_fields(
621             MARC::Field->new('200', ' ', ' ', f => $author),
622             MARC::Field->new('200', ' ', ' ', a => $title),
623         );
624     }
625     else{
626         $bib->append_fields(
627             MARC::Field->new('100', ' ', ' ', a => $author),
628             MARC::Field->new('245', ' ', ' ', a => $title),
629         );
630     }
631     my ($biblionumber)= AddBiblio($bib, '');
632     return $biblionumber;
633 }
634
635 $dbh->rollback;