You are here

public function SchedulerRulesConditionsTest::testNodeTypeEnabledConditions in Scheduler 8

Tests the conditions for whether a nodetype is enabled for Scheduler.

File

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

Class

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

Namespace

Drupal\Tests\scheduler\Functional

Code

public function testNodeTypeEnabledConditions() {

  // Create a reaction rule to display a message when viewing a node of a type
  // that is enabled for scheduled publishing.
  // "viewing content" actually means "viewing PUBLISHED content".
  $rule1 = $this->expressionManager
    ->createRule();
  $rule1
    ->addCondition('scheduler_condition_publishing_is_enabled', ContextConfig::create()
    ->map('node', 'node'));
  $message1 = 'RULES message 1. This node type is enabled for scheduled publishing.';
  $rule1
    ->addAction('rules_system_message', ContextConfig::create()
    ->setValue('message', $message1)
    ->setValue('type', 'status'));
  $config_entity = $this->rulesStorage
    ->create([
    'id' => 'rule1',
    'events' => [
      [
        'event_name' => 'rules_entity_view:node',
      ],
    ],
    'expression' => $rule1
      ->getConfiguration(),
  ]);
  $config_entity
    ->save();

  // Create a reaction rule to display a message when viewing a node of a type
  // that is enabled for scheduled unpublishing.
  $rule2 = $this->expressionManager
    ->createRule();
  $rule2
    ->addCondition('scheduler_condition_unpublishing_is_enabled', ContextConfig::create()
    ->map('node', 'node'));
  $message2 = 'RULES message 2. This node type is enabled for scheduled unpublishing.';
  $rule2
    ->addAction('rules_system_message', ContextConfig::create()
    ->setValue('message', $message2)
    ->setValue('type', 'status'));
  $config_entity = $this->rulesStorage
    ->create([
    'id' => 'rule2',
    'events' => [
      [
        'event_name' => 'rules_entity_view:node',
      ],
    ],
    'expression' => $rule2
      ->getConfiguration(),
  ]);
  $config_entity
    ->save();

  // Create a reaction rule to display a message when viewing a node of a type
  // that is NOT enabled for scheduled publishing.
  $rule3 = $this->expressionManager
    ->createRule();
  $rule3
    ->addCondition('scheduler_condition_publishing_is_enabled', ContextConfig::create()
    ->map('node', 'node')
    ->negateResult());
  $message3 = 'RULES message 3. This node type is not enabled for scheduled publishing.';
  $rule3
    ->addAction('rules_system_message', ContextConfig::create()
    ->setValue('message', $message3)
    ->setValue('type', 'status'));
  $config_entity = $this->rulesStorage
    ->create([
    'id' => 'rule3',
    'events' => [
      [
        'event_name' => 'rules_entity_view:node',
      ],
    ],
    'expression' => $rule3
      ->getConfiguration(),
  ]);
  $config_entity
    ->save();

  // Create a reaction rule to display a message when viewing a node of a type
  // that is NOT enabled for scheduled unpublishing.
  $rule4 = $this->expressionManager
    ->createRule();
  $rule4
    ->addCondition('scheduler_condition_unpublishing_is_enabled', ContextConfig::create()
    ->map('node', 'node')
    ->negateResult());
  $message4 = 'RULES message 4. This node type is not enabled for scheduled unpublishing.';
  $rule4
    ->addAction('rules_system_message', ContextConfig::create()
    ->setValue('message', $message4)
    ->setValue('type', 'status'));
  $config_entity = $this->rulesStorage
    ->create([
    'id' => 'rule4',
    'events' => [
      [
        'event_name' => 'rules_entity_view:node',
      ],
    ],
    'expression' => $rule4
      ->getConfiguration(),
  ]);
  $config_entity
    ->save();
  $assert = $this
    ->assertSession();

  // View the node and check the default position - that the node type is
  // enabled for both publishing and unpublishing.
  $this
    ->drupalGet('node/' . $this->node
    ->id());
  $assert
    ->pageTextContains($message1);
  $assert
    ->pageTextContains($message2);
  $assert
    ->pageTextNotContains($message3);
  $assert
    ->pageTextNotContains($message4);

  // Turn off scheduled publishing for the node type and check the rules.
  $this->nodetype
    ->setThirdPartySetting('scheduler', 'publish_enable', FALSE)
    ->save();

  // Flushing the caches was not required when using WebTestBase but is needed
  // after converting to BrowserTestBase.
  drupal_flush_all_caches();
  $this
    ->drupalGet('node/' . $this->node
    ->id());
  $assert
    ->pageTextNotContains($message1);
  $assert
    ->pageTextContains($message2);
  $assert
    ->pageTextContains($message3);
  $assert
    ->pageTextNotContains($message4);

  // Turn off scheduled unpublishing for the node type and the check again.
  $this->nodetype
    ->setThirdPartySetting('scheduler', 'unpublish_enable', FALSE)
    ->save();
  drupal_flush_all_caches();
  $this
    ->drupalGet('node/' . $this->node
    ->id());
  $assert
    ->pageTextNotContains($message1);
  $assert
    ->pageTextNotContains($message2);
  $assert
    ->pageTextContains($message3);
  $assert
    ->pageTextContains($message4);
}