Skip to content
Snippets Groups Projects
Commit c64a3f7d authored by catch's avatar catch
Browse files

Issue #2787187 by claudiu.cristea, grahl, azinck, Fabianx, dawehner: Data...

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
parent fb18e30d
No related branches found
No related tags found
2 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
......@@ -81,9 +81,11 @@ public function delete() {
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);
}
}
......
......@@ -30,7 +30,9 @@ protected function setUp() {
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());
......
......@@ -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 @@ function testTempFileCustomCleanup() {
$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()]);
}
}
......@@ -30,7 +30,9 @@ protected function setUp() {
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());
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment