View source
<?php
namespace Drupal\Tests\message\Kernel\Plugin\QueueWorker;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\KernelTests\KernelTestBase;
use Drupal\message\Entity\Message;
use Drupal\node\Entity\Node;
use Drupal\Tests\node\Traits\ContentTypeCreationTrait;
use Drupal\Tests\message\Kernel\MessageTemplateCreateTrait;
class MessageCheckAndDeleteWorkerTest extends KernelTestBase {
use MessageTemplateCreateTrait;
use ContentTypeCreationTrait;
public static $modules = [
'filter',
'message',
'user',
'system',
'field',
'entity_reference',
'text',
'node',
];
protected $plugin;
public function setUp() {
parent::setUp();
$this
->installEntitySchema('user');
$this
->installEntitySchema('node');
$this
->installEntitySchema('message');
$this
->installSchema('system', [
'sequences',
]);
$this
->installSchema('node', [
'node_access',
]);
$this
->installConfig([
'filter',
'node',
]);
}
public function testEmptyData() {
$this
->createPlugin();
$this->plugin
->processItem(NULL);
$this->plugin
->processItem([]);
}
public function testAlreadyDeletedMessage() {
$this
->createPlugin();
$this->plugin
->processItem([
1 => [
'field_test',
],
]);
$this->plugin
->processItem([
8 => [
'field_test',
],
100 => [
'field_two',
'field_three',
],
245 => [
'field_zebra',
],
]);
}
public function testProcessItem() {
$template = strtolower($this
->randomMachineName());
$this
->createMessageTemplate($template, 'Dummy message', 'This is a dummy message text', [
'Dummy message template.',
]);
$field_name = strtolower($this
->randomMachineName());
FieldStorageConfig::create([
'field_name' => $field_name,
'entity_type' => 'message',
'translatable' => FALSE,
'entity_types' => [],
'settings' => [
'target_type' => 'node',
],
'type' => 'entity_reference',
'cardinality' => FieldStorageConfig::CARDINALITY_UNLIMITED,
])
->save();
FieldConfig::create([
'label' => 'Entity reference field',
'field_name' => $field_name,
'entity_type' => 'message',
'bundle' => $template,
'settings' => [
'handler' => 'default',
'handler_settings' => [
'target_bundles' => [
'node',
],
'auto_create' => TRUE,
],
],
])
->save();
$this
->createPlugin();
$node_type = $this
->createContentType();
$node = Node::create([
'type' => $node_type
->id(),
'title' => 'Zebras rule',
]);
$node
->save();
$message = Message::create([
'template' => $template,
]);
$message
->set($field_name, [
1,
100,
]);
$message
->save();
$this->plugin
->processItem([
$message
->id() => [
$field_name,
],
]);
$this
->assertNotEmpty(Message::load($message
->id()), 'Message exists after deleting one of two referenced nodes.');
$node
->delete();
$this->plugin
->processItem([
$message
->id() => [
$field_name,
],
]);
$this
->assertEmpty(Message::load($message
->id()), 'Message deleted after deleting all referenced nodes.');
}
protected function createPlugin() {
$this->plugin = $this->container
->get('plugin.manager.queue_worker')
->createInstance('message_check_delete');
}
}