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 biblio');
63 $dbh->do('DELETE FROM deletedbiblio');
64 $dbh->do('DELETE FROM deletedbiblioitems');
65 $dbh->do('DELETE FROM deleteditems');
67 my $date_added = DateTime->now() . 'Z';
68 my $date_to = substr($date_added, 0, 10) . 'T23:59:59Z';
69 my (@header, @marcxml, @oaidc);
70 my $sth = $dbh->prepare('SELECT timestamp FROM biblioitems WHERE biblionumber=?');
73 foreach my $index ( 0 .. NUMBER_OF_MARC_RECORDS - 1 ) {
74 my $record = MARC::Record->new();
75 $record->append_fields( MARC::Field->new('245', '', '', 'a' => "Title $index" ) );
76 my ($biblionumber) = AddBiblio($record, '');
77 $sth->execute($biblionumber);
78 my $timestamp = $sth->fetchrow_array . 'Z';
80 $timestamp = manipulate_timestamp( $index, $biblionumber, $timestamp );
81 $record = GetMarcBiblio($biblionumber);
82 $record = XMLin($record->as_xml_record);
83 push @header, { datestamp => $timestamp, identifier => "TEST:$biblionumber" };
85 header => $header[$index],
88 'dc:title' => "Title $index",
91 'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
92 'xmlns:oai_dc' => 'http://www.openarchives.org/OAI/2.0/oai_dc/',
93 'xmlns:dc' => 'http://purl.org/dc/elements/1.1/',
94 'xsi:schemaLocation' => 'http://www.openarchives.org/OAI/2.0/oai_dc/ http://www.openarchives.org/OAI/2.0/oai_dc.xsd',
99 header => $header[$index],
107 'LibraryName' => 'My Library',
109 'OAI-PMH:archiveID' => 'TEST',
110 'OAI-PMH:ConfFile' => '',
111 'OAI-PMH:MaxCount' => 3,
112 'OAI-PMH:DeletedRecord' => 'persistent',
114 while ( my ($name, $value) = each %$syspref ) {
115 t::lib::Mocks::mock_preference( $name => $value );
119 my ($test, $param, $expected) = @_;
122 my %full_expected = (
125 request => 'http://localhost',
126 responseDate => DateTime->now . 'Z',
127 xmlns => 'http://www.openarchives.org/OAI/2.0/',
128 'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
129 'xsi:schemaLocation' => 'http://www.openarchives.org/OAI/2.0/ http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd',
137 open STDOUT, '>', \$stdout;
138 Koha::OAI::Server::Repository->new();
139 $response = XMLin($stdout);
142 unless (is_deeply($response, \%full_expected, $test)) {
144 "PARAM:" . Dump($param) .
145 "EXPECTED:" . Dump(\%full_expected) .
146 "RESPONSE:" . Dump($response);
150 test_query('ListMetadataFormats', {verb => 'ListMetadataFormats'}, {
151 ListMetadataFormats => {
154 metadataNamespace => 'http://www.openarchives.org/OAI/2.0/oai_dc/',
155 metadataPrefix=> 'oai_dc',
156 schema => 'http://www.openarchives.org/OAI/2.0/oai_dc.xsd',
159 metadataNamespace => 'http://www.loc.gov/MARC21/slim http://www.loc.gov/standards/marcxml/schema/MARC21slim',
160 metadataPrefix => 'marc21',
161 schema => 'http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd',
164 metadataNamespace => 'http://www.loc.gov/MARC21/slim http://www.loc.gov/standards/marcxml/schema/MARC21slim',
165 metadataPrefix => 'marcxml',
166 schema => 'http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd',
172 test_query('ListIdentifiers without metadataPrefix', {verb => 'ListIdentifiers'}, {
174 code => 'badArgument',
175 content => "Required argument 'metadataPrefix' was undefined",
179 test_query('ListIdentifiers', {verb => 'ListIdentifiers', metadataPrefix => 'marcxml'}, {
181 header => [ @header[0..2] ],
183 content => "marcxml/3/1970-01-01T00:00:00Z/$date_to//0/0",
189 test_query('ListIdentifiers', {verb => 'ListIdentifiers', metadataPrefix => 'marcxml'}, {
191 header => [ @header[0..2] ],
193 content => "marcxml/3/1970-01-01T00:00:00Z/$date_to//0/0",
200 'ListIdentifiers with resumptionToken 1',
201 { verb => 'ListIdentifiers', resumptionToken => "marcxml/3/1970-01-01T00:00:00Z/$date_to//0/0" },
204 header => [ @header[3..5] ],
206 content => "marcxml/6/1970-01-01T00:00:00Z/$date_to//0/0",
214 'ListIdentifiers with resumptionToken 2',
215 { verb => 'ListIdentifiers', resumptionToken => "marcxml/6/1970-01-01T00:00:00Z/$date_to//0/0" },
218 header => [ @header[6..8] ],
220 content => "marcxml/9/1970-01-01T00:00:00Z/$date_to//0/0",
228 'ListIdentifiers with resumptionToken 3, response without resumption',
229 { verb => 'ListIdentifiers', resumptionToken => "marcxml/9/1970-01-01T00:00:00Z/$date_to//0/0" },
232 header => $header[9],
237 test_query('ListRecords marcxml without metadataPrefix', {verb => 'ListRecords'}, {
239 code => 'badArgument',
240 content => "Required argument 'metadataPrefix' was undefined",
244 test_query('ListRecords marcxml', {verb => 'ListRecords', metadataPrefix => 'marcxml'}, {
246 record => [ @marcxml[0..2] ],
248 content => "marcxml/3/1970-01-01T00:00:00Z/$date_to//0/0",
255 'ListRecords marcxml with resumptionToken 1',
256 { verb => 'ListRecords', resumptionToken => "marcxml/3/1970-01-01T00:00:00Z/$date_to//0/0" },
258 record => [ @marcxml[3..5] ],
260 content => "marcxml/6/1970-01-01T00:00:00Z/$date_to//0/0",
267 'ListRecords marcxml with resumptionToken 2',
268 { verb => 'ListRecords', resumptionToken => "marcxml/6/1970-01-01T00:00:00Z/$date_to//0/0" },
270 record => [ @marcxml[6..8] ],
272 content => "marcxml/9/1970-01-01T00:00:00Z/$date_to//0/0",
278 # Last record, so no resumption token
280 'ListRecords marcxml with resumptionToken 3, response without resumption',
281 { verb => 'ListRecords', resumptionToken => "marcxml/9/1970-01-01T00:00:00Z/$date_to//0/0" },
283 record => $marcxml[9],
287 test_query('ListRecords oai_dc', {verb => 'ListRecords', metadataPrefix => 'oai_dc'}, {
289 record => [ @oaidc[0..2] ],
291 content => "oai_dc/3/1970-01-01T00:00:00Z/$date_to//0/0",
298 'ListRecords oai_dc with resumptionToken 1',
299 { verb => 'ListRecords', resumptionToken => "oai_dc/3/1970-01-01T00:00:00Z/$date_to//0/0" },
301 record => [ @oaidc[3..5] ],
303 content => "oai_dc/6/1970-01-01T00:00:00Z/$date_to//0/0",
310 'ListRecords oai_dc with resumptionToken 2',
311 { verb => 'ListRecords', resumptionToken => "oai_dc/6/1970-01-01T00:00:00Z/$date_to//0/0" },
313 record => [ @oaidc[6..8] ],
315 content => "oai_dc/9/1970-01-01T00:00:00Z/$date_to//0/0",
321 # Last record, so no resumption token
323 'ListRecords oai_dc with resumptionToken 3, response without resumption',
324 { verb => 'ListRecords', resumptionToken => "oai_dc/9/1970-01-01T00:00:00Z/$date_to//0/0" },
330 $schema->storage->txn_rollback;
332 sub manipulate_timestamp {
333 # This eliminates waiting a few seconds in order to get a higher timestamp
334 # Works only for 60 records..
335 my ( $index, $bibno, $timestamp ) = @_;
336 return $timestamp if $timestamp !~ /\d{2}Z/;
337 my $secs = sprintf( "%02d", $index );
338 $timestamp =~ s/\d{2}Z/${secs}Z/;
339 $dbh->do("UPDATE biblioitems SET timestamp=? WHERE biblionumber=?", undef,
340 ( $timestamp, $bibno ));