View source
<?php
namespace Drupal\Tests\image\Functional;
use Drupal\file\Entity\File;
use Drupal\Tests\TestFileCreationTrait;
class ImageOnTranslatedEntityTest extends ImageFieldTestBase {
use TestFileCreationTrait {
getTestFiles as drupalGetTestFiles;
compareFiles as drupalCompareFiles;
}
protected static $modules = [
'language',
'content_translation',
'field_ui',
];
protected $defaultTheme = 'stark';
protected $fieldName;
protected function setUp() : void {
parent::setUp();
$this
->config('file.settings')
->set('make_unused_managed_files_temporary', TRUE)
->save();
$this
->drupalCreateContentType([
'type' => 'basicpage',
'name' => 'Basic page',
'new_revision' => FALSE,
]);
$this->fieldName = strtolower($this
->randomMachineName());
$this
->createImageField($this->fieldName, 'basicpage', [], [
'title_field' => 1,
]);
$permissions = [
'access administration pages',
'administer content translation',
'administer content types',
'administer languages',
'administer node fields',
'create content translations',
'create basicpage content',
'edit any basicpage content',
'translate any entity',
'delete any basicpage content',
];
$admin_user = $this
->drupalCreateUser($permissions);
$this
->drupalLogin($admin_user);
$edit = [];
$edit['predefined_langcode'] = 'fr';
$this
->drupalGet('admin/config/regional/language/add');
$this
->submitForm($edit, 'Add language');
$edit = [];
$edit['predefined_langcode'] = 'nl';
$this
->drupalGet('admin/config/regional/language/add');
$this
->submitForm($edit, 'Add language');
}
public function testSyncedImages() {
$edit = [
'entity_types[node]' => 1,
'settings[node][basicpage][translatable]' => 1,
"settings[node][basicpage][fields][{$this->fieldName}]" => 1,
"settings[node][basicpage][columns][{$this->fieldName}][file]" => 1,
"settings[node][basicpage][columns][{$this->fieldName}][alt]" => FALSE,
"settings[node][basicpage][columns][{$this->fieldName}][title]" => FALSE,
];
$this
->drupalGet('admin/config/regional/content-language');
$this
->submitForm($edit, 'Save configuration');
$definitions = \Drupal::service('entity_field.manager')
->getFieldDefinitions('node', 'basicpage');
$this
->assertTrue($definitions[$this->fieldName]
->isTranslatable(), 'Node image field is translatable.');
$default_language_node = $this
->drupalCreateNode([
'type' => 'basicpage',
'title' => 'Lost in translation',
]);
$edit = [];
$name = 'files[' . $this->fieldName . '_0]';
$edit[$name] = \Drupal::service('file_system')
->realpath($this
->drupalGetTestFiles('image')[0]->uri);
$this
->drupalGet('node/' . $default_language_node
->id() . '/edit');
$this
->submitForm($edit, 'Save');
$edit = [
$this->fieldName . '[0][alt]' => 'Lost in translation image',
$this->fieldName . '[0][title]' => 'Lost in translation image title',
];
$this
->submitForm($edit, 'Save');
$first_fid = $this
->getLastFileId();
$this
->drupalGet('node/' . $default_language_node
->id() . '/translations/add/en/fr');
$this
->submitForm([], 'Remove');
$edit = [];
$edit['title[0][value]'] = 'Scarlett Johansson';
$name = 'files[' . $this->fieldName . '_0]';
$edit[$name] = \Drupal::service('file_system')
->realpath($this
->drupalGetTestFiles('image')[1]->uri);
$this
->submitForm($edit, 'Save (this translation)');
$edit = [
$this->fieldName . '[0][alt]' => 'Scarlett Johansson image',
$this->fieldName . '[0][title]' => 'Scarlett Johansson image title',
];
$this
->submitForm($edit, 'Save (this translation)');
$this
->assertSession()
->responseContains('alt="Lost in translation image"');
$this
->assertSession()
->responseContains('title="Lost in translation image title"');
$second_fid = $this
->getLastFileId();
$this
->drupalGet('fr/node/' . $default_language_node
->id());
$this
->assertSession()
->responseContains('alt="Scarlett Johansson image"');
\Drupal::entityTypeManager()
->getStorage('file')
->resetCache();
$file = File::load($first_fid);
$this
->assertTrue($file
->isPermanent());
$file = File::load($second_fid);
$this
->assertTrue($file
->isPermanent());
$this
->drupalGet('node/' . $default_language_node
->id() . '/translations/add/en/nl');
$this
->submitForm([], 'Remove');
$edit = [];
$edit['title[0][value]'] = 'Akiko Takeshita';
$name = 'files[' . $this->fieldName . '_0]';
$edit[$name] = \Drupal::service('file_system')
->realpath($this
->drupalGetTestFiles('image')[2]->uri);
$this
->submitForm($edit, 'Save (this translation)');
$edit = [
$this->fieldName . '[0][alt]' => 'Akiko Takeshita image',
$this->fieldName . '[0][title]' => 'Akiko Takeshita image title',
];
$this
->submitForm($edit, 'Save (this translation)');
$third_fid = $this
->getLastFileId();
\Drupal::entityTypeManager()
->getStorage('file')
->resetCache();
$file = File::load($first_fid);
$this
->assertTrue($file
->isPermanent(), 'First file still exists and is permanent.');
$this
->assertSession()
->responseContains('alt="Lost in translation image"');
$this
->assertSession()
->responseContains('title="Lost in translation image title"');
$this
->drupalGet('nl/node/' . $default_language_node
->id());
$this
->assertSession()
->responseContains('alt="Akiko Takeshita image"');
$this
->assertSession()
->responseContains('title="Akiko Takeshita image title"');
$file = File::load($second_fid);
$this
->assertTrue($file
->isPermanent());
$file = File::load($third_fid);
$this
->assertTrue($file
->isPermanent());
$this
->drupalGet('fr/node/' . $default_language_node
->id() . '/edit');
$this
->submitForm([], 'Remove');
$edit = [];
$edit['title[0][value]'] = 'Giovanni Ribisi';
$name = 'files[' . $this->fieldName . '_0]';
$edit[$name] = \Drupal::service('file_system')
->realpath($this
->drupalGetTestFiles('image')[3]->uri);
$this
->submitForm($edit, 'Save (this translation)');
$name = $this->fieldName . '[0][alt]';
$edit = [
$name => 'Giovanni Ribisi image',
];
$this
->submitForm($edit, 'Save (this translation)');
$replaced_second_fid = $this
->getLastFileId();
\Drupal::entityTypeManager()
->getStorage('file')
->resetCache();
$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($replaced_second_fid);
$this
->assertTrue($file
->isPermanent());
$this
->drupalGet('nl/node/' . $default_language_node
->id() . '/delete');
$this
->submitForm([], 'Delete Dutch translation');
\Drupal::entityTypeManager()
->getStorage('file')
->resetCache();
$file = File::load($first_fid);
$this
->assertTrue($file
->isPermanent(), 'First file still exists and is permanent.');
$file = File::load($replaced_second_fid);
$this
->assertTrue($file
->isPermanent());
$file = File::load($third_fid);
$this
->assertTrue($file
->isTemporary());
$this
->drupalGet('node/' . $default_language_node
->id() . '/delete');
$this
->submitForm([], 'Delete all translations');
\Drupal::entityTypeManager()
->getStorage('file')
->resetCache();
$file = File::load($first_fid);
$this
->assertTrue($file
->isTemporary(), 'First file still exists and is temporary.');
$file = File::load($replaced_second_fid);
$this
->assertTrue($file
->isTemporary());
}
}