public function MessageTemplateUiTest::testMessageTemplateTranslate in Message 8
Test the translation interface for message templates.
File
- tests/
src/ Functional/ MessageTemplateUiTest.php, line 56
Class
- MessageTemplateUiTest
- Testing the CRUD functionality for the Message template entity.
Namespace
Drupal\Tests\message\FunctionalCode
public function testMessageTemplateTranslate() {
$this
->drupalLogin($this->account);
// Test the creation of a message template.
$edit = [
'label' => 'Dummy message',
'template' => 'dummy_message',
'description' => 'This is a dummy text',
// Use some HTML to ensure text formatting is working in ::getText().
'text[0][value]' => '<p>This is a dummy message with some dummy text</p>',
'text[0][format]' => 'filtered_html',
];
$this
->drupalPostForm('admin/structure/message/template/add', $edit, t('Save message template'));
$this
->assertText('The message template Dummy message created successfully.', 'The message template was created successfully');
$this
->drupalGet('admin/structure/message/manage/dummy_message');
$elements = [
'//input[@value="Dummy message"]' => 'The label input text exists on the page with the right text.',
'//input[@value="This is a dummy text"]' => 'The description of the message exists on the page.',
'//textarea[.="<p>This is a dummy message with some dummy text</p>"]' => 'The body of the message exists in the page.',
];
$this
->verifyFormElements($elements);
// Test the editing of a message template.
$edit = [
'label' => 'Edited dummy message',
'description' => 'This is a dummy text after editing',
'text[0][value]' => '<p>This is a dummy message with some edited dummy text</p>',
];
$this
->drupalPostForm('admin/structure/message/manage/dummy_message', $edit, t('Save message template'));
$this
->drupalGet('admin/structure/message/manage/dummy_message');
$elements = [
'//input[@value="Edited dummy message"]' => 'The label input text exists on the page with the right text.',
'//input[@value="This is a dummy text after editing"]' => 'The description of the message exists on the page.',
'//textarea[.="<p>This is a dummy message with some edited dummy text</p>"]' => 'The body of the message exists in the page.',
];
$this
->verifyFormElements($elements);
// Add language.
ConfigurableLanguage::create([
'id' => 'he',
'name' => 'Hebrew',
])
->save();
// Change to post form and add text different then the original.
$edit = [
'translation[config_names][message.template.dummy_message][label]' => 'Translated dummy message to Hebrew',
'translation[config_names][message.template.dummy_message][description]' => 'This is a dummy text after translation to Hebrew',
'translation[config_names][message.template.dummy_message][text][0][value]' => '<p>This is a dummy message with translated text to Hebrew</p>',
];
$this
->drupalPostForm('admin/structure/message/manage/dummy_message/translate/he/add', $edit, t('Save translation'));
// Go to the edit form and verify text.
$this
->drupalGet('admin/structure/message/manage/dummy_message/translate/he/edit');
$elements = [
'//input[@value="Translated dummy message to Hebrew"]' => 'The text in the form translation is the expected string in Hebrew.',
'//textarea[.="This is a dummy text after translation to Hebrew"]' => 'The description element have the expected value in Hebrew.',
'//textarea[.="<p>This is a dummy message with translated text to Hebrew</p>"]' => 'The text element have the expected value in Hebrew.',
];
$this
->verifyFormElements($elements);
// Load the message template via code in hebrew and english and verify the
// text. Also verify that when no translation, nothing gets returned.
/* @var $template MessageTemplate */
$template = MessageTemplate::load('dummy_message');
$this
->assertEquals([
'<p>This is a dummy message with translated text to Hebrew</p>',
], $template
->getText('he'), 'The text in hebrew pulled correctly.');
$this
->assertEquals([
'<p>This is a dummy message with some edited dummy text</p>',
], $template
->getText(), 'The text in english pulled correctly.');
$this
->assertEquals([], $template
->getText('fi'), 'Nonexistent translation pulled empty.');
// Create a message using that same template and test that multilingual text
// still works.
/* @var $template Message */
$message = Message::create([
'template' => 'dummy_message',
]);
/** @var \Drupal\message\MessageViewBuilder $builder */
$builder = $this->entityTypeManager
->getViewBuilder('message');
$this
->assertEquals([
'<p>This is a dummy message with translated text to Hebrew</p>',
], $message
->getText('he'), 'The text in hebrew pulled correctly.');
$this
->assertEquals([
'<p>This is a dummy message with some edited dummy text</p>',
], $message
->getText(), 'The text in english pulled correctly.');
$build = $builder
->view($message);
$this
->assertEquals([
'#markup' => '<p>This is a dummy message with some edited dummy text</p>',
], $build['partial_0'], 'The text in english built correctly.');
// Test changing the language of the message template with setLanguage().
$message
->setLanguage('he');
$this
->assertEquals([
'<p>This is a dummy message with translated text to Hebrew</p>',
], $message
->getText(), 'The text in hebrew pulled correctly.');
$build = $builder
->view($message);
$this
->assertEquals([
'#markup' => '<p>This is a dummy message with translated text to Hebrew</p>',
], $build['partial_0'], 'The text in hebrew built correctly.');
// Test again with nonexistent translation.
$message
->setLanguage('fi');
$this
->assertEquals([], $message
->getText(), 'Nonexistent translation pulled empty.');
$build = $builder
->view($message);
$this
->assertArrayNotHasKey('partial_0', $build, 'Nonexistent translation built empty.');
// Delete message via the UI.
$this
->drupalPostForm('admin/structure/message/delete/dummy_message', [], 'Delete');
$this
->assertText(t('There are no message templates yet.'));
$this
->assertFalse(MessageTemplate::load('dummy_message'), 'The message deleted via the UI successfully.');
}