Commit e6fd7b29 authored by catch's avatar catch

Issue #3093563 by longwave, NigelCunningham, Dinesh18, kim.pepper: Remove all...

Issue #3093563 by longwave, NigelCunningham, Dinesh18, kim.pepper: Remove all @deprecated code in the file module
parent c85e3f77
......@@ -68,57 +68,6 @@ function file_field_widget_info_alter(array &$info) {
$info['uri']['field_types'][] = 'file_uri';
}
/**
* Loads file entities from the database.
*
* @param array|null $fids
* (optional) An array of entity IDs. If omitted or NULL, all entities are
* loaded.
* @param bool $reset
* (optional) Whether to reset the internal file_load_multiple() cache.
* Defaults to FALSE.
*
* @return array
* An array of file entities, indexed by fid.
*
* @deprecated in drupal:8.0.0 and is removed from drupal:9.0.0. Use
* \Drupal\file\Entity\File::loadMultiple().
*
* @see https://www.drupal.org/node/2266845
*/
function file_load_multiple(array $fids = NULL, $reset = FALSE) {
@trigger_error('file_load_multiple() is deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0. Use \Drupal\file\Entity\File::loadMultiple(). See https://www.drupal.org/node/2266845', E_USER_DEPRECATED);
if ($reset) {
\Drupal::entityTypeManager()->getStorage('file')->resetCache($fids);
}
return File::loadMultiple($fids);
}
/**
* Loads a single file entity from the database.
*
* @param int $fid
* A file ID.
* @param bool $reset
* (optional) Whether to reset the internal file_load_multiple() cache.
* Defaults to FALSE.
*
* @return \Drupal\file\FileInterface|null
* A file entity or NULL if the file was not found.
*
* @deprecated in drupal:8.0.0 and is removed from drupal:9.0.0. Use
* \Drupal\file\Entity\File::load().
*
* @see https://www.drupal.org/node/2266845
*/
function file_load($fid, $reset = FALSE) {
@trigger_error('file_load() is deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0. Use \Drupal\file\Entity\File::load(). See https://www.drupal.org/node/2266845', E_USER_DEPRECATED);
if ($reset) {
\Drupal::entityTypeManager()->getStorage('file')->resetCache([$fid]);
}
return File::load($fid);
}
/**
* Copies a file to a new location and adds a file record to the database.
*
......
......@@ -86,16 +86,6 @@ public function createFileUrl($relative = TRUE) {
return $url;
}
/**
* {@inheritdoc}
*
* @see file_url_transform_relative()
*/
public function url($rel = 'canonical', $options = []) {
@trigger_error('File entities returning the URL to the physical file in File::url() is deprecated, use $file->createFileUrl() instead. See https://www.drupal.org/node/3019830', E_USER_DEPRECATED);
return $this->createFileUrl(FALSE);
}
/**
* {@inheritdoc}
*/
......
......@@ -35,30 +35,8 @@ class DatabaseFileUsageBackend extends FileUsageBase {
* information.
* @param string $table
* (optional) The table to store file usage info. Defaults to 'file_usage'.
*
* @todo Properly type-hint the constructor arguments in
* https://www.drupal.org/project/drupal/issues/3070114 when the
* drupal:9.0.x branch is opened.
*/
// @codingStandardsIgnoreLine
public function __construct($config_factory, $connection = NULL, $table = 'file_usage') {
// @todo Remove below conditional when the drupal:9.0.x branch is opened.
// @see https://www.drupal.org/project/drupal/issues/3070114
if (!$config_factory instanceof ConfigFactoryInterface) {
@trigger_error('Passing the database connection as the first argument to ' . __METHOD__ . ' is deprecated in drupal:8.8.0 and will throw a fatal error in drupal:9.0.0. Pass the config factory first. See https://www.drupal.org/node/3070148', E_USER_DEPRECATED);
if (!$config_factory instanceof Connection) {
throw new \InvalidArgumentException("The first argument to " . __METHOD__ . " should be an instance of \Drupal\Core\Config\ConfigFactoryInterface, " . gettype($config_factory) . " given.");
}
list($connection, $table, $config_factory) = array_pad(func_get_args(), 3, NULL);
if (NULL === $table) {
$table = 'file_usage';
}
if (!$config_factory instanceof ConfigFactoryInterface) {
$config_factory = \Drupal::configFactory();
}
}
public function __construct(ConfigFactoryInterface $config_factory, Connection $connection, $table = 'file_usage') {
parent::__construct($config_factory);
$this->connection = $connection;
$this->tableName = $table;
......
......@@ -21,21 +21,9 @@ abstract class FileUsageBase implements FileUsageInterface {
* Creates a FileUsageBase object.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory. This parameter is required as of drupal:8.4.0 and
* trigger a fatal error if not passed in drupal:9.0.0.
*
* @todo Update the docblock and make $config_factory required in
* https://www.drupal.org/project/drupal/issues/3070114 when the
* drupal:9.0.x branch is opened.
* The config factory.
*/
public function __construct(ConfigFactoryInterface $config_factory = NULL) {
// @todo Remove below conditional when the drupal:9.0.x branch is opened.
// @see https://www.drupal.org/project/drupal/issues/3070114
if (empty($config_factory)) {
@trigger_error('Not passing the $config_factory parameter to ' . __METHOD__ . ' is deprecated in drupal:8.4.0 and will trigger a fatal error in drupal:9.0.0. See https://www.drupal.org/project/drupal/issues/2801777', E_USER_DEPRECATED);
$config_factory = \Drupal::configFactory();
}
public function __construct(ConfigFactoryInterface $config_factory) {
$this->configFactory = $config_factory;
}
......
<?php
namespace Drupal\file\Plugin\migrate\field\d6;
@trigger_error('ImageField is deprecated in Drupal 8.5.x and will be removed before Drupal 9.0.x. Use \Drupal\image\Plugin\migrate\field\d6\ImageField instead. See https://www.drupal.org/node/2936061.', E_USER_DEPRECATED);
use Drupal\image\Plugin\migrate\field\d6\ImageField as NonLegacyImageField;
/**
* Field plugin for image fields.
*
* @deprecated in drupal:8.5.0 and is removed from drupal:9.0.0. Use
* \Drupal\image\Plugin\migrate\field\d6\ImageField instead.
*
* @see https://www.drupal.org/node/2936061
*/
class ImageField extends NonLegacyImageField {}
<?php
namespace Drupal\file\Plugin\migrate\field\d7;
@trigger_error('ImageField is deprecated in Drupal 8.5.x and will be removed before Drupal 9.0.x. Use \Drupal\image\Plugin\migrate\field\d7\ImageField instead. See https://www.drupal.org/node/2936061.', E_USER_DEPRECATED);
use Drupal\image\Plugin\migrate\field\d7\ImageField as NonLegacyImageField;
/**
* Field plugin for image fields.
*
* @deprecated in drupal:8.5.0 and is removed from drupal:9.0.0. Use
* \Drupal\image\Plugin\migrate\field\d7\ImageField instead.
*
* @see https://www.drupal.org/node/2936061
*/
class ImageField extends NonLegacyImageField {}
......@@ -6,7 +6,6 @@
use Drupal\migrate\MigrateLookupInterface;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\Plugin\MigrateProcessInterface;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\Row;
use Symfony\Component\DependencyInjection\ContainerInterface;
......@@ -18,18 +17,6 @@
*/
class FieldFile extends ProcessPluginBase implements ContainerFactoryPluginInterface {
/**
* The migration process plugin, configured for lookups in d6_file.
*
* @var \Drupal\migrate\Plugin\MigrateProcessInterface
*
* @deprecated in drupal:8.8.x and is removed from drupal:9.0.0. Use
* the migrate.lookup service instead.
*
* @see https://www.drupal.org/node/3047268
*/
protected $migrationPlugin;
/**
* The migrate lookup service.
*
......@@ -51,18 +38,8 @@ class FieldFile extends ProcessPluginBase implements ContainerFactoryPluginInter
* @param \Drupal\migrate\MigrateLookupInterface $migrate_lookup
* The migrate lookup service.
*/
// @codingStandardsIgnoreLine
public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, $migrate_lookup) {
public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, MigrateLookupInterface $migrate_lookup) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
if ($migrate_lookup instanceof MigrateProcessInterface) {
@trigger_error('Passing a migration process plugin as the fourth argument to ' . __METHOD__ . ' is deprecated in drupal:8.8.0 and will throw an error in drupal:9.0.0. Pass the migrate.lookup service instead. See https://www.drupal.org/node/3047268', E_USER_DEPRECATED);
$this->migrationPlugin = $migrate_lookup;
$migrate_lookup = \Drupal::service('migrate.lookup');
}
elseif (!$migrate_lookup instanceof MigrateLookupInterface) {
throw new \InvalidArgumentException("The fifth argument to " . __METHOD__ . " must be an instance of MigrateLookupInterface.");
}
$this->migration = $migration;
$this->migrateLookup = $migrate_lookup;
......@@ -92,24 +69,10 @@ public function transform($value, MigrateExecutableInterface $migrate_executable
// some reason -- file migration is notoriously brittle -- and we do NOT
// want to send invalid file references into the field system (it causes
// fatals), so return an empty item instead.
// This BC layer is included because if the plugin constructor was called
// in the legacy way with a migration_lookup process plugin, it may have
// been preconfigured with a different migration to look up against. While
// this is unlikely, for maximum BC we will continue to use the plugin to do
// the lookup if it is provided, and support for this will be removed in
// Drupal 9.
if ($this->migrationPlugin) {
$fid = $this->migrationPlugin->transform($value['fid'], $migrate_executable, $row, $destination_property);
}
else {
$lookup_result = $this->migrateLookup->lookup('d6_file', [$value['fid']]);
if ($lookup_result) {
$fid = $lookup_result[0]['fid'];
}
}
if (!empty($fid)) {
$lookup_result = $this->migrateLookup->lookup('d6_file', [$value['fid']]);
if ($lookup_result) {
return [
'target_id' => $fid,
'target_id' => $lookup_result[0]['fid'],
'display' => $value['list'],
'description' => isset($options['description']) ? $options['description'] : '',
'alt' => isset($options['alt']) ? $options['alt'] : '',
......
<?php
namespace Drupal\file\Tests;
@trigger_error('The ' . __NAMESPACE__ . '\FileFieldTestBase is deprecated in Drupal 8.5.x and will be removed before Drupal 9.0.0. Instead, use \Drupal\Tests\file\Functional\FileFieldTestBase. See https://www.drupal.org/node/2969361.', E_USER_DEPRECATED);
use Drupal\Component\Render\FormattableMarkup;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\field\Entity\FieldConfig;
use Drupal\file\FileInterface;
use Drupal\simpletest\WebTestBase;
use Drupal\file\Entity\File;
/**
* Provides methods specifically for testing File module's field handling.
*
* @deprecated in drupal:8.?.? and is removed from drupal:9.0.0.
* Use \Drupal\Tests\file\Functional\FileFieldTestBase instead.
*/
abstract class FileFieldTestBase extends WebTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = ['node', 'file', 'file_module_test', 'field_ui'];
/**
* An user with administration permissions.
*
* @var \Drupal\user\UserInterface
*/
protected $adminUser;
protected function setUp() {
parent::setUp();
$this->adminUser = $this->drupalCreateUser(['access content', 'access administration pages', 'administer site configuration', 'administer users', 'administer permissions', 'administer content types', 'administer node fields', 'administer node display', 'administer nodes', 'bypass node access']);
$this->drupalLogin($this->adminUser);
$this->drupalCreateContentType(['type' => 'article', 'name' => 'Article']);
}
/**
* Retrieves a sample file of the specified type.
*
* @return \Drupal\file\FileInterface
*/
public function getTestFile($type_name, $size = NULL) {
// Get a file to upload.
$file = current($this->drupalGetTestFiles($type_name, $size));
// Add a filesize property to files as would be read by
// \Drupal\file\Entity\File::load().
$file->filesize = filesize($file->uri);
return File::create((array) $file);
}
/**
* Retrieves the fid of the last inserted file.
*/
public function getLastFileId() {
return (int) \Drupal::entityQueryAggregate('file')->aggregate('fid', 'max')->execute()[0]['fid_max'];
}
/**
* 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();
/** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $display_repository */
$display_repository = \Drupal::service('entity_display.repository');
$display_repository->getFormDisplay($entity_type, $bundle)
->setComponent($name, [
'type' => 'file_generic',
'settings' => $widget_settings,
])
->save();
// Assign display settings.
$display_repository->getViewDisplay($entity_type, $bundle)
->setComponent($name, [
'label' => 'hidden',
'type' => 'file_default',
])
->save();
}
/**
* Updates an existing file field with new settings.
*/
public function updateFileField($name, $type_name, $field_settings = [], $widget_settings = []) {
$field = FieldConfig::loadByName('node', $type_name, $name);
$field->setSettings(array_merge($field->getSettings(), $field_settings));
$field->save();
\Drupal::service('entity_display.repository')
->getFormDisplay('node', $type_name)
->setComponent($name, [
'settings' => $widget_settings,
])
->save();
}
/**
* Uploads a file to a node.
*
* @param \Drupal\file\FileInterface $file
* The File to be uploaded.
* @param string $field_name
* The name of the field on which the files should be saved.
* @param $nid_or_type
* A numeric node id to upload files to an existing node, or a string
* indicating the desired bundle for a new node.
* @param bool $new_revision
* The revision number.
* @param array $extras
* Additional values when a new node is created.
*
* @return int
* The node id.
*/
public function uploadNodeFile(FileInterface $file, $field_name, $nid_or_type, $new_revision = TRUE, array $extras = []) {
return $this->uploadNodeFiles([$file], $field_name, $nid_or_type, $new_revision, $extras);
}
/**
* Uploads multiple files to a node.
*
* @param \Drupal\file\FileInterface[] $files
* The files to be uploaded.
* @param string $field_name
* The name of the field on which the files should be saved.
* @param $nid_or_type
* A numeric node id to upload files to an existing node, or a string
* indicating the desired bundle for a new node.
* @param bool $new_revision
* The revision number.
* @param array $extras
* Additional values when a new node is created.
*
* @return int
* The node id.
*/
public function uploadNodeFiles(array $files, $field_name, $nid_or_type, $new_revision = TRUE, array $extras = []) {
$edit = [
'title[0][value]' => $this->randomMachineName(),
'revision' => (string) (int) $new_revision,
];
$node_storage = $this->container->get('entity_type.manager')->getStorage('node');
if (is_numeric($nid_or_type)) {
$nid = $nid_or_type;
$node_storage->resetCache([$nid]);
$node = $node_storage->load($nid);
}
else {
// Add a new node.
$extras['type'] = $nid_or_type;
$node = $this->drupalCreateNode($extras);
$nid = $node->id();
// Save at least one revision to better simulate a real site.
$node->setNewRevision();
$node->save();
$node_storage->resetCache([$nid]);
$node = $node_storage->load($nid);
$this->assertNotEqual($nid, $node->getRevisionId(), 'Node revision exists.');
}
// Attach files to the node.
$field_storage = FieldStorageConfig::loadByName('node', $field_name);
// File input name depends on number of files already uploaded.
$field_num = count($node->{$field_name});
$name = 'files[' . $field_name . "_$field_num]";
if ($field_storage->getCardinality() != 1) {
$name .= '[]';
}
foreach ($files as $file) {
$file_path = $this->container->get('file_system')->realpath($file->getFileUri());
if (count($files) == 1) {
$edit[$name] = $file_path;
}
else {
$edit[$name][] = $file_path;
}
}
$this->drupalPostForm("node/$nid/edit", $edit, t('Save'));
return $nid;
}
/**
* Removes a file from a node.
*
* Note that if replacing a file, it must first be removed then added again.
*/
public function removeNodeFile($nid, $new_revision = TRUE) {
$edit = [
'revision' => (string) (int) $new_revision,
];
$this->drupalPostForm('node/' . $nid . '/edit', [], t('Remove'));
$this->drupalPostForm(NULL, $edit, t('Save'));
}
/**
* Replaces a file within a node.
*/
public function replaceNodeFile($file, $field_name, $nid, $new_revision = TRUE) {
$edit = [
'files[' . $field_name . '_0]' => \Drupal::service('file_system')->realpath($file->getFileUri()),
'revision' => (string) (int) $new_revision,
];
$this->drupalPostForm('node/' . $nid . '/edit', [], t('Remove'));
$this->drupalPostForm(NULL, $edit, t('Save'));
}
/**
* Asserts that a file exists physically on disk.
*/
public function assertFileExists($file, $message = NULL) {
$message = isset($message) ? $message : new FormattableMarkup('File %file exists on the disk.', ['%file' => $file->getFileUri()]);
$this->assertTrue(is_file($file->getFileUri()), $message);
}
/**
* Asserts that a file exists in the database.
*/
public function assertFileEntryExists($file, $message = NULL) {
$this->container->get('entity_type.manager')->getStorage('file')->resetCache();
$db_file = File::load($file->id());
$message = isset($message) ? $message : new FormattableMarkup('File %file exists in database at the correct path.', ['%file' => $file->getFileUri()]);
$this->assertEqual($db_file->getFileUri(), $file->getFileUri(), $message);
}
/**
* Asserts that a file does not exist on disk.
*/
public function assertFileNotExists($file, $message = NULL) {
$message = isset($message) ? $message : new FormattableMarkup('File %file exists on the disk.', ['%file' => $file->getFileUri()]);
$this->assertFalse(is_file($file->getFileUri()), $message);
}
/**
* Asserts that a file does not exist in the database.
*/
public function assertFileEntryNotExists($file, $message) {
$this->container->get('entity_type.manager')->getStorage('file')->resetCache();
$message = isset($message) ? $message : new FormattableMarkup('File %file exists in database at the correct path.', ['%file' => $file->getFileUri()]);
$this->assertFalse(File::load($file->id()), $message);
}
/**
* Asserts that a file's status is set to permanent in the database.
*/
public function assertFileIsPermanent(FileInterface $file, $message = NULL) {
$message = isset($message) ? $message : new FormattableMarkup('File %file is permanent.', ['%file' => $file->getFileUri()]);
$this->assertTrue($file->isPermanent(), $message);
}
}
......@@ -4,10 +4,8 @@
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\TestTools\PhpUnitCompatibility\RunnerVersion;
use Drupal\field\Entity\FieldConfig;
use Drupal\file\Entity\File;
use Drupal\Tests\Traits\ExpectDeprecationTrait;
/**
* Tests validation functions such as file type, max file size, max size per
......@@ -17,8 +15,6 @@
*/
class FileFieldValidateTest extends FileFieldTestBase {
use ExpectDeprecationTrait;
/**
* {@inheritdoc}
*/
......@@ -197,42 +193,4 @@ public function testFileRemoval() {
$this->assertText('Article ' . $node->getTitle() . ' has been updated.');
}
/**
* Tests deprecation of the assertFileExists and assertFileNotExists methods.
*
* @group legacy
*
* @todo the expectedDeprecation annotation does not work if tests are marked
* skipped.
* @see https://github.com/symfony/symfony/pull/25757
*/
public function testAssertFileExistsDeprecation() {
if (RunnerVersion::getMajor() == 6) {
$this->expectDeprecation('Passing a File entity as $file argument to FileFieldTestBase::assertFileExists is deprecated in drupal:8.8.0. It will be removed from drupal:9.0.0. Instead, pass the File entity URI via File::getFileUri(). See https://www.drupal.org/node/3057326');
$this->expectDeprecation('Passing a File entity as $file argument to FileFieldTestBase::assertFileNotExists is deprecated in drupal:8.8.0. It will be removed from drupal:9.0.0. Instead, pass the File entity URI via File::getFileUri(). See https://www.drupal.org/node/3057326');
}
else {
$this->markTestSkipped('This test does not work in PHPUnit 7+ since assertFileExists only accepts string arguments for $file');
}