View source
<?php
namespace Drupal\Tests\message\Functional;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\message\Entity\MessageTemplate;
use Drupal\message\Entity\Message;
class MessageTemplateUiTest extends MessageTestBase {
public static $modules = [
'language',
'config_translation',
'message',
'filter_test',
];
protected $account;
protected $entityTypeManager;
public function setUp() {
parent::setUp();
$this->account = $this
->drupalCreateUser([
'administer message templates',
'translate configuration',
'use text format filtered_html',
]);
$this->entityTypeManager = $this->container
->get('entity_type.manager');
}
public function testMessageTemplateTranslate() {
$this
->drupalLogin($this->account);
$edit = [
'label' => 'Dummy message',
'template' => 'dummy_message',
'description' => 'This is a dummy text',
'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);
$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);
ConfigurableLanguage::create([
'id' => 'he',
'name' => 'Hebrew',
])
->save();
$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'));
$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);
$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.');
$message = Message::create([
'template' => 'dummy_message',
]);
$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.');
$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.');
$message
->setLanguage('fi');
$this
->assertEquals([], $message
->getText(), 'Nonexistent translation pulled empty.');
$build = $builder
->view($message);
$this
->assertArrayNotHasKey('partial_0', $build, 'Nonexistent translation built empty.');
$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.');
}
private function verifyFormElements(array $elements) {
$errors = [];
foreach ($elements as $xpath => $message) {
$element = $this
->xpath($xpath);
if (!$element) {
$errors[] = $message;
}
}
if (empty($errors)) {
$this
->pass('All elements were found.');
}
else {
$this
->fail('The next errors were found: ' . implode("", $errors));
}
}
}