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