ModerationContext.behat.inc in Lightning Workflow 8.2
File
tests/contexts/ModerationContext.behat.inc
View source
<?php
namespace Acquia\LightningExtension\Context;
use Drupal\Component\Utility\Random;
use Drupal\DrupalExtension\Context\DrupalSubContextBase;
use Drupal\workflows\StateInterface;
use Webmozart\Assert\Assert;
final class ModerationContext extends DrupalSubContextBase {
use AwaitTrait;
private $node;
private $moderated = [];
public function tearDown() {
if ($this->node) {
$this->node
->delete();
}
$workflow = entity_load('workflow', 'editorial');
Assert::isInstanceOf($workflow, '\\Drupal\\workflows\\WorkflowInterface');
while ($this->moderated) {
$workflow
->getTypePlugin()
->removeEntityTypeAndBundle('node', array_pop($this->moderated));
}
$workflow
->save();
}
private function getWorkflow() {
Assert::notEmpty($this->node);
$workflow = \Drupal::service('content_moderation.moderation_information')
->getWorkflowForEntity($this->node);
Assert::notEmpty($workflow);
return $workflow
->getTypePlugin();
}
private function getStateId($state_label) {
$states = array_filter($this
->getWorkflow()
->getStates(), function (StateInterface $state) use ($state_label) {
return $state
->label() === $state_label;
});
Assert::count($states, 1);
return reset($states)
->id();
}
public function assertAtContentInState($content_type, $state_label) {
$this->node = entity_create('node', [
'type' => $content_type,
'title' => (new Random())
->string(),
]);
$current_user = $this
->getUserManager()
->getCurrentUser();
if ($current_user) {
$this->node
->setOwnerId($current_user->uid);
}
$this->node
->set('moderation_state', $this
->getStateId($state_label))
->save();
$this
->visitPath($this->node
->toUrl()
->toString());
$assert = $this
->assertSession();
$assert
->statusCodeEquals(200);
$assert
->pageTextContains($this->node
->getTitle());
}
public function assertInPlaceStateTransition($state_label) {
Assert::notEmpty($this->node);
$transition = $this
->getWorkflow()
->getTransitionFromStateToState($this->node->moderation_state->value, $this
->getStateId($state_label));
$assert = $this
->assertSession();
$toolbar = $assert
->elementExists('css', '#toolbar-bar');
$toolbar
->clickLink('Tasks');
$sidebar = $this
->awaitElement('.moderation-sidebar-container');
$assert
->elementExists('named', [
'button',
'Send to review',
], $sidebar);
$sidebar
->pressButton($transition
->label());
$assert
->pageTextContains('The moderation state has been updated.');
$current_state = $assert
->elementExists('named', [
'link',
'Tasks',
], $toolbar)
->getAttribute('data-label');
Assert::same($current_state, $transition
->to()
->label());
}
public function addModerationToContentType($node_type) {
$node_type = entity_load('node_type', $node_type);
$node_type
->setThirdPartySetting('lightning_workflow', 'workflow', 'editorial');
lightning_workflow_node_type_insert($node_type);
array_push($this->moderated, $node_type
->id());
}
}