View source
<?php
namespace Drupal\file\Tests\Migrate;
use Drupal\Core\Site\Settings;
use Drupal\migrate\Row;
use Drupal\file\Plugin\migrate\destination\EntityFile;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\migrate\MigrateException;
use Drupal\simpletest\KernelTestBase;
class EntityFileTest extends KernelTestBase {
public static $modules = array(
'system',
'entity_test',
'user',
'file',
);
protected $destination;
public function setUp() {
parent::setUp();
$this->destination = new TestEntityFile([]);
$this->destination->streamWrapperManager = \Drupal::getContainer()
->get('stream_wrapper_manager');
$this->destination->fileSystem = \Drupal::getContainer()
->get('file_system');
$this
->installEntitySchema('file');
file_put_contents('/tmp/test-file.jpg', '');
}
public function testSuccessfulCopies() {
foreach ($this
->localFileDataProvider() as $data) {
list($row_values, $destination_path, $expected, $source_base_path) = $data;
$this
->doImport($row_values, $destination_path, $source_base_path);
$message = $expected ? sprintf('File %s exists', $destination_path) : sprintf('File %s does not exist', $destination_path);
$this
->assertIdentical($expected, is_file($destination_path), $message);
}
}
protected function localFileDataProvider() {
global $base_url;
return [
[
[
'filepath' => 'core/modules/simpletest/files/image-test.jpg',
],
'public://file1.jpg',
TRUE,
DRUPAL_ROOT . '/',
],
[
[
'filepath' => '/tmp/test-file.jpg',
],
'temporary://test.jpg',
TRUE,
'',
],
[
[
'filepath' => 'test-file.jpg',
],
'temporary://core/modules/simpletest/files/test.jpg',
TRUE,
'/tmp/',
],
[
[
'filepath' => 'core/modules/simpletest/files/image-test.jpg',
],
'public://remote-file.jpg',
TRUE,
$base_url . '/',
],
[
[
'filepath' => 'core/modules/simpletest/files/image-test.jpg',
],
'public://folder/remote-file.jpg',
TRUE,
DRUPAL_ROOT . '/',
],
];
}
public function testNonExistentSourceFile() {
$destination = '/non/existent/file';
try {
$this
->doImport([
'filepath' => $destination,
], 'public://wontmatter.jpg');
$this
->fail('Expected Drupal\\migrate\\MigrateException when importing ' . $destination);
} catch (MigrateException $e) {
$this
->assertIdentical($e
->getMessage(), "File '{$destination}' does not exist.");
}
}
public function testWriteFile() {
$plugin = $this->destination;
$method = new \ReflectionMethod($plugin, 'writeFile');
$method
->setAccessible(TRUE);
touch('temporary://baz.txt');
$plugin->configuration['move'] = TRUE;
$this
->assertTrue($method
->invoke($plugin, 'temporary://baz.txt', 'public://foo.txt'));
$this
->assertFalse($method
->invoke($plugin, 'temporary://invalid.txt', 'public://invalid.txt'));
$plugin->configuration['move'] = FALSE;
touch('temporary://baz.txt');
$this
->assertTrue($method
->invoke($plugin, 'temporary://baz.txt', 'public://foo.txt'));
$method
->invoke($plugin, 'temporary://baz.txt', 'public://foo.txt', FILE_EXISTS_RENAME);
$this
->assertTrue(file_exists('public://foo_0.txt'));
$this
->assertFalse($method
->invoke($plugin, 'temporary://invalid.txt', 'public://invalid.txt'));
}
public function testGetOverwriteMode() {
$plugin = $this->destination;
$method = new \ReflectionMethod($plugin, 'getOverwriteMode');
$method
->setAccessible(TRUE);
$row = new Row([], []);
$this
->assertIdentical(FILE_EXISTS_REPLACE, $method
->invoke($plugin, $row));
$plugin->configuration['rename'] = TRUE;
$plugin->storage = \Drupal::entityManager()
->getStorage('file');
$file = $plugin->storage
->create();
touch('public://foo.txt');
$file
->setFileUri('public://foo.txt');
$file
->save();
$row
->setDestinationProperty($plugin->storage
->getEntityType()
->getKey('id'), $file
->id());
$this
->assertIdentical(FILE_EXISTS_RENAME, $method
->invoke($plugin, $row));
unlink('public://foo.txt');
}
public function testGetDirectory() {
$plugin = $this->destination;
$method = new \ReflectionMethod($plugin, 'getDirectory');
$method
->setAccessible(TRUE);
$this
->assertEqual('public://foo', $method
->invoke($plugin, 'public://foo/baz.txt'));
$this
->assertEqual('/path/to', $method
->invoke($plugin, '/path/to/foo.txt'));
$fs = \Drupal::getContainer()
->get('file_system');
$this
->assertEqual($fs
->realpath(Settings::get('file_public_path')), $method
->invoke($plugin, 'public://foo.txt'));
}
public function testIsLocationUnchanged() {
$plugin = $this->destination;
$method = new \ReflectionMethod($plugin, 'isLocationUnchanged');
$method
->setAccessible(TRUE);
$public_dir = Settings::get('file_public_path');
touch('public://foo.txt');
$this
->assertTrue($method
->invoke($plugin, $public_dir . '/foo.txt', 'public://foo.txt'));
unlink('public://foo.txt');
$temporary_file = '/tmp/foo.txt';
touch($temporary_file);
$this
->assertTrue($method
->invoke($plugin, $temporary_file, 'temporary://foo.txt'));
unlink($temporary_file);
}
public function testIsLocalUri() {
$plugin = $this->destination;
$method = new \ReflectionMethod($plugin, 'isLocalUri');
$method
->setAccessible(TRUE);
$this
->assertTrue($method
->invoke($plugin, 'public://foo.txt'));
$this
->assertTrue($method
->invoke($plugin, 'public://path/to/foo.txt'));
$this
->assertTrue($method
->invoke($plugin, 'temporary://foo.txt'));
$this
->assertTrue($method
->invoke($plugin, 'temporary://path/to/foo.txt'));
$this
->assertTrue($method
->invoke($plugin, 'foo.txt'));
$this
->assertTrue($method
->invoke($plugin, '/path/to/files/foo.txt'));
$this
->assertTrue($method
->invoke($plugin, 'relative/path/to/foo.txt'));
$this
->assertFalse($method
->invoke($plugin, 'http://www.example.com/foo.txt'));
}
protected function doImport($row_values, $destination_path, $source_base_path = '') {
$row = new Row($row_values, []);
$row
->setDestinationProperty('uri', $destination_path);
$this->destination->configuration['source_base_path'] = $source_base_path;
return $this->destination
->import($row, array());
}
}
class TestEntityFile extends EntityFile {
public $mockEntity;
public $configuration;
public $storage;
public $streamWrapperManager;
public $fileSystem;
public function __construct($configuration) {
$configuration += array(
'source_base_path' => '',
'source_path_property' => 'filepath',
'destination_path_property' => 'uri',
'move' => FALSE,
'urlencode' => FALSE,
);
$this->configuration = $configuration;
$this->mockEntity = EntityTest::create();
}
protected function getEntity(Row $row, array $old_destination_id_values) {
return $this->mockEntity;
}
protected function save(ContentEntityInterface $entity, array $old_destination_id_values = array()) {
}
}