-
Notifications
You must be signed in to change notification settings - Fork 8
/
HarvesterTest.php
415 lines (393 loc) · 13.6 KB
/
HarvesterTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
<?php
/**
* OAI-PMH harvester unit test.
*
* PHP version 7
*
* Copyright (C) Villanova University 2016.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* @category VuFind
* @package Tests
* @author Demian Katz <demian.katz@villanova.edu>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org/wiki/development
*/
namespace VuFindTest\Harvest\OaiPmh;
use VuFindHarvest\OaiPmh\Harvester;
/**
* OAI-PMH harvester unit test.
*
* @category VuFind
* @package Tests
* @author Demian Katz <demian.katz@villanova.edu>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org/wiki/development
*/
class HarvesterTest extends \PHPUnit\Framework\TestCase
{
/**
* Time zone setting used with setup/tearDown
*
* @var string
*/
protected $oldTz;
/**
* Setup function -- standardize timezone for consistent results
*
* @return void
*/
public function setUp(): void
{
$this->oldTz = date_default_timezone_get();
date_default_timezone_set('America/New_York');
}
/**
* Teardown function -- restore previous timezone setting
*
* @return void
*/
public function tearDown(): void
{
date_default_timezone_set($this->oldTz);
}
/**
* Get mock communicator object
*
* @return \VuFindHarvest\OaiPmh\Communicator
*/
protected function getMockCommunicator()
{
return $this->getMockBuilder(\VuFindHarvest\OaiPmh\Communicator::class)
->disableOriginalConstructor()
->getMock();
}
/**
* Get mock RecordWriter object
*
* @return \VuFindHarvest\OaiPmh\RecordWriter
*/
protected function getMockRecordWriter()
{
return $this->getMockBuilder(\VuFindHarvest\OaiPmh\RecordWriter::class)
->disableOriginalConstructor()
->getMock();
}
/**
* Get mock StateManager object
*
* @return \VuFindHarvest\OaiPmh\StateManager
*/
protected function getMockStateManager()
{
return $this->getMockBuilder(\VuFindHarvest\OaiPmh\StateManager::class)
->disableOriginalConstructor()
->getMock();
}
/**
* Return protected or private property.
*
* Uses PHP's reflection API in order to modify property accessibility.
*
* @param object|string $object Object or class name
* @param string $property Property name
*
* @throws \ReflectionException Property does not exist
*
* @return mixed
*/
protected function getProperty($object, $property)
{
$reflectionProperty = new \ReflectionProperty($object, $property);
$reflectionProperty->setAccessible(true);
return $reflectionProperty->getValue($object);
}
/**
* Get harvester
*
* @param array $settings Settings
* @param object $communicator Communicator
* @param object $writer Writer
* @param object $stateManager State manager
*
* @return Harvester
*/
protected function getHarvester(
$settings = [],
$communicator = null,
$writer = null,
$stateManager = null
) {
return new Harvester(
$communicator ?: $this->getMockCommunicator(),
$writer ?: $this->getMockRecordWriter(),
$stateManager ?: $this->getMockStateManager(),
$settings
);
}
/**
* Get XML Identify response for testing.
*
* @return string
*/
protected function getFakeIdentifyResponse()
{
return <<<XML
<?xml version="1.0"?>
<OAI-PMH xmlns="http://www.openarchives.org/OAI/2.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/ http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd"><responseDate>2016-07-12T16:19:54Z</responseDate><request verb="Identify">http://fake/OAI/Server</request>
<Identify>
<repositoryName>fake</repositoryName>
<baseURL>http://fake/OAI/Server</baseURL>
<protocolVersion>2.0</protocolVersion>
<adminEmail>fake@fake.edu</adminEmail>
<earliestDatestamp>2000-01-01T00:00:00Z</earliestDatestamp>
<deletedRecord>transient</deletedRecord>
<granularity>YYYY-MM-DDThh:mm:ssZ</granularity>
<description>
<oai-identifier xmlns="http://www.openarchives.org/OAI/2.0/oai-identifier" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/oai-identifier http://www.openarchives.org/OAI/2.0/oai-identifier.xsd">
<scheme>oai</scheme>
<repositoryIdentifier>fake</repositoryIdentifier>
<delimiter>:</delimiter>
<sampleIdentifier>fake:123456</sampleIdentifier>
</oai-identifier>
</description>
</Identify>
</OAI-PMH>
XML;
}
/**
* Get an arbitrary OAI-PMH error
*
* @return string
*/
protected function getArbitraryErrorResponse()
{
return <<<XML
<?xml version="1.0"?>
<OAI-PMH xmlns="http://www.openarchives.org/OAI/2.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/ http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd"><responseDate>2016-07-13T14:11:24Z</responseDate><request verb="ListRecords" resumptionToken="foo">http://fake/OAI/Server</request>
<error code="foo">bar</error>
</OAI-PMH>
XML;
}
/**
* Get a token error
*
* @return string
*/
protected function getTokenErrorResponse()
{
return <<<XML
<?xml version="1.0"?>
<OAI-PMH xmlns="http://www.openarchives.org/OAI/2.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/ http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd"><responseDate>2016-07-13T14:11:24Z</responseDate><request verb="ListRecords" resumptionToken="foo">http://fake/OAI/Server</request>
<error code="badResumptionToken">Invalid or expired resumption token</error>
</OAI-PMH>
XML;
}
/**
* Get XML ListRecords response for testing.
*
* @return string
*/
protected function getFakeResponse()
{
return <<<XML
<OAI-PMH xmlns="http://www.openarchives.org/OAI/2.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/ http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd"><responseDate>2016-07-12T16:22:05Z</responseDate><request verb="ListRecords" metadataPrefix="oai_dc">http://fake/OAI/Server</request>
<ListRecords>
<record>
<header status="deleted">
<identifier>fake:foo1</identifier>
</header>
</record>
<record>
<header>
<identifier>fake:foo2</identifier>
</header>
<metadata>
<foo />
</metadata>
</record>
</ListRecords>
</OAI-PMH>
XML;
}
/**
* Get XML ListRecords response for testing (with resumption token).
*
* @return string
*/
protected function getFakeResponseWithToken()
{
return <<<XML
<OAI-PMH xmlns="http://www.openarchives.org/OAI/2.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/ http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd"><responseDate>2016-07-12T16:22:05Z</responseDate><request verb="ListRecords" metadataPrefix="oai_dc">http://fake/OAI/Server</request>
<ListRecords>
<record>
<header>
<identifier>fake:foo0</identifier>
</header>
<metadata>
<foo />
</metadata>
</record>
<resumptionToken>more</resumptionToken>
</ListRecords>
</OAI-PMH>
XML;
}
/**
* Test that granularity is autoloaded by default.
*
* @return void
*/
public function testGranularityAutoload()
{
$comm = $this->getMockCommunicator();
$comm->expects($this->once(0))->method('request')
->with($this->equalTo('Identify'))
->will(
$this->returnValue(
simplexml_load_string($this->getFakeIdentifyResponse())
)
);
$harvester = $this->getHarvester([], $comm);
$this->assertEquals(
'YYYY-MM-DDThh:mm:ssZ',
$this->getProperty($harvester, 'granularity')
);
}
/**
* Test that a single ListRecords call with no resumption token triggers
* a write to the writer.
*
* @return void
*/
public function testSimpleListRecords()
{
$comm = $this->getMockCommunicator();
$expectedSettings = ['metadataPrefix' => 'oai_dc'];
$comm->expects($this->once())->method('request')
->with($this->equalTo('ListRecords'), $this->equalTo($expectedSettings))
->will($this->returnValue(simplexml_load_string($this->getFakeResponse())));
$writer = $this->getMockRecordWriter();
$writer->expects($this->once())->method('write')
->with($this->isInstanceOf('SimpleXMLElement'))
->will($this->returnValue(1468434382));
$sm = $this->getMockStateManager();
$sm->expects($this->once())->method('saveDate')
->with($this->equalTo('2016-07-13T14:26:22Z'));
$harvester = $this->getHarvester(
['dateGranularity' => 'YYYY-MM-DDThh:mm:ssZ'],
$comm,
$writer,
$sm
);
$harvester->launch();
}
/**
* Test that we can retrieve two pages of results.
*
* @return void
*/
public function testListRecordsWithResumption()
{
$comm = $this->getMockCommunicator();
$expectedSettings0 = [
'metadataPrefix' => 'oai_dc', 'set' => 'xyzzy',
'from' => '2016-07-01', 'until' => '2016-07-31',
];
$expectedSettings1 = ['resumptionToken' => 'more'];
$comm->expects($this->exactly(2))->method('request')
->withConsecutive(
['ListRecords', $expectedSettings0],
['ListRecords', $expectedSettings1],
)
->willReturnOnConsecutiveCalls(
simplexml_load_string($this->getFakeResponseWithToken()),
simplexml_load_string($this->getFakeResponse())
);
$writer = $this->getMockRecordWriter();
$writer->expects($this->exactly(2))->method('write')
->with($this->isInstanceOf('SimpleXMLElement'));
$sm = $this->getMockStateManager();
$sm->expects($this->once())->method('saveState')
->with($this->equalTo('xyzzy'), $this->equalTo('more'), $this->equalTo('2016-07-01'));
$sm->expects($this->once())->method('clearState');
$harvester = $this->getHarvester(
[
'set' => 'xyzzy', 'dateGranularity' => 'YYYY-MM-DDThh:mm:ssZ',
'from' => '2016-07-01', 'until' => '2016-07-31',
],
$comm,
$writer,
$sm
);
$harvester->launch();
}
/**
* Test a bad resumption token error.
*
* @return void
*
*/
public function testBadResumptionToken()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Token expired; removing last_state.txt. Please restart harvest.');
$comm = $this->getMockCommunicator();
$expectedSettings = ['resumptionToken' => 'foo'];
$comm->expects($this->once())->method('request')
->with($this->equalTo('ListRecords'), $this->equalTo($expectedSettings))
->will(
$this->returnValue(
simplexml_load_string($this->getTokenErrorResponse())
)
);
$sm = $this->getMockStateManager();
$sm->expects($this->any())->method('loadState')
->will($this->returnValue([null, 'foo', 'bar']));
$sm->expects($this->once())->method('clearState');
$harvester = $this->getHarvester(
['dateGranularity' => 'YYYY-MM-DDThh:mm:ssZ'],
$comm,
null,
$sm
);
$harvester->launch();
}
/**
* Test a generic error.
*
* @return void
*
*/
public function testArbitraryOaiError()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('OAI-PMH error -- code: foo, value: bar');
$comm = $this->getMockCommunicator();
$expectedSettings = ['metadataPrefix' => 'oai_dc'];
$comm->expects($this->once())->method('request')
->with($this->equalTo('ListRecords'), $this->equalTo($expectedSettings))
->will(
$this->returnValue(
simplexml_load_string($this->getArbitraryErrorResponse())
)
);
$harvester = $this->getHarvester(
['dateGranularity' => 'YYYY-MM-DDThh:mm:ssZ'],
$comm
);
$harvester->launch();
}
}