updated core
This commit is contained in:
@@ -5,8 +5,8 @@ package: Testing
|
||||
# version: VERSION
|
||||
# core: 8.x
|
||||
|
||||
# Information added by Drupal.org packaging script on 2017-11-03
|
||||
version: '8.4.2'
|
||||
# Information added by Drupal.org packaging script on 2018-01-03
|
||||
version: '8.4.4'
|
||||
core: '8.x'
|
||||
project: 'drupal'
|
||||
datestamp: 1509719929
|
||||
datestamp: 1515021228
|
||||
|
||||
@@ -5,8 +5,8 @@ package: Testing
|
||||
# version: VERSION
|
||||
# core: 8.x
|
||||
|
||||
# Information added by Drupal.org packaging script on 2017-11-03
|
||||
version: '8.4.2'
|
||||
# Information added by Drupal.org packaging script on 2018-01-03
|
||||
version: '8.4.4'
|
||||
core: '8.x'
|
||||
project: 'drupal'
|
||||
datestamp: 1509719929
|
||||
datestamp: 1515021228
|
||||
|
||||
@@ -8,8 +8,8 @@ dependencies:
|
||||
- file
|
||||
- views
|
||||
|
||||
# Information added by Drupal.org packaging script on 2017-11-03
|
||||
version: '8.4.2'
|
||||
# Information added by Drupal.org packaging script on 2018-01-03
|
||||
version: '8.4.4'
|
||||
core: '8.x'
|
||||
project: 'drupal'
|
||||
datestamp: 1509719929
|
||||
datestamp: 1515021228
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\Tests\file\Functional;
|
||||
|
||||
use Drupal\field\Entity\FieldStorageConfig;
|
||||
use Drupal\field\Entity\FieldConfig;
|
||||
|
||||
/**
|
||||
* Provides methods for creating file fields.
|
||||
*/
|
||||
trait FileFieldCreationTrait {
|
||||
|
||||
/**
|
||||
* Creates a new file field.
|
||||
*
|
||||
* @param string $name
|
||||
* The name of the new field (all lowercase), exclude the "field_" prefix.
|
||||
* @param string $entity_type
|
||||
* The entity type.
|
||||
* @param string $bundle
|
||||
* The bundle that this field will be added to.
|
||||
* @param array $storage_settings
|
||||
* A list of field storage settings that will be added to the defaults.
|
||||
* @param array $field_settings
|
||||
* A list of instance settings that will be added to the instance defaults.
|
||||
* @param array $widget_settings
|
||||
* A list of widget settings that will be added to the widget defaults.
|
||||
*
|
||||
* @return \Drupal\field\FieldStorageConfigInterface
|
||||
* The file field.
|
||||
*/
|
||||
public function createFileField($name, $entity_type, $bundle, $storage_settings = [], $field_settings = [], $widget_settings = []) {
|
||||
$field_storage = FieldStorageConfig::create([
|
||||
'entity_type' => $entity_type,
|
||||
'field_name' => $name,
|
||||
'type' => 'file',
|
||||
'settings' => $storage_settings,
|
||||
'cardinality' => !empty($storage_settings['cardinality']) ? $storage_settings['cardinality'] : 1,
|
||||
]);
|
||||
$field_storage->save();
|
||||
|
||||
$this->attachFileField($name, $entity_type, $bundle, $field_settings, $widget_settings);
|
||||
return $field_storage;
|
||||
}
|
||||
|
||||
/**
|
||||
* Attaches a file field to an entity.
|
||||
*
|
||||
* @param string $name
|
||||
* The name of the new field (all lowercase), exclude the "field_" prefix.
|
||||
* @param string $entity_type
|
||||
* The entity type this field will be added to.
|
||||
* @param string $bundle
|
||||
* The bundle this field will be added to.
|
||||
* @param array $field_settings
|
||||
* A list of field settings that will be added to the defaults.
|
||||
* @param array $widget_settings
|
||||
* A list of widget settings that will be added to the widget defaults.
|
||||
*/
|
||||
public function attachFileField($name, $entity_type, $bundle, $field_settings = [], $widget_settings = []) {
|
||||
$field = [
|
||||
'field_name' => $name,
|
||||
'label' => $name,
|
||||
'entity_type' => $entity_type,
|
||||
'bundle' => $bundle,
|
||||
'required' => !empty($field_settings['required']),
|
||||
'settings' => $field_settings,
|
||||
];
|
||||
FieldConfig::create($field)->save();
|
||||
|
||||
entity_get_form_display($entity_type, $bundle, 'default')
|
||||
->setComponent($name, [
|
||||
'type' => 'file_generic',
|
||||
'settings' => $widget_settings,
|
||||
])
|
||||
->save();
|
||||
// Assign display settings.
|
||||
entity_get_display($entity_type, $bundle, 'default')
|
||||
->setComponent($name, [
|
||||
'label' => 'hidden',
|
||||
'type' => 'file_default',
|
||||
])
|
||||
->save();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -13,6 +13,8 @@ use Drupal\file\Entity\File;
|
||||
*/
|
||||
abstract class FileFieldTestBase extends BrowserTestBase {
|
||||
|
||||
use FileFieldCreationTrait;
|
||||
|
||||
/**
|
||||
* Modules to enable.
|
||||
*
|
||||
@@ -57,76 +59,6 @@ abstract class FileFieldTestBase extends BrowserTestBase {
|
||||
return (int) db_query('SELECT MAX(fid) FROM {file_managed}')->fetchField();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new file field.
|
||||
*
|
||||
* @param string $name
|
||||
* The name of the new field (all lowercase), exclude the "field_" prefix.
|
||||
* @param string $entity_type
|
||||
* The entity type.
|
||||
* @param string $bundle
|
||||
* The bundle that this field will be added to.
|
||||
* @param array $storage_settings
|
||||
* A list of field storage settings that will be added to the defaults.
|
||||
* @param array $field_settings
|
||||
* A list of instance settings that will be added to the instance defaults.
|
||||
* @param array $widget_settings
|
||||
* A list of widget settings that will be added to the widget defaults.
|
||||
*/
|
||||
public function createFileField($name, $entity_type, $bundle, $storage_settings = [], $field_settings = [], $widget_settings = []) {
|
||||
$field_storage = FieldStorageConfig::create([
|
||||
'entity_type' => $entity_type,
|
||||
'field_name' => $name,
|
||||
'type' => 'file',
|
||||
'settings' => $storage_settings,
|
||||
'cardinality' => !empty($storage_settings['cardinality']) ? $storage_settings['cardinality'] : 1,
|
||||
]);
|
||||
$field_storage->save();
|
||||
|
||||
$this->attachFileField($name, $entity_type, $bundle, $field_settings, $widget_settings);
|
||||
return $field_storage;
|
||||
}
|
||||
|
||||
/**
|
||||
* Attaches a file field to an entity.
|
||||
*
|
||||
* @param string $name
|
||||
* The name of the new field (all lowercase), exclude the "field_" prefix.
|
||||
* @param string $entity_type
|
||||
* The entity type this field will be added to.
|
||||
* @param string $bundle
|
||||
* The bundle this field will be added to.
|
||||
* @param array $field_settings
|
||||
* A list of field settings that will be added to the defaults.
|
||||
* @param array $widget_settings
|
||||
* A list of widget settings that will be added to the widget defaults.
|
||||
*/
|
||||
public function attachFileField($name, $entity_type, $bundle, $field_settings = [], $widget_settings = []) {
|
||||
$field = [
|
||||
'field_name' => $name,
|
||||
'label' => $name,
|
||||
'entity_type' => $entity_type,
|
||||
'bundle' => $bundle,
|
||||
'required' => !empty($field_settings['required']),
|
||||
'settings' => $field_settings,
|
||||
];
|
||||
FieldConfig::create($field)->save();
|
||||
|
||||
entity_get_form_display($entity_type, $bundle, 'default')
|
||||
->setComponent($name, [
|
||||
'type' => 'file_generic',
|
||||
'settings' => $widget_settings,
|
||||
])
|
||||
->save();
|
||||
// Assign display settings.
|
||||
entity_get_display($entity_type, $bundle, 'default')
|
||||
->setComponent($name, [
|
||||
'label' => 'hidden',
|
||||
'type' => 'file_default',
|
||||
])
|
||||
->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates an existing file field with new settings.
|
||||
*/
|
||||
|
||||
+119
@@ -0,0 +1,119 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\Tests\file\FunctionalJavascript;
|
||||
|
||||
use Drupal\Component\Utility\Bytes;
|
||||
use Drupal\FunctionalJavascriptTests\JavascriptTestBase;
|
||||
use Drupal\Tests\TestFileCreationTrait;
|
||||
use Drupal\Tests\file\Functional\FileFieldCreationTrait;
|
||||
|
||||
/**
|
||||
* Tests uploading a file that exceeds the maximum file size.
|
||||
*
|
||||
* @group file
|
||||
*/
|
||||
class MaximumFileSizeExceededUploadTest extends JavascriptTestBase {
|
||||
|
||||
use FileFieldCreationTrait;
|
||||
use TestFileCreationTrait;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = ['node', 'file'];
|
||||
|
||||
/**
|
||||
* The file system service.
|
||||
*
|
||||
* @var \Drupal\Core\File\FileSystemInterface
|
||||
*/
|
||||
protected $fileSystem;
|
||||
|
||||
/**
|
||||
* A test user.
|
||||
*
|
||||
* @var \Drupal\user\UserInterface
|
||||
*/
|
||||
protected $user;
|
||||
|
||||
/**
|
||||
* The original value of the 'display_errors' PHP configuration option.
|
||||
*
|
||||
* @todo Remove this when issue #2905597 is fixed.
|
||||
* @see https://www.drupal.org/node/2905597
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $originalDisplayErrorsValue;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->fileSystem = $this->container->get('file_system');
|
||||
|
||||
// Create the Article node type.
|
||||
$this->drupalCreateContentType(['type' => 'article', 'name' => 'Article']);
|
||||
|
||||
// Attach a file field to the node type.
|
||||
$field_settings = ['file_extensions' => 'txt'];
|
||||
$this->createFileField('field_file', 'node', 'article', [], $field_settings);
|
||||
|
||||
// Log in as a content author who can create Articles.
|
||||
$this->user = $this->drupalCreateUser([
|
||||
'access content',
|
||||
'create article content',
|
||||
]);
|
||||
$this->drupalLogin($this->user);
|
||||
|
||||
// Disable the displaying of errors, so that the AJAX responses are not
|
||||
// contaminated with error messages about exceeding the maximum POST size.
|
||||
// @todo Remove this when issue #2905597 is fixed.
|
||||
// @see https://www.drupal.org/node/2905597
|
||||
$this->originalDisplayErrorsValue = ini_set('display_errors', '0');
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function tearDown() {
|
||||
// Restore the displaying of errors to the original value.
|
||||
// @todo Remove this when issue #2905597 is fixed.
|
||||
// @see https://www.drupal.org/node/2905597
|
||||
ini_set('display_errors', $this->originalDisplayErrorsValue);
|
||||
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that uploading files exceeding maximum size are handled correctly.
|
||||
*/
|
||||
public function testUploadFileExceedingMaximumFileSize() {
|
||||
$session = $this->getSession();
|
||||
|
||||
// Create a test file that exceeds the maximum POST size with 1 kilobyte.
|
||||
$post_max_size = Bytes::toInt(ini_get('post_max_size'));
|
||||
$invalid_file = $this->generateFile('exceeding_post_max_size', ceil(($post_max_size + 1024) / 1024), 1024);
|
||||
|
||||
// Go to the node creation form and try to upload the test file.
|
||||
$this->drupalGet('node/add/article');
|
||||
$page = $session->getPage();
|
||||
$page->attachFileToField("files[field_file_0]", $this->fileSystem->realpath($invalid_file));
|
||||
|
||||
// An error message should appear informing the user that the file exceeded
|
||||
// the maximum file size.
|
||||
$this->assertSession()->waitForElement('css', '.messages--error');
|
||||
// The error message includes the actual file size limit which depends on
|
||||
// the current environment, so we check for a part of the message.
|
||||
$this->assertSession()->pageTextContains('An unrecoverable error occurred. The uploaded file likely exceeded the maximum file size');
|
||||
|
||||
// Now upload a valid file and check that the error message disappears.
|
||||
$valid_file = $this->generateFile('not_exceeding_post_max_size', 8, 8);
|
||||
$page->attachFileToField("files[field_file_0]", $this->fileSystem->realpath($valid_file));
|
||||
$this->assertSession()->waitForElement('named', ['id_or_name', 'field_file_0_remove_button']);
|
||||
$this->assertSession()->elementNotExists('css', '.messages--error');
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\Tests\file\Kernel;
|
||||
|
||||
/**
|
||||
* Tests the file url.
|
||||
*
|
||||
* @group file
|
||||
*/
|
||||
class FileUrlTest extends FileManagedUnitTestBase {
|
||||
|
||||
/**
|
||||
* Test public files with a different host name from settings.
|
||||
*/
|
||||
public function testFilesUrlWithDifferentHostName() {
|
||||
$test_base_url = 'http://www.example.com/cdn';
|
||||
$this->setSetting('file_public_base_url', $test_base_url);
|
||||
$filepath = file_create_filename('test.txt', '');
|
||||
$directory_uri = 'public://' . dirname($filepath);
|
||||
file_prepare_directory($directory_uri, FILE_CREATE_DIRECTORY);
|
||||
$file = $this->createFile($filepath, NULL, 'public');
|
||||
$url = file_create_url($file->getFileUri());
|
||||
$expected_url = $test_base_url . '/' . basename($filepath);
|
||||
$this->assertSame($url, $expected_url);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,278 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\Tests\file\Kernel\Migrate\EntityFileTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\Tests\file\Kernel\Migrate;
|
||||
|
||||
use Drupal\Core\StreamWrapper\StreamWrapperInterface;
|
||||
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\KernelTests\KernelTestBase;
|
||||
|
||||
/**
|
||||
* Tests the entity file destination plugin.
|
||||
*
|
||||
* @group file
|
||||
*/
|
||||
class EntityFileTest extends KernelTestBase {
|
||||
|
||||
/**
|
||||
* Modules to install.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('system', 'entity_test', 'user', 'file');
|
||||
|
||||
/**
|
||||
* @var \Drupal\Tests\file\Kernel\Migrate\TestEntityFile $destination
|
||||
*/
|
||||
protected $destination;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
\Drupal::getContainer()->get('stream_wrapper_manager')->registerWrapper('public', 'Drupal\Core\StreamWrapper\PublicStream', StreamWrapperInterface::NORMAL);
|
||||
$this->destination = new TestEntityFile([]);
|
||||
$this->installEntitySchema('file');
|
||||
|
||||
file_put_contents('/tmp/test-file.jpg', '');
|
||||
}
|
||||
|
||||
/**
|
||||
* Test successful imports/copies.
|
||||
*/
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The data provider for testing the file destination.
|
||||
*
|
||||
* @return array
|
||||
* An array of file permutations to test.
|
||||
*/
|
||||
protected function localFileDataProvider() {
|
||||
return [
|
||||
// Test a local to local copy.
|
||||
[['filepath' => 'core/modules/simpletest/files/image-test.jpg'], 'public://file1.jpg', TRUE, $this->root . '/'],
|
||||
// Test a temporary file using an absolute path.
|
||||
[['filepath' => '/tmp/test-file.jpg'], 'temporary://test.jpg', TRUE, ''],
|
||||
// Test a temporary file using a relative path.
|
||||
[['filepath' => 'test-file.jpg'], 'temporary://core/modules/simpletest/files/test.jpg', TRUE, '/tmp/'],
|
||||
// Test a remote path to local.
|
||||
[['filepath' => 'core/modules/simpletest/files/image-test.jpg'], 'public://remote-file.jpg', TRUE, $this->root . '/'],
|
||||
// Test a remote path to local inside a folder that doesn't exist.
|
||||
[['filepath' => 'core/modules/simpletest/files/image-test.jpg'], 'public://folder/remote-file.jpg', TRUE, $this->root . '/'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that non-existent files throw an exception.
|
||||
*/
|
||||
public function testNonExistentSourceFile() {
|
||||
$destination = '/non/existent/file';
|
||||
try {
|
||||
// If this test passes, doImport() will raise a MigrateException and
|
||||
// we'll never reach fail().
|
||||
$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.");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests various invocations of the writeFile() method.
|
||||
*/
|
||||
public function testWriteFile() {
|
||||
$plugin = $this->destination;
|
||||
$method = new \ReflectionMethod($plugin, 'writeFile');
|
||||
$method->setAccessible(TRUE);
|
||||
|
||||
touch('temporary://baz.txt');
|
||||
|
||||
// Moving an actual file should return TRUE.
|
||||
$plugin->configuration['move'] = TRUE;
|
||||
$this->assertTrue($method->invoke($plugin, 'temporary://baz.txt', 'public://foo.txt'));
|
||||
|
||||
// Trying to move a non-existent file should return FALSE.
|
||||
$this->assertFalse($method->invoke($plugin, 'temporary://invalid.txt', 'public://invalid.txt'));
|
||||
|
||||
// Copying over a file that already exists should replace the existing file.
|
||||
$plugin->configuration['move'] = FALSE;
|
||||
touch('temporary://baz.txt');
|
||||
$this->assertTrue($method->invoke($plugin, 'temporary://baz.txt', 'public://foo.txt'));
|
||||
// Copying over a file that already exists should rename the resulting file
|
||||
// if FILE_EXISTS_RENAME is specified.
|
||||
$method->invoke($plugin, 'temporary://baz.txt', 'public://foo.txt', FILE_EXISTS_RENAME);
|
||||
$this->assertTrue(file_exists('public://foo_0.txt'));
|
||||
|
||||
// Trying to copy a non-existent file should return FALSE.
|
||||
$this->assertFalse($method->invoke($plugin, 'temporary://invalid.txt', 'public://invalid.txt'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests various invocations of the getOverwriteMode() method.
|
||||
*/
|
||||
public function testGetOverwriteMode() {
|
||||
$plugin = $this->destination;
|
||||
$method = new \ReflectionMethod($plugin, 'getOverwriteMode');
|
||||
$method->setAccessible(TRUE);
|
||||
|
||||
$row = new Row([], []);
|
||||
// If the plugin is not configured to rename the destination file, we should
|
||||
// always get FILE_EXISTS_REPLACE.
|
||||
$this->assertIdentical(FILE_EXISTS_REPLACE, $method->invoke($plugin, $row));
|
||||
|
||||
// When the plugin IS configured to rename the destination file, it should
|
||||
// return FILE_EXISTS_RENAME if the destination entity already exists,
|
||||
// and FILE_EXISTS_REPLACE otherwise.
|
||||
$plugin->configuration['rename'] = TRUE;
|
||||
$plugin->storage = \Drupal::entityManager()->getStorage('file');
|
||||
/** @var \Drupal\file\FileInterface $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');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests various invocations of the getDirectory() method.
|
||||
*/
|
||||
public function testGetDirectory() {
|
||||
$plugin = $this->destination;
|
||||
$method = new \ReflectionMethod($plugin, 'getDirectory');
|
||||
$method->setAccessible(TRUE);
|
||||
|
||||
$this->assertSame('public://foo', $method->invoke($plugin, 'public://foo/baz.txt'));
|
||||
$this->assertSame('/path/to', $method->invoke($plugin, '/path/to/foo.txt'));
|
||||
// A directory like public:// (no path) needs to resolve to a physical path.
|
||||
$fs = \Drupal::getContainer()->get('file_system');
|
||||
$this->assertSame($fs->realpath('public://'), $method->invoke($plugin, 'public://foo.txt'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests various invocations of the isLocationUnchanged() method.
|
||||
*/
|
||||
public function testIsLocationUnchanged() {
|
||||
$plugin = $this->destination;
|
||||
$method = new \ReflectionMethod($plugin, 'isLocationUnchanged');
|
||||
$method->setAccessible(TRUE);
|
||||
|
||||
$temporary_file = '/tmp/foo.txt';
|
||||
touch($temporary_file);
|
||||
$this->assertTrue($method->invoke($plugin, $temporary_file, 'temporary://foo.txt'));
|
||||
unlink($temporary_file);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests various invocations of the isLocalUri() method.
|
||||
*/
|
||||
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'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Do an import using the destination.
|
||||
*
|
||||
* @param array $row_values
|
||||
* An array of row values.
|
||||
* @param string $destination_path
|
||||
* The destination path to copy to.
|
||||
* @param string $source_base_path
|
||||
* The source base path.
|
||||
* @return array
|
||||
* An array of saved entities ids.
|
||||
*
|
||||
* @throws \Drupal\migrate\MigrateException
|
||||
*/
|
||||
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;
|
||||
|
||||
// Importing asserts there are no errors, then we just check the file has
|
||||
// been copied into place.
|
||||
return $this->destination->import($row, array());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class TestEntityFile extends EntityFile {
|
||||
|
||||
/**
|
||||
* This is needed to be passed to $this->save().
|
||||
*
|
||||
* @var \Drupal\Core\Entity\ContentEntityInterface
|
||||
*/
|
||||
public $mockEntity;
|
||||
|
||||
/**
|
||||
* Make this public for easy writing during tests.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $configuration;
|
||||
|
||||
/**
|
||||
* @var \Drupal\Core\Entity\EntityStorageInterface
|
||||
*/
|
||||
public $storage;
|
||||
|
||||
public function __construct($configuration = []) {
|
||||
$configuration += array(
|
||||
'source_base_path' => '',
|
||||
'source_path_property' => 'filepath',
|
||||
'destination_path_property' => 'uri',
|
||||
'move' => FALSE,
|
||||
'urlencode' => FALSE,
|
||||
);
|
||||
$this->configuration = $configuration;
|
||||
// We need a mock entity to be passed to save to prevent strict exceptions.
|
||||
$this->mockEntity = EntityTest::create();
|
||||
$this->streamWrapperManager = \Drupal::getContainer()->get('stream_wrapper_manager');
|
||||
$this->fileSystem = \Drupal::getContainer()->get('file_system');
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function getEntity(Row $row, array $old_destination_id_values) {
|
||||
return $this->mockEntity;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function save(ContentEntityInterface $entity, array $old_destination_id_values = array()) {}
|
||||
|
||||
}
|
||||
@@ -51,7 +51,7 @@ class FileCckTest extends UnitTestCase {
|
||||
$this->plugin->processCckFieldValues($this->migration, 'somefieldname', []);
|
||||
|
||||
$expected = [
|
||||
'plugin' => 'iterator',
|
||||
'plugin' => 'sub_process',
|
||||
'source' => 'somefieldname',
|
||||
'process' => [
|
||||
'target_id' => 'fid',
|
||||
|
||||
@@ -50,7 +50,7 @@ class ImageCckTest extends UnitTestCase {
|
||||
$this->plugin->processCckFieldValues($this->migration, 'somefieldname', []);
|
||||
|
||||
$expected = [
|
||||
'plugin' => 'iterator',
|
||||
'plugin' => 'sub_process',
|
||||
'source' => 'somefieldname',
|
||||
'process' => [
|
||||
'target_id' => 'fid',
|
||||
|
||||
@@ -50,7 +50,7 @@ class FileFieldTest extends UnitTestCase {
|
||||
$this->plugin->processFieldValues($this->migration, 'somefieldname', []);
|
||||
|
||||
$expected = [
|
||||
'plugin' => 'iterator',
|
||||
'plugin' => 'sub_process',
|
||||
'source' => 'somefieldname',
|
||||
'process' => [
|
||||
'target_id' => 'fid',
|
||||
|
||||
@@ -49,7 +49,7 @@ class ImageFieldTest extends UnitTestCase {
|
||||
$this->plugin->processFieldValues($this->migration, 'somefieldname', []);
|
||||
|
||||
$expected = [
|
||||
'plugin' => 'iterator',
|
||||
'plugin' => 'sub_process',
|
||||
'source' => 'somefieldname',
|
||||
'process' => [
|
||||
'target_id' => 'fid',
|
||||
|
||||
@@ -1,54 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\Tests\file\Unit\Plugin\migrate\source\d6;
|
||||
|
||||
use Drupal\Tests\migrate\Unit\MigrateSqlSourceTestCase;
|
||||
|
||||
/**
|
||||
* Tests D6 file source plugin.
|
||||
*
|
||||
* @group file
|
||||
*/
|
||||
class FileTest extends MigrateSqlSourceTestCase {
|
||||
|
||||
const PLUGIN_CLASS = 'Drupal\file\Plugin\migrate\source\d6\File';
|
||||
|
||||
protected $migrationConfiguration = array(
|
||||
'id' => 'test',
|
||||
'source' => array(
|
||||
'plugin' => 'd6_file',
|
||||
),
|
||||
);
|
||||
|
||||
protected $expectedResults = array(
|
||||
array(
|
||||
'fid' => 1,
|
||||
'uid' => 1,
|
||||
'filename' => 'migrate-test-file-1.pdf',
|
||||
'filepath' => 'sites/default/files/migrate-test-file-1.pdf',
|
||||
'filemime' => 'application/pdf',
|
||||
'filesize' => 890404,
|
||||
'status' => 1,
|
||||
'timestamp' => 1382255613,
|
||||
),
|
||||
array(
|
||||
'fid' => 2,
|
||||
'uid' => 1,
|
||||
'filename' => 'migrate-test-file-2.pdf',
|
||||
'filepath' => 'sites/default/files/migrate-test-file-2.pdf',
|
||||
'filemime' => 'application/pdf',
|
||||
'filesize' => 204124,
|
||||
'status' => 1,
|
||||
'timestamp' => 1382255662,
|
||||
),
|
||||
);
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
$this->databaseContents['files'] = $this->expectedResults;
|
||||
parent::setUp();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,65 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\Tests\file\Unit\Plugin\migrate\source\d6;
|
||||
|
||||
use Drupal\file\Plugin\migrate\source\d6\UploadInstance;
|
||||
use Drupal\Tests\migrate\Unit\MigrateSqlSourceTestCase;
|
||||
|
||||
/**
|
||||
* Tests d6_upload_instance source plugin.
|
||||
*
|
||||
* @group file
|
||||
*/
|
||||
class UploadInstanceTest extends MigrateSqlSourceTestCase {
|
||||
|
||||
const PLUGIN_CLASS = UploadInstance::class;
|
||||
|
||||
protected $migrationConfiguration = array(
|
||||
'id' => 'test',
|
||||
'source' => array(
|
||||
'plugin' => 'd6_upload_instance',
|
||||
),
|
||||
);
|
||||
|
||||
protected $expectedResults = array(
|
||||
array(
|
||||
'node_type' => 'article',
|
||||
'max_filesize' => '16MB',
|
||||
'file_extensions' => 'txt pdf',
|
||||
),
|
||||
);
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
$this->databaseContents['node_type'] = array(
|
||||
array(
|
||||
'type' => 'article',
|
||||
),
|
||||
array(
|
||||
'type' => 'company',
|
||||
),
|
||||
);
|
||||
$this->databaseContents['variable'] = array(
|
||||
array(
|
||||
'name' => 'upload_article',
|
||||
'value' => serialize(TRUE),
|
||||
),
|
||||
array(
|
||||
'name' => 'upload_company',
|
||||
'value' => serialize(FALSE),
|
||||
),
|
||||
array(
|
||||
'name' => 'upload_uploadsize_default',
|
||||
'value' => serialize(16),
|
||||
),
|
||||
array(
|
||||
'name' => 'upload_extensions_default',
|
||||
'value' => serialize('txt pdf'),
|
||||
),
|
||||
);
|
||||
parent::setUp();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,75 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\Tests\file\Unit\Plugin\migrate\source\d6;
|
||||
|
||||
use Drupal\file\Plugin\migrate\source\d6\Upload;
|
||||
use Drupal\Tests\migrate\Unit\MigrateSqlSourceTestCase;
|
||||
|
||||
/**
|
||||
* Tests d6_upload source plugin.
|
||||
*
|
||||
* @group file
|
||||
*/
|
||||
class UploadTest extends MigrateSqlSourceTestCase {
|
||||
|
||||
const PLUGIN_CLASS = Upload::class;
|
||||
|
||||
protected $migrationConfiguration = array(
|
||||
'id' => 'test',
|
||||
'source' => array(
|
||||
'plugin' => 'd6_upload',
|
||||
),
|
||||
);
|
||||
|
||||
protected $expectedResults = array(
|
||||
array(
|
||||
'upload' => array(
|
||||
array(
|
||||
'fid' => '1',
|
||||
'description' => 'file 1-1-1',
|
||||
'list' => '0',
|
||||
),
|
||||
),
|
||||
'nid' => '1',
|
||||
'vid' => '1',
|
||||
'type' => 'story',
|
||||
),
|
||||
);
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
$this->databaseContents['upload'] = array(
|
||||
array(
|
||||
'fid' => '1',
|
||||
'nid' => '1',
|
||||
'vid' => '1',
|
||||
'description' => 'file 1-1-1',
|
||||
'list' => '0',
|
||||
'weight' => '-1',
|
||||
),
|
||||
);
|
||||
$this->databaseContents['node'] = array(
|
||||
array(
|
||||
'nid' => '1',
|
||||
'vid' => '1',
|
||||
'type' => 'story',
|
||||
'language' => '',
|
||||
'title' => 'Test title',
|
||||
'uid' => '1',
|
||||
'status' => '1',
|
||||
'created' => '1388271197',
|
||||
'changed' => '1420861423',
|
||||
'comment' => '0',
|
||||
'promote' => '0',
|
||||
'moderate' => '0',
|
||||
'sticky' => '0',
|
||||
'tnid' => '0',
|
||||
'translate' => '0',
|
||||
),
|
||||
);
|
||||
parent::setUp();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,144 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\Tests\file\Unit\Plugin\migrate\source\d7\FileTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\Tests\file\Unit\Plugin\migrate\source\d7;
|
||||
|
||||
use Drupal\Core\Database\Query\ConditionInterface;
|
||||
use Drupal\file\Plugin\migrate\source\d7\File;
|
||||
use Drupal\migrate\Row;
|
||||
use Drupal\Tests\migrate\Unit\MigrateSqlSourceTestCase;
|
||||
|
||||
/**
|
||||
* Tests D7 file source plugin.
|
||||
*
|
||||
* @group file
|
||||
*/
|
||||
class FileTest extends MigrateSqlSourceTestCase {
|
||||
|
||||
const PLUGIN_CLASS = 'Drupal\Tests\file\Unit\Plugin\migrate\source\d7\TestFile';
|
||||
|
||||
protected $migrationConfiguration = array(
|
||||
'id' => 'test',
|
||||
'source' => array(
|
||||
'plugin' => 'd7_file',
|
||||
// Used by testFilteringByScheme().
|
||||
'scheme' => array(
|
||||
'public',
|
||||
'private',
|
||||
),
|
||||
),
|
||||
'destination' => array(
|
||||
'plugin' => 'entity:file',
|
||||
'source_base_path' => '/path/to/files',
|
||||
),
|
||||
);
|
||||
|
||||
protected $expectedResults = [
|
||||
[
|
||||
'fid' => '1',
|
||||
'uid' => '1',
|
||||
'filename' => 'cube.jpeg',
|
||||
'uri' => 'public://cube.jpeg',
|
||||
'filemime' => 'image/jpeg',
|
||||
'filesize' => '3620',
|
||||
'status' => '1',
|
||||
'timestamp' => '1421727515',
|
||||
],
|
||||
];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
$this->databaseContents['file_managed'] = $this->expectedResults;
|
||||
parent::setUp();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that public file URIs are properly transformed by prepareRow().
|
||||
*/
|
||||
public function testPublicUri() {
|
||||
$this->source->publicPath = 'sites/default/files';
|
||||
$row = new Row(['uri' => 'public://foo.png'], ['uri' => []]);
|
||||
$this->source->prepareRow($row);
|
||||
$this->assertEquals('sites/default/files/foo.png',
|
||||
$row->getSourceProperty('filepath'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that private file URIs are properly transformed by prepareRow().
|
||||
*/
|
||||
public function testPrivateUri() {
|
||||
$this->source->privatePath = '/path/to/private/files';
|
||||
$row = new Row(['uri' => 'private://baz.jpeg'], ['uri' => []]);
|
||||
$this->source->prepareRow($row);
|
||||
$this->assertEquals('/path/to/private/files/baz.jpeg',
|
||||
$row->getSourceProperty('filepath'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that temporary file URIs are property transformed by prepareRow().
|
||||
*/
|
||||
public function testTemporaryUri() {
|
||||
$this->source->temporaryPath = '/tmp';
|
||||
$row = new Row(['uri' => 'temporary://camelot/lancelot.gif'],
|
||||
['uri' => []]);
|
||||
$this->source->prepareRow($row);
|
||||
$this->assertEquals('/tmp/camelot/lancelot.gif',
|
||||
$row->getSourceProperty('filepath'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that it's possible to filter files by scheme.
|
||||
*/
|
||||
public function testFilteringByScheme() {
|
||||
$query_conditions = $this->source->query()->conditions();
|
||||
$scheme_condition = end($query_conditions);
|
||||
|
||||
$this->assertInstanceOf(ConditionInterface::class, $scheme_condition['field']);
|
||||
$conditions = $scheme_condition['field']->conditions();
|
||||
|
||||
$this->assertSame('uri', $conditions[0]['field']);
|
||||
$this->assertSame('LIKE', $conditions[0]['operator']);
|
||||
$this->assertSame('public://%', $conditions[0]['value']);
|
||||
|
||||
$this->assertSame('uri', $conditions[1]['field']);
|
||||
$this->assertSame('LIKE', $conditions[1]['operator']);
|
||||
$this->assertSame('private://%', $conditions[1]['value']);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Testing version of \Drupal\file\Plugin\migrate\source\d7\File.
|
||||
*
|
||||
* Exposes inaccessible properties.
|
||||
*/
|
||||
class TestFile extends File {
|
||||
|
||||
/**
|
||||
* The public file directory path.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $publicPath;
|
||||
|
||||
/**
|
||||
* The private file directory path, if any.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $privatePath;
|
||||
|
||||
/**
|
||||
* The temporary file directory path.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $temporaryPath;
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user