diff --git a/core/modules/file/src/Tests/FileOnTranslatedEntityTest.php b/core/modules/file/src/Tests/FileOnTranslatedEntityTest.php index 49907a8..9f98b5c 100644 --- a/core/modules/file/src/Tests/FileOnTranslatedEntityTest.php +++ b/core/modules/file/src/Tests/FileOnTranslatedEntityTest.php @@ -24,13 +24,18 @@ class FileOnTranslatedEntityTest extends FileFieldTestBase { protected $fieldName; /** + * @var \Drupal\node\Entity\NodeType + */ + protected $contentType; + + /** * {@inheritdoc} */ protected function setUp() { parent::setUp(); // Create the "Basic page" node type. - $this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']); + $this->contentType = $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()); @@ -71,9 +76,9 @@ protected function setUp() { } /** - * Tests synced file fields on translated nodes. + * Tests synced file fields on translated nodes without revisions. */ - public function testSyncedFiles() { + public function testSyncedFilesWithoutRevisions() { // Verify that the file field on the "Basic page" node type is translatable. $definitions = \Drupal::entityManager()->getFieldDefinitions('node', 'page'); $this->assertTrue($definitions[$this->fieldName]->isTranslatable(), 'Node file field is translatable.'); @@ -104,15 +109,85 @@ public function testSyncedFiles() { \Drupal::entityTypeManager()->getStorage('file')->resetCache(); - /* @var $file \Drupal\file\FileInterface */ + // Ensure the file status of the first file permanent. + $file = File::load($first_fid); + $this->assertTrue($file->isPermanent(), 'First file still exists and is permanent.'); + + // Ensure the file status of the second file is permanent. + $file = File::load($second_fid); + $this->assertTrue($file->isPermanent(), 'Second file still exists and is permanent.'); + + // Delete the french translation. + $this->drupalPostForm('fr/node/' . $default_language_node->id() . '/delete', array(), t('Delete French translation')); + + \Drupal::entityTypeManager()->getStorage('file')->resetCache(); + + // Ensure the first is untouched. + $file = File::load($first_fid); + $this->assertTrue($file->isPermanent(), 'First file still exists and is permanent.'); + + // Because there are no revisions the second file is now not used. + $file = File::load($second_fid); + $this->assertTrue($file->isTemporary(), 'Second file still exists and is temporary.'); + + // Delete the content. + $this->drupalPostForm('node/' . $default_language_node->id() . '/delete', array(), t('Delete')); + + \Drupal::entityTypeManager()->getStorage('file')->resetCache(); + + // Ensure the file status of the all files are now temporary. + $file = File::load($first_fid); + $this->assertTrue($file->isTemporary(), 'First file still exists and is temporary.'); + + $file = File::load($second_fid); + $this->assertTrue($file->isTemporary(), 'Second file still exists and is temporary.'); + } + + /** + * Tests synced file fields on translated nodes with revisions. + */ + public function testSyncedFilesWithRevisions() { + // Enable new revisions for each content edit. + $this->contentType->setNewRevision(TRUE); + $this->contentType->save(); + + // Verify that the file field on the "Basic page" node type is translatable. + $definitions = \Drupal::entityManager()->getFieldDefinitions('node', 'page'); + $this->assertTrue($definitions[$this->fieldName]->isTranslatable(), 'Node file field is translatable.'); + + // Create a default language node. + $default_language_node = $this->drupalCreateNode(array('type' => 'page', 'title' => 'Lost in translation')); + + // Edit the node to upload a file. + $edit = array(); + $name = 'files[' . $this->fieldName . '_0]'; + $edit[$name] = drupal_realpath($this->drupalGetTestFiles('text')[0]->uri); + $this->drupalPostForm('node/' . $default_language_node->id() . '/edit', $edit, t('Save')); + $first_fid = $this->getLastFileId(); + + // Translate the node into French: remove the existing file. + $this->drupalPostForm('node/' . $default_language_node->id() . '/translations/add/en/fr', array(), t('Remove')); + + // Upload a different file. + $edit = array(); + $edit['title[0][value]'] = 'Bill Murray'; + $name = 'files[' . $this->fieldName . '_0]'; + $edit[$name] = drupal_realpath($this->drupalGetTestFiles('text')[1]->uri); + $this->drupalPostForm(NULL, $edit, t('Save (this translation)')); + // This inspects the HTML after the post of the translation, the file + // should be displayed on the original node. + $this->assertRaw('file--mime-text-plain'); + $second_fid = $this->getLastFileId(); + + \Drupal::entityTypeManager()->getStorage('file')->resetCache(); // Ensure the file status of the first file permanent. $file = File::load($first_fid); - $this->assertTrue($file->isPermanent()); + $this->assertTrue($file->isPermanent(), 'First file still exists and is permanent.'); // Ensure the file status of the second file is permanent. $file = File::load($second_fid); - $this->assertTrue($file->isPermanent()); + $this->assertTrue($file->isPermanent(), 'Second file still exists and is permanent.'); // Translate the node into dutch: remove the existing file. $this->drupalPostForm('node/' . $default_language_node->id() . '/translations/add/en/nl', array(), t('Remove')); @@ -136,11 +211,11 @@ public function testSyncedFiles() { // Ensure the file status of the second file is permanent. $file = File::load($second_fid); - $this->assertTrue($file->isPermanent()); + $this->assertTrue($file->isPermanent(), 'Second file still exists and is permanent.'); // Ensure the file status of the third file is permanent. $file = File::load($third_fid); - $this->assertTrue($file->isPermanent()); + $this->assertTrue($file->isPermanent(), 'Third file still exists and is permanent.'); // Edit the second translation: remove the existing file. $this->drupalPostForm('fr/node/' . $default_language_node->id() . '/edit', array(), t('Remove')); @@ -155,32 +230,39 @@ public function testSyncedFiles() { \Drupal::entityTypeManager()->getStorage('file')->resetCache(); - // Ensure the first and third files are untouched. + // Ensure all are untouched because they are being used by different + // revisions. $file = File::load($first_fid); $this->assertTrue($file->isPermanent(), 'First file still exists and is permanent.'); - $file = File::load($third_fid); - $this->assertTrue($file->isPermanent()); + $file = File::load($second_fid); + $this->assertTrue($file->isPermanent(), 'Second file still exists and is permanent.'); - // Ensure the file status of the replaced second file is permanent. $file = File::load($replaced_second_fid); - $this->assertTrue($file->isPermanent()); + $this->assertTrue($file->isPermanent(), 'Second replaced file still exists and is permanent.'); + + $file = File::load($third_fid); + $this->assertTrue($file->isPermanent(), 'Third file still exists and is permanent.'); // Delete the third translation. $this->drupalPostForm('nl/node/' . $default_language_node->id() . '/delete', array(), t('Delete Dutch translation')); \Drupal::entityTypeManager()->getStorage('file')->resetCache(); - // Ensure the first and replaced second files are untouched. + // All files still being used by different revisions. $file = File::load($first_fid); $this->assertTrue($file->isPermanent(), 'First file still exists and is permanent.'); + $file = File::load($second_fid); + $this->assertTrue($file->isPermanent(), 'Second file still exists and is permanent.'); + $file = File::load($replaced_second_fid); - $this->assertTrue($file->isPermanent()); + $this->assertTrue($file->isPermanent(), 'Second replaced file still exists and is permanent.'); - // Ensure the file status of the third file is now temporary. + // The dutch translation only doesn't exist in the latest revision but is + // still used by the previous revision. $file = File::load($third_fid); - $this->assertTrue($file->isTemporary()); + $this->assertTrue($file->isPermanent(), 'Third file still exists and is permanent.'); // Delete the all translations. $this->drupalPostForm('node/' . $default_language_node->id() . '/delete', array(), t('Delete all translations')); @@ -191,8 +273,14 @@ public function testSyncedFiles() { $file = File::load($first_fid); $this->assertTrue($file->isTemporary(), 'First file still exists and is temporary.'); + $file = File::load($second_fid); + $this->assertTrue($file->isTemporary(), 'Second file still exists and is temporary.'); + $file = File::load($replaced_second_fid); - $this->assertTrue($file->isTemporary()); + $this->assertTrue($file->isTemporary(), 'Second replaced file still exists and is temporary.'); + + $file = File::load($third_fid); + $this->assertTrue($file->isTemporary(), 'Third file still exists and is temporary.'); } }