MetatagConfigTranslationTest.php in Metatag 8
File
tests/src/Functional/MetatagConfigTranslationTest.php
View source
<?php
namespace Drupal\Tests\metatag\Functional;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\metatag\Entity\MetatagDefaults;
use Drupal\Tests\BrowserTestBase;
use Drupal\Core\StringTranslation\StringTranslationTrait;
class MetatagConfigTranslationTest extends BrowserTestBase {
use StringTranslationTrait;
protected $profile = 'testing';
protected $adminUser;
protected static $modules = [
'metatag',
'language',
'config_translation',
'node',
];
protected $defaultTheme = 'stark';
protected $permissions = [
'administer meta tags',
'access administration pages',
'administer languages',
'translate configuration',
];
protected function setUp() {
parent::setUp();
$this->adminUser = $this
->drupalCreateUser($this->permissions);
$this
->drupalLogin($this->adminUser);
ConfigurableLanguage::createFromLangcode('fr')
->save();
}
public function testConfigTranslationsExist() {
$this
->drupalGet('admin/config/regional/config-translation');
$session = $this
->assertSession();
$session
->statusCodeEquals(200);
$session
->pageTextContains('Metatag defaults');
$this
->drupalGet('admin/config/regional/config-translation/metatag_defaults');
$session
->statusCodeEquals(200);
$session
->responseContains($this
->t('Metatag defaults'));
$defaults = \Drupal::configFactory()
->listAll('metatag.metatag_defaults');
$config_manager = \Drupal::service('config.manager');
foreach ($defaults as $config_name) {
if ($config_entity = $config_manager
->loadConfigEntityByName($config_name)) {
$session
->pageTextContains($config_entity
->label());
}
}
foreach ($defaults as $config_name) {
if ($config_entity = $config_manager
->loadConfigEntityByName($config_name)) {
$this
->drupalGet('admin/config/search/metatag/' . $config_entity
->id() . '/translate');
$session
->statusCodeEquals(200);
}
else {
$this
->error('Unable to load a Metatag default config: ' . $config_name);
}
}
}
public function testConfigTranslations() {
$this
->drupalGet('admin/config/search/metatag/global');
$session = $this
->assertSession();
$session
->statusCodeEquals(200);
$edit = [
'title' => 'Test title',
'description' => 'Test description',
];
$this
->drupalPostForm(NULL, $edit, $this
->t('Save'));
$session
->statusCodeEquals(200);
$session
->pageTextContains('Saved the Global Metatag defaults.');
$this
->drupalGet('admin/config/search/metatag/global/translate');
$session
->statusCodeEquals(200);
$this
->drupalGet('admin/config/search/metatag/global/translate/fr/add');
$session
->statusCodeEquals(200);
$session
->fieldExists('translation[config_names][metatag.metatag_defaults.global][tags][title]');
$session
->fieldValueEquals('translation[config_names][metatag.metatag_defaults.global][tags][title]', $edit['title']);
$session
->fieldExists('translation[config_names][metatag.metatag_defaults.global][tags][description]');
$session
->fieldValueEquals('translation[config_names][metatag.metatag_defaults.global][tags][description]', $edit['description']);
$edit = [
'translation[config_names][metatag.metatag_defaults.global][tags][title]' => 'Le title',
'translation[config_names][metatag.metatag_defaults.global][tags][description]' => 'Le description',
];
$this
->drupalPostForm(NULL, $edit, $this
->t('Save translation'));
$session
->statusCodeEquals(200);
$session
->pageTextContains('Successfully saved French translation');
MetatagDefaults::load('node')
->delete();
$this
->drupalCreateContentType([
'type' => 'page',
]);
$node = $this
->drupalCreateNode([
'title' => 'Metatag Test FR',
'langcode' => 'fr',
]);
$language_manager = \Drupal::languageManager();
$this
->assertEquals('en', $language_manager
->getConfigOverrideLanguage()
->getId());
$fr_default_tags = metatag_get_default_tags($node);
$this
->assertEquals('Le title', $fr_default_tags['title']);
$this
->assertEquals('Le description', $fr_default_tags['description']);
$this
->assertEquals('en', $language_manager
->getConfigOverrideLanguage()
->getId());
MetatagDefaults::load('global')
->delete();
$fr_default_tags = metatag_get_default_tags($node);
$this
->assertNull($fr_default_tags);
$this
->assertEquals('en', $language_manager
->getConfigOverrideLanguage()
->getId());
}
}