1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\TextExtraction\Tests; |
4
|
|
|
|
5
|
|
|
use PHPUnit_Framework_MockObject_MockObject; |
6
|
|
|
use SilverStripe\Assets\File; |
7
|
|
|
use SilverStripe\Dev\SapphireTest; |
8
|
|
|
use SilverStripe\TextExtraction\Extractor\TikaServerTextExtractor; |
9
|
|
|
use SilverStripe\TextExtraction\Rest\TikaRestClient; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @group tika-tests |
13
|
|
|
*/ |
14
|
|
|
class TikaServerTextExtractorTest extends SapphireTest |
15
|
|
|
{ |
16
|
|
|
protected $usesDatabase = true; |
17
|
|
|
|
18
|
|
|
public function testServerExtraction() |
19
|
|
|
{ |
20
|
|
|
$extractor = TikaServerTextExtractor::create(); |
21
|
|
|
if (!$extractor->isAvailable()) { |
22
|
|
|
$this->markTestSkipped('tika server not available'); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
// Check file |
26
|
|
|
$file = new File(); |
27
|
|
|
$file->setFromLocalFile(dirname(__FILE__) . '/fixtures/test1.pdf'); |
28
|
|
|
$file->write(); |
29
|
|
|
|
30
|
|
|
$content = $extractor->getContent($file); |
31
|
|
|
$this->assertContains('This is a test file with a link', $content); |
32
|
|
|
|
33
|
|
|
// Check mime validation |
34
|
|
|
$this->assertTrue($extractor->supportsMime('application/pdf')); |
35
|
|
|
$this->assertTrue($extractor->supportsMime('text/html')); |
36
|
|
|
$this->assertFalse($extractor->supportsMime('application/not-supported')); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param string $version |
41
|
|
|
* @param bool $expected |
42
|
|
|
* @dataProvider isAvailableProvider |
43
|
|
|
*/ |
44
|
|
|
public function testIsAvailable($version, $expected) |
45
|
|
|
{ |
46
|
|
|
/** @var PHPUnit_Framework_MockObject_MockObject|TikaServerTextExtractor $extractor */ |
47
|
|
|
$extractor = $this->getMockBuilder(TikaServerTextExtractor::class) |
48
|
|
|
->setMethods(['getClient', 'getServerEndpoint']) |
49
|
|
|
->getMock(); |
50
|
|
|
|
51
|
|
|
$client = $this->createMock(TikaRestClient::class); |
52
|
|
|
$client->method('isAvailable')->willReturn(true); |
53
|
|
|
$client->method('getVersion')->willReturn($version); |
54
|
|
|
|
55
|
|
|
$extractor->method('getClient')->willReturn($client); |
56
|
|
|
$extractor->method('getServerEndpoint')->willReturn('tikaserver.example'); |
57
|
|
|
|
58
|
|
|
$result = $extractor->isAvailable(); |
59
|
|
|
$this->assertSame($expected, $result); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return array[] |
64
|
|
|
*/ |
65
|
|
|
public function isAvailableProvider() |
66
|
|
|
{ |
67
|
|
|
return [ |
68
|
|
|
['1.5.2', false], |
69
|
|
|
['1.5', false], |
70
|
|
|
['1.7.0', true], |
71
|
|
|
['1.7.5', true], |
72
|
|
|
['1.8.0', true], |
73
|
|
|
['1.7', true], |
74
|
|
|
['1.8', true], |
75
|
|
|
['2.0.0', true], |
76
|
|
|
]; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|