View source
<?php
namespace Drupal\Tests\message_notify\Kernel;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\KernelTests\KernelTestBase;
use Drupal\message\Entity\Message;
use Drupal\message\Entity\MessageTemplate;
use Drupal\message_notify\Exception\MessageNotifyException;
use Drupal\user\Entity\User;
class MessageNotifyTest extends KernelTestBase {
protected $messageTemplate;
protected $messageNotifier;
public static $modules = [
'message_notify_test',
'message_notify',
'message',
'user',
'system',
'field',
'text',
'filter',
'filter_test',
];
public function setUp() {
parent::setUp();
$this
->installEntitySchema('user');
$this
->installEntitySchema('message');
$this
->installConfig([
'message',
'message_notify',
'message_notify_test',
'filter_test',
]);
$this
->installSchema('system', [
'sequences',
]);
$this->messageTemplate = MessageTemplate::load('message_notify_test');
$this->messageNotifier = $this->container
->get('message_notify.sender');
}
public function testDeliver() {
$message = Message::create([
'template' => $this->messageTemplate
->id(),
]);
$message->message_text_another = 'another field';
$this->messageNotifier
->send($message, [], 'test');
$output = $message->output;
$text = $message
->getText();
$this
->assertContains((string) $text[1], (string) $output['foo']);
$this
->assertContains('another field', (string) $output['foo']);
$this
->assertContains((string) $text[0], (string) $output['bar']);
$this
->assertNotContains('another field', (string) $output['bar']);
}
public function testPostSendMessageSave() {
$account = User::create([
'name' => $this
->randomMachineName(),
]);
$account
->save();
$message = Message::create([
'template' => $this->messageTemplate
->id(),
'uid' => $account
->id(),
]);
$message->fail = FALSE;
$this->messageNotifier
->send($message, [], 'test');
$this
->assertTrue($message
->id(), 'Message saved after successful delivery.');
$message = Message::create([
'template' => $this->messageTemplate
->id(),
'uid' => $account
->id(),
]);
$message->fail = TRUE;
$this->messageNotifier
->send($message, [], 'test');
$this
->assertFalse($message
->id(), 'Message not saved after unsuccessful delivery.');
$options = [
'save on fail' => FALSE,
'save on success' => FALSE,
];
$message = Message::create([
'template' => $this->messageTemplate
->id(),
'uid' => $account
->id(),
]);
$message->fail = FALSE;
$this->messageNotifier
->send($message, $options, 'test');
$this
->assertTrue($message
->isNew(), 'Message not saved after successful delivery.');
$message = Message::create([
'template' => $this->messageTemplate
->id(),
'uid' => $account
->id(),
]);
$message->fail = TRUE;
$this->messageNotifier
->send($message, $options, 'test');
$this
->assertTrue($message
->isNew(), 'Message not saved after unsuccessful delivery.');
}
public function testPostSendRenderedField() {
$this
->attachRenderedFields();
$options = [
'rendered fields' => [
'foo' => 'rendered_foo',
'bar' => 'rendered_bar',
],
];
$message = Message::create([
'template' => $this->messageTemplate
->id(),
]);
$this->messageNotifier
->send($message, $options, 'test');
$this
->assertTrue($message->rendered_foo->value && $message->rendered_bar->value, 'Message is rendered to fields.');
$options = [
'rendered fields' => [
'foo' => 'rendered_baz',
'bar' => 'rendered_bar',
],
];
$message = Message::create([
'template' => $this->messageTemplate
->id(),
]);
$this->messageNotifier
->send($message, $options, 'test');
$this
->assertTrue($message->rendered_baz->value && $message->rendered_bar->value, 'Message is rendered to fields with text-processing.');
$options = [
'rendered fields' => [
'foo' => 'rendered_foo',
],
];
$message = Message::create([
'template' => $this->messageTemplate
->id(),
]);
try {
$this->messageNotifier
->send($message, $options, 'test');
$this
->fail('Can save rendered message with missing view mode.');
} catch (MessageNotifyException $e) {
$this
->pass('Cannot save rendered message with missing view mode.');
}
$options = [
'rendered fields' => [
'foo' => 'wrong_field',
'bar' => 'rendered_bar',
],
];
$message = Message::create([
'template' => $this->messageTemplate
->id(),
]);
try {
$this->messageNotifier
->send($message, $options, 'test');
$this
->fail('Can save rendered message to non-existing field.');
} catch (MessageNotifyException $e) {
$this
->pass('Cannot save rendered message to non-existing field.');
}
}
protected function attachRenderedFields() {
foreach ([
'rendered_foo',
'rendered_bar',
'rendered_baz',
] as $field_name) {
$config = [
'field_name' => $field_name,
'type' => 'string_long',
'entity_type' => 'message',
];
if ($field_name == 'rendered_baz') {
$config['type'] = 'text_long';
}
$field_storage = FieldStorageConfig::create($config);
$field_storage
->save();
$field = FieldConfig::create([
'field_name' => $field_name,
'bundle' => $this->messageTemplate
->id(),
'entity_type' => 'message',
'label' => $field_name,
]);
$field
->save();
}
}
}