View source
<?php
namespace Drupal\Tests\scheduler\Functional;
use Drupal\rules\Context\ContextConfig;
class SchedulerRulesEventsTest 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');
$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.',
],
];
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();
}
$this
->drupalLogin($this->schedulerUser);
}
public function testRulesEventsNone() {
$assert = $this
->assertSession();
$edit = [
'title[0][value]' => 'Test for no events',
'body[0][value]' => $this
->randomString(30),
];
$this
->drupalPostForm('node/add/' . $this->type, $edit, 'Save');
$node = $this
->drupalGetNodeByTitle($edit['title[0][value]']);
$assert
->pageTextNotContains($this->message[1]);
$assert
->pageTextNotContains($this->message[2]);
$assert
->pageTextNotContains($this->message[3]);
$assert
->pageTextNotContains($this->message[4]);
$assert
->pageTextNotContains($this->message[5]);
$assert
->pageTextNotContains($this->message[6]);
$edit = [
'body[0][value]' => $this
->randomString(30),
];
$this
->drupalPostForm('node/' . $node
->id() . '/edit', $edit, 'Save');
$assert
->pageTextNotContains($this->message[1]);
$assert
->pageTextNotContains($this->message[2]);
$assert
->pageTextNotContains($this->message[3]);
$assert
->pageTextNotContains($this->message[4]);
$assert
->pageTextNotContains($this->message[5]);
$assert
->pageTextNotContains($this->message[6]);
}
public function testRulesEventsPublish() {
$assert = $this
->assertSession();
$edit = [
'title[0][value]' => 'Create node with publish-on date',
'publish_on[0][value][date]' => date('Y-m-d', time() + 3),
'publish_on[0][value][time]' => date('H:i:s', time() + 3),
'body[0][value]' => $this
->randomString(30),
];
$this
->drupalPostForm('node/add/' . $this->type, $edit, 'Save');
$node = $this
->drupalGetNodeByTitle($edit['title[0][value]']);
$assert
->pageTextContains($this->message[1]);
$assert
->pageTextNotContains($this->message[2]);
$assert
->pageTextNotContains($this->message[3]);
$assert
->pageTextNotContains($this->message[4]);
$assert
->pageTextNotContains($this->message[5]);
$assert
->pageTextNotContains($this->message[6]);
$edit = [
'title[0][value]' => 'Edit node with publish-on date',
'body[0][value]' => $this
->randomString(30),
];
$this
->drupalPostForm('node/' . $node
->id() . '/edit', $edit, 'Save');
$assert
->pageTextNotContains($this->message[1]);
$assert
->pageTextContains($this->message[2]);
$assert
->pageTextNotContains($this->message[3]);
$assert
->pageTextNotContains($this->message[4]);
$assert
->pageTextNotContains($this->message[5]);
$assert
->pageTextNotContains($this->message[6]);
sleep(5);
$this
->cronRun();
$this
->drupalGet('admin/reports/dblog');
$assert
->pageTextNotContains($this->message[1]);
$assert
->pageTextNotContains($this->message[2]);
$assert
->pageTextContains($this->message[3]);
$assert
->pageTextNotContains($this->message[4]);
$assert
->pageTextNotContains($this->message[5]);
$assert
->pageTextNotContains($this->message[6]);
}
public function testRulesEventsUnpublish() {
$assert = $this
->assertSession();
$edit = [
'title[0][value]' => 'Create node with unpublish-on date',
'unpublish_on[0][value][date]' => date('Y-m-d', time() + 3),
'unpublish_on[0][value][time]' => date('H:i:s', time() + 3),
'body[0][value]' => $this
->randomString(30),
];
$this
->drupalPostForm('node/add/' . $this->type, $edit, 'Save');
$node = $this
->drupalGetNodeByTitle($edit['title[0][value]']);
$assert
->pageTextNotContains($this->message[1]);
$assert
->pageTextNotContains($this->message[2]);
$assert
->pageTextNotContains($this->message[3]);
$assert
->pageTextContains($this->message[4]);
$assert
->pageTextNotContains($this->message[5]);
$assert
->pageTextNotContains($this->message[6]);
$edit = [
'title[0][value]' => 'Edit node with unpublish-on date',
'body[0][value]' => $this
->randomString(30),
];
$this
->drupalPostForm('node/' . $node
->id() . '/edit', $edit, 'Save');
$assert
->pageTextNotContains($this->message[1]);
$assert
->pageTextNotContains($this->message[2]);
$assert
->pageTextNotContains($this->message[3]);
$assert
->pageTextNotContains($this->message[4]);
$assert
->pageTextContains($this->message[5]);
$assert
->pageTextNotContains($this->message[6]);
sleep(5);
$this
->cronRun();
$this
->drupalGet('admin/reports/dblog');
$assert
->pageTextNotContains($this->message[1]);
$assert
->pageTextNotContains($this->message[2]);
$assert
->pageTextNotContains($this->message[3]);
$assert
->pageTextNotContains($this->message[4]);
$assert
->pageTextNotContains($this->message[5]);
$assert
->pageTextContains($this->message[6]);
}
public function testRulesEventsBoth() {
$assert = $this
->assertSession();
$edit = [
'title[0][value]' => 'Create node with both dates',
'publish_on[0][value][date]' => date('Y-m-d', time() + 3),
'publish_on[0][value][time]' => date('H:i:s', time() + 3),
'unpublish_on[0][value][date]' => date('Y-m-d', time() + 4),
'unpublish_on[0][value][time]' => date('H:i:s', time() + 4),
'body[0][value]' => $this
->randomString(30),
];
$this
->drupalPostForm('node/add/' . $this->type, $edit, 'Save');
$node = $this
->drupalGetNodeByTitle($edit['title[0][value]']);
$assert
->pageTextContains($this->message[1]);
$assert
->pageTextNotContains($this->message[2]);
$assert
->pageTextNotContains($this->message[3]);
$assert
->pageTextContains($this->message[4]);
$assert
->pageTextNotContains($this->message[5]);
$assert
->pageTextNotContains($this->message[6]);
$edit = [
'title[0][value]' => 'Edit node with both dates',
'body[0][value]' => $this
->randomString(30),
];
$this
->drupalPostForm('node/' . $node
->id() . '/edit', $edit, 'Save');
$node = $this
->drupalGetNodeByTitle($edit['title[0][value]']);
$assert
->pageTextNotContains($this->message[1]);
$assert
->pageTextContains($this->message[2]);
$assert
->pageTextNotContains($this->message[3]);
$assert
->pageTextNotContains($this->message[4]);
$assert
->pageTextContains($this->message[5]);
$assert
->pageTextNotContains($this->message[6]);
sleep(6);
$this
->cronRun();
$this
->drupalGet('admin/reports/dblog');
$assert
->pageTextNotContains($this->message[1]);
$assert
->pageTextNotContains($this->message[2]);
$assert
->pageTextContains($this->message[3]);
$assert
->pageTextNotContains($this->message[4]);
$assert
->pageTextContains($this->message[5]);
$assert
->pageTextContains($this->message[6]);
}
}