View source
<?php
namespace Drupal\Tests\lingotek\Functional;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\language\Entity\ConfigurableLanguage;
class LingotekFieldOptionsTranslationTest extends LingotekTestBase {
public static $modules = [
'block',
'node',
'options',
'field_ui',
];
protected $node;
protected function setUp() : void {
parent::setUp();
$this
->drupalPlaceBlock('local_tasks_block');
$this
->drupalPlaceBlock('page_title_block');
$type = $this
->drupalCreateContentType([
'type' => 'article',
'name' => 'Article',
]);
$this
->createOptionsField('list_string', 'article', 'field_options', 'Options');
ConfigurableLanguage::createFromLangcode('es')
->setThirdPartySetting('lingotek', 'locale', 'es_MX')
->save();
\Drupal::state()
->set('lingotek.uploaded_content_type', 'options');
}
public function testFieldTranslation() {
$assert_session = $this
->assertSession();
$this
->drupalLogin($this->rootUser);
$this
->drupalGet('/admin/config/regional/config-translation');
$this
->drupalGet('/admin/config/regional/config-translation/node_fields');
$this
->clickLink(t('Translate'), 1);
$this
->clickLink(t('Upload'));
$this
->assertText(t('Options uploaded successfully'));
$data = json_decode(\Drupal::state()
->get('lingotek.uploaded_content', '[]'), TRUE);
$this
->verbose(var_export($data, TRUE));
$this
->assertTrue(array_key_exists('label', $data['field.field.node.article.field_options']));
$this
->assertTrue(array_key_exists('description', $data['field.field.node.article.field_options']));
$this
->assertTrue(array_key_exists('settings.allowed_values.0.label', $data['field.storage.node.field_options']));
$this
->assertTrue(array_key_exists('settings.allowed_values.1.label', $data['field.storage.node.field_options']));
$this
->assertEqual('Options', $data['field.field.node.article.field_options']['label']);
$this
->assertEqual('Zero', $data['field.storage.node.field_options']['settings.allowed_values.0.label']);
$this
->assertEqual('One', $data['field.storage.node.field_options']['settings.allowed_values.1.label']);
$used_profile = \Drupal::state()
->get('lingotek.used_profile');
$this
->assertIdentical('automatic', $used_profile, 'The automatic profile was used.');
$this
->clickLink(t('Check upload status'));
$this
->assertText('Options status checked successfully');
$this
->clickLink(t('Request translation'));
$this
->assertText(t('Translation to es_MX requested successfully'));
$this
->assertIdentical('es_MX', \Drupal::state()
->get('lingotek.added_target_locale'));
$this
->clickLink(t('Check Download'));
$this
->assertText(t('Translation to es_MX status checked successfully'));
$this
->clickLink('Download');
$this
->assertText(t('Translation to es_MX downloaded successfully'));
$basepath = \Drupal::request()
->getBasePath();
$assert_session
->linkByHrefExists($basepath . '/admin/structure/types/manage/article/fields/node.article.field_options/translate/es/edit');
$this
->clickLink('Edit', 1);
$this
->assertFieldByName('translation[config_names][field.field.node.article.field_options][label]', 'Opciones');
$this
->assertFieldByName('translation[config_names][field.field.node.article.field_options][description]', 'Descripción del campo');
$this
->assertFieldByName('translation[config_names][field.storage.node.field_options][settings][allowed_values][0][label]', 'Cero');
$this
->assertFieldByName('translation[config_names][field.storage.node.field_options][settings][allowed_values][1][label]', 'Uno');
}
protected function createOptionsField($type, $bundle, $field_name, $label) {
FieldStorageConfig::create([
'field_name' => $field_name,
'entity_type' => 'node',
'type' => $type,
])
->save();
FieldConfig::create([
'field_name' => $field_name,
'label' => $label,
'entity_type' => 'node',
'bundle' => $bundle,
])
->save();
EntityFormDisplay::load('node.' . $bundle . '.default')
->setComponent($field_name)
->save();
$adminPath = 'admin/structure/types/manage/' . $bundle . '/fields/node.' . $bundle . '.' . $field_name . '/storage';
$input_string = "zero|Zero\none|One";
$edit = [
'settings[allowed_values]' => $input_string,
];
$this
->drupalPostForm($adminPath, $edit, t('Save field settings'));
}
}