You are here

protected function RngMessageRules::createMessageRule in RNG - Events and Registrations 3.x

Same name and namespace in other branches
  1. 8.2 tests/src/Kernel/RngMessageRules.php \Drupal\Tests\rng\Kernel\RngMessageRules::createMessageRule()
  2. 8 tests/src/Kernel/RngMessageRules.php \Drupal\Tests\rng\Kernel\RngMessageRules::createMessageRule()

Create a rule and associated components.

Parameters

$event: An event entity.

Return value

\Drupal\rng\RuleInterface An unsaved rule entity.

1 call to RngMessageRules::createMessageRule()
RngMessageRules::setUp in tests/src/Kernel/RngMessageRules.php

File

tests/src/Kernel/RngMessageRules.php, line 73

Class

RngMessageRules
Tests RNG message rules are executed.

Namespace

Drupal\Tests\rng\Kernel

Code

protected function createMessageRule($event) {

  /** @var \Drupal\courier\Service\CourierManagerInterface $courier_manager */
  $courier_manager = $this->container
    ->get('courier.manager');

  /** @var \Drupal\Core\Action\ActionManager $action_manager */
  $action_manager = $this->container
    ->get('plugin.manager.action');
  $template_collection = TemplateCollection::create();
  $template_collection
    ->save();
  $courier_manager
    ->addTemplates($template_collection);
  $template_collection
    ->save();
  $templates = $template_collection
    ->getTemplates();

  /** @var \Drupal\courier\EmailInterface $courier_email */
  $courier_email = $templates[0];
  $courier_email
    ->setSubject($this
    ->randomMachineName());
  $courier_email
    ->setBody('Greetings, [identity:label]');
  $courier_email
    ->save();
  $rule = Rule::create([
    'event' => [
      'entity' => $event,
    ],
  ]);
  $rule
    ->setIsActive(TRUE);
  $actionPlugin = $action_manager
    ->createInstance('rng_courier_message');
  $configuration = $actionPlugin
    ->getConfiguration();
  $configuration['template_collection'] = $template_collection
    ->id();
  $action = RuleComponent::create([])
    ->setPluginId($actionPlugin
    ->getPluginId())
    ->setConfiguration($configuration)
    ->setType('action');
  $rule
    ->addComponent($action);
  return $rule;
}