View | Details | Raw Unified | Return to bug 17493
Collapse All | Expand All

(-)a/t/db_dependent/OAI/Server.t (-50 / +265 lines)
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 => 27;
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
use YAML;
30
32
31
33
32
BEGIN {
34
BEGIN {
Lines 47-94 BEGIN { Link Here
47
# Mocked CGI module in order to be able to send CGI parameters to OAI Server
49
# Mocked CGI module in order to be able to send CGI parameters to OAI Server
48
my %param;
50
my %param;
49
my $module = Test::MockModule->new('CGI');
51
my $module = Test::MockModule->new('CGI');
50
$module->mock('Vars', sub { %param; });
52
    $module->mock('Vars', sub { %param; });
51
53
54
my $schema = Koha::Database->schema;
55
$schema->storage->txn_begin;
52
my $dbh = C4::Context->dbh;
56
my $dbh = C4::Context->dbh;
53
$dbh->{AutoCommit} = 0;
57
54
$dbh->{RaiseError} = 1;
58
my $builder = t::lib::TestBuilder->new;
55
$dbh->do('DELETE FROM issues');
59
56
$dbh->do('DELETE FROM biblio');
60
$dbh->do('SET FOREIGN_KEY_CHECKS = 0');
57
$dbh->do('DELETE FROM biblioitems');
61
$dbh->do('TRUNCATE biblio');
58
$dbh->do('DELETE FROM items');
62
$dbh->do('TRUNCATE biblioitems');
63
$dbh->do('TRUNCATE issues');
59
64
60
# Add 10 biblio records
65
# Add 10 biblio records
61
my @bibs = map {
66
our $tz = DateTime::TimeZone->new( name => 'local' );
67
my $date_added = DateTime->now(time_zone =>$tz) . 'Z';
68
my $date_to = substr($date_added, 0, 10) . 'T23:59:59Z';
69
70
my (@header, @marcxml, @oaidc);
71
map {
62
    my $record = MARC::Record->new();
72
    my $record = MARC::Record->new();
63
    $record->append_fields( MARC::Field->new('245', '', '', 'a' => "Title $_" ) );
73
    $record->append_fields( MARC::Field->new('245', '', '', 'a' => "Title $_" ) );
64
    my ($biblionumber) = AddBiblio($record, '');
74
    my ($biblionumber) = AddBiblio($record, '');
65
    $biblionumber;
75
    $record = GetMarcBiblio($biblionumber);
76
    $record = XMLin($record->as_xml_record);
77
    $header[$biblionumber] = { datestamp => $date_added, identifier => "TEST:$biblionumber" };
78
    delete $record->{$_} for (('xmlns','xmlns:xsi','xsi:schemaLocation'));
79
    $oaidc[$biblionumber] = {
80
        header => $header[$biblionumber],
81
        metadata => {
82
            'oai_dc:dcCollection' => {
83
                'oai_dc:dc' => {
84
                    'dc:title' => {
85
                        'content' => "Title $biblionumber",
86
                        'xmlns:dc' => 'http://purl.org/dc/elements/1.1/'
87
                    },
88
                    'dc:language' => {
89
                        'xmlns:dc' => 'http://purl.org/dc/elements/1.1/'
90
                    },
91
                    'dc:type' => {
92
                        'xmlns:dc' => 'http://purl.org/dc/elements/1.1/'
93
                    },
94
                },
95
                "xmlns:oai_dc" => "http://www.openarchives.org/OAI/2.0/oai_dc/",
96
                "xmlns:xsi" => "http://www.w3.org/2001/XMLSchema-instance",
97
                "xsi:schemaLocation" => "http://www.openarchives.org/OAI/2.0/oai_dc/ http://www.openarchives.org/OAI/2.0/oai_dc.xsd",
98
            },
99
        },
100
    };
101
    $marcxml[$biblionumber] = {
102
        header => $header[$biblionumber],
103
        metadata => {
104
            collection => {
105
            record => $record,
106
                "xmlns" => "http://www.loc.gov/MARC21/slim",
107
                "xmlns:xsi" => "http://www.w3.org/2001/XMLSchema-instance",
108
                "xsi:schemaLocation" => "http://www.loc.gov/MARC21/slim http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd",
109
            },
110
        },
111
    };
112
    $biblionumber => undef;
66
} (1..10);
113
} (1..10);
67
114
68
t::lib::Mocks::mock_preference('LibraryName', 'My Library');
115
69
t::lib::Mocks::mock_preference('OAI::PMH', 1);
116
my $syspref = {
70
t::lib::Mocks::mock_preference('OAI-PMH:archiveID', 'TEST');
117
    'LibraryName'           => 'My Library',
71
t::lib::Mocks::mock_preference('OAI-PMH:ConfFile', '' );
118
    'OAI::PMH'              => 1,
72
t::lib::Mocks::mock_preference('OAI-PMH:MaxCount', 3);
119
    'OAI-PMH:archiveID'     => 'TEST',
73
t::lib::Mocks::mock_preference('OAI-PMH:DeletedRecord', 'persistent');
120
    'OAI-PMH:ConfFile'      => '',
74
121
    'OAI-PMH:MaxCount'      => 3,
75
%param = ( verb => 'ListMetadataFormats' );
122
    '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
};
123
};
84
$get_response->();
124
while ( my ($name, $value) = each %$syspref ) {
85
my $now = DateTime->now . 'Z';
125
    t::lib::Mocks::mock_preference( $name => $value );
86
my $expected = {
126
}
87
    request => 'http://localhost',
127
88
    responseDate => $now,
128
89
    xmlns => 'http://www.openarchives.org/OAI/2.0/',
129
sub test_query {
90
    'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
130
    my ($test, $param, $expected) = @_;
91
    'xsi:schemaLocation' => 'http://www.openarchives.org/OAI/2.0/ http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd',
131
132
    %param = %$param;
133
    my %full_expected = (
134
        %$expected,
135
        (
136
            request => 'http://localhost',
137
            responseDate => DateTime->now . 'Z',
138
            xmlns => 'http://www.openarchives.org/OAI/2.0/',
139
            'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
140
            'xsi:schemaLocation' => 'http://www.openarchives.org/OAI/2.0/ http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd',
141
        )
142
    );
143
144
    my $response;
145
    {
146
        my $stdout;
147
        local *STDOUT;
148
        open STDOUT, '>', \$stdout;
149
        Koha::OAI::Server::Repository->new();
150
        $response = XMLin($stdout);
151
    }
152
153
    unless (is_deeply($response, \%full_expected, $test)) {
154
        diag
155
            "PARAM:" . Dump($param) .
156
            "EXPECTED:" . Dump(\%full_expected) .
157
            "RESPONSE:" . Dump($response);
158
    }
159
}
160
161
162
test_query('ListMetadataFormats', {verb => 'ListMetadataFormats'}, {
92
    ListMetadataFormats => {
163
    ListMetadataFormats => {
93
        metadataFormat => [
164
        metadataFormat => [
94
            {
165
            {
Lines 103-125 my $expected = { Link Here
103
            },
174
            },
104
        ],
175
        ],
105
    },
176
    },
106
};
177
} );
107
is_deeply($response, $expected, "ListMetadataFormats");
178
108
179
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 => {
180
    error => {
119
        code => 'badArgument',
181
        code => 'badArgument',
120
        content => "Required argument 'metadataPrefix' was undefined",
182
        content => "Required argument 'metadataPrefix' was undefined",
121
    },
183
    },
122
};
184
});
123
is_deeply($response, $expected, "ListIdentifiers without metadaPrefix argument");
185
186
187
test_query('ListIdentifiers', {verb => 'ListIdentifiers', metadataPrefix => 'marcxml'}, {
188
    ListIdentifiers => {
189
        header => [ @header[1..3] ],
190
        resumptionToken => {
191
            content => "marcxml/3/1970-01-01T00:00:00Z/$date_to/",
192
            cursor  => 3,
193
        },
194
    },
195
});
196
197
test_query('ListIdentifiers', {verb => 'ListIdentifiers', metadataPrefix => 'marcxml'}, {
198
    ListIdentifiers => {
199
        header => [ @header[1..3] ],
200
        resumptionToken => {
201
            content => "marcxml/3/1970-01-01T00:00:00Z/$date_to/",
202
            cursor  => 3,
203
        },
204
    },
205
});
206
207
test_query(
208
    'ListIdentifiers with resumptionToken 1',
209
    { verb => 'ListIdentifiers', resumptionToken => "marcxml/3/1970-01-01T00:00:00Z/$date_to/" },
210
    {
211
        ListIdentifiers => {
212
            header => [ @header[4..6] ],
213
            resumptionToken => {
214
              content => "marcxml/6/1970-01-01T00:00:00Z/$date_to/",
215
              cursor  => 6,
216
            },
217
          },
218
219
    },
220
);
221
222
test_query(
223
    'ListIdentifiers with resumptionToken 2',
224
    { verb => 'ListIdentifiers', resumptionToken => "marcxml/6/1970-01-01T00:00:00Z/$date_to/" },
225
    {
226
        ListIdentifiers => {
227
            header => [ @header[7..9] ],
228
            resumptionToken => {
229
              content => "marcxml/9/1970-01-01T00:00:00Z/$date_to/",
230
              cursor  => 9,
231
            },
232
          },
233
234
    },
235
);
236
237
test_query(
238
    'ListIdentifiers with resumptionToken 3, response without resumption',
239
    { verb => 'ListIdentifiers', resumptionToken => "marcxml/9/1970-01-01T00:00:00Z/$date_to/" },
240
    {
241
        ListIdentifiers => {
242
            header => $header[10],
243
          },
244
245
    },
246
);
247
248
test_query('ListRecords marcxml without metadataPrefix', {verb => 'ListRecords'}, {
249
    error => {
250
        code => 'badArgument',
251
        content => "Required argument 'metadataPrefix' was undefined",
252
    },
253
});
254
255
test_query('ListRecords marcxml', {verb => 'ListRecords', metadataPrefix => 'marcxml'}, {
256
    ListRecords => {
257
        record => [ @marcxml[1..3] ],
258
        resumptionToken => {
259
          content => "marcxml/3/1970-01-01T00:00:00Z/$date_to/",
260
          cursor  => 3,
261
        },
262
    },
263
});
264
265
test_query(
266
    'ListRecords marcxml with resumptionToken 1',
267
    { verb => 'ListRecords', resumptionToken => "marcxml/3/1970-01-01T00:00:00Z/$date_to/" },
268
    { ListRecords => {
269
        record => [ @marcxml[4..6] ],
270
        resumptionToken => {
271
          content => "marcxml/6/1970-01-01T00:00:00Z/$date_to/",
272
          cursor  => 6,
273
        },
274
    },
275
});
276
277
test_query(
278
    'ListRecords marcxml with resumptionToken 2',
279
    { verb => 'ListRecords', resumptionToken => "marcxml/6/1970-01-01T00:00:00Z/$date_to/" },
280
    { ListRecords => {
281
        record => [ @marcxml[7..9] ],
282
        resumptionToken => {
283
          content => "marcxml/9/1970-01-01T00:00:00Z/$date_to/",
284
          cursor  => 9,
285
        },
286
    },
287
});
288
289
# Last record, so no resumption token
290
test_query(
291
    'ListRecords marcxml with resumptionToken 3, response without resumption',
292
    { verb => 'ListRecords', resumptionToken => "marcxml/9/1970-01-01T00:00:00Z/$date_to/" },
293
    { ListRecords => {
294
        record => $marcxml[10],
295
    },
296
});
297
298
test_query('ListRecords oai_dc', {verb => 'ListRecords', metadataPrefix => 'oai_dc'}, {
299
    ListRecords => {
300
        record => [ @oaidc[1..3] ],
301
        resumptionToken => {
302
          content => "oai_dc/3/1970-01-01T00:00:00Z/$date_to/",
303
          cursor  => 3,
304
        },
305
    },
306
});
307
308
test_query(
309
    'ListRecords oai_dc with resumptionToken 1',
310
    { verb => 'ListRecords', resumptionToken => "oai_dc/3/1970-01-01T00:00:00Z/$date_to/" },
311
    { ListRecords => {
312
        record => [ @oaidc[4..6] ],
313
        resumptionToken => {
314
          content => "oai_dc/6/1970-01-01T00:00:00Z/$date_to/",
315
          cursor  => 6,
316
        },
317
    },
318
});
319
320
test_query(
321
    'ListRecords oai_dc with resumptionToken 2',
322
    { verb => 'ListRecords', resumptionToken => "oai_dc/6/1970-01-01T00:00:00Z/$date_to/" },
323
    { ListRecords => {
324
        record => [ @oaidc[7..9] ],
325
        resumptionToken => {
326
          content => "oai_dc/9/1970-01-01T00:00:00Z/$date_to/",
327
          cursor  => 9,
328
        },
329
    },
330
});
331
332
# Last record, so no resumption token
333
test_query(
334
    'ListRecords oai_dc with resumptionToken 3, response without resumption',
335
    { verb => 'ListRecords', resumptionToken => "oai_dc/9/1970-01-01T00:00:00Z/$date_to/" },
336
    { ListRecords => {
337
        record => $oaidc[10],
338
    },
339
});
124
340
125
$dbh->rollback;
341
$schema->storage->txn_rollback;
126
- 

Return to bug 17493