Update to Drupal 8.2.0. For more information, see https://www.drupal.org/project/drupal/releases/8.2.0

This commit is contained in:
Pantheon Automation 2016-10-06 15:16:20 -07:00 committed by Greg Anderson
parent 2f563ab520
commit f1c8716f57
1732 changed files with 52334 additions and 11780 deletions

View file

@ -1,18 +0,0 @@
/**
* @file
* Admin stylesheet for file module.
*/
/* File upload widget.*/
.form-managed-file .form-submit {
margin: 0 0.5em;
}
.form-managed-file div.ajax-progress-bar {
display: none;
margin-top: 4px;
padding: 0;
width: 28em;
}
.form-managed-file .ajax-progress-bar .bar {
margin: 0;
}

View file

@ -2,9 +2,6 @@ drupal.file:
version: VERSION
js:
file.js: {}
css:
theme:
css/file.admin.css: {}
dependencies:
- core/jquery
- core/jquery.once

View file

@ -37,7 +37,7 @@ function file_help($route_name, RouteMatchInterface $route_match) {
$output .= '<dd>' . t('The <em>settings</em> and the <em>display</em> of the file field can be configured separately. See the <a href=":field_ui">Field UI help</a> for more information on how to manage fields and their display.', array(':field_ui' => (\Drupal::moduleHandler()->moduleExists('field_ui')) ? \Drupal::url('help.page', array('name' => 'field_ui')) : '#')) . '</dd>';
$output .= '<dt>' . t('Allowing file extensions') . '</dt>';
$output .= '<dd>' . t('In the field settings, you can define the allowed file extensions (for example <em>pdf docx psd</em>) for the files that will be uploaded with the file field.') . '</dd>';
$output .= '<dt>' . t('Storing files ') . '</dt>';
$output .= '<dt>' . t('Storing files') . '</dt>';
$output .= '<dd>' . t('Uploaded files can either be stored as <em>public</em> or <em>private</em>, depending on the <a href=":file-system">File system settings</a>. For more information, see the <a href=":system-help">System module help page</a>.', array(':file-system' => \Drupal::url('system.file_system_settings'), ':system-help' => \Drupal::url('help.page', array('name' => 'system')))) . '</dd>';
$output .= '<dt>' . t('Restricting the maximum file size') . '</dt>';
$output .= '<dd>' . t('The maximum file size that users can upload is limited by PHP settings of the server, but you can restrict by entering the desired value as the <em>Maximum upload size</em> setting. The maximum file size is automatically displayed to users in the help text of the file field.') . '</dd>';
@ -442,8 +442,16 @@ function file_validate_image_resolution(FileInterface $file, $maximum_dimensions
// Try to resize the image to fit the dimensions.
if ($image->scale($width, $height)) {
$image->save();
$file->filesize = $image->getFileSize();
drupal_set_message(t('The image was resized to fit within the maximum allowed dimensions of %dimensions pixels.', array('%dimensions' => $maximum_dimensions)));
if (!empty($width) && !empty($height)) {
$message = t('The image was resized to fit within the maximum allowed dimensions of %dimensions pixels.', array('%dimensions' => $maximum_dimensions));
}
elseif (empty($width)) {
$message = t('The image was resized to fit within the maximum allowed height of %height pixels.', array('%height' => $height));
}
elseif (empty($height)) {
$message = t('The image was resized to fit within the maximum allowed width of %width pixels.', array('%width' => $width));
}
drupal_set_message($message);
}
else {
$errors[] = t('The image exceeds the maximum allowed dimensions and an attempt to resize it failed.');
@ -1310,7 +1318,7 @@ function file_icon_class($mime_type) {
* @param string $mime_type
* A MIME type.
*
* @return string|FALSE
* @return string|false
* The generic icon MIME package expected for this file.
*/
function file_icon_map($mime_type) {
@ -1474,7 +1482,8 @@ function file_get_file_references(FileInterface $file, FieldDefinitionInterface
$usage_list = \Drupal::service('file.usage')->listUsage($file);
$file_usage_list = isset($usage_list['file']) ? $usage_list['file'] : array();
foreach ($file_usage_list as $entity_type_id => $entity_ids) {
$entities = entity_load_multiple($entity_type_id, array_keys($entity_ids));
$entities = \Drupal::entityTypeManager()
->getStorage($entity_type_id)->loadMultiple(array_keys($entity_ids));
foreach ($entities as $entity) {
$bundle = $entity->bundle();
// We need to find file fields for this entity type and bundle.

View file

@ -1,3 +1,2 @@
access files overview:
title: 'Access the Files overview page'
description: 'Get an overview of all files.'

View file

@ -1,21 +1,41 @@
# Every migration that saves into {file_managed} must have the d6_file
# migration as an optional dependency to ensure d6_file runs first.
# Every migration that references a file by Drupal 6 fid should specify this
# migration as an optional dependency.
id: d6_file
label: Files
migration_tags:
- Drupal 6
source:
plugin: d6_file
constants:
# source_base_path must be set by the tool configuring this migration. It
# represents the fully qualified path relative to which URIs in the files
# table are specified, and must end with a /. See source_full_path
# configuration in this migration's process pipeline as an example.
source_base_path: ''
process:
fid: fid
filename: filename
uri:
source_full_path:
-
plugin: concat
delimiter: /
source:
- constants/source_base_path
- filepath
-
plugin: urlencode
destination_full_path:
plugin: file_uri
source:
- filepath
- file_directory_path
- temp_directory_path
- is_public
uri:
plugin: file_copy
source:
- '@source_full_path'
- '@destination_full_path'
filemime: filemime
filesize: filesize
status: status
@ -23,4 +43,3 @@ process:
uid: uid
destination:
plugin: entity:file
urlencode: true

View file

@ -1,15 +1,34 @@
# Every migration that references a file by fid should specify this migration
# as an optional dependency.
# Every migration that references a file by Drupal 7 fid should specify this
# migration as an optional dependency.
id: d7_file
label: Files
migration_tags:
- Drupal 7
source:
plugin: d7_file
constants:
# source_base_path must be set by the tool configuring this migration. It
# represents the fully qualified path relative to which uris in the files
# table are specified, and must end with a /. See source_full_path
# configuration in this migration's process pipeline as an example.
source_base_path: ''
process:
fid: fid
filename: filename
uri: uri
source_full_path:
-
plugin: concat
delimiter: /
source:
- constants/source_base_path
- filepath
-
plugin: urlencode
uri:
plugin: file_copy
source:
- '@source_full_path'
- uri
filemime: filemime
# filesize is dynamically computed when file entities are saved, so there is
# no point in migrating it.
@ -22,5 +41,3 @@ process:
uid: uid
destination:
plugin: entity:file
source_path_property: filepath
urlencode: true

View file

@ -1,7 +1,8 @@
id: d6_file_settings
id: file_settings
label: File configuration
migration_tags:
- Drupal 6
- Drupal 7
source:
plugin: variable
variables:

View file

@ -220,19 +220,15 @@ class File extends ContentEntityBase implements FileInterface {
* {@inheritdoc}
*/
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields['fid'] = BaseFieldDefinition::create('integer')
->setLabel(t('File ID'))
->setDescription(t('The file ID.'))
->setReadOnly(TRUE)
->setSetting('unsigned', TRUE);
/** @var \Drupal\Core\Field\BaseFieldDefinition[] $fields */
$fields = parent::baseFieldDefinitions($entity_type);
$fields['uuid'] = BaseFieldDefinition::create('uuid')
->setLabel(t('UUID'))
->setDescription(t('The file UUID.'))
->setReadOnly(TRUE);
$fields['fid']->setLabel(t('File ID'))
->setDescription(t('The file ID.'));
$fields['langcode'] = BaseFieldDefinition::create('language')
->setLabel(t('Language code'))
$fields['uuid']->setDescription(t('The file UUID.'));
$fields['langcode']->setLabel(t('Language code'))
->setDescription(t('The file language code.'));
$fields['uid'] = BaseFieldDefinition::create('entity_reference')

View file

@ -3,75 +3,18 @@
namespace Drupal\file\Plugin\migrate\destination;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Field\FieldTypePluginManagerInterface;
use Drupal\Core\Field\Plugin\Field\FieldType\UriItem;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\StreamWrapper\LocalStream;
use Drupal\Core\StreamWrapper\StreamWrapperManagerInterface;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\Row;
use Drupal\migrate\MigrateException;
use Drupal\migrate\Plugin\migrate\destination\EntityContentBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Every migration that uses this destination must have an optional
* dependency on the d6_file migration to ensure it runs first.
*
* @MigrateDestination(
* id = "entity:file"
* )
*/
class EntityFile extends EntityContentBase {
/**
* @var \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface
*/
protected $streamWrapperManager;
/**
* @var \Drupal\Core\File\FileSystemInterface
*/
protected $fileSystem;
/**
* {@inheritdoc}
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, EntityStorageInterface $storage, array $bundles, EntityManagerInterface $entity_manager, FieldTypePluginManagerInterface $field_type_manager, StreamWrapperManagerInterface $stream_wrappers, FileSystemInterface $file_system) {
$configuration += array(
'source_base_path' => '',
'source_path_property' => 'filepath',
'destination_path_property' => 'uri',
'move' => FALSE,
'urlencode' => FALSE,
);
parent::__construct($configuration, $plugin_id, $plugin_definition, $migration, $storage, $bundles, $entity_manager, $field_type_manager);
$this->streamWrapperManager = $stream_wrappers;
$this->fileSystem = $file_system;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
$entity_type = static::getEntityTypeId($plugin_id);
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$migration,
$container->get('entity.manager')->getStorage($entity_type),
array_keys($container->get('entity.manager')->getBundleInfo($entity_type)),
$container->get('entity.manager'),
$container->get('plugin.manager.field.field_type'),
$container->get('stream_wrapper_manager'),
$container->get('file_system')
);
}
/**
* {@inheritdoc}
*/
@ -82,7 +25,12 @@ class EntityFile extends EntityContentBase {
return parent::getEntity($row, $old_destination_id_values);
}
$destination = $row->getDestinationProperty($this->configuration['destination_path_property']);
// By default the entity key (fid) would be used, but we want to make sure
// we're loading the matching URI.
$destination = $row->getDestinationProperty('uri');
if (empty($destination)) {
throw new MigrateException('Destination property uri not provided');
}
$entity = $this->storage->loadByProperties(['uri' => $destination]);
if ($entity) {
return reset($entity);
@ -92,181 +40,6 @@ class EntityFile extends EntityContentBase {
}
}
/**
* {@inheritdoc}
*/
public function import(Row $row, array $old_destination_id_values = array()) {
// For stub rows, there is no real file to deal with, let the stubbing
// process create the stub entity.
if ($row->isStub()) {
return parent::import($row, $old_destination_id_values);
}
$file = $row->getSourceProperty($this->configuration['source_path_property']);
$destination = $row->getDestinationProperty($this->configuration['destination_path_property']);
$source = $this->configuration['source_base_path'] . $file;
// Ensure the source file exists, if it's a local URI or path.
if ($this->isLocalUri($source) && !file_exists($source)) {
throw new MigrateException("File '$source' does not exist.");
}
// If the start and end file is exactly the same, there is nothing to do.
if ($this->isLocationUnchanged($source, $destination)) {
return parent::import($row, $old_destination_id_values);
}
$replace = $this->getOverwriteMode($row);
$success = $this->writeFile($source, $destination, $replace);
if (!$success) {
$dir = $this->getDirectory($destination);
if (file_prepare_directory($dir, FILE_CREATE_DIRECTORY)) {
$success = $this->writeFile($source, $destination, $replace);
}
else {
throw new MigrateException("Could not create directory '$dir'");
}
}
if ($success) {
return parent::import($row, $old_destination_id_values);
}
else {
throw new MigrateException("File $source could not be copied to $destination.");
}
}
/**
* Tries to move or copy a file.
*
* @param string $source
* The source path or URI.
* @param string $destination
* The destination path or URI.
* @param int $replace
* (optional) FILE_EXISTS_REPLACE (default) or FILE_EXISTS_RENAME.
*
* @return bool
* TRUE on success, FALSE on failure.
*/
protected function writeFile($source, $destination, $replace = FILE_EXISTS_REPLACE) {
if ($this->configuration['move']) {
return (boolean) file_unmanaged_move($source, $destination, $replace);
}
else {
$destination = file_destination($destination, $replace);
$source = $this->urlencode($source);
return @copy($source, $destination);
}
}
/**
* Determines how to handle file conflicts.
*
* @param \Drupal\migrate\Row $row
*
* @return int
* Either FILE_EXISTS_REPLACE (default) or FILE_EXISTS_RENAME, depending
* on the current configuration.
*/
protected function getOverwriteMode(Row $row) {
if (!empty($this->configuration['rename'])) {
$entity_id = $row->getDestinationProperty($this->getKey('id'));
if ($entity_id && ($entity = $this->storage->load($entity_id))) {
return FILE_EXISTS_RENAME;
}
}
return FILE_EXISTS_REPLACE;
}
/**
* Returns the directory component of a URI or path.
*
* For URIs like public://foo.txt, the full physical path of public://
* will be returned, since a scheme by itself will trip up certain file
* API functions (such as file_prepare_directory()).
*
* @param string $uri
* The URI or path.
*
* @return string|false
* The directory component of the path or URI, or FALSE if it could not
* be determined.
*/
protected function getDirectory($uri) {
$dir = $this->fileSystem->dirname($uri);
if (substr($dir, -3) == '://') {
return $this->fileSystem->realpath($dir);
}
else {
return $dir;
}
}
/**
* Returns if the source and destination URIs represent identical paths.
* If either URI is a remote stream, will return FALSE.
*
* @param string $source
* The source URI.
* @param string $destination
* The destination URI.
*
* @return bool
* TRUE if the source and destination URIs refer to the same physical path,
* otherwise FALSE.
*/
protected function isLocationUnchanged($source, $destination) {
if ($this->isLocalUri($source) && $this->isLocalUri($destination)) {
return $this->fileSystem->realpath($source) === $this->fileSystem->realpath($destination);
}
else {
return FALSE;
}
}
/**
* Returns if the given URI or path is considered local.
*
* A URI or path is considered local if it either has no scheme component,
* or the scheme is implemented by a stream wrapper which extends
* \Drupal\Core\StreamWrapper\LocalStream.
*
* @param string $uri
* The URI or path to test.
*
* @return bool
*/
protected function isLocalUri($uri) {
$scheme = $this->fileSystem->uriScheme($uri);
return $scheme === FALSE || $this->streamWrapperManager->getViaScheme($scheme) instanceof LocalStream;
}
/**
* Urlencode all the components of a remote filename.
*
* @param string $filename
* The filename of the file to be urlencoded.
*
* @return string
* The urlencoded filename.
*/
protected function urlencode($filename) {
// Only apply to a full URL
if ($this->configuration['urlencode'] && strpos($filename, '://')) {
$components = explode('/', $filename);
foreach ($components as $key => $component) {
$components[$key] = rawurlencode($component);
}
$filename = implode('/', $components);
// Actually, we don't want certain characters encoded
$filename = str_replace('%3A', ':', $filename);
$filename = str_replace('%3F', '?', $filename);
$filename = str_replace('%26', '&', $filename);
}
return $filename;
}
/**
* {@inheritdoc}
*/

View file

@ -84,9 +84,7 @@ class File extends DrupalSqlBase {
// At this point, $path could be an absolute path or a relative path,
// depending on how the scheme's variable was set. So we need to shear out
// the source_base_path in order to make them all relative.
// @todo https://www.drupal.org/node/2577871 Don't depend on destination
// configuration and figure out if this is even needed at all?
$path = str_replace($this->migration->getDestinationConfiguration()['source_base_path'], NULL, $path);
$path = str_replace($this->configuration['constants']['source_base_path'], NULL, $path);
$row->setSourceProperty('filepath', $path);
return parent::prepareRow($row);
}

View file

@ -65,7 +65,7 @@ abstract class FileManagedTestBase extends WebTestBase {
* String with the hook name; for instance, 'load', 'save', 'insert', etc.
* @param int $expected_count
* Optional integer count.
* @param string|NULL $message
* @param string|null $message
* Optional translated string message.
*/
function assertFileHookCalled($hook, $expected_count = 1, $message = NULL) {

View file

@ -30,7 +30,9 @@ class FileOnTranslatedEntityTest extends FileFieldTestBase {
parent::setUp();
// Create the "Basic page" node type.
$this->drupalCreateContentType(array('type' => 'page', 'name' => 'Basic page'));
// @todo Remove the disabling of new revision creation in
// https://www.drupal.org/node/1239558.
$this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page', 'new_revision' => FALSE]);
// Create a file field on the "Basic page" node type.
$this->fieldName = strtolower($this->randomMachineName());
@ -166,10 +168,6 @@ class FileOnTranslatedEntityTest extends FileFieldTestBase {
$file = File::load($replaced_second_fid);
$this->assertTrue($file->isPermanent());
// Ensure the file status of the old second file is now temporary.
$file = File::load($second_fid);
$this->assertTrue($file->isTemporary());
// Delete the third translation.
$this->drupalPostForm('nl/node/' . $default_language_node->id() . '/delete', array(), t('Delete Dutch translation'));

View file

@ -317,7 +317,7 @@ function file_test_validator(File $file, $errors) {
* When the function is called with no $filepath parameter, the results are
* returned.
*
* @param string|NULL $filepath
* @param string|null $filepath
* File path
* @return array
* If $filepath is NULL, an array of all previous $filepath parameters

View file

@ -0,0 +1,33 @@
<?php
namespace Drupal\file_test\StreamWrapper;
use Drupal\Core\StreamWrapper\StreamWrapperInterface;
/**
* Dummy read-only remote stream wrapper (dummy-remote-readonly://).
*/
class DummyRemoteReadOnlyStreamWrapper extends DummyRemoteStreamWrapper {
/**
* {@inheritdoc}
*/
public static function getType() {
return StreamWrapperInterface::READ_VISIBLE;
}
/**
* {@inheritdoc}
*/
public function getName() {
return t('Dummy remote read-only files');
}
/**
* {@inheritdoc}
*/
public function getDescription() {
return t('Dummy remote read-only stream wrapper for testing.');
}
}

View file

@ -83,7 +83,7 @@ class FileItemTest extends FieldKernelTestBase {
$entity->name->value = $this->randomMachineName();
$entity->save();
$entity = entity_load('entity_test', $entity->id());
$entity = EntityTest::load($entity->id());
$this->assertTrue($entity->file_test instanceof FieldItemListInterface, 'Field implements interface.');
$this->assertTrue($entity->file_test[0] instanceof FieldItemInterface, 'Field item implements interface.');
$this->assertEqual($entity->file_test->target_id, $this->file->id());

View file

@ -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()) {}
}

View file

@ -1,6 +1,7 @@
<?php
namespace Drupal\Tests\file\Kernel\Migrate\d6;
use Drupal\migrate\Plugin\MigrationInterface;
/**
* Helper for setting up a file migration test.
@ -14,15 +15,23 @@ trait FileMigrationTestTrait {
$this->installEntitySchema('file');
$this->installConfig(['file']);
/** @var \Drupal\migrate\Plugin\MigrationInterface $migration */
$migration_plugin_manager = $this->container->get('plugin.manager.migration');
$this->executeMigration('d6_file');
}
/** @var \Drupal\migrate\Plugin\migration $migration */
$migration = $migration_plugin_manager->createInstance('d6_file');
$source = $migration->getSourceConfiguration();
$source['site_path'] = 'core/modules/simpletest';
$migration->set('source', $source);
$this->executeMigration($migration);
/**
* {@inheritdoc}
*/
protected function prepareMigration(MigrationInterface $migration) {
// File migrations need a source_base_path.
// @see MigrateUpgradeRunBatch::run
$destination = $migration->getDestinationConfiguration();
if ($destination['plugin'] === 'entity:file') {
// Make sure we have a single trailing slash.
$source = $migration->getSourceConfiguration();
$source['site_path'] = 'core/modules/simpletest';
$source['constants']['source_base_path'] = \Drupal::root() . '/';
$migration->set('source', $source);
}
}
}

View file

@ -19,7 +19,7 @@ class MigrateFileConfigsTest extends MigrateDrupal6TestBase {
*/
protected function setUp() {
parent::setUp();
$this->executeMigration('d6_file_settings');
$this->executeMigration('file_settings');
}
/**

View file

@ -72,9 +72,8 @@ class MigrateFileTest extends MigrateDrupal6TestBase implements MigrateDumpAlter
$this->assertEntity(5, 'html-1.txt', '24', 'public://html-1.txt', 'text/plain', '1');
// Test that we can re-import and also test with file_directory_path set.
$migration_plugin_manager = $this->container->get('plugin.manager.migration');
\Drupal::database()
->truncate($migration_plugin_manager->createInstance('d6_file')->getIdMap()->mapTableName())
->truncate($this->getMigration('d6_file')->getIdMap()->mapTableName())
->execute();
// Update the file_directory_path.
@ -89,16 +88,11 @@ class MigrateFileTest extends MigrateDrupal6TestBase implements MigrateDumpAlter
->condition('name', 'file_directory_temp')
->execute();
$migration = $migration_plugin_manager->createInstance('d6_file');
$this->executeMigration($migration);
$this->executeMigration('d6_file');
$file = File::load(2);
$this->assertIdentical('public://core/modules/simpletest/files/image-2.jpg', $file->getFileUri());
// Ensure that a temporary file has been migrated.
$file = File::load(6);
$this->assertIdentical('temporary://' . static::getUniqueFilename(), $file->getFileUri());
// File 7, created in static::migrateDumpAlter(), shares a path with
// file 5, which means it should be skipped entirely.
$this->assertNull(File::load(7));

View file

@ -0,0 +1,36 @@
<?php
namespace Drupal\Tests\file\Kernel\Migrate\d7;
use Drupal\config\Tests\SchemaCheckTestTrait;
use Drupal\Tests\migrate_drupal\Kernel\d7\MigrateDrupal7TestBase;
/**
* Upgrade variables to file.settings.yml.
*
* @group migrate_drupal_7
*/
class MigrateFileConfigsTest extends MigrateDrupal7TestBase {
use SchemaCheckTestTrait;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->executeMigration('file_settings');
}
/**
* Tests migration of file variables to file.settings.yml.
*/
public function testFileSettings() {
$config = $this->config('file.settings');
$this->assertSame('textfield', $config->get('description.type'));
$this->assertSame(256, $config->get('description.length'));
$this->assertSame('sites/default/files/icons', $config->get('icon.directory'));
$this->assertConfigSchema(\Drupal::service('config.typed'), 'file.settings', $config->get());
}
}

View file

@ -33,17 +33,11 @@ class MigrateFileTest extends MigrateDrupal7TestBase {
/** @var \Drupal\migrate\Plugin\Migration $migration */
$migration = $this->getMigration('d7_file');
// Set the destination plugin's source_base_path configuration value, which
// Set the source plugin's source_base_path configuration value, which
// would normally be set by the user running the migration.
$migration->set('destination', [
'plugin' => 'entity:file',
// Note that source_base_path must include a trailing slash because it's
// prepended directly to the value of the source path property.
'source_base_path' => $fs->realpath('public://') . '/',
// This is set in the migration's YAML file, but we need to repeat it
// here because all the destination configuration must be set at once.
'source_path_property' => 'filepath',
]);
$source = $migration->getSourceConfiguration();
$source['constants']['source_base_path'] = $fs->realpath('public://');
$migration->set('source', $source);
$this->executeMigration($migration);
}

View file

@ -25,6 +25,9 @@ class FileTest extends MigrateSqlSourceTestCase {
'id' => 'test',
'source' => array(
'plugin' => 'd7_file',
'constants' => array(
'source_base_path' => '/path/to/files',
),
// Used by testFilteringByScheme().
'scheme' => array(
'public',
@ -33,7 +36,6 @@ class FileTest extends MigrateSqlSourceTestCase {
),
'destination' => array(
'plugin' => 'entity:file',
'source_base_path' => '/path/to/files',
),
);