You are here

protected function SchedulerRulesEventsTest::setUp in Scheduler 8

Same name and namespace in other branches
  1. 2.x tests/src/Functional/SchedulerRulesEventsTest.php \Drupal\Tests\scheduler\Functional\SchedulerRulesEventsTest::setUp()

Overrides SchedulerBrowserTestBase::setUp

File

tests/src/Functional/SchedulerRulesEventsTest.php, line 26

Class

SchedulerRulesEventsTest
Tests the six events that Scheduler provides for use in Rules module.

Namespace

Drupal\Tests\scheduler\Functional

Code

protected function setUp() : void {
  parent::setUp();
  $this->rulesStorage = $this->container
    ->get('entity_type.manager')
    ->getStorage('rules_reaction_rule');
  $this->expressionManager = $this->container
    ->get('plugin.manager.rules_expression');

  // Create six reaction rules, one for each event that Scheduler triggers.
  // These rules are all active throughout all of the tests, which makes the
  // tests stronger, because it will show not only that the correct events are
  // triggered in the right places, but also that they are not triggered in
  // the wrong places.
  $rule_data = [
    1 => [
      'scheduler_new_node_is_scheduled_for_publishing_event',
      'A new node is created and is scheduled for publishing.',
    ],
    2 => [
      'scheduler_existing_node_is_scheduled_for_publishing_event',
      'An existing node is saved and is scheduled for publishing.',
    ],
    3 => [
      'scheduler_has_published_this_node_event',
      'Scheduler has published this node during cron.',
    ],
    4 => [
      'scheduler_new_node_is_scheduled_for_unpublishing_event',
      'A new node is created and is scheduled for unpublishing.',
    ],
    5 => [
      'scheduler_existing_node_is_scheduled_for_unpublishing_event',
      'An existing node is saved and is scheduled for unpublishing.',
    ],
    6 => [
      'scheduler_has_unpublished_this_node_event',
      'Scheduler has unpublished this node during cron.',
    ],
  ];

  // PHPCS throws a false-positive 'variable $var is undefined' message when
  // the variable is defined by list( ) syntax. To avoid the unwanted warnings
  // we can wrap the section with codingStandardsIgnoreStart and IgnoreEnd.
  // @see https://www.drupal.org/project/coder/issues/2876245
  // @codingStandardsIgnoreStart
  foreach ($rule_data as $i => list($event_name, $description)) {
    $rule[$i] = $this->expressionManager
      ->createRule();
    $this->message[$i] = 'RULES message ' . $i . '. ' . $description;
    $rule[$i]
      ->addAction('rules_system_message', ContextConfig::create()
      ->setValue('message', $this->message[$i])
      ->setValue('type', 'status'));
    $config_entity = $this->rulesStorage
      ->create([
      'id' => 'rule' . $i,
      'events' => [
        [
          'event_name' => $event_name,
        ],
      ],
      'expression' => $rule[$i]
        ->getConfiguration(),
    ]);
    $config_entity
      ->save();
  }

  // @codingStandardsIgnoreEnd
  $this
    ->drupalLogin($this->schedulerUser);
}