|
Lines 1-6
Link Here
|
| 1 |
#!/usr/bin/perl |
1 |
#!/usr/bin/perl |
| 2 |
|
2 |
|
| 3 |
# Copyright Tamil s.a.r.l. 2015 |
3 |
# Copyright Tamil s.a.r.l. 2016 |
| 4 |
# |
4 |
# |
| 5 |
# This file is part of Koha. |
5 |
# This file is part of Koha. |
| 6 |
# |
6 |
# |
|
Lines 21-32
Link Here
|
| 21 |
use Modern::Perl; |
21 |
use Modern::Perl; |
| 22 |
use C4::Context; |
22 |
use C4::Context; |
| 23 |
use C4::Biblio; |
23 |
use C4::Biblio; |
| 24 |
use Test::More tests => 13; |
24 |
use Test::More tests => 23; |
| 25 |
use Test::MockModule; |
25 |
use Test::MockModule; |
| 26 |
use Test::Warn; |
26 |
use Test::Warn; |
| 27 |
use DateTime; |
27 |
use DateTime; |
| 28 |
use XML::Simple; |
28 |
use XML::Simple; |
| 29 |
use t::lib::Mocks; |
29 |
use t::lib::Mocks; |
|
|
30 |
use t::lib::TestBuilder; |
| 31 |
|
| 32 |
use YAML; |
| 33 |
use Data::Dump qw/dump/; |
| 34 |
|
| 30 |
|
35 |
|
| 31 |
|
36 |
|
| 32 |
BEGIN { |
37 |
BEGIN { |
|
Lines 47-94
BEGIN {
Link Here
|
| 47 |
# Mocked CGI module in order to be able to send CGI parameters to OAI Server |
52 |
# Mocked CGI module in order to be able to send CGI parameters to OAI Server |
| 48 |
my %param; |
53 |
my %param; |
| 49 |
my $module = Test::MockModule->new('CGI'); |
54 |
my $module = Test::MockModule->new('CGI'); |
| 50 |
$module->mock('Vars', sub { %param; }); |
55 |
$module->mock('Vars', sub { %param; }); |
| 51 |
|
56 |
|
|
|
57 |
my $schema = Koha::Database->schema; |
| 58 |
$schema->storage->txn_begin; |
| 52 |
my $dbh = C4::Context->dbh; |
59 |
my $dbh = C4::Context->dbh; |
| 53 |
$dbh->{AutoCommit} = 0; |
60 |
|
| 54 |
$dbh->{RaiseError} = 1; |
61 |
my $builder = t::lib::TestBuilder->new; |
| 55 |
$dbh->do('DELETE FROM issues'); |
62 |
|
| 56 |
$dbh->do('DELETE FROM biblio'); |
63 |
$dbh->do('SET FOREIGN_KEY_CHECKS = 0'); |
| 57 |
$dbh->do('DELETE FROM biblioitems'); |
64 |
$dbh->do('TRUNCATE biblio'); |
| 58 |
$dbh->do('DELETE FROM items'); |
65 |
$dbh->do('TRUNCATE biblioitems'); |
|
|
66 |
$dbh->do('TRUNCATE issues'); |
| 59 |
|
67 |
|
| 60 |
# Add 10 biblio records |
68 |
# Add 10 biblio records |
| 61 |
my @bibs = map { |
69 |
our $tz = DateTime::TimeZone->new( name => 'local' ); |
|
|
70 |
my $date_added = DateTime->now(time_zone =>$tz) . 'Z'; |
| 71 |
my $date_to = substr($date_added, 0, 10) . 'T23:59:59Z'; |
| 72 |
|
| 73 |
my (@header, @metadata, @record); |
| 74 |
map { |
| 62 |
my $record = MARC::Record->new(); |
75 |
my $record = MARC::Record->new(); |
| 63 |
$record->append_fields( MARC::Field->new('245', '', '', 'a' => "Title $_" ) ); |
76 |
$record->append_fields( MARC::Field->new('245', '', '', 'a' => "Title $_" ) ); |
| 64 |
my ($biblionumber) = AddBiblio($record, ''); |
77 |
my ($biblionumber) = AddBiblio($record, ''); |
| 65 |
$biblionumber; |
78 |
$record = GetMarcBiblio($biblionumber); |
|
|
79 |
$record = XMLin($record->as_xml_record); |
| 80 |
$header[$biblionumber] = { datestamp => $date_added, identifier => "TEST:$biblionumber" }; |
| 81 |
delete $record->{$_} for (('xmlns','xmlns:xsi','xsi:schemaLocation')); |
| 82 |
$metadata[$biblionumber] = { collection => { |
| 83 |
record => $record, |
| 84 |
"xmlns" => "http://www.loc.gov/MARC21/slim", |
| 85 |
"xmlns:xsi" => "http://www.w3.org/2001/XMLSchema-instance", |
| 86 |
"xsi:schemaLocation" => "http://www.loc.gov/MARC21/slim http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd", |
| 87 |
} }; |
| 88 |
$record[$biblionumber] = { |
| 89 |
header => $header[$biblionumber], |
| 90 |
metadata => $metadata[$biblionumber], |
| 91 |
}; |
| 92 |
$biblionumber => undef; |
| 66 |
} (1..10); |
93 |
} (1..10); |
| 67 |
|
94 |
|
| 68 |
t::lib::Mocks::mock_preference('LibraryName', 'My Library'); |
95 |
|
| 69 |
t::lib::Mocks::mock_preference('OAI::PMH', 1); |
96 |
my $syspref = { |
| 70 |
t::lib::Mocks::mock_preference('OAI-PMH:archiveID', 'TEST'); |
97 |
'LibraryName' => 'My Library', |
| 71 |
t::lib::Mocks::mock_preference('OAI-PMH:ConfFile', '' ); |
98 |
'OAI::PMH' => 1, |
| 72 |
t::lib::Mocks::mock_preference('OAI-PMH:MaxCount', 3); |
99 |
'OAI-PMH:archiveID' => 'TEST', |
| 73 |
t::lib::Mocks::mock_preference('OAI-PMH:DeletedRecord', 'persistent'); |
100 |
'OAI-PMH:ConfFile' => '', |
| 74 |
|
101 |
'OAI-PMH:MaxCount' => 3, |
| 75 |
%param = ( verb => 'ListMetadataFormats' ); |
102 |
'OAI-PMH:DeletedRecord' => 'persistent', |
| 76 |
my $response; |
|
|
| 77 |
my $get_response = sub { |
| 78 |
my $stdout; |
| 79 |
local *STDOUT; |
| 80 |
open STDOUT, '>', \$stdout; |
| 81 |
Koha::OAI::Server::Repository->new(); |
| 82 |
$response = XMLin($stdout); |
| 83 |
}; |
103 |
}; |
| 84 |
$get_response->(); |
104 |
while ( my ($name, $value) = each %$syspref ) { |
| 85 |
my $now = DateTime->now . 'Z'; |
105 |
t::lib::Mocks::mock_preference( $name => $value ); |
| 86 |
my $expected = { |
106 |
} |
| 87 |
request => 'http://localhost', |
107 |
|
| 88 |
responseDate => $now, |
108 |
|
| 89 |
xmlns => 'http://www.openarchives.org/OAI/2.0/', |
109 |
sub test_query { |
| 90 |
'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance', |
110 |
my ($test, $param, $expected) = @_; |
| 91 |
'xsi:schemaLocation' => 'http://www.openarchives.org/OAI/2.0/ http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd', |
111 |
|
|
|
112 |
%param = %$param; |
| 113 |
my %full_expected = ( |
| 114 |
%$expected, |
| 115 |
( |
| 116 |
request => 'http://localhost', |
| 117 |
responseDate => DateTime->now . 'Z', |
| 118 |
xmlns => 'http://www.openarchives.org/OAI/2.0/', |
| 119 |
'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance', |
| 120 |
'xsi:schemaLocation' => 'http://www.openarchives.org/OAI/2.0/ http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd', |
| 121 |
) |
| 122 |
); |
| 123 |
|
| 124 |
my $response; |
| 125 |
{ |
| 126 |
my $stdout; |
| 127 |
local *STDOUT; |
| 128 |
open STDOUT, '>', \$stdout; |
| 129 |
Koha::OAI::Server::Repository->new(); |
| 130 |
$response = XMLin($stdout); |
| 131 |
} |
| 132 |
|
| 133 |
is_deeply($response, \%full_expected, $test); |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
test_query('ListMetadataFormats', {verb => 'ListMetadataFormats'}, { |
| 92 |
ListMetadataFormats => { |
138 |
ListMetadataFormats => { |
| 93 |
metadataFormat => [ |
139 |
metadataFormat => [ |
| 94 |
{ |
140 |
{ |
|
Lines 103-125
my $expected = {
Link Here
|
| 103 |
}, |
149 |
}, |
| 104 |
], |
150 |
], |
| 105 |
}, |
151 |
}, |
| 106 |
}; |
152 |
} ); |
| 107 |
is_deeply($response, $expected, "ListMetadataFormats"); |
153 |
|
| 108 |
|
154 |
test_query('ListIdentifiers without metadataPrefix', {verb => 'ListIdentifiers'}, { |
| 109 |
%param = ( verb => 'ListIdentifiers' ); |
|
|
| 110 |
$get_response->(); |
| 111 |
$now = DateTime->now . 'Z'; |
| 112 |
$expected = { |
| 113 |
request => 'http://localhost', |
| 114 |
responseDate => $now, |
| 115 |
xmlns => 'http://www.openarchives.org/OAI/2.0/', |
| 116 |
'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance', |
| 117 |
'xsi:schemaLocation' => 'http://www.openarchives.org/OAI/2.0/ http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd', |
| 118 |
error => { |
155 |
error => { |
| 119 |
code => 'badArgument', |
156 |
code => 'badArgument', |
| 120 |
content => "Required argument 'metadataPrefix' was undefined", |
157 |
content => "Required argument 'metadataPrefix' was undefined", |
| 121 |
}, |
158 |
}, |
| 122 |
}; |
159 |
}); |
| 123 |
is_deeply($response, $expected, "ListIdentifiers without metadaPrefix argument"); |
160 |
|
|
|
161 |
|
| 162 |
test_query('ListIdentifiers', {verb => 'ListIdentifiers', metadataPrefix => 'marcxml'}, { |
| 163 |
ListIdentifiers => { |
| 164 |
header => [ @header[1..3] ], |
| 165 |
resumptionToken => { |
| 166 |
content => "marcxml/3/1970-01-01T00:00:00Z/$date_to/", |
| 167 |
cursor => 3, |
| 168 |
}, |
| 169 |
}, |
| 170 |
}); |
| 171 |
|
| 172 |
test_query('ListIdentifiers', {verb => 'ListIdentifiers', metadataPrefix => 'marcxml'}, { |
| 173 |
ListIdentifiers => { |
| 174 |
header => [ @header[1..3] ], |
| 175 |
resumptionToken => { |
| 176 |
content => "marcxml/3/1970-01-01T00:00:00Z/$date_to/", |
| 177 |
cursor => 3, |
| 178 |
}, |
| 179 |
}, |
| 180 |
}); |
| 181 |
|
| 182 |
test_query( |
| 183 |
'ListIdentifiers with resumptionToken 1', |
| 184 |
{ verb => 'ListIdentifiers', resumptionToken => "marcxml/3/1970-01-01T00:00:00Z/$date_to/" }, |
| 185 |
{ |
| 186 |
ListIdentifiers => { |
| 187 |
header => [ @header[4..6] ], |
| 188 |
resumptionToken => { |
| 189 |
content => "marcxml/6/1970-01-01T00:00:00Z/$date_to/", |
| 190 |
cursor => 6, |
| 191 |
}, |
| 192 |
}, |
| 193 |
|
| 194 |
}, |
| 195 |
); |
| 196 |
|
| 197 |
test_query( |
| 198 |
'ListIdentifiers with resumptionToken 2', |
| 199 |
{ verb => 'ListIdentifiers', resumptionToken => "marcxml/6/1970-01-01T00:00:00Z/$date_to/" }, |
| 200 |
{ |
| 201 |
ListIdentifiers => { |
| 202 |
header => [ @header[7..9] ], |
| 203 |
resumptionToken => { |
| 204 |
content => "marcxml/9/1970-01-01T00:00:00Z/$date_to/", |
| 205 |
cursor => 9, |
| 206 |
}, |
| 207 |
}, |
| 208 |
|
| 209 |
}, |
| 210 |
); |
| 211 |
|
| 212 |
test_query( |
| 213 |
'ListIdentifiers with resumptionToken 3, response without resumption', |
| 214 |
{ verb => 'ListIdentifiers', resumptionToken => "marcxml/9/1970-01-01T00:00:00Z/$date_to/" }, |
| 215 |
{ |
| 216 |
ListIdentifiers => { |
| 217 |
header => $header[10], |
| 218 |
}, |
| 219 |
|
| 220 |
}, |
| 221 |
); |
| 222 |
|
| 223 |
test_query('ListRecords without metadataPrefix', {verb => 'ListRecords'}, { |
| 224 |
error => { |
| 225 |
code => 'badArgument', |
| 226 |
content => "Required argument 'metadataPrefix' was undefined", |
| 227 |
}, |
| 228 |
}); |
| 229 |
|
| 230 |
test_query('ListRecords', {verb => 'ListRecords', metadataPrefix => 'marcxml'}, { |
| 231 |
ListRecords => { |
| 232 |
record => [ @record[1..3] ], |
| 233 |
resumptionToken => { |
| 234 |
content => "marcxml/3/1970-01-01T00:00:00Z/$date_to/", |
| 235 |
cursor => 3, |
| 236 |
}, |
| 237 |
}, |
| 238 |
}); |
| 239 |
|
| 240 |
test_query( |
| 241 |
'ListRecords with resumptionToken 1', |
| 242 |
{ verb => 'ListRecords', resumptionToken => "marcxml/3/1970-01-01T00:00:00Z/$date_to/" }, |
| 243 |
{ ListRecords => { |
| 244 |
record => [ @record[4..6] ], |
| 245 |
resumptionToken => { |
| 246 |
content => "marcxml/6/1970-01-01T00:00:00Z/$date_to/", |
| 247 |
cursor => 6, |
| 248 |
}, |
| 249 |
}, |
| 250 |
}); |
| 251 |
|
| 252 |
test_query( |
| 253 |
'ListRecords with resumptionToken 2', |
| 254 |
{ verb => 'ListRecords', resumptionToken => "marcxml/6/1970-01-01T00:00:00Z/$date_to/" }, |
| 255 |
{ ListRecords => { |
| 256 |
record => [ @record[7..9] ], |
| 257 |
resumptionToken => { |
| 258 |
content => "marcxml/9/1970-01-01T00:00:00Z/$date_to/", |
| 259 |
cursor => 9, |
| 260 |
}, |
| 261 |
}, |
| 262 |
}); |
| 263 |
|
| 264 |
# Last record, so no resumption token |
| 265 |
test_query( |
| 266 |
'ListRecords with resumptionToken 3, response without resumption', |
| 267 |
{ verb => 'ListRecords', resumptionToken => "marcxml/9/1970-01-01T00:00:00Z/$date_to/" }, |
| 268 |
{ ListRecords => { |
| 269 |
record => $record[10], |
| 270 |
}, |
| 271 |
}); |
| 124 |
|
272 |
|
| 125 |
$dbh->rollback; |
273 |
$schema->storage->txn_rollback; |
| 126 |
- |
|
|