You are here

public function SchedulerRulesConditionsTest::testNodeIsScheduledConditions in Scheduler 8

Tests the conditions for whether a node is scheduled.

File

tests/src/Functional/SchedulerRulesConditionsTest.php, line 151

Class

SchedulerRulesConditionsTest
Tests the four conditions that Scheduler provides for use in Rules module.

Namespace

Drupal\Tests\scheduler\Functional

Code

public function testNodeIsScheduledConditions() {

  // Create a reaction rule to display a message when a node is updated and
  // is not scheduled for publishing.
  $rule5 = $this->expressionManager
    ->createRule();
  $rule5
    ->addCondition('scheduler_condition_node_scheduled_for_publishing', ContextConfig::create()
    ->map('node', 'node')
    ->negateResult());
  $message5 = 'RULES message 5. This content is not scheduled for publishing.';
  $rule5
    ->addAction('rules_system_message', ContextConfig::create()
    ->setValue('message', $message5)
    ->setValue('type', 'status'));
  $config_entity = $this->rulesStorage
    ->create([
    'id' => 'rule5',
    'events' => [
      [
        'event_name' => 'rules_entity_update:node',
      ],
    ],
    'expression' => $rule5
      ->getConfiguration(),
  ]);
  $config_entity
    ->save();

  // Create a reaction rule to display a message when a node is updated and
  // is not scheduled for unpublishing.
  $rule6 = $this->expressionManager
    ->createRule();
  $rule6
    ->addCondition('scheduler_condition_node_scheduled_for_unpublishing', ContextConfig::create()
    ->map('node', 'node')
    ->negateResult());
  $message6 = 'RULES message 6. This content is not scheduled for unpublishing.';
  $rule6
    ->addAction('rules_system_message', ContextConfig::create()
    ->setValue('message', $message6)
    ->setValue('type', 'status'));
  $config_entity = $this->rulesStorage
    ->create([
    'id' => 'rule6',
    'events' => [
      [
        'event_name' => 'rules_entity_update:node',
      ],
    ],
    'expression' => $rule6
      ->getConfiguration(),
  ]);
  $config_entity
    ->save();

  // Create a reaction rule to display a message when a node is updated and
  // is scheduled for publishing.
  $rule7 = $this->expressionManager
    ->createRule();
  $rule7
    ->addCondition('scheduler_condition_node_scheduled_for_publishing', ContextConfig::create()
    ->map('node', 'node'));
  $message7 = 'RULES message 7. This content is scheduled for publishing.';
  $rule7
    ->addAction('rules_system_message', ContextConfig::create()
    ->setValue('message', $message7)
    ->setValue('type', 'status'));
  $config_entity = $this->rulesStorage
    ->create([
    'id' => 'rule7',
    'events' => [
      [
        'event_name' => 'rules_entity_update:node',
      ],
    ],
    'expression' => $rule7
      ->getConfiguration(),
  ]);
  $config_entity
    ->save();
  $assert = $this
    ->assertSession();

  // Create a reaction rule to display a message when a node is updated and
  // is scheduled for unpublishing.
  $rule8 = $this->expressionManager
    ->createRule();
  $rule8
    ->addCondition('scheduler_condition_node_scheduled_for_unpublishing', ContextConfig::create()
    ->map('node', 'node'));
  $message8 = 'RULES message 8. This content is scheduled for unpublishing.';
  $rule8
    ->addAction('rules_system_message', ContextConfig::create()
    ->setValue('message', $message8)
    ->setValue('type', 'status'));
  $config_entity = $this->rulesStorage
    ->create([
    'id' => 'rule8',
    'events' => [
      [
        'event_name' => 'rules_entity_update:node',
      ],
    ],
    'expression' => $rule8
      ->getConfiguration(),
  ]);
  $config_entity
    ->save();
  $this
    ->drupalLogin($this->schedulerUser);

  // Edit the node but do not enter any scheduling dates.
  $edit = [
    'body[0][value]' => $this
      ->randomString(30),
  ];
  $this
    ->drupalPostForm('node/' . $this->node
    ->id() . '/edit', $edit, 'Save');
  $assert
    ->pageTextContains($message5);
  $assert
    ->pageTextContains($message6);
  $assert
    ->pageTextNotContains($message7);
  $assert
    ->pageTextNotContains($message8);

  // Edit the node and set a publish_on date.
  $edit = [
    'publish_on[0][value][date]' => date('Y-m-d', strtotime('+1 day', $this->requestTime)),
    'publish_on[0][value][time]' => date('H:i:s', strtotime('+1 day', $this->requestTime)),
  ];
  $this
    ->drupalPostForm('node/' . $this->node
    ->id() . '/edit', $edit, 'Save');
  $assert
    ->pageTextNotContains($message5);
  $assert
    ->pageTextContains($message6);
  $assert
    ->pageTextContains($message7);
  $assert
    ->pageTextNotContains($message8);

  // Edit the node and set an unpublish_on date.
  $edit = [
    'unpublish_on[0][value][date]' => date('Y-m-d', strtotime('+2 day', $this->requestTime)),
    'unpublish_on[0][value][time]' => date('H:i:s', strtotime('+2 day', $this->requestTime)),
  ];
  $this
    ->drupalPostForm('node/' . $this->node
    ->id() . '/edit', $edit, 'Save');
  $assert
    ->pageTextNotContains($message5);
  $assert
    ->pageTextNotContains($message6);
  $assert
    ->pageTextContains($message7);
  $assert
    ->pageTextContains($message8);
}