2012-08-22 18:23:06 +02:00
|
|
|
<?php
|
2018-07-03 01:35:24 +02:00
|
|
|
|
|
|
|
namespace SilverStripe\TextExtraction\Tests;
|
|
|
|
|
|
|
|
use SilverStripe\Assets\File;
|
|
|
|
use SilverStripe\Core\Config\Config;
|
|
|
|
use SilverStripe\Dev\SapphireTest;
|
2019-08-28 00:07:21 +02:00
|
|
|
use SilverStripe\TextExtraction\Cache\FileTextCache;
|
2018-07-03 01:35:24 +02:00
|
|
|
use SilverStripe\TextExtraction\Extension\FileTextExtractable;
|
|
|
|
|
2015-11-18 05:07:31 +01:00
|
|
|
class FileTextExtractableTest extends SapphireTest
|
|
|
|
{
|
2018-07-03 01:35:24 +02:00
|
|
|
protected $usesDatabase = true;
|
2015-11-18 05:07:31 +01:00
|
|
|
|
2018-07-03 01:35:24 +02:00
|
|
|
protected static $required_extensions = [
|
|
|
|
File::class => [
|
|
|
|
FileTextExtractable::class,
|
|
|
|
],
|
|
|
|
];
|
|
|
|
|
|
|
|
protected function setUp()
|
2015-11-18 05:07:31 +01:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
// Ensure that html is a valid extension
|
2018-07-03 01:35:24 +02:00
|
|
|
Config::modify()->merge(File::class, 'allowed_extensions', ['html']);
|
2015-11-18 05:07:31 +01:00
|
|
|
|
|
|
|
// Create a copy of the file, as it may be clobbered by the test
|
|
|
|
// ($file->extractFileAsText() calls $file->write)
|
2018-07-03 01:35:24 +02:00
|
|
|
copy(
|
|
|
|
dirname(__FILE__) . '/fixtures/test1.html',
|
|
|
|
dirname(__FILE__) . '/fixtures/test1-copy.html'
|
|
|
|
);
|
2018-07-03 05:55:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function tearDown()
|
|
|
|
{
|
|
|
|
if (file_exists(dirname(__FILE__) . '/fixtures/test1-copy.html')) {
|
|
|
|
unlink(dirname(__FILE__) . '/fixtures/test1-copy.html');
|
|
|
|
}
|
2018-07-03 01:35:24 +02:00
|
|
|
|
2018-07-03 05:55:02 +02:00
|
|
|
parent::tearDown();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testExtractFileAsText()
|
|
|
|
{
|
2015-11-18 05:07:31 +01:00
|
|
|
// Use HTML, since the extractor is always available
|
2019-08-28 00:07:21 +02:00
|
|
|
/** @var File&FileTextExtractable $file */
|
2018-07-03 05:55:02 +02:00
|
|
|
$file = new File(['Name' => 'test1-copy.html']);
|
2019-08-28 00:07:21 +02:00
|
|
|
$file->setTextCache(new FileTextCache\Database());
|
2018-07-03 05:55:02 +02:00
|
|
|
$file->setFromLocalFile(dirname(__FILE__) . '/fixtures/test1-copy.html');
|
2015-11-18 05:07:31 +01:00
|
|
|
$file->write();
|
2018-07-03 01:35:24 +02:00
|
|
|
|
2015-11-18 05:07:31 +01:00
|
|
|
$content = $file->extractFileAsText();
|
2018-07-03 05:55:02 +02:00
|
|
|
$this->assertNotNull($content);
|
2015-11-18 05:07:31 +01:00
|
|
|
$this->assertContains('Test Headline', $content);
|
|
|
|
$this->assertContains('Test Text', $content);
|
|
|
|
$this->assertEquals($content, $file->FileContentCache);
|
|
|
|
}
|
|
|
|
}
|