3 # Copyright Tamil s.a.r.l. 2016
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
21 use Test::Deep qw( cmp_deeply re );
22 use Test::MockTime qw/set_fixed_time set_relative_time restore_time/;
24 use Test::More tests => 33;
34 use t::lib::TestBuilder;
38 use C4::OAI::Sets qw(AddOAISet);
40 use Koha::Biblio::Metadatas;
45 use_ok('Koha::OAI::Server::DeletedRecord');
46 use_ok('Koha::OAI::Server::Description');
47 use_ok('Koha::OAI::Server::GetRecord');
48 use_ok('Koha::OAI::Server::Identify');
49 use_ok('Koha::OAI::Server::ListBase');
50 use_ok('Koha::OAI::Server::ListIdentifiers');
51 use_ok('Koha::OAI::Server::ListMetadataFormats');
52 use_ok('Koha::OAI::Server::ListRecords');
53 use_ok('Koha::OAI::Server::ListSets');
54 use_ok('Koha::OAI::Server::Record');
55 use_ok('Koha::OAI::Server::Repository');
56 use_ok('Koha::OAI::Server::ResumptionToken');
59 use constant NUMBER_OF_MARC_RECORDS => 10;
61 # Mocked CGI module in order to be able to send CGI parameters to OAI Server
63 my $module = Test::MockModule->new('CGI');
64 $module->mock('Vars', sub { %param; });
66 my $schema = Koha::Database->schema;
67 $schema->storage->txn_begin;
68 my $dbh = C4::Context->dbh;
70 $dbh->do("SET time_zone='+00:00'");
71 $dbh->do('DELETE FROM issues');
72 $dbh->do('DELETE FROM biblio');
73 $dbh->do('DELETE FROM deletedbiblio');
74 $dbh->do('DELETE FROM deletedbiblioitems');
75 $dbh->do('DELETE FROM deleteditems');
76 $dbh->do('DELETE FROM oai_sets');
78 set_fixed_time(CORE::time());
80 my $base_datetime = dt_from_string(undef, undef, 'UTC');
81 my $date_added = $base_datetime->ymd . ' ' .$base_datetime->hms . 'Z';
82 my $date_to = substr($date_added, 0, 10) . 'T23:59:59Z';
83 my (@header, @marcxml, @oaidc, @marcxml_transformed);
84 my $sth = $dbh->prepare('UPDATE biblioitems SET timestamp=? WHERE biblionumber=?');
85 my $sth2 = $dbh->prepare('UPDATE biblio_metadata SET timestamp=? WHERE biblionumber=?');
89 foreach my $index ( 0 .. NUMBER_OF_MARC_RECORDS - 1 ) {
90 my $record = MARC::Record->new();
91 if (C4::Context->preference('marcflavour') eq 'UNIMARC') {
92 $record->append_fields( MARC::Field->new('101', '', '', 'a' => "lng" ) );
93 $record->append_fields( MARC::Field->new('200', '', '', 'a' => "Title $index" ) );
94 $record->append_fields( MARC::Field->new('952', '', '', 'a' => "Code" ) );
96 $record->append_fields( MARC::Field->new('008', ' lng' ) );
97 $record->append_fields( MARC::Field->new('245', '', '', 'a' => "Title $index" ) );
98 $record->append_fields( MARC::Field->new('952', '', '', 'a' => "Code" ) );
100 my ($biblionumber) = AddBiblio($record, '');
101 $first_bn = $biblionumber unless $first_bn;
102 my $timestamp = $base_datetime->ymd . ' ' .$base_datetime->hms;
103 $sth->execute($timestamp,$biblionumber);
104 $sth2->execute($timestamp,$biblionumber);
106 $timestamp =~ s/ /T/;
107 $record = GetMarcBiblio({ biblionumber => $biblionumber });
108 my $record_transformed = $record->clone;
109 $record_transformed->delete_fields( $record_transformed->field('952'));
110 $record_transformed = XMLin($record_transformed->as_xml_record);
111 $record = XMLin($record->as_xml_record);
112 push @header, { datestamp => $timestamp, identifier => "TEST:$biblionumber" };
114 'dc:title' => "Title $index",
115 'dc:language' => "lng",
117 'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
118 'xmlns:oai_dc' => 'http://www.openarchives.org/OAI/2.0/oai_dc/',
119 'xmlns:dc' => 'http://purl.org/dc/elements/1.1/',
120 'xsi:schemaLocation' => 'http://www.openarchives.org/OAI/2.0/oai_dc/ http://www.openarchives.org/OAI/2.0/oai_dc.xsd',
122 if (C4::Context->preference('marcflavour') eq 'UNIMARC') {
123 $dc->{'dc:identifier'} = $biblionumber;
126 header => $header[$index],
132 header => $header[$index],
138 push @marcxml_transformed, {
139 header => $header[$index],
141 record => $record_transformed,
147 'LibraryName' => 'My Library',
149 'OAI-PMH:archiveID' => 'TEST',
150 'OAI-PMH:ConfFile' => '',
151 'OAI-PMH:MaxCount' => 3,
152 'OAI-PMH:DeletedRecord' => 'persistent',
154 while ( my ($name, $value) = each %$syspref ) {
155 t::lib::Mocks::mock_preference( $name => $value );
159 my ($test, $param, $expected) = @_;
162 my %full_expected = (
165 request => 'http://localhost',
166 xmlns => 'http://www.openarchives.org/OAI/2.0/',
167 'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
168 'xsi:schemaLocation' => 'http://www.openarchives.org/OAI/2.0/ http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd',
176 open STDOUT, '>', \$stdout;
177 Koha::OAI::Server::Repository->new();
178 $response = XMLin($stdout);
181 delete $response->{responseDate};
182 unless (cmp_deeply($response, \%full_expected, $test)) {
184 "PARAM:" . YAML::XS::Dump($param) .
185 "EXPECTED:" . YAML::XS::Dump(\%full_expected) .
186 "RESPONSE:" . YAML::XS::Dump($response);
190 test_query('ListMetadataFormats', {verb => 'ListMetadataFormats'}, {
191 ListMetadataFormats => {
194 metadataNamespace => 'http://www.openarchives.org/OAI/2.0/oai_dc/',
195 metadataPrefix=> 'oai_dc',
196 schema => 'http://www.openarchives.org/OAI/2.0/oai_dc.xsd',
199 metadataNamespace => 'http://www.loc.gov/MARC21/slim http://www.loc.gov/standards/marcxml/schema/MARC21slim',
200 metadataPrefix => 'marc21',
201 schema => 'http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd',
204 metadataNamespace => 'http://www.loc.gov/MARC21/slim http://www.loc.gov/standards/marcxml/schema/MARC21slim',
205 metadataPrefix => 'marcxml',
206 schema => 'http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd',
212 test_query('ListIdentifiers without metadataPrefix', {verb => 'ListIdentifiers'}, {
214 code => 'badArgument',
215 content => "Required argument 'metadataPrefix' was undefined",
219 test_query('ListIdentifiers', {verb => 'ListIdentifiers', metadataPrefix => 'marcxml'}, {
221 header => [ @header[0..2] ],
223 content => re( qr{^marcxml/3////0/0/\d+$} ),
229 test_query('ListIdentifiers', {verb => 'ListIdentifiers', metadataPrefix => 'marcxml'}, {
231 header => [ @header[0..2] ],
233 content => re( qr{^marcxml/3////0/0/\d+$} ),
240 'ListIdentifiers with resumptionToken 1',
241 { verb => 'ListIdentifiers', resumptionToken => "marcxml/3/1970-01-01T00:00:00Z/$date_to//0/0/" . ($first_bn + 3) },
244 header => [ @header[3..5] ],
246 content => re( qr{^marcxml/6/1970-01-01T00:00:00Z/$date_to//0/0/\d+$} ),
254 'ListIdentifiers with resumptionToken 2',
255 { verb => 'ListIdentifiers', resumptionToken => "marcxml/6/1970-01-01T00:00:00Z/$date_to//0/0/" . ($first_bn + 6) },
258 header => [ @header[6..8] ],
260 content => re( qr{^marcxml/9/1970-01-01T00:00:00Z/$date_to//0/0/\d+$} ),
268 'ListIdentifiers with resumptionToken 3, response without resumption',
269 { verb => 'ListIdentifiers', resumptionToken => "marcxml/9/1970-01-01T00:00:00Z/$date_to//0/0/" . ($first_bn + 9) },
272 header => $header[9],
277 test_query('ListRecords marcxml without metadataPrefix', {verb => 'ListRecords'}, {
279 code => 'badArgument',
280 content => "Required argument 'metadataPrefix' was undefined",
284 test_query('ListRecords marcxml', {verb => 'ListRecords', metadataPrefix => 'marcxml'}, {
286 record => [ @marcxml[0..2] ],
288 content => re( qr{^marcxml/3////0/0/\d+$} ),
295 'ListRecords marcxml with resumptionToken 1',
296 { verb => 'ListRecords', resumptionToken => "marcxml/3////0/0/" . ($first_bn + 3) },
298 record => [ @marcxml[3..5] ],
300 content => re( qr{^marcxml/6////0/0/\d+$} ),
307 'ListRecords marcxml with resumptionToken 2',
308 { verb => 'ListRecords', resumptionToken => "marcxml/6/1970-01-01T00:00:00Z/$date_to//0/0/" . ($first_bn + 6) },
310 record => [ @marcxml[6..8] ],
312 content => re( qr{^marcxml/9/1970-01-01T00:00:00Z/$date_to//0/0/\d+$} ),
318 # Last record, so no resumption token
320 'ListRecords marcxml with resumptionToken 3, response without resumption',
321 { verb => 'ListRecords', resumptionToken => "marcxml/9/1970-01-01T00:00:00Z/$date_to//0/0/" . ($first_bn + 9) },
323 record => $marcxml[9],
327 test_query('ListRecords oai_dc', {verb => 'ListRecords', metadataPrefix => 'oai_dc'}, {
329 record => [ @oaidc[0..2] ],
331 content => re( qr{^oai_dc/3////0/0/\d+$} ),
338 'ListRecords oai_dc with resumptionToken 1',
339 { verb => 'ListRecords', resumptionToken => "oai_dc/3////0/0/" . ($first_bn + 3) },
341 record => [ @oaidc[3..5] ],
343 content => re( qr{^oai_dc/6////0/0/\d+$} ),
350 'ListRecords oai_dc with resumptionToken 2',
351 { verb => 'ListRecords', resumptionToken => "oai_dc/6/1970-01-01T00:00:00Z/$date_to//0/0/" . ($first_bn + 6) },
353 record => [ @oaidc[6..8] ],
355 content => re( qr{^oai_dc/9/1970-01-01T00:00:00Z/$date_to//0/0/\d+$} ),
361 # Last record, so no resumption token
363 'ListRecords oai_dc with resumptionToken 3, response without resumption',
364 { verb => 'ListRecords', resumptionToken => "oai_dc/9/1970-01-01T00:00:00Z/$date_to//0/0/" . ($first_bn + 9) },
370 # List records, but now transformed by XSLT
371 t::lib::Mocks::mock_preference("OAI-PMH:ConfFile" => File::Spec->rel2abs(dirname(__FILE__)) . "/oaiconf.yaml");
372 test_query('ListRecords marcxml with xsl transformation',
373 { verb => 'ListRecords', metadataPrefix => 'marcxml' },
375 record => [ @marcxml_transformed[0..2] ],
377 content => re( qr{^marcxml/3////0/0/\d+$} ),
382 t::lib::Mocks::mock_preference("OAI-PMH:ConfFile" => '');
386 subtest 'Bug 19725: OAI-PMH ListRecords and ListIdentifiers should use biblio_metadata.timestamp' => sub {
389 # Wait 1 second to be sure no timestamp will be equal to $from defined below
392 # Modify record to trigger auto update of timestamp
393 (my $biblionumber = $marcxml[0]->{header}->{identifier}) =~ s/^.*:(.*)/$1/;
394 my $record = GetMarcBiblio({biblionumber => $biblionumber});
395 $record->append_fields(MARC::Field->new(999, '', '', z => '_'));
396 ModBiblio( $record, $biblionumber );
397 my $from_dt = dt_from_string(
398 Koha::Biblio::Metadatas->find({ biblionumber => $biblionumber, format => 'marcxml', schema => 'MARC21' })->timestamp
400 my $from = $from_dt->ymd . 'T' . $from_dt->hms . 'Z';
401 $oaidc[0]->{header}->{datestamp} = $from;
404 'ListRecords oai_dc with parameter from',
405 { verb => 'ListRecords', metadataPrefix => 'oai_dc', from => $from },
412 subtest 'Bug 20665: OAI-PMH Provider should reset the MySQL connection time zone' => sub {
415 # Set time zone to SYSTEM so that it can be checked later
416 $dbh->do("SET time_zone='SYSTEM'");
419 test_query('ListIdentifiers without metadataPrefix', {verb => 'ListIdentifiers'}, {
421 code => 'badArgument',
422 content => "Required argument 'metadataPrefix' was undefined",
426 my $sth = C4::Context->dbh->prepare('SELECT @@session.time_zone');
428 my ( $tz ) = $sth->fetchrow();
430 ok ( $tz eq 'SYSTEM', 'MySQL connection time zone is SYSTEM' );
434 $schema->storage->txn_rollback;
436 subtest 'ListSets tests' => sub {
440 t::lib::Mocks::mock_preference( 'OAI::PMH' => 1 );
441 t::lib::Mocks::mock_preference( 'OAI-PMH:MaxCount' => 3 );
443 $schema->storage->txn_begin;
445 $dbh->do('DELETE FROM oai_sets');
447 # Add a bunch of sets
448 my @first_page_sets = ();
449 for my $i ( 1 .. 3 ) {
452 { 'spec' => "setSpec_$i",
453 'name' => "setName_$i",
456 push @first_page_sets, { setSpec => "setSpec_$i", setName => "setName_$i" };
459 # Add more to force pagination
460 my @second_page_sets = ();
461 for my $i ( 4 .. 6 ) {
464 { 'spec' => "setSpec_$i",
465 'name' => "setName_$i",
468 push @second_page_sets, { setSpec => "setSpec_$i", setName => "setName_$i" };
472 { 'spec' => "setSpec_7",
473 'name' => "setName_7",
479 { verb => 'ListSets' },
482 content => re( qr{^/3////1/0/4$} ),
485 set => \@first_page_sets
492 { verb => 'ListSets', resumptionToken => '/3////1/0/4' },
495 content => re( qr{^/6////1/0/7$} ),
498 set => \@second_page_sets
505 { verb => 'ListSets', resumptionToken => "/6////1/0/7" },
507 set => { setSpec => "setSpec_7", setName => "setName_7" }
512 $schema->storage->txn_rollback;
515 subtest 'Tests for timestamp handling' => sub {
519 t::lib::Mocks::mock_preference( 'OAI::PMH' => 1 );
520 t::lib::Mocks::mock_preference( 'OAI-PMH:MaxCount' => 3 );
521 t::lib::Mocks::mock_preference( 'OAI-PMH:ConfFile' => File::Spec->rel2abs(dirname(__FILE__)) . '/oaiconf_items.yaml' );
523 $schema->storage->txn_begin;
525 my $sth_metadata = $dbh->prepare('UPDATE biblio_metadata SET timestamp=? WHERE biblionumber=?');
526 my $sth_del_metadata = $dbh->prepare('UPDATE deletedbiblio_metadata SET timestamp=? WHERE biblionumber=?');
527 my $sth_item = $dbh->prepare('UPDATE items SET timestamp=? WHERE itemnumber=?');
528 my $sth_del_item = $dbh->prepare('UPDATE deleteditems SET timestamp=? WHERE itemnumber=?');
530 my $builder = t::lib::TestBuilder->new;
532 set_fixed_time(CORE::time());
534 my $utc_datetime = dt_from_string(undef, undef, 'UTC');
535 my $utc_timestamp = $utc_datetime->ymd . 'T' . $utc_datetime->hms . 'Z';
536 my $timestamp = dt_from_string(undef, 'sql');
538 # Test a bib with one item
539 my $biblio1 = $builder->build_sample_biblio();
540 $sth_metadata->execute($timestamp, $biblio1->biblionumber);
541 my $item1 = $builder->build_sample_item(
543 biblionumber => $biblio1->biblionumber
546 $sth_item->execute($timestamp, $item1->itemnumber);
549 verb => 'ListRecords',
550 metadataPrefix => 'marc21',
551 from => $utc_timestamp
553 my $list_no_items = {
554 verb => 'ListRecords',
555 metadataPrefix => 'marcxml',
556 from => $utc_timestamp
561 metadataPrefix => 'marc21',
562 identifier => 'TEST:' . $biblio1->biblionumber
566 metadataPrefix => 'marcxml',
567 identifier => 'TEST:' . $biblio1->biblionumber
573 datestamp => $utc_timestamp,
574 identifier => 'TEST:' . $biblio1->biblionumber
578 GetMarcBiblio({ biblionumber => $biblio1->biblionumber, embed_items => 1, opac => 1 })->as_xml_record()
583 my $expected_no_items = {
586 datestamp => $utc_timestamp,
587 identifier => 'TEST:' . $biblio1->biblionumber
591 GetMarcBiblio({ biblionumber => $biblio1->biblionumber, embed_items => 0, opac => 1 })->as_xml_record()
598 'ListRecords - biblio with a single item',
600 { ListRecords => $expected }
603 'ListRecords - biblio with a single item (items not returned)',
605 { ListRecords => $expected_no_items }
608 'GetRecord - biblio with a single item',
610 { GetRecord => $expected }
613 'GetRecord - biblio with a single item (items not returned)',
615 { GetRecord => $expected_no_items }
618 # Add an item 10 seconds later and check results
619 set_relative_time(10);
621 $utc_datetime = dt_from_string(undef, undef, 'UTC');
622 $utc_timestamp = $utc_datetime->ymd . 'T' . $utc_datetime->hms . 'Z';
623 $timestamp = dt_from_string(undef, 'sql');
625 my $item2 = $builder->build_sample_item(
627 biblionumber => $biblio1->biblionumber
630 $sth_item->execute($timestamp, $item2->itemnumber);
632 $expected->{record}{header}{datestamp} = $utc_timestamp;
633 $expected->{record}{metadata}{record} = XMLin(
634 GetMarcBiblio({ biblionumber => $biblio1->biblionumber, embed_items => 1, opac => 1 })->as_xml_record()
638 'ListRecords - biblio with two items',
640 { ListRecords => $expected }
643 'ListRecords - biblio with two items (items not returned)',
645 { ListRecords => $expected_no_items }
648 'GetRecord - biblio with a two items',
650 { GetRecord => $expected }
653 'GetRecord - biblio with a two items (items not returned)',
655 { GetRecord => $expected_no_items }
658 # Set biblio timestamp 10 seconds later and check results
659 set_relative_time(10);
660 $utc_datetime = dt_from_string(undef, undef, 'UTC');
661 $utc_timestamp= $utc_datetime->ymd . 'T' . $utc_datetime->hms . 'Z';
662 $timestamp = dt_from_string(undef, 'sql');
664 $sth_metadata->execute($timestamp, $biblio1->biblionumber);
666 $expected->{record}{header}{datestamp} = $utc_timestamp;
667 $expected_no_items->{record}{header}{datestamp} = $utc_timestamp;
670 "ListRecords - biblio with timestamp higher than item's",
672 { ListRecords => $expected }
675 "ListRecords - biblio with timestamp higher than item's (items not returned)",
677 { ListRecords => $expected_no_items }
680 "GetRecord - biblio with timestamp higher than item's",
682 { GetRecord => $expected }
685 "GetRecord - biblio with timestamp higher than item's (items not returned)",
687 { GetRecord => $expected_no_items }
690 # Delete an item 10 seconds later and check results
691 set_relative_time(10);
692 $utc_datetime = dt_from_string(undef, undef, 'UTC');
693 $utc_timestamp = $utc_datetime->ymd . 'T' . $utc_datetime->hms . 'Z';
695 $item1->safe_delete({ skip_record_index =>1 });
696 $sth_del_item->execute($timestamp, $item1->itemnumber);
698 $expected->{record}{header}{datestamp} = $utc_timestamp;
699 $expected->{record}{metadata}{record} = XMLin(
700 GetMarcBiblio({ biblionumber => $biblio1->biblionumber, embed_items => 1, opac => 1 })->as_xml_record()
704 'ListRecords - biblio with existing and deleted item',
706 { ListRecords => $expected }
709 'ListRecords - biblio with existing and deleted item (items not returned)',
711 { ListRecords => $expected_no_items }
714 'GetRecord - biblio with existing and deleted item',
716 { GetRecord => $expected }
719 'GetRecord - biblio with existing and deleted item (items not returned)',
721 { GetRecord => $expected_no_items }
724 # Delete also the second item and verify results
725 $item2->safe_delete({ skip_record_index =>1 });
726 $sth_del_item->execute($timestamp, $item2->itemnumber);
728 $expected->{record}{metadata}{record} = XMLin(
729 GetMarcBiblio({ biblionumber => $biblio1->biblionumber, embed_items => 1, opac => 1 })->as_xml_record()
733 'ListRecords - biblio with two deleted items',
735 { ListRecords => $expected }
738 'ListRecords - biblio with two deleted items (items not returned)',
740 { ListRecords => $expected_no_items }
743 'GetRecord - biblio with two deleted items',
745 { GetRecord => $expected }
748 'GetRecord - biblio with two deleted items (items not returned)',
750 { GetRecord => $expected_no_items }
753 # Delete the biblio 10 seconds later and check results
754 set_relative_time(10);
755 $utc_datetime = dt_from_string(undef, undef, 'UTC');
756 $utc_timestamp = $utc_datetime->ymd . 'T' . $utc_datetime->hms . 'Z';
757 $timestamp = dt_from_string(undef, 'sql');
759 is(undef, DelBiblio($biblio1->biblionumber, { skip_record_index =>1 }), 'Biblio deleted');
760 $sth_del_metadata->execute($timestamp, $biblio1->biblionumber);
762 my $expected_header = {
765 datestamp => $utc_timestamp,
766 identifier => 'TEST:' . $biblio1->biblionumber,
773 'ListRecords - deleted biblio with two deleted items',
775 { ListRecords => $expected_header }
778 'ListRecords - deleted biblio with two deleted items (items not returned)',
780 { ListRecords => $expected_header }
783 'GetRecord - deleted biblio with two deleted items',
785 { GetRecord => $expected_header }
788 'GetRecord - deleted biblio with two deleted items (items not returned)',
790 { GetRecord => $expected_header }
793 # Add a second biblio 10 seconds later and check that both are returned properly
794 set_relative_time(10);
795 $utc_datetime = dt_from_string(undef, undef, 'UTC');
796 $utc_timestamp = $utc_datetime->ymd . 'T' . $utc_datetime->hms . 'Z';
797 $timestamp = dt_from_string(undef, 'sql');
799 my $biblio2 = $builder->build_sample_biblio();
800 $sth_metadata->execute($timestamp, $biblio2->biblionumber);
804 $expected_header->{record},
807 datestamp => $utc_timestamp,
808 identifier => 'TEST:' . $biblio2->biblionumber
812 GetMarcBiblio({ biblionumber => $biblio2->biblionumber, embed_items => 1, opac => 1 })->as_xml_record()
818 my $expected2_no_items = {
820 $expected_header->{record},
823 datestamp => $utc_timestamp,
824 identifier => 'TEST:' . $biblio2->biblionumber
828 GetMarcBiblio({ biblionumber => $biblio2->biblionumber, embed_items => 0, opac => 1 })->as_xml_record()
836 'ListRecords - deleted biblio and normal biblio',
838 { ListRecords => $expected2 }
841 'ListRecords - deleted biblio and normal biblio (items not returned)',
843 { ListRecords => $expected2_no_items }
848 $schema->storage->txn_rollback;