diff --git a/code/extractors/TikaServerTextExtractor.php b/code/extractors/TikaServerTextExtractor.php index f3503f4..7558eea 100644 --- a/code/extractors/TikaServerTextExtractor.php +++ b/code/extractors/TikaServerTextExtractor.php @@ -71,13 +71,7 @@ class TikaServerTextExtractor extends FileTextExtractor public function isAvailable() { $version = $this->getVersion(); - // ensure that the version number has a major, minor and patch number - // reason being that version_compare('1.7', '1.7.0') will return -1 instead of 0 - for ($i = 0; $i < 2; $i++) { - if (substr_count($version, '.') < 2) { - $version .= '.0'; - } - } + $version = $this->normaliseVersion($version); return $this->getServerEndpoint() && $this->getClient()->isAvailable() && version_compare($version, '1.7.0') >= 0; @@ -121,4 +115,24 @@ class TikaServerTextExtractor extends FileTextExtractor { return $this->getClient()->tika($path); } + + /** + * Ensure that the version number has a major, minor and patch number + * Reason being that version_compare('1.7', '1.7.0') will return -1 instead of 0 + * + * @param $version + * @return string + */ + protected function normaliseVersion($version) + { + if (!$version) { + return '0.0.0'; + } + for ($i = 0; $i < 2; $i++) { + if (substr_count($version, '.') < 2) { + $version .= '.0'; + } + } + return $version; + } } diff --git a/tests/TikaTextExtractorTest.php b/tests/TikaTextExtractorTest.php index 0342dcf..3d6b6c6 100644 --- a/tests/TikaTextExtractorTest.php +++ b/tests/TikaTextExtractorTest.php @@ -40,4 +40,24 @@ class TikaTextExtractorTest extends SapphireTest $this->assertTrue($extractor->supportsMime('text/html')); $this->assertFalse($extractor->supportsMime('application/not-supported')); } + public function testNormaliseVersion() + { + $extractor = new TikaServerTextExtractor(); + $reflection = new ReflectionClass($extractor); + $method = $reflection->getMethod('normaliseVersion'); + $method->setAccessible(true); + + $arr = [ + '1.7.1' => '1.7.1', + '1.7' => '1.7.0', + '1' => '1.0.0', + null => '0.0.0', + 'v1.5' => 'v1.5.0', + 'carrot' => 'carrot.0.0' + ]; + foreach ($arr as $input => $expected) { + $actual = $method->invoke($extractor, $input); + $this->assertEquals($expected, $actual); + } + } }