Bug 14939: Remove the Capture::Tiny dependency
[koha.git] / t / db_dependent / OAI / Server.t
1 #!/usr/bin/perl
2
3 # Copyright Tamil s.a.r.l. 2015
4 #
5 # This file is part of Koha.
6 #
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.
11 #
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.
16 #
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>.
19
20
21 use Modern::Perl;
22 use C4::Context;
23 use C4::Biblio;
24 use Test::More tests => 13;
25 use Test::MockModule;
26 use Test::Warn;
27 use DateTime;
28 use XML::Simple;
29 use t::lib::Mocks;
30
31
32 BEGIN {
33     use_ok('Koha::OAI::Server::DeletedRecord');
34     use_ok('Koha::OAI::Server::Description');
35     use_ok('Koha::OAI::Server::GetRecord');
36     use_ok('Koha::OAI::Server::Identify');
37     use_ok('Koha::OAI::Server::ListIdentifiers');
38     use_ok('Koha::OAI::Server::ListMetadataFormats');
39     use_ok('Koha::OAI::Server::ListRecords');
40     use_ok('Koha::OAI::Server::ListSets');
41     use_ok('Koha::OAI::Server::Record');
42     use_ok('Koha::OAI::Server::Repository');
43     use_ok('Koha::OAI::Server::ResumptionToken');
44 }
45
46
47 # Mocked CGI module in order to be able to send CGI parameters to OAI Server
48 my %param;
49 my $module = Test::MockModule->new('CGI');
50 $module->mock('Vars', sub { %param; });
51
52 my $dbh = C4::Context->dbh;
53 $dbh->{AutoCommit} = 0;
54 $dbh->{RaiseError} = 1;
55 $dbh->do('DELETE FROM issues');
56 $dbh->do('DELETE FROM biblio');
57 $dbh->do('DELETE FROM biblioitems');
58 $dbh->do('DELETE FROM items');
59
60 # Add 10 biblio records
61 my @bibs = map {
62     my $record = MARC::Record->new();
63     $record->append_fields( MARC::Field->new('245', '', '', 'a' => "Title $_" ) );
64     my ($biblionumber) = AddBiblio($record, '');
65     $biblionumber;
66 } (1..10);
67
68 t::lib::Mocks::mock_preference('LibraryName', 'My Library');
69 t::lib::Mocks::mock_preference('OAI::PMH', 1);
70 t::lib::Mocks::mock_preference('OAI-PMH:archiveID', 'TEST');
71 t::lib::Mocks::mock_preference('OAI-PMH:ConfFile', '' );
72 t::lib::Mocks::mock_preference('OAI-PMH:MaxCount', 3);
73 t::lib::Mocks::mock_preference('OAI-PMH:DeletedRecord', 'persistent');
74
75 %param = ( verb => 'ListMetadataFormats' );
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 };
84 $get_response->();
85 my $now = DateTime->now . 'Z';
86 my $expected = {
87     request => 'http://localhost',
88     responseDate => $now,
89     xmlns => 'http://www.openarchives.org/OAI/2.0/',
90     'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
91     'xsi:schemaLocation' => 'http://www.openarchives.org/OAI/2.0/ http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd',
92     ListMetadataFormats => {
93         metadataFormat => [
94             {
95                 metadataNamespace => 'http://www.openarchives.org/OAI/2.0/oai_dc/',
96                 metadataPrefix=> 'oai_dc',
97                 schema => 'http://www.openarchives.org/OAI/2.0/oai_dc.xsd',
98             },
99             {
100                 metadataNamespace => 'http://www.loc.gov/MARC21/slim http://www.loc.gov/ standards/marcxml/schema/MARC21slim',
101                 metadataPrefix => 'marcxml',
102                 schema => 'http://www.loc.gov/MARC21/slim http://www.loc.gov/ standards/marcxml/schema/MARC21slim.xsd',
103             },
104         ],
105     },
106 };
107 is_deeply($response, $expected, "ListMetadataFormats");
108
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 => {
119         code => 'badArgument',
120         content => "Required argument 'metadataPrefix' was undefined",
121     },
122 };
123 is_deeply($response, $expected, "ListIdentifiers without metadaPrefix argument");
124
125 $dbh->rollback;