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>.
22 use Test::More tests => 28;
36 use_ok('Koha::OAI::Server::DeletedRecord');
37 use_ok('Koha::OAI::Server::Description');
38 use_ok('Koha::OAI::Server::GetRecord');
39 use_ok('Koha::OAI::Server::Identify');
40 use_ok('Koha::OAI::Server::ListBase');
41 use_ok('Koha::OAI::Server::ListIdentifiers');
42 use_ok('Koha::OAI::Server::ListMetadataFormats');
43 use_ok('Koha::OAI::Server::ListRecords');
44 use_ok('Koha::OAI::Server::ListSets');
45 use_ok('Koha::OAI::Server::Record');
46 use_ok('Koha::OAI::Server::Repository');
47 use_ok('Koha::OAI::Server::ResumptionToken');
50 use constant NUMBER_OF_MARC_RECORDS => 10;
52 # Mocked CGI module in order to be able to send CGI parameters to OAI Server
54 my $module = Test::MockModule->new('CGI');
55 $module->mock('Vars', sub { %param; });
57 my $schema = Koha::Database->schema;
58 $schema->storage->txn_begin;
59 my $dbh = C4::Context->dbh;
61 $dbh->do("SET time_zone='+00:00'");
62 $dbh->do('DELETE FROM issues');
63 $dbh->do('DELETE FROM biblio');
64 $dbh->do('DELETE FROM deletedbiblio');
65 $dbh->do('DELETE FROM deletedbiblioitems');
66 $dbh->do('DELETE FROM deleteditems');
67 $dbh->do('DELETE FROM oai_sets');
69 my $date_added = DateTime->now() . 'Z';
70 my $date_to = substr($date_added, 0, 10) . 'T23:59:59Z';
71 my (@header, @marcxml, @oaidc);
72 my $sth = $dbh->prepare('SELECT timestamp FROM biblioitems WHERE biblionumber=?');
75 foreach my $index ( 0 .. NUMBER_OF_MARC_RECORDS - 1 ) {
76 my $record = MARC::Record->new();
77 if (C4::Context->preference('marcflavour') eq 'UNIMARC') {
78 $record->append_fields( MARC::Field->new('101', '', '', 'a' => "lng" ) );
79 $record->append_fields( MARC::Field->new('200', '', '', 'a' => "Title $index" ) );
81 $record->append_fields( MARC::Field->new('008', ' lng' ) );
82 $record->append_fields( MARC::Field->new('245', '', '', 'a' => "Title $index" ) );
84 my ($biblionumber) = AddBiblio($record, '');
85 $sth->execute($biblionumber);
86 my $timestamp = $sth->fetchrow_array . 'Z';
88 $timestamp = manipulate_timestamp( $index, $biblionumber, $timestamp );
89 $record = GetMarcBiblio({ biblionumber => $biblionumber });
90 $record = XMLin($record->as_xml_record);
91 push @header, { datestamp => $timestamp, identifier => "TEST:$biblionumber" };
93 header => $header[$index],
96 'dc:title' => "Title $index",
97 'dc:language' => "lng",
99 'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
100 'xmlns:oai_dc' => 'http://www.openarchives.org/OAI/2.0/oai_dc/',
101 'xmlns:dc' => 'http://purl.org/dc/elements/1.1/',
102 'xsi:schemaLocation' => 'http://www.openarchives.org/OAI/2.0/oai_dc/ http://www.openarchives.org/OAI/2.0/oai_dc.xsd',
107 header => $header[$index],
115 'LibraryName' => 'My Library',
117 'OAI-PMH:archiveID' => 'TEST',
118 'OAI-PMH:ConfFile' => '',
119 'OAI-PMH:MaxCount' => 3,
120 'OAI-PMH:DeletedRecord' => 'persistent',
122 while ( my ($name, $value) = each %$syspref ) {
123 t::lib::Mocks::mock_preference( $name => $value );
127 my ($test, $param, $expected) = @_;
130 my %full_expected = (
133 request => 'http://localhost',
134 xmlns => 'http://www.openarchives.org/OAI/2.0/',
135 'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
136 'xsi:schemaLocation' => 'http://www.openarchives.org/OAI/2.0/ http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd',
144 open STDOUT, '>', \$stdout;
145 Koha::OAI::Server::Repository->new();
146 $response = XMLin($stdout);
149 delete $response->{responseDate};
150 unless (is_deeply($response, \%full_expected, $test)) {
152 "PARAM:" . Dump($param) .
153 "EXPECTED:" . Dump(\%full_expected) .
154 "RESPONSE:" . Dump($response);
158 test_query('ListMetadataFormats', {verb => 'ListMetadataFormats'}, {
159 ListMetadataFormats => {
162 metadataNamespace => 'http://www.openarchives.org/OAI/2.0/oai_dc/',
163 metadataPrefix=> 'oai_dc',
164 schema => 'http://www.openarchives.org/OAI/2.0/oai_dc.xsd',
167 metadataNamespace => 'http://www.loc.gov/MARC21/slim http://www.loc.gov/standards/marcxml/schema/MARC21slim',
168 metadataPrefix => 'marc21',
169 schema => 'http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd',
172 metadataNamespace => 'http://www.loc.gov/MARC21/slim http://www.loc.gov/standards/marcxml/schema/MARC21slim',
173 metadataPrefix => 'marcxml',
174 schema => 'http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd',
180 test_query('ListIdentifiers without metadataPrefix', {verb => 'ListIdentifiers'}, {
182 code => 'badArgument',
183 content => "Required argument 'metadataPrefix' was undefined",
187 test_query('ListIdentifiers', {verb => 'ListIdentifiers', metadataPrefix => 'marcxml'}, {
189 header => [ @header[0..2] ],
191 content => "marcxml/3/1970-01-01T00:00:00Z/$date_to//0/0",
197 test_query('ListIdentifiers', {verb => 'ListIdentifiers', metadataPrefix => 'marcxml'}, {
199 header => [ @header[0..2] ],
201 content => "marcxml/3/1970-01-01T00:00:00Z/$date_to//0/0",
208 'ListIdentifiers with resumptionToken 1',
209 { verb => 'ListIdentifiers', resumptionToken => "marcxml/3/1970-01-01T00:00:00Z/$date_to//0/0" },
212 header => [ @header[3..5] ],
214 content => "marcxml/6/1970-01-01T00:00:00Z/$date_to//0/0",
222 'ListIdentifiers with resumptionToken 2',
223 { verb => 'ListIdentifiers', resumptionToken => "marcxml/6/1970-01-01T00:00:00Z/$date_to//0/0" },
226 header => [ @header[6..8] ],
228 content => "marcxml/9/1970-01-01T00:00:00Z/$date_to//0/0",
236 'ListIdentifiers with resumptionToken 3, response without resumption',
237 { verb => 'ListIdentifiers', resumptionToken => "marcxml/9/1970-01-01T00:00:00Z/$date_to//0/0" },
240 header => $header[9],
245 test_query('ListRecords marcxml without metadataPrefix', {verb => 'ListRecords'}, {
247 code => 'badArgument',
248 content => "Required argument 'metadataPrefix' was undefined",
252 test_query('ListRecords marcxml', {verb => 'ListRecords', metadataPrefix => 'marcxml'}, {
254 record => [ @marcxml[0..2] ],
256 content => "marcxml/3/1970-01-01T00:00:00Z/$date_to//0/0",
263 'ListRecords marcxml with resumptionToken 1',
264 { verb => 'ListRecords', resumptionToken => "marcxml/3/1970-01-01T00:00:00Z/$date_to//0/0" },
266 record => [ @marcxml[3..5] ],
268 content => "marcxml/6/1970-01-01T00:00:00Z/$date_to//0/0",
275 'ListRecords marcxml with resumptionToken 2',
276 { verb => 'ListRecords', resumptionToken => "marcxml/6/1970-01-01T00:00:00Z/$date_to//0/0" },
278 record => [ @marcxml[6..8] ],
280 content => "marcxml/9/1970-01-01T00:00:00Z/$date_to//0/0",
286 # Last record, so no resumption token
288 'ListRecords marcxml with resumptionToken 3, response without resumption',
289 { verb => 'ListRecords', resumptionToken => "marcxml/9/1970-01-01T00:00:00Z/$date_to//0/0" },
291 record => $marcxml[9],
295 test_query('ListRecords oai_dc', {verb => 'ListRecords', metadataPrefix => 'oai_dc'}, {
297 record => [ @oaidc[0..2] ],
299 content => "oai_dc/3/1970-01-01T00:00:00Z/$date_to//0/0",
306 'ListRecords oai_dc with resumptionToken 1',
307 { verb => 'ListRecords', resumptionToken => "oai_dc/3/1970-01-01T00:00:00Z/$date_to//0/0" },
309 record => [ @oaidc[3..5] ],
311 content => "oai_dc/6/1970-01-01T00:00:00Z/$date_to//0/0",
318 'ListRecords oai_dc with resumptionToken 2',
319 { verb => 'ListRecords', resumptionToken => "oai_dc/6/1970-01-01T00:00:00Z/$date_to//0/0" },
321 record => [ @oaidc[6..8] ],
323 content => "oai_dc/9/1970-01-01T00:00:00Z/$date_to//0/0",
329 # Last record, so no resumption token
331 'ListRecords oai_dc with resumptionToken 3, response without resumption',
332 { verb => 'ListRecords', resumptionToken => "oai_dc/9/1970-01-01T00:00:00Z/$date_to//0/0" },
338 $schema->storage->txn_rollback;
340 sub manipulate_timestamp {
341 # This eliminates waiting a few seconds in order to get a higher timestamp
342 # Works only for 60 records..
343 my ( $index, $bibno, $timestamp ) = @_;
344 return $timestamp if $timestamp !~ /\d{2}Z/;
345 my $secs = sprintf( "%02d", $index );
346 $timestamp =~ s/\d{2}Z/${secs}Z/;
347 $dbh->do("UPDATE biblioitems SET timestamp=? WHERE biblionumber=?", undef,
348 ( $timestamp, $bibno ));