scheduled_publish-8.x-3.9/tests/src/Kernel/EcaScheduledPublishEventsTest.php
tests/src/Kernel/EcaScheduledPublishEventsTest.php
<?php
namespace Drupal\Tests\scheduled_publish\Kernel;
use Drupal\Component\Datetime\DateTimePlus;
use Drupal\eca\Entity\Eca;
use Drupal\eca_test_array\Plugin\Action\ArrayIncrement;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\KernelTests\KernelTestBase;
use Drupal\node\Entity\Node;
use Drupal\Tests\content_moderation\Traits\ContentModerationTestTrait;
use Drupal\Tests\eca\ContentTypeCreationTrait;
/**
* Tests for the ECA events provided by the Scheduled Publish module.
*
* @see \Drupal\Tests\eca_workflow\Kernel\WorkflowEventsTest
*
* @group scheduled_publish
* @group eca
*/
class EcaScheduledPublishEventsTest extends KernelTestBase {
use ContentModerationTestTrait;
use ContentTypeCreationTrait;
/**
* {@inheritdoc}
*/
protected static $modules = [
'content_moderation',
'datetime',
'eca',
'eca_test_array',
'field',
'node',
'scheduled_publish',
'system',
'text',
'user',
'workflows',
];
/**
* {@inheritdoc}
*/
public function setUp(): void {
parent::setUp();
// Setup: Install schemas, entity schemas, config.
$this->installSchema('node', 'node_access');
$this->installEntitySchema('node');
$this->installEntitySchema('user');
$this->installEntitySchema('content_moderation_state');
$this->installConfig(['system', 'field', 'content_moderation', 'scheduled_publish']);
// Setup: Create a content type with a scheduled publish field.
$this->createContentType(['type' => 'article', 'name' => 'Article']);
FieldStorageConfig::create([
'field_name' => 'field_publish',
'type' => 'scheduled_publish',
'entity_type' => 'node',
'cardinality' => 1,
])->save();
FieldConfig::create([
'field_name' => 'field_publish',
'entity_type' => 'node',
'bundle' => 'article',
'label' => 'Publish',
])->save();
// Setup: Establish a workflow and assign it to the content type.
$workflow = $this->createEditorialWorkflow();
$workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'article');
$workflow->save();
}
/**
* Tests the "scheduled_publish:scheduled_state_change" event.
*/
public function testScheduledStateChangeEvent(): void {
// Setup: Create an ECA configuration that reacts on the
// scheduled_state_change event and increments an array entry for each
// triggered event.
$ecaConfigValues = [
'langcode' => 'en',
'status' => TRUE,
'id' => 'scheduled_publish_eca_scheduled_state_change_events',
'label' => 'Scheduled Publish / ECA scheduled state change events',
'modeller' => 'fallback',
'version' => '1.0.0',
'events' => [
'scheduled_state_change' => [
'plugin' => 'scheduled_publish:scheduled_state_change',
'label' => 'Scheduled state change',
'configuration' => [],
'successors' => [
['id' => 'increment_scheduled_publish_state_change_counter', 'condition' => ''],
],
],
],
'conditions' => [],
'gateways' => [],
'actions' => [
'increment_scheduled_publish_state_change_counter' => [
'plugin' => 'eca_test_array_increment',
'label' => 'Increment scheduled_publish_state_change_counter',
'configuration' => [
'key' => 'scheduled_publish_state_change_counter',
],
'successors' => [],
],
],
];
$ecaConfig = Eca::create($ecaConfigValues);
$ecaConfig->trustData()->save();
// Setup: Create a test article and schedule it for publishing 1 day ago.
$date = DateTimePlus::createFromTimestamp(\strtotime('-1 day'));
$node = Node::create([
'title' => 'Test node',
'type' => 'article',
'status' => FALSE,
'moderation_state' => 'draft',
'field_publish' => [
[
'moderation_state' => 'published',
'value' => $date->format('Y-m-d\TH:i:s'),
],
],
]);
$node->save();
// Assert: The counter is un-set before the test begins.
$this->assertTrue(!isset(ArrayIncrement::$array['scheduled_publish_state_change_counter']));
// SUT: Run the scheduled publishing process.
$this->container->get('scheduled_publish.update')->doUpdate();
// Assert: The counter should now be set to 1.
$this->assertSame(1, ArrayIncrement::$array['scheduled_publish_state_change_counter']);
}
}
