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.',
],
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.',
],
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.',
],
];
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 checkMessages(string $entityTypeId = NULL, array $expectedMessages = []) {
$offset = [
'node' => 0,
'media' => 6,
'commerce_product' => 12,
];
array_walk($expectedMessages, function (&$item) use ($offset, $entityTypeId) {
$item = $item + $offset[$entityTypeId];
});
foreach ($expectedMessages as $i) {
$this
->assertSession()
->pageTextContains($this->message[$i]);
}
$notExpecting = array_diff(array_keys($this->message), $expectedMessages);
foreach ($notExpecting as $i) {
$this
->assertSession()
->pageTextNotContains($this->message[$i]);
}
}
public function testRulesEventsNone($entityTypeId, $bundle) {
$titleField = $entityTypeId == 'media' ? 'name' : 'title';
$title = 'A. Create with no dates';
$this
->drupalGet($this
->entityAddUrl($entityTypeId, $bundle));
$this
->submitForm([
"{$titleField}[0][value]" => $title,
], 'Save');
$this
->checkMessages();
$entity = $this
->getEntityByTitle($entityTypeId, $title);
$this
->drupalGet($entity
->toUrl('edit-form'));
$this
->submitForm([
"{$titleField}[0][value]" => 'B. Edit with no dates',
], 'Save');
$this
->checkMessages();
}
public function testRulesEventsPublish($entityTypeId, $bundle) {
$this
->entityTypeObject($entityTypeId, $bundle)
->setThirdPartySetting('scheduler', 'publish_past_date', 'schedule')
->save();
$titleField = $entityTypeId == 'media' ? 'name' : 'title';
$title = 'C. Create with publish-on date';
$edit = [
"{$titleField}[0][value]" => $title,
'publish_on[0][value][date]' => date('Y-m-d', time() - 60),
'publish_on[0][value][time]' => date('H:i:s', time() - 60),
];
$this
->drupalGet($this
->entityAddUrl($entityTypeId, $bundle));
$this
->submitForm($edit, 'Save');
$this
->checkMessages($entityTypeId, [
1,
]);
$entity = $this
->getEntityByTitle($entityTypeId, $title);
$this
->drupalGet($entity
->toUrl('edit-form'));
$this
->submitForm([
"{$titleField}[0][value]" => 'D. Edit with publish-on date',
], 'Save');
$this
->checkMessages($entityTypeId, [
2,
]);
$this
->cronRun();
$this
->drupalGet($entity
->toUrl());
$this
->checkMessages($entityTypeId, [
3,
]);
}
public function testRulesEventsUnpublish($entityTypeId, $bundle) {
$titleField = $entityTypeId == 'media' ? 'name' : 'title';
$title = 'E. Create with unpublish-on date';
$edit = [
"{$titleField}[0][value]" => $title,
'unpublish_on[0][value][date]' => date('Y-m-d', time() + 5),
'unpublish_on[0][value][time]' => date('H:i:s', time() + 5),
];
$this
->drupalGet($this
->entityAddUrl($entityTypeId, $bundle));
$this
->submitForm($edit, 'Save');
$this
->checkMessages($entityTypeId, [
4,
]);
$entity = $this
->getEntityByTitle($entityTypeId, $title);
$this
->drupalGet($entity
->toUrl('edit-form'));
$this
->submitForm([
"{$titleField}[0][value]" => 'F. Edit with unpublish-on date',
], 'Save');
$this
->checkMessages($entityTypeId, [
5,
]);
sleep(6);
$this
->cronRun();
$this
->drupalGet($entity
->toUrl());
$this
->checkMessages($entityTypeId, [
6,
]);
}
public function testRulesEventsBoth($entityTypeId, $bundle) {
$this
->entityTypeObject($entityTypeId, $bundle)
->setThirdPartySetting('scheduler', 'publish_past_date', 'schedule')
->save();
$titleField = $entityTypeId == 'media' ? 'name' : 'title';
$title = 'G. Create with both dates';
$edit = [
"{$titleField}[0][value]" => $title,
'publish_on[0][value][date]' => date('Y-m-d', time() - 60),
'publish_on[0][value][time]' => date('H:i:s', time() - 60),
'unpublish_on[0][value][date]' => date('Y-m-d', time() + 5),
'unpublish_on[0][value][time]' => date('H:i:s', time() + 5),
];
$this
->drupalGet($this
->entityAddUrl($entityTypeId, $bundle));
$this
->submitForm($edit, 'Save');
$this
->checkMessages($entityTypeId, [
1,
4,
]);
$entity = $this
->getEntityByTitle($entityTypeId, $title);
$this
->drupalGet($entity
->toUrl('edit-form'));
$this
->submitForm([
"{$titleField}[0][value]" => 'H. Edit with both dates',
], 'Save');
$this
->checkMessages($entityTypeId, [
2,
5,
]);
sleep(6);
$this
->cronRun();
$this
->drupalGet($entity
->toUrl());
$this
->checkMessages($entityTypeId, [
3,
5,
6,
]);
}
}