outline-8.x-1.x-dev/tests/src/Functional/EntryTranslationTest.php
tests/src/Functional/EntryTranslationTest.php
<?php
namespace Drupal\Tests\outline\Functional;
use Drupal\Core\Url;
use Drupal\Tests\system\Functional\Menu\AssertBreadcrumbTrait;
/**
* Tests for proper breadcrumb translation.
*
* @group outline
*/
class EntryTranslationTest extends OutlineTestBase {
use AssertBreadcrumbTrait;
use OutlineTranslationTestTrait;
/**
* Entry to translated entry mapping.
*
* @var array
*/
protected $entryTranslationMap = [
'one' => 'translatedOne',
'two' => 'translatedTwo',
'three' => 'translatedThree',
];
/**
* Created entries.
*
* @var \Drupal\outline\Entity\Entry[]
*/
protected $entries = [];
/**
* {@inheritdoc}
*/
protected static $modules = ['outline', 'language', 'content_translation'];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'classy';
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->setupLanguages();
$this->outline = $this->createOutline();
$this->enableTranslation();
$this->setUpEntrys();
$this->setUpEntryReferenceField();
}
/**
* Test translated breadcrumbs.
*/
public function testTranslatedBreadcrumbs() {
// Ensure non-translated breadcrumb is correct.
$breadcrumb = [Url::fromRoute('<front>')->toString() => 'Home'];
foreach ($this->entries as $entry) {
$breadcrumb[$entry->toUrl()->toString()] = $entry->label();
}
// The last item will not be in the breadcrumb.
array_pop($breadcrumb);
// Check the breadcrumb on the leaf entry page.
$entry = $this->getLeafEntry();
$this->assertBreadcrumb($entry->toUrl(), $breadcrumb, $entry->label());
$languages = \Drupal::languageManager()->getLanguages();
// Construct the expected translated breadcrumb.
$breadcrumb = [Url::fromRoute('<front>', [], ['language' => $languages[$this->translateToLangcode]])->toString() => 'Home'];
foreach ($this->entries as $entry) {
$translated = $entry->getTranslation($this->translateToLangcode);
$url = $translated->toUrl('canonical', ['language' => $languages[$this->translateToLangcode]])->toString();
$breadcrumb[$url] = $translated->label();
}
array_pop($breadcrumb);
// Check for the translated breadcrumb on the translated leaf entry page.
$entry = $this->getLeafEntry();
$translated = $entry->getTranslation($this->translateToLangcode);
$this->assertBreadcrumb($translated->toUrl('canonical', ['language' => $languages[$this->translateToLangcode]]), $breadcrumb, $translated->label());
}
/**
* Test translation of entries are showed in the node.
*/
public function testEntrysTranslation() {
// Set the display of the entry reference field on the article content type
// to "Check boxes/radio buttons".
\Drupal::service('entity_display.repository')
->getFormDisplay('node', 'article')
->setComponent($this->entryFieldName, [
'type' => 'options_buttons',
])
->save();
$this->drupalLogin($this->drupalCreateUser(['create article content']));
// Test entries are listed.
$this->drupalget('node/add/article');
$this->assertText('one');
$this->assertText('two');
$this->assertText('three');
// Test entries translated are listed.
$this->drupalget('hu/node/add/article');
$this->assertText('translatedOne');
$this->assertText('translatedTwo');
$this->assertText('translatedThree');
}
/**
* Setup translated entries in a hierarchy.
*/
protected function setUpEntrys() {
$parent_vid = 0;
foreach ($this->entryTranslationMap as $name => $translation) {
$entry = $this->createEntry($this->outline, [
'name' => $name,
'langcode' => $this->baseLangcode,
'parent' => $parent_vid,
]);
$entry->addTranslation($this->translateToLangcode, [
'name' => $translation,
]);
$entry->save();
// Each entry is nested under the last.
$parent_vid = $entry->id();
$this->entries[] = $entry;
}
}
/**
* Get the final (leaf) entry in the hierarchy.
*
* @return \Drupal\outline\Entity\Entry
* The final entry in the hierarchy.
*/
protected function getLeafEntry() {
return $this->entries[count($this->entryTranslationMap) - 1];
}
}
