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