IngredientTranslationTest.php in Recipe 8.2
File
modules/ingredient/tests/src/Functional/IngredientTranslationTest.php
View source
<?php
namespace Drupal\Tests\ingredient\Functional;
use Drupal\ingredient\Entity\Ingredient;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\node\Entity\Node;
use Drupal\Tests\BrowserTestBase;
class IngredientTranslationTest extends BrowserTestBase {
use IngredientTestTrait;
protected $defaultTheme = 'stark';
protected $contentTranslationManager;
protected $routerBuilder;
protected $entityDefinitionUpdateManager;
protected $baseLangcode = 'en';
protected $translateToLangcode = 'fr';
protected $node;
protected $ingredient;
protected $baseIngredientName = 'OriginalIngredientName';
protected $translatedIngredientName = 'TranslatedIngredientName';
protected static $modules = [
'content_translation',
'ingredient',
'node',
];
protected $adminUser;
protected function setUp() : void {
parent::setUp();
$this
->ingredientCreateContentType();
$this
->setUpLanguages();
$this
->enableTranslation();
$this
->setUpIngredient();
$this
->createIngredientField();
$this
->setUpNode();
$permissions = [
'access content',
'view ingredient',
];
$this->adminUser = $this
->drupalCreateUser($permissions);
$this
->drupalLogin($this->adminUser);
}
public function testTranslatedIngredientDisplay() {
$ingredient_path = 'ingredient/' . $this->ingredient
->id();
$ingredient_translation_path = $this->translateToLangcode . '/' . $ingredient_path;
$node_path = 'node/' . $this->node
->id();
$node_translation_path = $this->translateToLangcode . '/' . $node_path;
$this
->drupalGet($ingredient_path);
$this
->assertSession()
->pageTextNotContains($this->translatedIngredientName);
$this
->assertSession()
->pageTextContains($this->baseIngredientName);
$this
->drupalGet($ingredient_translation_path);
$this
->assertSession()
->pageTextContains($this->translatedIngredientName);
$this
->assertSession()
->pageTextNotContains($this->baseIngredientName);
$this
->drupalGet($node_path);
$this
->assertSession()
->pageTextNotContains($this->translatedIngredientName);
$this
->assertSession()
->pageTextContains($this->baseIngredientName);
$this
->drupalGet($node_translation_path);
$this
->assertSession()
->pageTextContains($this->translatedIngredientName);
$this
->assertSession()
->pageTextNotContains($this->baseIngredientName);
}
protected function setUpLanguages() {
ConfigurableLanguage::createFromLangcode($this->translateToLangcode)
->save();
$this
->rebuildContainer();
}
protected function enableTranslation() {
$this
->getContentTranslationManager()
->setEnabled('node', 'test_bundle', TRUE);
$this
->getContentTranslationManager()
->setEnabled('ingredient', 'ingredient', TRUE);
drupal_static_reset();
$this
->getEntityTypeManager()
->clearCachedDefinitions();
$this
->getRouterBuilder()
->rebuild();
$this
->getEntityDefinitionUpdateManager()
->getChangeList();
}
protected function setUpNode() {
$this->node = Node::create([
'title' => $this
->randomMachineName(),
'type' => 'test_bundle',
'field_ingredient' => [
'quantity' => 1,
'unit_key' => 'cup',
'target_id' => $this->ingredient
->id(),
'note' => '',
],
'langcode' => $this->baseLangcode,
]);
$this->node
->save();
$this->node
->addTranslation($this->translateToLangcode, $this->node
->toArray());
$this->node
->save();
}
protected function setUpIngredient() {
$this->ingredient = Ingredient::create([
'name' => $this->baseIngredientName,
]);
$this->ingredient
->save();
$this->ingredient
->addTranslation($this->translateToLangcode, [
'name' => $this->translatedIngredientName,
]);
$this->ingredient
->save();
}
protected function getContentTranslationManager() {
if (!$this->contentTranslationManager) {
$this->contentTranslationManager = $this->container
->get('content_translation.manager');
}
return $this->contentTranslationManager;
}
protected function getRouterBuilder() {
if (!$this->routerBuilder) {
$this->routerBuilder = $this->container
->get('router.builder');
}
return $this->routerBuilder;
}
protected function getEntityDefinitionUpdateManager() {
if (!$this->entityDefinitionUpdateManager) {
$this->entityDefinitionUpdateManager = $this->container
->get('entity.definition_update_manager');
}
return $this->entityDefinitionUpdateManager;
}
}