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