View source
<?php
namespace Drupal\Tests\layout_builder\Functional;
use Drupal\Tests\content_translation\Functional\ContentTranslationTestBase;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\Core\Url;
class LayoutBuilderTranslationTest extends ContentTranslationTestBase {
protected static $modules = [
'content_translation',
'contextual',
'entity_test',
'layout_builder',
'block',
];
protected $defaultTheme = 'stark';
protected $entity;
protected function setUp() : void {
parent::setUp();
$this
->setUpViewDisplay();
$this
->setUpEntities();
}
public function testTranslationBeforeLayoutOverride() {
$assert_session = $this
->assertSession();
$this
->addEntityTranslation();
$entity_url = $this->entity
->toUrl()
->toString();
$language = \Drupal::languageManager()
->getLanguage($this->langcodes[2]);
$translated_entity_url = $this->entity
->toUrl('canonical', [
'language' => $language,
])
->toString();
$translated_layout_url = $translated_entity_url . '/layout';
$this
->drupalGet($entity_url);
$assert_session
->pageTextNotContains('The translated field value');
$assert_session
->pageTextContains('The untranslated field value');
$assert_session
->linkExists('Layout');
$this
->drupalGet($translated_entity_url);
$assert_session
->pageTextNotContains('The untranslated field value');
$assert_session
->pageTextContains('The translated field value');
$assert_session
->linkNotExists('Layout');
$this
->drupalGet($translated_layout_url);
$assert_session
->pageTextContains('Access denied');
$this
->addLayoutOverride();
$this
->drupalGet($entity_url);
$assert_session
->pageTextNotContains('The translated field value');
$assert_session
->pageTextContains('The untranslated field value');
$assert_session
->pageTextContains('Powered by Drupal');
$this
->drupalGet($translated_entity_url);
$assert_session
->pageTextNotContains('The untranslated field value');
$assert_session
->pageTextContains('The translated field value');
$assert_session
->pageTextContains('Powered by Drupal');
}
public function testLayoutOverrideBeforeTranslation() {
$assert_session = $this
->assertSession();
$entity_url = $this->entity
->toUrl()
->toString();
$language = \Drupal::languageManager()
->getLanguage($this->langcodes[2]);
$this
->addLayoutOverride();
$this
->drupalGet($entity_url);
$assert_session
->pageTextNotContains('The translated field value');
$assert_session
->pageTextContains('The untranslated field value');
$assert_session
->pageTextContains('Powered by Drupal');
$assert_session
->linkExists('Layout');
$this
->addEntityTranslation();
$translated_entity_url = $this->entity
->toUrl('canonical', [
'language' => $language,
])
->toString();
$translated_layout_url = $translated_entity_url . '/layout';
$this
->drupalGet($entity_url);
$assert_session
->pageTextNotContains('The translated field value');
$assert_session
->pageTextContains('The untranslated field value');
$assert_session
->pageTextContains('Powered by Drupal');
$assert_session
->linkExists('Layout');
$this
->drupalGet($translated_entity_url);
$assert_session
->pageTextNotContains('The untranslated field value');
$assert_session
->pageTextContains('The translated field value');
$assert_session
->pageTextContains('Powered by Drupal');
$assert_session
->linkNotExists('Layout');
$this
->drupalGet($translated_layout_url);
$assert_session
->pageTextContains('Access denied');
}
protected function getTranslatorPermissions() {
$permissions = parent::getTranslatorPermissions();
$permissions[] = 'view test entity translations';
$permissions[] = 'view test entity';
$permissions[] = 'configure any layout';
return $permissions;
}
protected function setUpEntities() {
$this
->drupalLogin($this->administrator);
$this
->drupalPlaceBlock('local_tasks_block');
$id = $this
->createEntity([
$this->fieldName => [
[
'value' => 'The untranslated field value',
],
],
'name' => 'Test entity',
], $this->langcodes[0]);
$storage = $this->container
->get('entity_type.manager')
->getStorage($this->entityTypeId);
$storage
->resetCache([
$id,
]);
$this->entity = $storage
->load($id);
}
protected function setUpViewDisplay() {
EntityViewDisplay::create([
'targetEntityType' => $this->entityTypeId,
'bundle' => $this->bundle,
'mode' => 'default',
'status' => TRUE,
])
->setComponent($this->fieldName, [
'type' => 'string',
])
->enableLayoutBuilder()
->setOverridable()
->save();
}
protected function addEntityTranslation() {
$user = $this->loggedInUser;
$this
->drupalLogin($this->translator);
$add_translation_url = Url::fromRoute("entity.{$this->entityTypeId}.content_translation_add", [
$this->entityTypeId => $this->entity
->id(),
'source' => $this->langcodes[0],
'target' => $this->langcodes[2],
]);
$this
->drupalGet($add_translation_url);
$this
->submitForm([
"{$this->fieldName}[0][value]" => 'The translated field value',
], 'Save');
$this
->drupalLogin($user);
}
protected function addLayoutOverride() {
$assert_session = $this
->assertSession();
$page = $this
->getSession()
->getPage();
$entity_url = $this->entity
->toUrl()
->toString();
$layout_url = $entity_url . '/layout';
$this
->drupalGet($layout_url);
$assert_session
->pageTextNotContains('The translated field value');
$assert_session
->pageTextContains('The untranslated field value');
$assert_session
->linkExists('Add block');
$this
->clickLink('Add block');
$assert_session
->linkExists('Powered by Drupal');
$this
->clickLink('Powered by Drupal');
$page
->pressButton('Add block');
$assert_session
->pageTextContains('Powered by Drupal');
$assert_session
->buttonExists('Save layout');
$page
->pressButton('Save layout');
}
}