View source
<?php
namespace Drupal\Tests\acquia_contenthub\Kernel\EventSubscriber\PreEntitySave;
use Acquia\ContentHubClient\CDF\CDFObject;
use Drupal\acquia_contenthub\Event\PreEntitySaveEvent;
use Drupal\acquia_contenthub_moderation\EventSubscriber\PreEntitySave\CreateModeratedForwardRevision;
use Drupal\depcalc\DependencyStack;
use Drupal\KernelTests\KernelTestBase;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\Tests\content_moderation\Traits\ContentModerationTestTrait;
class CreateModeratedForwardRevisionTest extends KernelTestBase {
use ContentModerationTestTrait;
protected $workflow;
public static $modules = [
'acquia_contenthub',
'user',
'field',
'node',
'system',
'depcalc',
'content_moderation',
'workflows',
'acquia_contenthub_moderation',
];
protected $node;
protected function setUp() {
parent::setUp();
$this
->installEntitySchema('node');
$this
->installEntitySchema('user');
$this
->installEntitySchema('content_moderation_state');
$this
->installSchema('node', [
'node_access',
]);
$this
->installConfig([
'system',
'content_moderation',
'acquia_contenthub_moderation',
]);
NodeType::create([
'type' => 'bundle_test',
'new_revision' => TRUE,
])
->save();
$this->workflow = $this
->createEditorialWorkflow();
$this->workflow
->getTypePlugin()
->addEntityTypeAndBundle('node', 'bundle_test');
$this->workflow
->save();
$this->node = Node::create([
'type' => 'bundle_test',
'moderation_state' => 'draft',
'langcode' => 'en',
'title' => 'Check forward revisions',
]);
}
public function testCreateModeratedForwardRevision() {
$config_factory = $this->container
->get('config.factory');
$config = $config_factory
->getEditable('acquia_contenthub_moderation.settings');
$config
->set("workflows.{$this->workflow->id()}.moderation_state", 'archived');
$config
->save();
$stack = $this
->prophesize(DependencyStack::class);
$cdf = $this
->prophesize(CDFObject::class);
$event = new PreEntitySaveEvent($this->node, $stack
->reveal(), $cdf
->reveal());
$create_forward_revision = new CreateModeratedForwardRevision($this->container
->get('entity_type.manager'), $this->container
->get('config.factory'), $this->container
->get('content_moderation.moderation_information'), $this->container
->get('logger.factory'));
$mod_state = $this->node
->get('moderation_state')
->getString();
$this
->assertEqual('draft', $mod_state);
$create_forward_revision
->onPreEntitySave($event);
$entity = $event
->getEntity();
$mod_state = $entity
->get('moderation_state')
->getString();
$this
->assertEqual('archived', $mod_state);
}
public function testCreateModFwdRevisionWithoutConfig() : void {
$stack = $this
->prophesize(DependencyStack::class);
$cdf = $this
->prophesize(CDFObject::class);
$event = new PreEntitySaveEvent($this->node, $stack
->reveal(), $cdf
->reveal());
$create_forward_revision = new CreateModeratedForwardRevision($this->container
->get('entity_type.manager'), $this->container
->get('config.factory'), $this->container
->get('content_moderation.moderation_information'), $this->container
->get('logger.factory'));
$create_forward_revision
->onPreEntitySave($event);
$entity = $event
->getEntity();
$mod_state = $entity
->get('moderation_state')
->getString();
$this
->assertEqual('draft', $mod_state);
}
}