View source
<?php
namespace Drupal\yamlform\Tests;
use Drupal\simpletest\WebTestBase;
use Drupal\Core\Serialization\Yaml;
use Drupal\yamlform\Entity\YamlForm;
class YamlFormTranslationTest extends WebTestBase {
use YamlFormTestTrait;
protected static $modules = [
'system',
'user',
'block',
'yamlform',
'yamlform_examples',
'yamlform_test_translation',
];
public function setUp() {
parent::setUp();
$this
->placeBlocks();
$admin_user = $this
->drupalCreateUser([
'access content',
'administer yamlform',
'administer yamlform submission',
'translate configuration',
]);
$this
->drupalLogin($admin_user);
}
public function testTranslate() {
$translation_manager = \Drupal::service('yamlform.translation_manager');
$yamlform = YamlForm::load('test_translation');
$elements_raw = \Drupal::config('yamlform.yamlform.test_translation')
->get('elements');
$elements = Yaml::decode($elements_raw);
$this
->drupalGet('admin/structure/yamlform/manage/test_translation');
$this
->assertRaw('>Translate<');
$this
->drupalGet('admin/structure/yamlform/manage/test_translation/translate');
$this
->assertRaw('<a href="' . base_path() . 'admin/structure/yamlform/manage/test_translation/translate/es/edit">Edit</a>');
$this
->drupalGet('admin/structure/yamlform/manage/test_translation/translate/es/edit');
$this
->assertFieldByName('translation[config_names][yamlform.yamlform.test_translation][title]', 'Prueba: Traducción');
$this
->assertField('translation[config_names][yamlform.yamlform.test_translation][elements]');
$this
->drupalGet('es/yamlform/test_translation');
$this
->assertRaw('<label for="edit-textfield">Campo de texto</label>');
$this
->assertRaw('<option value="1">Uno</option>');
$this
->assertRaw('<option value="4">Las cuatro</option>');
$this
->drupalGet('fr/yamlform/test_translation');
$this
->assertRaw('<label for="edit-textfield">Text field</label>');
$this
->assertRaw('<option value="1">One</option>');
$this
->assertRaw('<option value="4">Four</option>');
$translation_element = $translation_manager
->getConfigElements($yamlform, 'fr', TRUE);
$this
->assertEqual($elements, $translation_element);
$translation_elements = [
'textfield' => [
'#title' => 'French',
'#custom' => 'custom',
],
'custom' => [
'#title' => 'Custom',
],
] + $elements;
$edit = [
'translation[config_names][yamlform.yamlform.test_translation][elements]' => Yaml::encode($translation_elements),
];
$this
->drupalPostForm('admin/structure/yamlform/manage/test_translation/translate/fr/add', $edit, t('Save translation'));
$this
->drupalGet('fr/yamlform/test_translation');
$this
->assertRaw('<label for="edit-textfield">French</label>');
$translation_element = $translation_manager
->getConfigElements($yamlform, 'fr', TRUE);
$this
->assertEqual([
'textfield' => [
'#title' => 'French',
],
], $translation_element);
}
}