summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNathaniel Catchpole2016-09-06 10:02:26 (GMT)
committerNathaniel Catchpole2016-09-06 10:02:26 (GMT)
commitc64a3f7d7b1b85b831271e5b5a2f8fffcb2cb41c (patch)
tree3ed7fe521824bbc124f01ca41f4e1e9e5fd3ef1a
parentfb18e30d934aab134943eceb93dd39f5070de0e8 (diff)
Issue #2787187 by claudiu.cristea, grahl, azinck, Fabianx, dawehner: Data loss: Deleting a translation of an entity deletes all file_usage entries for files referenced by the entity
-rw-r--r--core/modules/file/src/Plugin/Field/FieldType/FileFieldItemList.php6
-rw-r--r--core/modules/file/src/Tests/FileOnTranslatedEntityTest.php4
-rw-r--r--core/modules/file/tests/src/Kernel/UsageTest.php60
-rw-r--r--core/modules/image/src/Tests/ImageOnTranslatedEntityTest.php4
4 files changed, 70 insertions, 4 deletions
diff --git a/core/modules/file/src/Plugin/Field/FieldType/FileFieldItemList.php b/core/modules/file/src/Plugin/Field/FieldType/FileFieldItemList.php
index 3d064af..20f4797 100644
--- a/core/modules/file/src/Plugin/Field/FieldType/FileFieldItemList.php
+++ b/core/modules/file/src/Plugin/Field/FieldType/FileFieldItemList.php
@@ -81,9 +81,11 @@ class FileFieldItemList extends EntityReferenceFieldItemList {
parent::delete();
$entity = $this->getEntity();
- // Delete all file usages within this entity.
+ // If a translation is deleted only decrement the file usage by one. If the
+ // default translation is deleted remove all file usages within this entity.
+ $count = $entity->isDefaultTranslation() ? 0 : 1;
foreach ($this->referencedEntities() as $file) {
- \Drupal::service('file.usage')->delete($file, 'file', $entity->getEntityTypeId(), $entity->id(), 0);
+ \Drupal::service('file.usage')->delete($file, 'file', $entity->getEntityTypeId(), $entity->id(), $count);
}
}
diff --git a/core/modules/file/src/Tests/FileOnTranslatedEntityTest.php b/core/modules/file/src/Tests/FileOnTranslatedEntityTest.php
index a7150e5..7987bcf 100644
--- a/core/modules/file/src/Tests/FileOnTranslatedEntityTest.php
+++ b/core/modules/file/src/Tests/FileOnTranslatedEntityTest.php
@@ -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());
diff --git a/core/modules/file/tests/src/Kernel/UsageTest.php b/core/modules/file/tests/src/Kernel/UsageTest.php
index fdf1231..8e26015 100644
--- a/core/modules/file/tests/src/Kernel/UsageTest.php
+++ b/core/modules/file/tests/src/Kernel/UsageTest.php
@@ -2,6 +2,13 @@
namespace Drupal\Tests\file\Kernel;
+use Drupal\field\Entity\FieldConfig;
+use Drupal\field\Entity\FieldStorageConfig;
+use Drupal\language\Entity\ConfigurableLanguage;
+use Drupal\language\Entity\ContentLanguageSettings;
+use Drupal\node\Entity\Node;
+use Drupal\node\Entity\NodeType;
+
/**
* Tests file usage functions.
*
@@ -203,4 +210,57 @@ class UsageTest extends FileManagedUnitTestBase {
$this->assertTrue(file_exists($perm_new->getFileUri()), 'New permanent file was correctly ignored.');
}
+ /**
+ * Tests file usage with translated entities.
+ */
+ public function testFileUsageWithEntityTranslation() {
+ /** @var \Drupal\file\FileUsage\FileUsageInterface $file_usage */
+ $file_usage = $this->container->get('file.usage');
+
+ $this->enableModules(['node', 'language']);
+ $this->installEntitySchema('node');
+ $this->installSchema('node', ['node_access']);
+
+ // Activate English and Romanian languages.
+ ConfigurableLanguage::create(['id' => 'en'])->save();
+ ConfigurableLanguage::create(['id' => 'ro'])->save();
+
+ NodeType::create(['type' => 'page'])->save();
+ ContentLanguageSettings::loadByEntityTypeBundle('node', 'page')
+ ->setLanguageAlterable(FALSE)
+ ->setDefaultLangcode('en')
+ ->save();
+ // Create a file field attached to 'page' node-type.
+ FieldStorageConfig::create([
+ 'type' => 'file',
+ 'entity_type' => 'node',
+ 'field_name' => 'file',
+ ])->save();
+ FieldConfig::create([
+ 'entity_type' => 'node',
+ 'bundle' => 'page',
+ 'field_name' => 'file',
+ 'label' => 'File',
+ ])->save();
+
+ // Create a node, attach a file and add a Romanian translation.
+ $node = Node::create(['type' => 'page', 'title' => 'Page']);
+ $node
+ ->set('file', $file = $this->createFile())
+ ->addTranslation('ro', $node->getTranslation('en')->toArray())
+ ->save();
+
+ // Check that the file is used twice.
+ $usage = $file_usage->listUsage($file);
+ $this->assertEquals(2, $usage['file']['node'][$node->id()]);
+
+ // Remove the Romanian translation.
+ $node->removeTranslation('ro');
+ $node->save();
+
+ // Check that one usage has been removed and is used only once now.
+ $usage = $file_usage->listUsage($file);
+ $this->assertEquals(1, $usage['file']['node'][$node->id()]);
+ }
+
}
diff --git a/core/modules/image/src/Tests/ImageOnTranslatedEntityTest.php b/core/modules/image/src/Tests/ImageOnTranslatedEntityTest.php
index e716d28..a8985ce 100644
--- a/core/modules/image/src/Tests/ImageOnTranslatedEntityTest.php
+++ b/core/modules/image/src/Tests/ImageOnTranslatedEntityTest.php
@@ -30,7 +30,9 @@ class ImageOnTranslatedEntityTest extends ImageFieldTestBase {
parent::setUp();
// Create the "Basic page" node type.
- $this->drupalCreateContentType(array('type' => 'basicpage', 'name' => 'Basic page'));
+ // @todo Remove the disabling of new revision creation in
+ // https://www.drupal.org/node/1239558.
+ $this->drupalCreateContentType(['type' => 'basicpage', 'name' => 'Basic page', 'new_revision' => FALSE]);
// Create a image field on the "Basic page" node type.
$this->fieldName = strtolower($this->randomMachineName());