View source
<?php
namespace Drupal\Tests\scheduler\Functional;
use Drupal\Core\Logger\RfcLogLevel;
use Drupal\rules\Context\ContextConfig;
class SchedulerRulesActionsTest extends SchedulerBrowserTestBase {
protected static $modules = [
'scheduler_rules_integration',
];
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');
$this
->drupalLogin($this->adminUser);
}
public function dataRulesActions() {
$data = [
'#node' => [
'node',
$this->type,
$this->nonSchedulerType,
],
'#media' => [
'media',
$this->mediaTypeName,
$this->nonSchedulerMediaTypeName,
],
'#commerce_product' => [
'commerce_product',
$this->productTypeName,
$this->nonSchedulerProductTypeName,
],
];
return $data;
}
public function testPublishOnActions($entityTypeId, $enabled, $nonEnabled) {
$titleField = $entityTypeId == 'media' ? 'name' : 'title';
$publish_on = $this->requestTime + 1800;
$publish_on_formatted = $this->dateFormatter
->format($publish_on, 'long');
$action_suffix = $entityTypeId == 'node' ? '_action' : ":{$entityTypeId}";
$storage = $this
->entityStorageObject($entityTypeId);
$rule1 = $this->expressionManager
->createRule();
$rule1
->addCondition('rules_data_comparison', ContextConfig::create()
->map('data', "{$entityTypeId}.{$titleField}.value")
->setValue('operation', 'contains')
->setValue('value', 'Trigger Rule 1'));
$message1 = 'RULES message 1. Action to set Publish-on date.';
$rule1
->addAction("scheduler_set_publishing_date{$action_suffix}", ContextConfig::create()
->map("{$entityTypeId}", "{$entityTypeId}")
->setValue('date', $publish_on))
->addAction('rules_system_message', ContextConfig::create()
->setValue('message', $message1)
->setValue('type', 'status'));
$config_entity = $this->rulesStorage
->create([
'id' => 'rule1',
'events' => [
[
'event_name' => "rules_entity_presave:{$entityTypeId}",
],
],
'expression' => $rule1
->getConfiguration(),
]);
$config_entity
->save();
$rule2 = $this->expressionManager
->createRule();
$rule2
->addCondition('rules_data_comparison', ContextConfig::create()
->map('data', "{$entityTypeId}.{$titleField}.value")
->setValue('operation', 'contains')
->setValue('value', 'Trigger Rule 2'));
$message2 = 'RULES message 2. Action to remove Publish-on date and publish immediately.';
$rule2
->addAction("scheduler_remove_publishing_date{$action_suffix}", ContextConfig::create()
->map("{$entityTypeId}", "{$entityTypeId}"))
->addAction("scheduler_publish_now{$action_suffix}", ContextConfig::create()
->map("{$entityTypeId}", "{$entityTypeId}"))
->addAction('rules_system_message', ContextConfig::create()
->setValue('message', $message2)
->setValue('type', 'status'));
$config_entity = $this->rulesStorage
->create([
'id' => 'rule2',
'events' => [
[
'event_name' => "rules_entity_presave:{$entityTypeId}",
],
],
'expression' => $rule2
->getConfiguration(),
]);
$config_entity
->save();
$assert = $this
->assertSession();
$title = "First - new enabled {$enabled} - Trigger Rule 1";
$this
->drupalGet($this
->entityAddUrl($entityTypeId, $enabled));
$this
->submitForm([
"{$titleField}[0][value]" => $title,
], 'Save');
$entity = $this
->getEntityByTitle($entityTypeId, $title);
$this
->assertSession()
->pageTextContains(sprintf('%s is scheduled to be published %s', $title, $publish_on_formatted));
$assert
->pageTextContains($message1);
$assert
->pageTextNotContains($message2);
$this
->assertEquals($entity->publish_on->value, $publish_on, 'Entity should be scheduled for publishing at the correct time');
$this
->assertEmpty($entity->unpublish_on->value, 'Entity should not be scheduled for unpublishing.');
$this
->assertFalse($entity
->isPublished(), 'Entity should be unpublished');
$entity = $this
->createEntity($entityTypeId, $enabled, [
"{$titleField}" => "Second - existing enabled {$enabled}",
]);
$this
->drupalGet($entity
->toUrl('edit-form'));
$this
->submitForm([
"{$titleField}[0][value]" => "Edit enabled {$enabled} - but no rules will be triggered",
], 'Save');
$storage
->resetCache([
$entity
->id(),
]);
$entity = $storage
->load($entity
->id());
$assert
->pageTextNotContains($message1);
$assert
->pageTextNotContains($message2);
$this
->assertEmpty($entity->publish_on->value, 'Entity should not be scheduled for publishing');
$this
->assertEmpty($entity->unpublish_on->value, 'Entity should not be scheduled for unpublishing');
$this
->assertTrue($entity
->isPublished(), 'Entity should remain published');
$this
->drupalGet($entity
->toUrl('edit-form'));
$this
->submitForm([
"{$titleField}[0][value]" => "Edit enabled {$enabled} - Trigger Rule 1",
], 'Save');
$storage
->resetCache([
$entity
->id(),
]);
$entity = $storage
->load($entity
->id());
$assert
->pageTextContains($message1);
$assert
->pageTextNotContains($message2);
$this
->assertEquals($entity->publish_on->value, $publish_on, 'Entity should be scheduled for publishing at the correct time');
$this
->assertEmpty($entity->unpublish_on->value, 'Entity should not be scheduled for unpublishing');
$this
->assertFalse($entity
->isPublished(), 'Entity should be unpublished');
$this
->drupalGet($entity
->toUrl('edit-form'));
$this
->submitForm([
"{$titleField}[0][value]" => "Edit enabled {$enabled} - Trigger Rule 2",
], 'Save');
$storage
->resetCache([
$entity
->id(),
]);
$entity = $storage
->load($entity
->id());
$assert
->pageTextNotContains($message1);
$assert
->pageTextContains($message2);
$this
->assertEmpty($entity->publish_on->value, 'Entity should not be scheduled for publishing.');
$this
->assertEmpty($entity->unpublish_on->value, 'Entity should not be scheduled for unpublishing.');
$this
->assertTrue($entity
->isPublished(), 'Entity should be published.');
$title = "Third - new non-enabled {$nonEnabled} - Trigger Rule 1";
$this
->drupalGet($this
->entityAddUrl($entityTypeId, $nonEnabled));
$this
->submitForm([
"{$titleField}[0][value]" => $title,
], 'Save');
$entity = $this
->getEntityByTitle($entityTypeId, $title);
$assert
->pageTextContains('warning message');
$assert
->elementExists('xpath', '//div[@aria-label="Warning message" and contains(string(), "Action")]');
$this
->assertEmpty($entity->publish_on->value, 'Entity should not be scheduled for publishing');
$log = \Drupal::database()
->select('watchdog', 'w')
->condition('type', 'scheduler')
->condition('severity', RfcLogLevel::WARNING)
->countQuery()
->execute()
->fetchField();
$this
->assertEquals(1, $log, 'There is 1 watchdog warning message from Scheduler');
$entity = $this
->createEntity($entityTypeId, $nonEnabled, [
"{$titleField}" => "Fourth - existing non-enabled {$nonEnabled}",
]);
$this
->drupalGet($entity
->toUrl('edit-form'));
$this
->submitForm([
"{$titleField}[0][value]" => "Edit non-enabled {$nonEnabled} - Trigger Rule 1",
], 'Save');
$assert
->pageTextContains('warning message');
$assert
->elementExists('xpath', '//div[@aria-label="Warning message" and contains(string(), "Action")]');
$this
->assertEmpty($entity->publish_on->value, 'Entity should not be scheduled for publishing.');
$log = \Drupal::database()
->select('watchdog', 'w')
->condition('type', 'scheduler')
->condition('severity', RfcLogLevel::WARNING)
->countQuery()
->execute()
->fetchField();
$this
->assertEquals(2, $log, 'There are now 2 watchdog warning messages from Scheduler');
$this
->drupalGet($entity
->toUrl('edit-form'));
$this
->submitForm([
"{$titleField}[0][value]" => "Edit non-enabled {$nonEnabled} - Trigger Rule 2",
], 'Save');
$assert
->pageTextContains('warning message');
$assert
->elementExists('xpath', '//div[@aria-label="Warning message" and contains(string(), "Action")]');
$log = \Drupal::database()
->select('watchdog', 'w')
->condition('type', 'scheduler')
->condition('severity', RfcLogLevel::WARNING)
->countQuery()
->execute()
->fetchField();
$this
->assertEquals(3, $log, 'There are now 3 watchdog warning messages from Scheduler');
$this
->drupalGet('admin/reports/dblog');
}
public function testUnpublishOnActions($entityTypeId, $enabled, $nonEnabled) {
$titleField = $entityTypeId == 'media' ? 'name' : 'title';
$unpublish_on = $this->requestTime + 2400;
$unpublish_on_formatted = $this->dateFormatter
->format($unpublish_on, 'long');
$action_suffix = $entityTypeId == 'node' ? '_action' : ":{$entityTypeId}";
$storage = $this
->entityStorageObject($entityTypeId);
$rule3 = $this->expressionManager
->createRule();
$rule3
->addCondition('rules_data_comparison', ContextConfig::create()
->map('data', "{$entityTypeId}.{$titleField}.value")
->setValue('operation', 'contains')
->setValue('value', 'Trigger Rule 3'));
$message3 = 'RULES message 3. Action to set Unpublish-on date.';
$rule3
->addAction("scheduler_set_unpublishing_date{$action_suffix}", ContextConfig::create()
->map("{$entityTypeId}", "{$entityTypeId}")
->setValue('date', $unpublish_on))
->addAction('rules_system_message', ContextConfig::create()
->setValue('message', $message3)
->setValue('type', 'status'));
$config_entity = $this->rulesStorage
->create([
'id' => 'rule3',
'events' => [
[
'event_name' => "rules_entity_presave:{$entityTypeId}",
],
],
'expression' => $rule3
->getConfiguration(),
]);
$config_entity
->save();
$rule4 = $this->expressionManager
->createRule();
$rule4
->addCondition('rules_data_comparison', ContextConfig::create()
->map('data', "{$entityTypeId}.{$titleField}.value")
->setValue('operation', 'contains')
->setValue('value', 'Trigger Rule 4'));
$message4 = 'RULES message 4. Action to remove Unpublish-on date and unpublish the node immediately.';
$rule4
->addAction("scheduler_remove_unpublishing_date{$action_suffix}", ContextConfig::create()
->map("{$entityTypeId}", "{$entityTypeId}"))
->addAction("scheduler_unpublish_now{$action_suffix}", ContextConfig::create()
->map("{$entityTypeId}", "{$entityTypeId}"))
->addAction('rules_system_message', ContextConfig::create()
->setValue('message', $message4)
->setValue('type', 'status'));
$config_entity = $this->rulesStorage
->create([
'id' => 'rule4',
'events' => [
[
'event_name' => "rules_entity_presave:{$entityTypeId}",
],
],
'expression' => $rule4
->getConfiguration(),
]);
$config_entity
->save();
$assert = $this
->assertSession();
$title = "First - new enabled {$enabled} - Trigger Rule 3";
$this
->drupalGet($this
->entityAddUrl($entityTypeId, $enabled));
$this
->submitForm([
"{$titleField}[0][value]" => $title,
], 'Save');
$entity = $this
->getEntityByTitle($entityTypeId, $title);
$this
->assertSession()
->pageTextContains(sprintf('%s is scheduled to be unpublished %s', $title, $unpublish_on_formatted));
$assert
->pageTextContains($message3);
$assert
->pageTextNotContains($message4);
$this
->assertEquals($entity->unpublish_on->value, $unpublish_on, 'Entity should be scheduled for unpublishing at the correct time');
$this
->assertEmpty($entity->publish_on->value, 'Entity should not be scheduled for publishing.');
$this
->assertTrue($entity
->isPublished(), 'Entity should be published');
$entity = $this
->createEntity($entityTypeId, $enabled, [
"{$titleField}" => "Second - existing enabled {$enabled}",
]);
$this
->drupalGet($entity
->toUrl('edit-form'));
$this
->submitForm([
"{$titleField}[0][value]" => "Edit enabled {$enabled} - but no rules will be triggered",
], 'Save');
$storage
->resetCache([
$entity
->id(),
]);
$entity = $storage
->load($entity
->id());
$assert
->pageTextNotContains($message3);
$assert
->pageTextNotContains($message4);
$this
->assertEmpty($entity->publish_on->value, 'Entity should not be scheduled for publishing');
$this
->assertEmpty($entity->unpublish_on->value, 'Entity should not be scheduled for unpublishing');
$this
->assertTrue($entity
->isPublished(), 'Entity should remain published');
$this
->drupalGet($entity
->toUrl('edit-form'));
$this
->submitForm([
"{$titleField}[0][value]" => "Edit enabled {$enabled} - Trigger Rule 3",
], 'Save');
$storage
->resetCache([
$entity
->id(),
]);
$entity = $storage
->load($entity
->id());
$assert
->pageTextContains($message3);
$assert
->pageTextNotContains($message4);
$this
->assertEmpty($entity->publish_on->value, 'Entity should not be scheduled for publishing');
$this
->assertEquals($entity->unpublish_on->value, $unpublish_on, 'Entity should be scheduled for unpublishing at the correct time');
$this
->assertTrue($entity
->isPublished(), 'Entity is still published');
$this
->drupalGet($entity
->toUrl('edit-form'));
$this
->submitForm([
"{$titleField}[0][value]" => "Edit enabled {$enabled} - Trigger Rule 4",
], 'Save');
$storage
->resetCache([
$entity
->id(),
]);
$entity = $storage
->load($entity
->id());
$assert
->pageTextNotContains($message3);
$assert
->pageTextContains($message4);
$this
->assertEmpty($entity->publish_on->value, 'Entity should not be scheduled for publishing.');
$this
->assertEmpty($entity->unpublish_on->value, 'Entity should not be scheduled for unpublishing.');
$this
->assertFalse($entity
->isPublished(), 'Entity should be unpublished.');
$title = "Third - new non-enabled {$nonEnabled} - Trigger Rule 3";
$this
->drupalGet($this
->entityAddUrl($entityTypeId, $nonEnabled));
$this
->submitForm([
"{$titleField}[0][value]" => $title,
], 'Save');
$entity = $this
->getEntityByTitle($entityTypeId, $title);
$assert
->pageTextContains('warning message');
$assert
->elementExists('xpath', '//div[@aria-label="Warning message" and contains(string(), "Action")]');
$this
->assertEmpty($entity->unpublish_on->value, 'Entity should not be scheduled for unpublishing');
$log = \Drupal::database()
->select('watchdog', 'w')
->condition('type', 'scheduler')
->condition('severity', RfcLogLevel::WARNING)
->countQuery()
->execute()
->fetchField();
$this
->assertEquals(1, $log, 'There is 1 watchdog warning message from Scheduler');
$entity = $this
->createEntity($entityTypeId, $nonEnabled, [
"{$titleField}" => "Fourth - existing non-enabled {$nonEnabled}",
]);
$this
->drupalGet($entity
->toUrl('edit-form'));
$this
->submitForm([
"{$titleField}[0][value]" => "Edit non-enabled {$nonEnabled} - Trigger Rule 3",
], 'Save');
$assert
->pageTextContains('warning message');
$assert
->elementExists('xpath', '//div[@aria-label="Warning message" and contains(string(), "Action")]');
$this
->assertEmpty($entity->unpublish_on->value, 'Entity should not be scheduled for unpublishing.');
$log = \Drupal::database()
->select('watchdog', 'w')
->condition('type', 'scheduler')
->condition('severity', RfcLogLevel::WARNING)
->countQuery()
->execute()
->fetchField();
$this
->assertEquals(2, $log, 'There are now 2 watchdog warning messages from Scheduler');
$this
->drupalGet($entity
->toUrl('edit-form'));
$this
->submitForm([
"{$titleField}[0][value]" => "Edit non-enabled {$nonEnabled} - Trigger Rule 4",
], 'Save');
$assert
->pageTextContains('warning message');
$assert
->elementExists('xpath', '//div[@aria-label="Warning message" and contains(string(), "Action")]');
$log = \Drupal::database()
->select('watchdog', 'w')
->condition('type', 'scheduler')
->condition('severity', RfcLogLevel::WARNING)
->countQuery()
->execute()
->fetchField();
$this
->assertEquals(3, $log, 'There are now 3 watchdog warning messages from Scheduler');
$this
->drupalGet('admin/reports/dblog');
}
}