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
fb18e30d93
commit
c64a3f7d7b
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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());
|
||||
|
|
|
@ -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()]);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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());
|
||||
|
|
Loading…
Reference in New Issue