You are here

protected function SchedulerRulesEventsTest::setUp in Scheduler 2.x

Same name and namespace in other branches
  1. 8 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 a reaction rule to display a system message for each of the six
  // events that Scheduler triggers, for each entity type. The array of data
  // contains the event name and the text to display.
  // 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 = [
    // The first six events are the originals, only dispatched for Nodes.
    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.',
    ],
    // These six events are dispatched only for Media entities.
    7 => [
      'scheduler:new_media_is_scheduled_for_publishing',
      'A new media item is created and scheduled for publishing.',
    ],
    8 => [
      'scheduler:existing_media_is_scheduled_for_publishing',
      'An existing media item is saved and scheduled for publishing.',
    ],
    9 => [
      'scheduler:media_has_been_published_via_cron',
      'Scheduler has published this media item during cron.',
    ],
    10 => [
      'scheduler:new_media_is_scheduled_for_unpublishing',
      'A new media item is created and scheduled for unpublishing.',
    ],
    11 => [
      'scheduler:existing_media_is_scheduled_for_unpublishing',
      'An existing media item is saved and scheduled for unpublishing.',
    ],
    12 => [
      'scheduler:media_has_been_unpublished_via_cron',
      'Scheduler has unpublished this media item during cron.',
    ],
    // These six events are dispatched only for Commerce Product entities.
    13 => [
      'scheduler:new_commerce_product_is_scheduled_for_publishing',
      'A new product is created and scheduled for publishing.',
    ],
    14 => [
      'scheduler:existing_commerce_product_is_scheduled_for_publishing',
      'An existing product is scheduled for publishing.',
    ],
    15 => [
      'scheduler:commerce_product_has_been_published_via_cron',
      'Scheduler has published this product during cron.',
    ],
    16 => [
      'scheduler:new_commerce_product_is_scheduled_for_unpublishing',
      'A new product is created and scheduled for unpublishing.',
    ],
    17 => [
      'scheduler:existing_commerce_product_is_scheduled_for_unpublishing',
      'An existing product is scheduled for unpublishing.',
    ],
    18 => [
      'scheduler:commerce_product_has_been_unpublished_via_cron',
      'Scheduler has unpublished this product 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 put phpcs:ignore DrupalPractice.CodeAnalysis.VariableAnalysis
  // before each line that produces a warning of this type.
  // This has been fixed in Coder 8.3.10 which is used in Core 9.1.
  // @see https://www.drupal.org/project/coder/issues/2876245
  // phpcs:ignore DrupalPractice.CodeAnalysis.VariableAnalysis
  foreach ($rule_data as $i => list($event_name, $description)) {

    // phpcs:ignore DrupalPractice.CodeAnalysis.VariableAnalysis
    $rule[$i] = $this->expressionManager
      ->createRule();

    // phpcs:ignore DrupalPractice.CodeAnalysis.VariableAnalysis
    $this->message[$i] = 'RULES message ' . $i . '. ' . $description;

    // phpcs:ignore DrupalPractice.CodeAnalysis.VariableAnalysis
    $rule[$i]
      ->addAction('rules_system_message', ContextConfig::create()
      ->setValue('message', $this->message[$i])
      ->setValue('type', 'status'));
    $config_entity = $this->rulesStorage
      ->create([
      'id' => 'rule' . $i,
      // phpcs:ignore DrupalPractice.CodeAnalysis.VariableAnalysis
      'events' => [
        [
          'event_name' => $event_name,
        ],
      ],
      // phpcs:ignore DrupalPractice.CodeAnalysis.VariableAnalysis
      'expression' => $rule[$i]
        ->getConfiguration(),
    ]);
    $config_entity
      ->save();
  }
  $this
    ->drupalLogin($this->schedulerUser);
}