Issue #2893778 by timmillwood, Sam152, Wim Leers: Remove deprecated Workflows methods

8.5.x
Lee Rowlands 2017-07-29 17:08:18 +10:00
parent 04d69926b4
commit a16b9c74c2
No known key found for this signature in database
GPG Key ID: 2B829A3DF9204DC4
33 changed files with 265 additions and 676 deletions

View File

@ -103,7 +103,7 @@ class EntityOperations implements ContainerInjectionInterface {
if ($entity->moderation_state->value) {
$workflow = $this->moderationInfo->getWorkflowForEntity($entity);
/** @var \Drupal\content_moderation\ContentModerationState $current_state */
$current_state = $workflow->getState($entity->moderation_state->value);
$current_state = $workflow->getTypePlugin()->getState($entity->moderation_state->value);
// This entity is default if it is new, a new translation, the default
// revision, or the default revision is not published.

View File

@ -59,7 +59,7 @@ class ConfigImportSubscriber extends ConfigImportValidateEventSubscriberBase {
->read($unprocessed_configuration);
$diff = array_diff_key($workflow_config['type_settings']['states'], $original_workflow_config['type_settings']['states']);
foreach (array_keys($diff) as $state_id) {
$state = $workflow->getState($state_id);
$state = $workflow->getTypePlugin()->getState($state_id);
if ($workflow->getTypePlugin()->workflowStateHasData($workflow, $state)) {
$event->getConfigImporter()->logError($this->t('The moderation state @state_label is being used, but is not in the source storage.', ['@state_label' => $state->label()]));
}

View File

@ -89,7 +89,7 @@ class EntityModerationForm extends FormBase {
$form['current'] = [
'#type' => 'item',
'#title' => $this->t('Status'),
'#markup' => $workflow->getState($current_state)->label(),
'#markup' => $workflow->getTypePlugin()->getState($current_state)->label(),
];
}
@ -137,7 +137,7 @@ class EntityModerationForm extends FormBase {
drupal_set_message($this->t('The moderation state has been updated.'));
$new_state = $this->moderationInfo->getWorkflowForEntity($entity)->getState($new_state);
$new_state = $this->moderationInfo->getWorkflowForEntity($entity)->getTypePlugin()->getState($new_state);
// The page we're on likely won't be visible if we just set the entity to
// the default state, as we hide that latest-revision tab if there is no
// forward revision. Redirect to the canonical URL instead, since that will

View File

@ -154,7 +154,7 @@ class ModerationInformation implements ModerationInformationInterface {
return $this->isLatestRevision($entity)
&& $entity->isDefaultRevision()
&& $entity->moderation_state->value
&& $workflow->getState($entity->moderation_state->value)->isPublishedState();
&& $workflow->getTypePlugin()->getState($entity->moderation_state->value)->isPublishedState();
}
/**
@ -171,13 +171,13 @@ class ModerationInformation implements ModerationInformationInterface {
// Load the translated revision.
$language_revision = $default_revision->getTranslation($language->getId());
// Return TRUE if a translation with a published state is found.
if ($workflow->getState($language_revision->moderation_state->value)->isPublishedState()) {
if ($workflow->getTypePlugin()->getState($language_revision->moderation_state->value)->isPublishedState()) {
return TRUE;
}
}
}
return $workflow->getState($default_revision->moderation_state->value)->isPublishedState();
return $workflow->getTypePlugin()->getState($default_revision->moderation_state->value)->isPublishedState();
}
/**

View File

@ -24,7 +24,7 @@ class Permissions {
$permissions = [];
/** @var \Drupal\workflows\WorkflowInterface $workflow */
foreach (Workflow::loadMultipleByType('content_moderation') as $id => $workflow) {
foreach ($workflow->getTransitions() as $transition) {
foreach ($workflow->getTypePlugin()->getTransitions() as $transition) {
$permissions['use ' . $workflow->id() . ' transition ' . $transition->id()] = [
'title' => $this->t('Use %transition transition from %workflow workflow.', [
'%transition' => $transition->label(),

View File

@ -61,7 +61,7 @@ class ContentModerationStateFormatter extends FormatterBase implements Container
$workflow = $this->moderationInformation->getWorkflowForEntity($items->getEntity());
foreach ($items as $delta => $item) {
$elements[$delta] = [
'#markup' => $workflow->getState($item->value)->label(),
'#markup' => $workflow->getTypePlugin()->getState($item->value)->label(),
];
}
return $elements;

View File

@ -116,7 +116,7 @@ class ModerationStateWidget extends OptionsSelectWidget implements ContainerFact
}
$workflow = $this->moderationInformation->getWorkflowForEntity($entity);
$default = $items->get($delta)->value ? $workflow->getState($items->get($delta)->value) : $workflow->getTypePlugin()->getInitialState($workflow, $entity);
$default = $items->get($delta)->value ? $workflow->getTypePlugin()->getState($items->get($delta)->value) : $workflow->getTypePlugin()->getInitialState($workflow, $entity);
/** @var \Drupal\workflows\Transition[] $transitions */
$transitions = $this->validator->getValidTransitions($entity, $this->currentUser);

View File

@ -132,9 +132,9 @@ class ModerationStateFieldItemList extends FieldItemList {
// Change the entity's default revision flag and the publishing status only
// if the new workflow state is a valid one.
if ($workflow->hasState($current_state_id)) {
if ($workflow->getTypePlugin()->hasState($current_state_id)) {
/** @var \Drupal\content_moderation\ContentModerationState $current_state */
$current_state = $workflow->getState($current_state_id);
$current_state = $workflow->getTypePlugin()->getState($current_state_id);
// This entity is default if it is new, a new translation, the default
// revision state, or the default revision is not published.

View File

@ -78,7 +78,7 @@ class ModerationStateConstraintValidator extends ConstraintValidator implements
$workflow = $this->moderationInformation->getWorkflowForEntity($entity);
if (!$workflow->hasState($entity->moderation_state->value)) {
if (!$workflow->getTypePlugin()->hasState($entity->moderation_state->value)) {
// If the state we are transitioning to doesn't exist, we can't validate
// the transitions for this entity further.
$this->context->addViolation($constraint->invalidStateMessage, [
@ -100,12 +100,12 @@ class ModerationStateConstraintValidator extends ConstraintValidator implements
// we cannot do any further validation of transitions, because none will
// be setup for a state that doesn't exist. Instead allow any state to
// take its place.
if (!$workflow->hasState($original_entity->moderation_state->value)) {
if (!$workflow->getTypePlugin()->hasState($original_entity->moderation_state->value)) {
return;
}
$new_state = $workflow->getState($entity->moderation_state->value);
$original_state = $workflow->getState($original_entity->moderation_state->value);
$new_state = $workflow->getTypePlugin()->getState($entity->moderation_state->value);
$original_state = $workflow->getTypePlugin()->getState($original_entity->moderation_state->value);
if (!$original_state->canTransitionTo($new_state->id())) {
$this->context->addViolation($constraint->message, [

View File

@ -369,7 +369,7 @@ class ContentModeration extends WorkflowTypeFormBase implements ContainerFactory
*/
public function getInitialState(WorkflowInterface $workflow, $entity = NULL) {
if ($entity instanceof EntityPublishedInterface) {
return $workflow->getState($entity->isPublished() && !$entity->isNew() ? 'published' : 'draft');
return $workflow->getTypePlugin()->getState($entity->isPublished() && !$entity->isNew() ? 'published' : 'draft');
}
return parent::getInitialState($workflow);
}

View File

@ -40,7 +40,7 @@ class StateTransitionValidation implements StateTransitionValidationInterface {
*/
public function getValidTransitions(ContentEntityInterface $entity, AccountInterface $user) {
$workflow = $this->moderationInfo->getWorkflowForEntity($entity);
$current_state = $entity->moderation_state->value ? $workflow->getState($entity->moderation_state->value) : $workflow->getTypePlugin()->getInitialState($workflow, $entity);
$current_state = $entity->moderation_state->value ? $workflow->getTypePlugin()->getState($entity->moderation_state->value) : $workflow->getTypePlugin()->getInitialState($workflow, $entity);
return array_filter($current_state->getTransitions(), function(Transition $transition) use ($workflow, $user) {
return $user->hasPermission('use ' . $workflow->id() . ' transition ' . $transition->id());

View File

@ -137,7 +137,7 @@ class EntityStateChangeValidationTest extends KernelTestBase {
// Enable moderation to test validation on existing content, with no
// explicit state.
$workflow = Workflow::load('editorial');
$workflow->addState('deleted_state', 'Deleted state');
$workflow->getTypePlugin()->addState('deleted_state', 'Deleted state');
$workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'example');
$workflow->save();
@ -152,7 +152,7 @@ class EntityStateChangeValidationTest extends KernelTestBase {
// Delete the state so $node->original contains an invalid state when
// validating.
$workflow->deleteState('deleted_state');
$workflow->getTypePlugin()->deleteState('deleted_state');
$workflow->save();
$node->moderation_state->value = 'draft';
$violations = $node->validate();

View File

@ -69,6 +69,7 @@ class StateTransitionValidationTest extends UnitTestCase {
$workflow = new Workflow(['id' => 'process', 'type' => 'content_moderation'], 'workflow');
$workflow
->getTypePlugin()
->addState('draft', 'draft')
->addState('needs_review', 'needs_review')
->addState('published', 'published')

View File

@ -104,7 +104,7 @@ class Workflow extends ConfigEntityBase implements WorkflowInterface, EntityWith
*/
public function preSave(EntityStorageInterface $storage) {
$workflow_type = $this->getTypePlugin();
$missing_states = array_diff($workflow_type->getRequiredStates(), array_keys($this->getStates()));
$missing_states = array_diff($workflow_type->getRequiredStates(), array_keys($this->getTypePlugin()->getStates()));
if (!empty($missing_states)) {
throw new RequiredStateMissingException(sprintf("Workflow type '{$workflow_type->label()}' requires states with the ID '%s' in workflow '{$this->id()}'", implode("', '", $missing_states)));
}
@ -172,146 +172,4 @@ class Workflow extends ConfigEntityBase implements WorkflowInterface, EntityWith
return parent::onDependencyRemoval($dependencies) || $changed;
}
/**
* {@inheritdoc}
*/
public function addState($state_id, $label) {
$this->getTypePlugin()->addState($state_id, $label);
return $this;
}
/**
* {@inheritdoc}
*/
public function hasState($state_id) {
return $this->getTypePlugin()->hasState($state_id);
}
/**
* {@inheritdoc}
*/
public function getStates($state_ids = NULL) {
return $this->getTypePlugin()->getStates($state_ids);
}
/**
* {@inheritdoc}
*/
public function getState($state_id) {
return $this->getTypePlugin()->getState($state_id);
}
/**
* {@inheritdoc}
*/
public function setStateLabel($state_id, $label) {
$this->getTypePlugin()->setStateLabel($state_id, $label);
return $this;
}
/**
* {@inheritdoc}
*/
public function setStateWeight($state_id, $weight) {
$this->getTypePlugin()->setStateWeight($state_id, $weight);
return $this;
}
/**
* {@inheritdoc}
*/
public function deleteState($state_id) {
$this->getTypePlugin()->deleteState($state_id);
return $this;
}
/**
* {@inheritdoc}
*/
public function getInitialState() {
return $this->getTypePlugin()->getInitialState($this);
}
/**
* {@inheritdoc}
*/
public function addTransition($id, $label, array $from_state_ids, $to_state_id) {
$this->getTypePlugin()->addTransition($id, $label, $from_state_ids, $to_state_id);
return $this;
}
/**
* {@inheritdoc}
*/
public function getTransition($transition_id) {
return $this->getTypePlugin()->getTransition($transition_id);
}
/**
* {@inheritdoc}
*/
public function hasTransition($transition_id) {
return $this->getTypePlugin()->hasTransition($transition_id);
}
/**
* {@inheritdoc}
*/
public function getTransitions(array $transition_ids = NULL) {
return $this->getTypePlugin()->getTransitions($transition_ids);
}
/**
* {@inheritdoc}
*/
public function getTransitionsForState($state_id, $direction = 'from') {
return $this->getTypePlugin()->getTransitionsForState($state_id, $direction);
}
/**
* {@inheritdoc}
*/
public function getTransitionFromStateToState($from_state_id, $to_state_id) {
return $this->getTypePlugin()->getTransitionFromStateToState($from_state_id, $to_state_id);
}
/**
* {@inheritdoc}
*/
public function hasTransitionFromStateToState($from_state_id, $to_state_id) {
return $this->getTypePlugin()->hasTransitionFromStateToState($from_state_id, $to_state_id);
}
/**
* {@inheritdoc}
*/
public function setTransitionLabel($transition_id, $label) {
$this->getTypePlugin()->setTransitionLabel($transition_id, $label);
return $this;
}
/**
* {@inheritdoc}
*/
public function setTransitionWeight($transition_id, $weight) {
$this->getTypePlugin()->setTransitionWeight($transition_id, $weight);
return $this;
}
/**
* {@inheritdoc}
*/
public function setTransitionFromStates($transition_id, array $from_state_ids) {
$this->getTypePlugin()->setTransitionFromStates($transition_id, $from_state_ids);
return $this;
}
/**
* {@inheritdoc}
*/
public function deleteTransition($transition_id) {
$this->getTypePlugin()->deleteTransition($transition_id);
return $this;
}
}

View File

@ -87,7 +87,7 @@ class WorkflowAddForm extends EntityForm {
// Initialize the workflow using the selected type plugin.
$workflow = $workflow->getTypePlugin()->initializeWorkflow($workflow);
$return = $workflow->save();
if (empty($workflow->getStates())) {
if (empty($workflow->getTypePlugin()->getStates())) {
drupal_set_message($this->t('Created the %label Workflow. In order for the workflow to be enabled there needs to be at least one state.', [
'%label' => $workflow->label(),
]));

View File

@ -69,7 +69,7 @@ class WorkflowEditForm extends EntityForm {
],
];
$states = $workflow->getStates();
$states = $workflow->getTypePlugin()->getStates();
// Warn the user if there are no states.
if (empty($states)) {
@ -146,7 +146,7 @@ class WorkflowEditForm extends EntityForm {
],
],
];
foreach ($workflow->getTransitions() as $transition) {
foreach ($workflow->getTypePlugin()->getTransitions() as $transition) {
$links['edit'] = [
'title' => $this->t('Edit'),
'url' => Url::fromRoute('entity.workflow.edit_transition_form', ['workflow' => $workflow->id(), 'workflow_transition' => $transition->id()]),
@ -233,10 +233,10 @@ class WorkflowEditForm extends EntityForm {
$entity->set('label', $values['label']);
$entity->set('id', $values['id']);
foreach ($values['states'] as $state_id => $state_values) {
$entity->setStateWeight($state_id, $state_values['weight']);
$entity->getTypePlugin()->setStateWeight($state_id, $state_values['weight']);
}
foreach ($values['transitions'] as $transition_id => $transition_values) {
$entity->setTransitionWeight($transition_id, $transition_values['weight']);
$entity->getTypePlugin()->setTransitionWeight($transition_id, $transition_values['weight']);
}
}

View File

@ -63,7 +63,7 @@ class WorkflowStateAddForm extends EntityForm {
public function exists($state_id) {
/** @var \Drupal\workflows\WorkflowInterface $original_workflow */
$original_workflow = \Drupal::entityTypeManager()->getStorage('workflow')->loadUnchanged($this->getEntity()->id());
return $original_workflow->hasState($state_id);
return $original_workflow->getTypePlugin()->hasState($state_id);
}
/**
@ -84,8 +84,8 @@ class WorkflowStateAddForm extends EntityForm {
// Replicate the validation that Workflow::addState() does internally as the
// form values have not been validated at this point.
if (!$entity->hasState($values['id']) && !preg_match('/[^a-z0-9_]+/', $values['id'])) {
$entity->addState($values['id'], $values['label']);
if (!$entity->getTypePlugin()->hasState($values['id']) && !preg_match('/[^a-z0-9_]+/', $values['id'])) {
$entity->getTypePlugin()->addState($values['id'], $values['label']);
if (isset($values['type_settings'])) {
$configuration = $entity->getTypePlugin()->getConfiguration();
// @todo move to submitStateConfigurationForm. #2849827.
@ -103,7 +103,7 @@ class WorkflowStateAddForm extends EntityForm {
$workflow = $this->entity;
$workflow->save();
drupal_set_message($this->t('Created %label state.', [
'%label' => $workflow->getState($form_state->getValue('id'))->label(),
'%label' => $workflow->getTypePlugin()->getState($form_state->getValue('id'))->label(),
]));
$form_state->setRedirectUrl($workflow->toUrl('edit-form'));
}

View File

@ -37,7 +37,7 @@ class WorkflowStateDeleteForm extends ConfirmFormBase {
* {@inheritdoc}
*/
public function getQuestion() {
return $this->t('Are you sure you want to delete %state from %workflow?', ['%state' => $this->workflow->getState($this->stateId)->label(), '%workflow' => $this->workflow->label()]);
return $this->t('Are you sure you want to delete %state from %workflow?', ['%state' => $this->workflow->getTypePlugin()->getState($this->stateId)->label(), '%workflow' => $this->workflow->label()]);
}
/**
@ -70,13 +70,13 @@ class WorkflowStateDeleteForm extends ConfirmFormBase {
* The form structure.
*/
public function buildForm(array $form, FormStateInterface $form_state, WorkflowInterface $workflow = NULL, $workflow_state = NULL) {
if (!$workflow->hasState($workflow_state)) {
if (!$workflow->getTypePlugin()->hasState($workflow_state)) {
throw new NotFoundHttpException();
}
$this->workflow = $workflow;
$this->stateId = $workflow_state;
if ($this->workflow->getTypePlugin()->workflowStateHasData($this->workflow, $this->workflow->getState($this->stateId))) {
if ($this->workflow->getTypePlugin()->workflowStateHasData($this->workflow, $this->workflow->getTypePlugin()->getState($this->stateId))) {
$form['#title'] = $this->getQuestion();
$form['description'] = ['#markup' => $this->t('This workflow state is in use. You cannot remove this workflow state until you have removed all content using it.')];
return $form;
@ -90,10 +90,11 @@ class WorkflowStateDeleteForm extends ConfirmFormBase {
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$workflow_label = $this->workflow->getState($this->stateId)->label();
$workflow_label = $this->workflow->getTypePlugin()->getState($this->stateId)->label();
$this->workflow
->deleteState($this->stateId)
->save();
->getTypePlugin()
->deleteState($this->stateId);
$this->workflow->save();
drupal_set_message($this->t(
'State %label deleted.',

View File

@ -42,7 +42,7 @@ class WorkflowStateEditForm extends EntityForm {
/* @var \Drupal\workflows\WorkflowInterface $workflow */
$workflow = $this->getEntity();
$state = $workflow->getState($this->stateId);
$state = $workflow->getTypePlugin()->getState($this->stateId);
$form['label'] = [
'#type' => 'textfield',
'#title' => $this->t('Label'),
@ -124,7 +124,7 @@ class WorkflowStateEditForm extends EntityForm {
protected function copyFormValuesToEntity(EntityInterface $entity, array $form, FormStateInterface $form_state) {
/** @var \Drupal\workflows\WorkflowInterface $entity */
$values = $form_state->getValues();
$entity->setStateLabel($values['id'], $values['label']);
$entity->getTypePlugin()->setStateLabel($values['id'], $values['label']);
if (isset($values['type_settings'])) {
$configuration = $entity->getTypePlugin()->getConfiguration();
$configuration['states'][$values['id']] += $values['type_settings'][$entity->getTypePlugin()->getPluginId()];
@ -140,7 +140,7 @@ class WorkflowStateEditForm extends EntityForm {
$workflow = $this->entity;
$workflow->save();
drupal_set_message($this->t('Saved %label state.', [
'%label' => $workflow->getState($this->stateId)->label(),
'%label' => $workflow->getTypePlugin()->getState($this->stateId)->label(),
]));
$form_state->setRedirectUrl($workflow->toUrl('edit-form'));
}

View File

@ -45,7 +45,7 @@ class WorkflowTransitionAddForm extends EntityForm {
// @todo https://www.drupal.org/node/2830584 Add some ajax to ensure that
// only valid transitions are selectable.
$states = array_map([State::class, 'labelCallback'], $workflow->getStates());
$states = array_map([State::class, 'labelCallback'], $workflow->getTypePlugin()->getStates());
$form['from'] = [
'#type' => 'checkboxes',
'#title' => $this->t('From'),
@ -82,7 +82,7 @@ class WorkflowTransitionAddForm extends EntityForm {
public function exists($transition_id) {
/** @var \Drupal\workflows\WorkflowInterface $original_workflow */
$original_workflow = \Drupal::entityTypeManager()->getStorage('workflow')->loadUnchanged($this->getEntity()->id());
return $original_workflow->hasTransition($transition_id);
return $original_workflow->getTypePlugin()->hasTransition($transition_id);
}
/**
@ -104,7 +104,7 @@ class WorkflowTransitionAddForm extends EntityForm {
}
/** @var \Drupal\workflows\WorkflowInterface $entity */
$values = $form_state->getValues();
$entity->addTransition($values['id'], $values['label'], array_filter($values['from']), $values['to']);
$entity->getTypePlugin()->addTransition($values['id'], $values['label'], array_filter($values['from']), $values['to']);
if (isset($values['type_settings'])) {
$configuration = $entity->getTypePlugin()->getConfiguration();
$configuration['transitions'][$values['id']] += $values['type_settings'][$entity->getTypePlugin()->getPluginId()];
@ -121,10 +121,10 @@ class WorkflowTransitionAddForm extends EntityForm {
$workflow = $this->getEntity();
$values = $form_state->getValues();
foreach (array_filter($values['from']) as $from_state_id) {
if ($workflow->hasTransitionFromStateToState($from_state_id, $values['to'])) {
if ($workflow->getTypePlugin()->hasTransitionFromStateToState($from_state_id, $values['to'])) {
$form_state->setErrorByName('from][' . $from_state_id, $this->t('The transition from %from to %to already exists.', [
'%from' => $workflow->getState($from_state_id)->label(),
'%to' => $workflow->getState($values['to'])->label(),
'%from' => $workflow->getTypePlugin()->getState($from_state_id)->label(),
'%to' => $workflow->getTypePlugin()->getState($values['to'])->label(),
]));
}
}

View File

@ -78,7 +78,7 @@ class WorkflowTransitionDeleteForm extends ConfirmFormBase {
*/
public function buildForm(array $form, FormStateInterface $form_state, WorkflowInterface $workflow = NULL, $workflow_transition = NULL) {
try {
$this->transition = $workflow->getTransition($workflow_transition);
$this->transition = $workflow->getTypePlugin()->getTransition($workflow_transition);
}
catch (\InvalidArgumentException $e) {
throw new NotFoundHttpException();
@ -92,8 +92,9 @@ class WorkflowTransitionDeleteForm extends ConfirmFormBase {
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->workflow
->deleteTransition($this->transition->id())
->save();
->getTypePlugin()
->deleteTransition($this->transition->id());
$this->workflow->save();
drupal_set_message($this->t('%transition transition deleted.', ['%transition' => $this->transition->label()]));
$form_state->setRedirectUrl($this->getCancelUrl());

View File

@ -43,7 +43,7 @@ class WorkflowTransitionEditForm extends EntityForm {
/* @var \Drupal\workflows\WorkflowInterface $workflow */
$workflow = $this->getEntity();
$transition = $workflow->getTransition($this->transitionId);
$transition = $workflow->getTypePlugin()->getTransition($this->transitionId);
$form['label'] = [
'#type' => 'textfield',
'#title' => $this->t('Label'),
@ -60,7 +60,7 @@ class WorkflowTransitionEditForm extends EntityForm {
// @todo https://www.drupal.org/node/2830584 Add some ajax to ensure that
// only valid transitions are selectable.
$states = array_map([State::class, 'labelCallback'], $workflow->getStates());
$states = array_map([State::class, 'labelCallback'], $workflow->getTypePlugin()->getStates());
$form['from'] = [
'#type' => 'checkboxes',
'#title' => $this->t('From'),
@ -94,12 +94,12 @@ class WorkflowTransitionEditForm extends EntityForm {
$workflow = $this->getEntity();
$values = $form_state->getValues();
foreach (array_filter($values['from']) as $from_state_id) {
if ($workflow->hasTransitionFromStateToState($from_state_id, $values['to'])) {
$transition = $workflow->getTransitionFromStateToState($from_state_id, $values['to']);
if ($workflow->getTypePlugin()->hasTransitionFromStateToState($from_state_id, $values['to'])) {
$transition = $workflow->getTypePlugin()->getTransitionFromStateToState($from_state_id, $values['to']);
if ($transition->id() !== $values['id']) {
$form_state->setErrorByName('from][' . $from_state_id, $this->t('The transition from %from to %to already exists.', [
'%from' => $workflow->getState($from_state_id)->label(),
'%to' => $workflow->getState($values['to'])->label(),
'%from' => $workflow->getTypePlugin()->getState($from_state_id)->label(),
'%to' => $workflow->getTypePlugin()->getState($values['to'])->label(),
]));
}
}
@ -126,8 +126,8 @@ class WorkflowTransitionEditForm extends EntityForm {
/** @var \Drupal\workflows\WorkflowInterface $entity */
$values = $form_state->getValues();
$form_state->set('created_transition', FALSE);
$entity->setTransitionLabel($values['id'], $values['label']);
$entity->setTransitionFromStates($values['id'], array_filter($values['from']));
$entity->getTypePlugin()->setTransitionLabel($values['id'], $values['label']);
$entity->getTypePlugin()->setTransitionFromStates($values['id'], array_filter($values['from']));
if (isset($values['type_settings'])) {
$configuration = $entity->getTypePlugin()->getConfiguration();
$configuration['transitions'][$values['id']] += $values['type_settings'][$entity->getTypePlugin()->getPluginId()];
@ -143,7 +143,7 @@ class WorkflowTransitionEditForm extends EntityForm {
$workflow = $this->entity;
$workflow->save();
drupal_set_message($this->t('Saved %label transition.', [
'%label' => $workflow->getTransition($this->transitionId)->label(),
'%label' => $workflow->getTypePlugin()->getTransition($this->transitionId)->label(),
]));
$form_state->setRedirectUrl($workflow->toUrl('edit-form'));
}

View File

@ -152,7 +152,7 @@ abstract class WorkflowTypeBase extends PluginBase implements WorkflowTypeInterf
* {@inheritdoc}
*/
public function getInitialState(WorkflowInterface $workflow) {
$ordered_states = $workflow->getStates();
$ordered_states = $this->getStates();
return reset($ordered_states);
}

View File

@ -62,7 +62,7 @@ class WorkflowAccessControlHandler extends EntityAccessControlHandler implements
list(, $state_id) = explode(':', $operation, 2);
// Deleting a state is editing a workflow, but also we should forbid
// access if there is only one state.
$admin_access = AccessResult::allowedIf(count($entity->getStates()) > 1)
$admin_access = AccessResult::allowedIf(count($entity->getTypePlugin()->getStates()) > 1)
->andIf(parent::checkAccess($entity, 'edit', $account))
->andIf(AccessResult::allowedIf(!in_array($state_id, $workflow_type->getRequiredStates(), TRUE)))
->addCacheableDependency($entity);

View File

@ -13,309 +13,6 @@ use Drupal\Core\Config\Entity\ConfigEntityInterface;
*/
interface WorkflowInterface extends ConfigEntityInterface {
/**
* Adds a state to the workflow.
*
* @param string $state_id
* The state's ID.
* @param string $label
* The state's label.
*
* @return \Drupal\workflows\WorkflowInterface
* The workflow entity.
*
* @deprecated in Drupal 8.4.x and will be removed before 9.0.0.
*/
public function addState($state_id, $label);
/**
* Determines if the workflow has a state with the provided ID.
*
* @param string $state_id
* The state's ID.
*
* @return bool
* TRUE if the workflow has a state with the provided ID, FALSE if not.
*
* @deprecated in Drupal 8.4.x and will be removed before 9.0.0.
*/
public function hasState($state_id);
/**
* Gets state objects for the provided state IDs.
*
* @param string[] $state_ids
* A list of state IDs to get. If NULL then all states will be returned.
*
* @return \Drupal\workflows\StateInterface[]
* An array of workflow states.
*
* @throws \InvalidArgumentException
* Thrown if $state_ids contains a state ID that does not exist.
*
* @deprecated in Drupal 8.4.x and will be removed before 9.0.0.
*/
public function getStates($state_ids = NULL);
/**
* Gets a workflow state.
*
* @param string $state_id
* The state's ID.
*
* @return \Drupal\workflows\StateInterface
* The workflow state.
*
* @throws \InvalidArgumentException
* Thrown if $state_id does not exist.
*
* @deprecated in Drupal 8.4.x and will be removed before 9.0.0.
*/
public function getState($state_id);
/**
* Sets a state's label.
*
* @param string $state_id
* The state ID to set the label for.
* @param string $label
* The state's label.
*
* @return \Drupal\workflows\WorkflowInterface
* The workflow entity.
*
* @deprecated in Drupal 8.4.x and will be removed before 9.0.0.
*/
public function setStateLabel($state_id, $label);
/**
* Sets a state's weight value.
*
* @param string $state_id
* The state ID to set the weight for.
* @param int $weight
* The state's weight.
*
* @return \Drupal\workflows\WorkflowInterface
* The workflow entity.
*
* @deprecated in Drupal 8.4.x and will be removed before 9.0.0.
*/
public function setStateWeight($state_id, $weight);
/**
* Deletes a state from the workflow.
*
* @param string $state_id
* The state ID to delete.
*
* @return \Drupal\workflows\WorkflowInterface
* The workflow entity.
*
* @throws \InvalidArgumentException
* Thrown if $state_id does not exist.
*
* @deprecated in Drupal 8.4.x and will be removed before 9.0.0.
*/
public function deleteState($state_id);
/**
* Gets the initial state for the workflow.
*
* @return \Drupal\workflows\StateInterface
* The initial state.
*
* @deprecated in Drupal 8.4.x and will be removed before 9.0.0.
*/
public function getInitialState();
/**
* Adds a transition to the workflow.
*
* @param string $id
* The transition ID.
* @param string $label
* The transition's label.
* @param array $from_state_ids
* The state IDs to transition from.
* @param string $to_state_id
* The state ID to transition to.
*
* @return \Drupal\workflows\WorkflowInterface
* The workflow entity.
*
* @throws \InvalidArgumentException
* Thrown if either state does not exist.
*
* @deprecated in Drupal 8.4.x and will be removed before 9.0.0.
*/
public function addTransition($id, $label, array $from_state_ids, $to_state_id);
/**
* Gets a transition object for the provided transition ID.
*
* @param string $transition_id
* A transition ID.
*
* @return \Drupal\workflows\TransitionInterface
* The transition.
*
* @throws \InvalidArgumentException
* Thrown if $transition_id does not exist.
*
* @deprecated in Drupal 8.4.x and will be removed before 9.0.0.
*/
public function getTransition($transition_id);
/**
* Determines if a transition exists.
*
* @param string $transition_id
* The transition ID.
*
* @return bool
* TRUE if the transition exists, FALSE if not.
*
* @deprecated in Drupal 8.4.x and will be removed before 9.0.0.
*/
public function hasTransition($transition_id);
/**
* Gets transition objects for the provided transition IDs.
*
* @param string[] $transition_ids
* A list of transition IDs to get. If NULL then all transitions will be
* returned.
*
* @return \Drupal\workflows\TransitionInterface[]
* An array of transition objects.
*
* @throws \InvalidArgumentException
* Thrown if $transition_ids contains a transition ID that does not exist.
*
* @deprecated in Drupal 8.4.x and will be removed before 9.0.0.
*/
public function getTransitions(array $transition_ids = NULL);
/**
* Gets the transition IDs for a state for the provided direction.
*
* @param $state_id
* The state to get transitions for.
* @param string $direction
* (optional) The direction of the transition. Defaults to 'from'. Possible
* values are: 'from' and 'to'.
*
* @return array
* The transition IDs for a state for the provided direction.
*
* @deprecated in Drupal 8.4.x and will be removed before 9.0.0.
*/
public function getTransitionsForState($state_id, $direction = 'from');
/**
* Gets a transition from state to state.
*
* @param string $from_state_id
* The state ID to transition from.
* @param string $to_state_id
* The state ID to transition to.
*
* @return \Drupal\workflows\TransitionInterface
* The transitions.
*
* @throws \InvalidArgumentException
* Thrown if the transition does not exist.
*
* @deprecated in Drupal 8.4.x and will be removed before 9.0.0.
*/
public function getTransitionFromStateToState($from_state_id, $to_state_id);
/**
* Determines if a transition from state to state exists.
*
* @param string $from_state_id
* The state ID to transition from.
* @param string $to_state_id
* The state ID to transition to.
*
* @return bool
* TRUE if the transition exists, FALSE if not.
*
* @deprecated in Drupal 8.4.x and will be removed before 9.0.0.
*/
public function hasTransitionFromStateToState($from_state_id, $to_state_id);
/**
* Sets a transition's label.
*
* @param string $transition_id
* The transition ID.
* @param string $label
* The transition's label.
*
* @return \Drupal\workflows\WorkflowInterface
* The workflow entity.
*
* @throws \InvalidArgumentException
* Thrown if the transition does not exist.
*
* @deprecated in Drupal 8.4.x and will be removed before 9.0.0.
*/
public function setTransitionLabel($transition_id, $label);
/**
* Sets a transition's weight.
*
* @param string $transition_id
* The transition ID.
* @param int $weight
* The transition's weight.
*
* @return \Drupal\workflows\WorkflowInterface
* The workflow entity.
*
* @throws \InvalidArgumentException
* Thrown if the transition does not exist.
*
* @deprecated in Drupal 8.4.x and will be removed before 9.0.0.
*/
public function setTransitionWeight($transition_id, $weight);
/**
* Sets a transition's from states.
*
* @param string $transition_id
* The transition ID.
* @param array $from_state_ids
* The state IDs to transition from.
*
* @return \Drupal\workflows\WorkflowInterface
* The workflow entity.
*
* @throws \InvalidArgumentException
* Thrown if the transition does not exist or the states do not exist.
*
* @deprecated in Drupal 8.4.x and will be removed before 9.0.0.
*/
public function setTransitionFromStates($transition_id, array $from_state_ids);
/**
* Deletes a transition.
*
* @param string $transition_id
* The transition ID.
*
* @return \Drupal\workflows\WorkflowInterface
* The workflow entity.
*
* @throws \InvalidArgumentException
* Thrown if the transition does not exist.
*
* @deprecated in Drupal 8.4.x and will be removed before 9.0.0.
*/
public function deleteTransition($transition_id);
/**
* Gets the workflow type plugin.
*

View File

@ -76,7 +76,7 @@ class WorkflowListBuilder extends ConfigEntityListBuilder {
'#markup' => $entity->getTypePlugin()->label()
];
$items = array_map([State::class, 'labelCallback'], $entity->getStates());
$items = array_map([State::class, 'labelCallback'], $entity->getTypePlugin()->getStates());
$row['states']['data'] = [
'#theme' => 'item_list',
'#context' => ['list_style' => 'comma-list'],

View File

@ -27,6 +27,7 @@ class RequiredStateTestType extends WorkflowTypeBase {
*/
public function initializeWorkflow(WorkflowInterface $workflow) {
$workflow
->getTypePlugin()
->addState('fresh', $this->t('Fresh'))
->setStateWeight('fresh', -5)
->addState('rotten', $this->t('Rotten'))

View File

@ -52,6 +52,7 @@ abstract class WorkflowResourceTestBase extends EntityResourceTestBase {
'type' => 'workflow_type_complex_test',
]);
$workflow
->getTypePlugin()
->addState('draft', 'Draft')
->addState('published', 'Published');
$configuration = $workflow->getTypePlugin()->getConfiguration();

View File

@ -36,10 +36,11 @@ class WorkflowUiTest extends BrowserTestBase {
// Create a minimal workflow for testing.
$workflow = Workflow::create(['id' => 'test', 'type' => 'workflow_type_test']);
$workflow
->getTypePlugin()
->addState('draft', 'Draft')
->addState('published', 'Published')
->addTransition('publish', 'Publish', ['draft', 'published'], 'published')
->save();
->addTransition('publish', 'Publish', ['draft', 'published'], 'published');
$workflow->save();
$paths = [
'admin/config/workflow/workflows',
@ -120,26 +121,26 @@ class WorkflowUiTest extends BrowserTestBase {
$this->submitForm(['label' => 'Published', 'id' => 'published'], 'Save');
$this->assertSession()->pageTextContains('Created Published state.');
$workflow = $workflow_storage->loadUnchanged('test');
$this->assertFalse($workflow->getState('published')->canTransitionTo('published'), 'No default transition from published to published exists.');
$this->assertFalse($workflow->getTypePlugin()->getState('published')->canTransitionTo('published'), 'No default transition from published to published exists.');
$this->clickLink('Add a new state');
// Don't create a draft to draft transition by default.
$this->submitForm(['label' => 'Draft', 'id' => 'draft'], 'Save');
$this->assertSession()->pageTextContains('Created Draft state.');
$workflow = $workflow_storage->loadUnchanged('test');
$this->assertFalse($workflow->getState('draft')->canTransitionTo('draft'), 'Can not transition from draft to draft');
$this->assertFalse($workflow->getTypePlugin()->getState('draft')->canTransitionTo('draft'), 'Can not transition from draft to draft');
$this->clickLink('Add a new transition');
$this->submitForm(['id' => 'publish', 'label' => 'Publish', 'from[draft]' => 'draft', 'to' => 'published'], 'Save');
$this->assertSession()->pageTextContains('Created Publish transition.');
$workflow = $workflow_storage->loadUnchanged('test');
$this->assertTrue($workflow->getState('draft')->canTransitionTo('published'), 'Can transition from draft to published');
$this->assertTrue($workflow->getTypePlugin()->getState('draft')->canTransitionTo('published'), 'Can transition from draft to published');
$this->clickLink('Add a new transition');
$this->submitForm(['id' => 'create_new_draft', 'label' => 'Create new draft', 'from[draft]' => 'draft', 'to' => 'draft'], 'Save');
$this->assertSession()->pageTextContains('Created Create new draft transition.');
$workflow = $workflow_storage->loadUnchanged('test');
$this->assertTrue($workflow->getState('draft')->canTransitionTo('draft'), 'Can transition from draft to draft');
$this->assertTrue($workflow->getTypePlugin()->getState('draft')->canTransitionTo('draft'), 'Can transition from draft to draft');
// The fist state to edit on the page should be published.
$this->clickLink('Edit');
@ -153,7 +154,7 @@ class WorkflowUiTest extends BrowserTestBase {
$this->submitForm(['from[published]' => 'published'], 'Save');
$this->assertSession()->pageTextContains('Saved Create new draft transition.');
$workflow = $workflow_storage->loadUnchanged('test');
$this->assertTrue($workflow->getState('published')->canTransitionTo('draft'), 'Can transition from published to draft');
$this->assertTrue($workflow->getTypePlugin()->getState('published')->canTransitionTo('draft'), 'Can transition from published to draft');
// Try creating a duplicate transition.
$this->clickLink('Add a new transition');
@ -173,12 +174,12 @@ class WorkflowUiTest extends BrowserTestBase {
// Delete the transition.
$workflow = $workflow_storage->loadUnchanged('test');
$this->assertTrue($workflow->hasTransitionFromStateToState('published', 'published'), 'Can transition from published to published');
$this->assertTrue($workflow->getTypePlugin()->hasTransitionFromStateToState('published', 'published'), 'Can transition from published to published');
$this->clickLink('Delete');
$this->assertSession()->pageTextContains('Are you sure you want to delete Save and publish from Test?');
$this->submitForm([], 'Delete');
$workflow = $workflow_storage->loadUnchanged('test');
$this->assertFalse($workflow->hasTransitionFromStateToState('published', 'published'), 'Cannot transition from published to published');
$this->assertFalse($workflow->getTypePlugin()->hasTransitionFromStateToState('published', 'published'), 'Cannot transition from published to published');
// Try creating a duplicate state.
$this->drupalGet('admin/config/workflow/workflows/manage/test');
@ -198,11 +199,11 @@ class WorkflowUiTest extends BrowserTestBase {
$this->assertSession()->addressEquals('admin/config/workflow/workflows/manage/test');
// Ensure that weight changes the transition ordering.
$this->assertEquals(['publish', 'create_new_draft'], array_keys($workflow->getTransitions()));
$this->assertEquals(['publish', 'create_new_draft'], array_keys($workflow->getTypePlugin()->getTransitions()));
$this->drupalGet('admin/config/workflow/workflows/manage/test');
$this->submitForm(['transitions[create_new_draft][weight]' => '-1'], 'Save');
$workflow = $workflow_storage->loadUnchanged('test');
$this->assertEquals(['create_new_draft', 'publish'], array_keys($workflow->getTransitions()));
$this->assertEquals(['create_new_draft', 'publish'], array_keys($workflow->getTypePlugin()->getTransitions()));
// Verify that we are still on the workflow edit page.
$this->assertSession()->addressEquals('admin/config/workflow/workflows/manage/test');
@ -240,8 +241,8 @@ class WorkflowUiTest extends BrowserTestBase {
$this->submitForm([], 'Delete');
$this->assertSession()->pageTextContains('State Draft deleted.');
$workflow = $workflow_storage->loadUnchanged('test');
$this->assertFalse($workflow->hasState('draft'), 'Draft state deleted');
$this->assertTrue($workflow->hasState('published'), 'Workflow still has published state');
$this->assertFalse($workflow->getTypePlugin()->hasState('draft'), 'Draft state deleted');
$this->assertTrue($workflow->getTypePlugin()->hasState('published'), 'Workflow still has published state');
// The last state cannot be deleted so the only delete link on the page will
// be for the workflow.
@ -261,9 +262,9 @@ class WorkflowUiTest extends BrowserTestBase {
$this->submitForm(['label' => 'Test 2', 'id' => 'test2', 'workflow_type' => 'workflow_type_required_state_test'], 'Save');
$this->assertSession()->addressEquals('admin/config/workflow/workflows/manage/test2');
$workflow = $workflow_storage->loadUnchanged('test2');
$this->assertTrue($workflow->hasState('fresh'), 'The workflow has the "fresh" state');
$this->assertTrue($workflow->hasState('rotten'), 'The workflow has the "rotten" state');
$this->assertTrue($workflow->hasTransition('rot'), 'The workflow has the "rot" transition');
$this->assertTrue($workflow->getTypePlugin()->hasState('fresh'), 'The workflow has the "fresh" state');
$this->assertTrue($workflow->getTypePlugin()->hasState('rotten'), 'The workflow has the "rotten" state');
$this->assertTrue($workflow->getTypePlugin()->hasTransition('rot'), 'The workflow has the "rot" transition');
$this->assertSession()->pageTextContains('Fresh');
$this->assertSession()->pageTextContains('Rotten');
}
@ -274,9 +275,10 @@ class WorkflowUiTest extends BrowserTestBase {
public function testWorkflowConfigurationForm() {
$workflow = Workflow::create(['id' => 'test', 'type' => 'workflow_type_complex_test', 'label' => 'Test']);
$workflow
->getTypePlugin()
->addState('published', 'Published')
->addTransition('publish', 'Publish', ['published'], 'published')
->save();
->addTransition('publish', 'Publish', ['published'], 'published');
$workflow->save();
$this->drupalLogin($this->createUser(['administer workflows']));
@ -297,12 +299,13 @@ class WorkflowUiTest extends BrowserTestBase {
// Create a minimal workflow for testing.
$workflow = Workflow::create(['id' => 'test', 'type' => 'workflow_type_complex_test']);
$workflow
->getTypePlugin()
->addState('published', 'Published')
->addTransition('publish', 'Publish', ['published'], 'published')
->save();
->addTransition('publish', 'Publish', ['published'], 'published');
$workflow->save();
$this->assertEquals('', $workflow->getState('published')->getExtra());
$this->assertEquals('', $workflow->getTransition('publish')->getExtra());
$this->assertEquals('', $workflow->getTypePlugin()->getState('published')->getExtra());
$this->assertEquals('', $workflow->getTypePlugin()->getTransition('publish')->getExtra());
$this->drupalLogin($this->createUser(['administer workflows']));
@ -319,8 +322,8 @@ class WorkflowUiTest extends BrowserTestBase {
$workflow_storage = $this->container->get('entity_type.manager')->getStorage('workflow');
/** @var \Drupal\workflows\WorkflowInterface $workflow */
$workflow = $workflow_storage->loadUnchanged('test');
$this->assertEquals('Extra state information', $workflow->getState('published')->getExtra());
$this->assertEquals('Extra transition information', $workflow->getTransition('publish')->getExtra());
$this->assertEquals('Extra state information', $workflow->getTypePlugin()->getState('published')->getExtra());
$this->assertEquals('Extra transition information', $workflow->getTypePlugin()->getTransition('publish')->getExtra());
// Add additional state information when adding.
$this->drupalGet('admin/config/workflow/workflows/manage/test/add_state');
@ -333,8 +336,8 @@ class WorkflowUiTest extends BrowserTestBase {
$this->submitForm(['id' => 'draft_published', 'label' => 'Publish', 'from[draft]' => 'draft', 'to' => 'published', 'type_settings[workflow_type_complex_test][extra]' => 'Extra transition information on add'], 'Save');
$workflow = $workflow_storage->loadUnchanged('test');
$this->assertEquals('Extra state information on add', $workflow->getState('draft')->getExtra());
$this->assertEquals('Extra transition information on add', $workflow->getTransition('draft_published')->getExtra());
$this->assertEquals('Extra state information on add', $workflow->getTypePlugin()->getState('draft')->getExtra());
$this->assertEquals('Extra transition information on add', $workflow->getTypePlugin()->getTransition('draft_published')->getExtra());
}
}

View File

@ -10,7 +10,7 @@ use Drupal\workflow_type_test\DecoratedTransition;
/**
* Workflow entity tests that require modules or storage.
*
* @coversDefaultClass \Drupal\workflows\Entity\Workflow
* @coversDefaultClass \Drupal\workflow_type_test\Plugin\WorkflowType\ComplexTestType
*
* @group workflows
*/
@ -30,14 +30,15 @@ class ComplexWorkflowTypeTest extends KernelTestBase {
public function testComplexType() {
$workflow = new Workflow(['id' => 'test', 'type' => 'workflow_type_complex_test'], 'workflow');
$workflow
->getTypePlugin()
->addState('draft', 'Draft')
->addTransition('create_new_draft', 'Create new draft', ['draft'], 'draft');
$this->assertInstanceOf(DecoratedState::class, $workflow->getState('draft'));
$this->assertInstanceOf(DecoratedTransition::class, $workflow->getTransition('create_new_draft'));
$this->assertInstanceOf(DecoratedState::class, $workflow->getTypePlugin()->getState('draft'));
$this->assertInstanceOf(DecoratedTransition::class, $workflow->getTypePlugin()->getTransition('create_new_draft'));
}
/**
* @covers ::loadMultipleByType
* @covers \Drupal\workflows\Entity\Workflow::loadMultipleByType
*/
public function testLoadMultipleByType() {
$workflow1 = new Workflow(['id' => 'test1', 'type' => 'workflow_type_complex_test'], 'workflow');

View File

@ -30,15 +30,16 @@ class RequiredStatesTest extends KernelTestBase {
'id' => 'test',
'type' => 'workflow_type_required_state_test',
], 'workflow');
/** @var \Drupal\workflows\WorkflowInterface $workflow */
$workflow = $workflow->getTypePlugin()->initializeWorkflow($workflow);
$workflow->save();
$this->assertEquals(['fresh', 'rotten'], $workflow->getTypePlugin()
->getRequiredStates());
// Ensure that the workflow has the default configuration.
$this->assertTrue($workflow->hasState('rotten'));
$this->assertTrue($workflow->hasState('fresh'));
$this->assertTrue($workflow->hasTransitionFromStateToState('fresh', 'rotten'));
$this->assertTrue($workflow->getTypePlugin()->hasState('rotten'));
$this->assertTrue($workflow->getTypePlugin()->hasState('fresh'));
$this->assertTrue($workflow->getTypePlugin()->hasTransitionFromStateToState('fresh', 'rotten'));
}
/**
@ -49,11 +50,13 @@ class RequiredStatesTest extends KernelTestBase {
'id' => 'test',
'type' => 'workflow_type_required_state_test',
], 'workflow');
/** @var \Drupal\workflows\WorkflowInterface $workflow */
$workflow = $workflow->getTypePlugin()->initializeWorkflow($workflow);
$workflow->save();
// Ensure that required states can't be deleted.
$this->setExpectedException(RequiredStateMissingException::class, "Required State Type Test' requires states with the ID 'fresh' in workflow 'test'");
$workflow->deleteState('fresh')->save();
$workflow->getTypePlugin()->deleteState('fresh');
$workflow->save();
}
/**
@ -76,32 +79,37 @@ class RequiredStatesTest extends KernelTestBase {
'id' => 'test',
'type' => 'workflow_type_required_state_test',
], 'workflow');
/** @var \Drupal\workflows\WorkflowInterface $workflow */
$workflow = $workflow->getTypePlugin()->initializeWorkflow($workflow);
$workflow->save();
// Ensure states added by default configuration can be changed.
$this->assertEquals('Fresh', $workflow->getState('fresh')->label());
$this->assertEquals('Fresh', $workflow->getTypePlugin()->getState('fresh')->label());
$workflow
->setStateLabel('fresh', 'Fresher')
->save();
$this->assertEquals('Fresher', $workflow->getState('fresh')->label());
->getTypePlugin()
->setStateLabel('fresh', 'Fresher');
$workflow->save();
$this->assertEquals('Fresher', $workflow->getTypePlugin()->getState('fresh')->label());
// Ensure transitions can be altered.
$workflow
->getTypePlugin()
->addState('cooked', 'Cooked')
->setTransitionFromStates('rot', ['fresh', 'cooked'])
->save();
$this->assertTrue($workflow->hasTransitionFromStateToState('fresh', 'rotten'));
$this->assertTrue($workflow->hasTransitionFromStateToState('cooked', 'rotten'));
->setTransitionFromStates('rot', ['fresh', 'cooked']);
$workflow->save();
$this->assertTrue($workflow->getTypePlugin()->hasTransitionFromStateToState('fresh', 'rotten'));
$this->assertTrue($workflow->getTypePlugin()->hasTransitionFromStateToState('cooked', 'rotten'));
$workflow
->setTransitionFromStates('rot', ['cooked'])
->save();
$this->assertFalse($workflow->hasTransitionFromStateToState('fresh', 'rotten'));
$this->assertTrue($workflow->hasTransitionFromStateToState('cooked', 'rotten'));
->getTypePlugin()
->setTransitionFromStates('rot', ['cooked']);
$workflow->save();
$this->assertFalse($workflow->getTypePlugin()->hasTransitionFromStateToState('fresh', 'rotten'));
$this->assertTrue($workflow->getTypePlugin()->hasTransitionFromStateToState('cooked', 'rotten'));
// Ensure the default configuration does not cause ordering issues.
$workflow->addTransition('cook', 'Cook', ['fresh'], 'cooked')->save();
$workflow->getTypePlugin()->addTransition('cook', 'Cook', ['fresh'], 'cooked');
$workflow->save();
$this->assertSame([
'cooked',
'fresh',
@ -113,8 +121,9 @@ class RequiredStatesTest extends KernelTestBase {
], array_keys($workflow->getTypePlugin()->getConfiguration()['transitions']));
// Ensure that transitions can be deleted.
$workflow->deleteTransition('rot')->save();
$this->assertFalse($workflow->hasTransition('rot'));
$workflow->getTypePlugin()->deleteTransition('rot');
$workflow->save();
$this->assertFalse($workflow->getTypePlugin()->hasTransition('rot'));
}
}

View File

@ -33,6 +33,7 @@ class WorkflowDependenciesTest extends KernelTestBase {
// \Drupal\workflows\Entity\Workflow::onDependencyRemoval() works as
// expected.
\Drupal::service('module_installer')->uninstall(['node', 'workflow_third_party_settings_test']);
/** @var \Drupal\workflows\WorkflowInterface $workflow */
$workflow = \Drupal::entityTypeManager()->getStorage('workflow')->loadUnchanged($workflow->id());
$this->assertSame(['workflow_type_test'], $workflow->getDependencies()['module']);
}

View File

@ -12,7 +12,7 @@ use Drupal\workflows\WorkflowTypeManager;
use Prophecy\Argument;
/**
* @coversDefaultClass \Drupal\workflows\Entity\Workflow
* @coversDefaultClass \Drupal\workflows\Plugin\WorkflowTypeBase
*
* @group workflows
*/
@ -38,20 +38,20 @@ class WorkflowTest extends UnitTestCase {
*/
public function testAddAndHasState() {
$workflow = new Workflow(['id' => 'test', 'type' => 'test_type'], 'workflow');
$this->assertFalse($workflow->hasState('draft'));
$this->assertFalse($workflow->getTypePlugin()->hasState('draft'));
// By default states are ordered in the order added.
$workflow->addState('draft', 'Draft');
$this->assertTrue($workflow->hasState('draft'));
$this->assertFalse($workflow->hasState('published'));
$this->assertEquals(0, $workflow->getState('draft')->weight());
$workflow->getTypePlugin()->addState('draft', 'Draft');
$this->assertTrue($workflow->getTypePlugin()->hasState('draft'));
$this->assertFalse($workflow->getTypePlugin()->hasState('published'));
$this->assertEquals(0, $workflow->getTypePlugin()->getState('draft')->weight());
// Adding a state does not set up a transition to itself.
$this->assertFalse($workflow->hasTransitionFromStateToState('draft', 'draft'));
$this->assertFalse($workflow->getTypePlugin()->hasTransitionFromStateToState('draft', 'draft'));
// New states are added with a new weight 1 more than the current highest
// weight.
$workflow->addState('published', 'Published');
$this->assertEquals(1, $workflow->getState('published')->weight());
$workflow->getTypePlugin()->addState('published', 'Published');
$this->assertEquals(1, $workflow->getTypePlugin()->getState('published')->weight());
}
/**
@ -60,8 +60,8 @@ class WorkflowTest extends UnitTestCase {
public function testAddStateException() {
$this->setExpectedException(\InvalidArgumentException::class, "The state 'draft' already exists in workflow.");
$workflow = new Workflow(['id' => 'test', 'type' => 'test_type'], 'workflow');
$workflow->addState('draft', 'Draft');
$workflow->addState('draft', 'Draft');
$workflow->getTypePlugin()->addState('draft', 'Draft');
$workflow->getTypePlugin()->addState('draft', 'Draft');
}
/**
@ -70,7 +70,7 @@ class WorkflowTest extends UnitTestCase {
public function testAddStateInvalidIdException() {
$this->setExpectedException(\InvalidArgumentException::class, "The state ID 'draft-draft' must contain only lowercase letters, numbers, and underscores");
$workflow = new Workflow(['id' => 'test', 'type' => 'test_type'], 'workflow');
$workflow->addState('draft-draft', 'Draft');
$workflow->getTypePlugin()->addState('draft-draft', 'Draft');
}
/**
@ -80,10 +80,11 @@ class WorkflowTest extends UnitTestCase {
$workflow = new Workflow(['id' => 'test', 'type' => 'test_type'], 'workflow');
// Getting states works when there are none.
$this->assertArrayEquals([], array_keys($workflow->getStates()));
$this->assertArrayEquals([], array_keys($workflow->getStates([])));
$this->assertArrayEquals([], array_keys($workflow->getTypePlugin()->getStates()));
$this->assertArrayEquals([], array_keys($workflow->getTypePlugin()->getStates([])));
$workflow
->getTypePlugin()
->addState('draft', 'Draft')
->addState('published', 'Published')
->addState('archived', 'Archived');
@ -96,24 +97,24 @@ class WorkflowTest extends UnitTestCase {
], array_keys($workflow->getTypePlugin()->getConfiguration()['states']));
// Ensure we're returning state objects.
$this->assertInstanceOf(State::class, $workflow->getStates()['draft']);
$this->assertInstanceOf(State::class, $workflow->getTypePlugin()->getStates()['draft']);
// Passing in no IDs returns all states.
$this->assertArrayEquals(['draft', 'published', 'archived'], array_keys($workflow->getStates()));
$this->assertArrayEquals(['draft', 'published', 'archived'], array_keys($workflow->getTypePlugin()->getStates()));
// The order of states is by weight.
$workflow->setStateWeight('published', -1);
$this->assertArrayEquals(['published', 'draft', 'archived'], array_keys($workflow->getStates()));
$workflow->getTypePlugin()->setStateWeight('published', -1);
$this->assertArrayEquals(['published', 'draft', 'archived'], array_keys($workflow->getTypePlugin()->getStates()));
// The label is also used for sorting if weights are equal.
$workflow->setStateWeight('archived', 0);
$this->assertArrayEquals(['published', 'archived', 'draft'], array_keys($workflow->getStates()));
$workflow->getTypePlugin()->setStateWeight('archived', 0);
$this->assertArrayEquals(['published', 'archived', 'draft'], array_keys($workflow->getTypePlugin()->getStates()));
// You can limit the states returned by passing in states IDs.
$this->assertArrayEquals(['archived', 'draft'], array_keys($workflow->getStates(['draft', 'archived'])));
$this->assertArrayEquals(['archived', 'draft'], array_keys($workflow->getTypePlugin()->getStates(['draft', 'archived'])));
// An empty array does not load all states.
$this->assertArrayEquals([], array_keys($workflow->getStates([])));
$this->assertArrayEquals([], array_keys($workflow->getTypePlugin()->getStates([])));
}
/**
@ -122,7 +123,7 @@ class WorkflowTest extends UnitTestCase {
public function testGetStatesException() {
$this->setExpectedException(\InvalidArgumentException::class, "The state 'state_that_does_not_exist' does not exist in workflow.");
$workflow = new Workflow(['id' => 'test', 'type' => 'test_type'], 'workflow');
$workflow->getStates(['state_that_does_not_exist']);
$workflow->getTypePlugin()->getStates(['state_that_does_not_exist']);
}
/**
@ -132,6 +133,7 @@ class WorkflowTest extends UnitTestCase {
$workflow = new Workflow(['id' => 'test', 'type' => 'test_type'], 'workflow');
// By default states are ordered in the order added.
$workflow
->getTypePlugin()
->addState('draft', 'Draft')
->addState('published', 'Published')
->addState('archived', 'Archived')
@ -139,18 +141,18 @@ class WorkflowTest extends UnitTestCase {
->addTransition('publish', 'Publish', ['draft'], 'published');
// Ensure we're returning state objects and they are set up correctly
$this->assertInstanceOf(State::class, $workflow->getState('draft'));
$this->assertEquals('archived', $workflow->getState('archived')->id());
$this->assertEquals('Archived', $workflow->getState('archived')->label());
$this->assertInstanceOf(State::class, $workflow->getTypePlugin()->getState('draft'));
$this->assertEquals('archived', $workflow->getTypePlugin()->getState('archived')->id());
$this->assertEquals('Archived', $workflow->getTypePlugin()->getState('archived')->label());
$draft = $workflow->getState('draft');
$draft = $workflow->getTypePlugin()->getState('draft');
$this->assertTrue($draft->canTransitionTo('draft'));
$this->assertTrue($draft->canTransitionTo('published'));
$this->assertFalse($draft->canTransitionTo('archived'));
$this->assertEquals('Publish', $draft->getTransitionTo('published')->label());
$this->assertEquals(0, $draft->weight());
$this->assertEquals(1, $workflow->getState('published')->weight());
$this->assertEquals(2, $workflow->getState('archived')->weight());
$this->assertEquals(1, $workflow->getTypePlugin()->getState('published')->weight());
$this->assertEquals(2, $workflow->getTypePlugin()->getState('archived')->weight());
}
/**
@ -159,7 +161,7 @@ class WorkflowTest extends UnitTestCase {
public function testGetStateException() {
$this->setExpectedException(\InvalidArgumentException::class, "The state 'state_that_does_not_exist' does not exist in workflow.");
$workflow = new Workflow(['id' => 'test', 'type' => 'test_type'], 'workflow');
$workflow->getState('state_that_does_not_exist');
$workflow->getTypePlugin()->getState('state_that_does_not_exist');
}
/**
@ -167,10 +169,10 @@ class WorkflowTest extends UnitTestCase {
*/
public function testSetStateLabel() {
$workflow = new Workflow(['id' => 'test', 'type' => 'test_type'], 'workflow');
$workflow->addState('draft', 'Draft');
$this->assertEquals('Draft', $workflow->getState('draft')->label());
$workflow->setStateLabel('draft', 'Unpublished');
$this->assertEquals('Unpublished', $workflow->getState('draft')->label());
$workflow->getTypePlugin()->addState('draft', 'Draft');
$this->assertEquals('Draft', $workflow->getTypePlugin()->getState('draft')->label());
$workflow->getTypePlugin()->setStateLabel('draft', 'Unpublished');
$this->assertEquals('Unpublished', $workflow->getTypePlugin()->getState('draft')->label());
}
/**
@ -179,7 +181,7 @@ class WorkflowTest extends UnitTestCase {
public function testSetStateLabelException() {
$this->setExpectedException(\InvalidArgumentException::class, "The state 'draft' does not exist in workflow.");
$workflow = new Workflow(['id' => 'test', 'type' => 'test_type'], 'workflow');
$workflow->setStateLabel('draft', 'Draft');
$workflow->getTypePlugin()->setStateLabel('draft', 'Draft');
}
/**
@ -187,10 +189,10 @@ class WorkflowTest extends UnitTestCase {
*/
public function testSetStateWeight() {
$workflow = new Workflow(['id' => 'test', 'type' => 'test_type'], 'workflow');
$workflow->addState('draft', 'Draft');
$this->assertEquals(0, $workflow->getState('draft')->weight());
$workflow->setStateWeight('draft', -10);
$this->assertEquals(-10, $workflow->getState('draft')->weight());
$workflow->getTypePlugin()->addState('draft', 'Draft');
$this->assertEquals(0, $workflow->getTypePlugin()->getState('draft')->weight());
$workflow->getTypePlugin()->setStateWeight('draft', -10);
$this->assertEquals(-10, $workflow->getTypePlugin()->getState('draft')->weight());
}
/**
@ -199,7 +201,7 @@ class WorkflowTest extends UnitTestCase {
public function testSetStateWeightException() {
$this->setExpectedException(\InvalidArgumentException::class, "The state 'draft' does not exist in workflow.");
$workflow = new Workflow(['id' => 'test', 'type' => 'test_type'], 'workflow');
$workflow->setStateWeight('draft', 10);
$workflow->getTypePlugin()->setStateWeight('draft', 10);
}
/**
@ -226,7 +228,7 @@ class WorkflowTest extends UnitTestCase {
public function testDeleteStateException() {
$this->setExpectedException(\InvalidArgumentException::class, "The state 'draft' does not exist in workflow.");
$workflow = new Workflow(['id' => 'test', 'type' => 'test_type'], 'workflow');
$workflow->deleteState('draft');
$workflow->getTypePlugin()->deleteState('draft');
}
/**
@ -235,8 +237,8 @@ class WorkflowTest extends UnitTestCase {
public function testDeleteOnlyStateException() {
$this->setExpectedException(\InvalidArgumentException::class, "The state 'draft' can not be deleted from workflow as it is the only state");
$workflow = new Workflow(['id' => 'test', 'type' => 'test_type'], 'workflow');
$workflow->addState('draft', 'Draft');
$workflow->deleteState('draft');
$workflow->getTypePlugin()->addState('draft', 'Draft');
$workflow->getTypePlugin()->deleteState('draft');
}
/**
@ -248,18 +250,19 @@ class WorkflowTest extends UnitTestCase {
// By default states are ordered in the order added.
$workflow
->getTypePlugin()
->addState('draft', 'Draft')
->addState('published', 'Published');
$this->assertFalse($workflow->getState('draft')->canTransitionTo('published'));
$workflow->addTransition('publish', 'Publish', ['draft'], 'published');
$this->assertTrue($workflow->getState('draft')->canTransitionTo('published'));
$this->assertEquals(0, $workflow->getTransition('publish')->weight());
$this->assertTrue($workflow->hasTransition('publish'));
$this->assertFalse($workflow->hasTransition('draft'));
$this->assertFalse($workflow->getTypePlugin()->getState('draft')->canTransitionTo('published'));
$workflow->getTypePlugin()->addTransition('publish', 'Publish', ['draft'], 'published');
$this->assertTrue($workflow->getTypePlugin()->getState('draft')->canTransitionTo('published'));
$this->assertEquals(0, $workflow->getTypePlugin()->getTransition('publish')->weight());
$this->assertTrue($workflow->getTypePlugin()->hasTransition('publish'));
$this->assertFalse($workflow->getTypePlugin()->hasTransition('draft'));
$workflow->addTransition('save_publish', 'Save', ['published'], 'published');
$this->assertEquals(1, $workflow->getTransition('save_publish')->weight());
$workflow->getTypePlugin()->addTransition('save_publish', 'Save', ['published'], 'published');
$this->assertEquals(1, $workflow->getTypePlugin()->getTransition('save_publish')->weight());
}
/**
@ -268,9 +271,9 @@ class WorkflowTest extends UnitTestCase {
public function testAddTransitionDuplicateException() {
$this->setExpectedException(\InvalidArgumentException::class, "The transition 'publish' already exists in workflow.");
$workflow = new Workflow(['id' => 'test', 'type' => 'test_type'], 'workflow');
$workflow->addState('published', 'Published');
$workflow->addTransition('publish', 'Publish', ['published'], 'published');
$workflow->addTransition('publish', 'Publish', ['published'], 'published');
$workflow->getTypePlugin()->addState('published', 'Published');
$workflow->getTypePlugin()->addTransition('publish', 'Publish', ['published'], 'published');
$workflow->getTypePlugin()->addTransition('publish', 'Publish', ['published'], 'published');
}
/**
@ -279,8 +282,8 @@ class WorkflowTest extends UnitTestCase {
public function testAddTransitionInvalidIdException() {
$this->setExpectedException(\InvalidArgumentException::class, "The transition ID 'publish-publish' must contain only lowercase letters, numbers, and underscores");
$workflow = new Workflow(['id' => 'test', 'type' => 'test_type'], 'workflow');
$workflow->addState('published', 'Published');
$workflow->addTransition('publish-publish', 'Publish', ['published'], 'published');
$workflow->getTypePlugin()->addState('published', 'Published');
$workflow->getTypePlugin()->addTransition('publish-publish', 'Publish', ['published'], 'published');
}
/**
@ -289,8 +292,8 @@ class WorkflowTest extends UnitTestCase {
public function testAddTransitionMissingFromException() {
$this->setExpectedException(\InvalidArgumentException::class, "The state 'draft' does not exist in workflow.");
$workflow = new Workflow(['id' => 'test', 'type' => 'test_type'], 'workflow');
$workflow->addState('published', 'Published');
$workflow->addTransition('publish', 'Publish', ['draft'], 'published');
$workflow->getTypePlugin()->addState('published', 'Published');
$workflow->getTypePlugin()->addTransition('publish', 'Publish', ['draft'], 'published');
}
/**
@ -300,10 +303,11 @@ class WorkflowTest extends UnitTestCase {
$this->setExpectedException(\InvalidArgumentException::class, "The 'publish' transition already allows 'draft' to 'published' transitions in workflow.");
$workflow = new Workflow(['id' => 'test', 'type' => 'test_type'], 'workflow');
$workflow
->getTypePlugin()
->addState('draft', 'Draft')
->addState('published', 'Published');
$workflow->addTransition('publish', 'Publish', ['draft', 'published'], 'published');
$workflow->addTransition('draft_to_published', 'Publish a draft', ['draft'], 'published');
$workflow->getTypePlugin()->addTransition('publish', 'Publish', ['draft', 'published'], 'published');
$workflow->getTypePlugin()->addTransition('draft_to_published', 'Publish a draft', ['draft'], 'published');
}
/**
@ -311,16 +315,16 @@ class WorkflowTest extends UnitTestCase {
*/
public function testAddTransitionConsistentAfterFromCatch() {
$workflow = new Workflow(['id' => 'test', 'type' => 'test_type'], 'workflow');
$workflow->addState('published', 'Published');
$workflow->getTypePlugin()->addState('published', 'Published');
try {
$workflow->addTransition('publish', 'Publish', ['draft'], 'published');
$workflow->getTypePlugin()->addTransition('publish', 'Publish', ['draft'], 'published');
}
catch (\InvalidArgumentException $e) {
}
// Ensure that the workflow is not left in an inconsistent state after an
// exception is thrown from Workflow::setTransitionFromStates() whilst
// calling Workflow::addTransition().
$this->assertFalse($workflow->hasTransition('publish'));
$this->assertFalse($workflow->getTypePlugin()->hasTransition('publish'));
}
/**
@ -329,8 +333,8 @@ class WorkflowTest extends UnitTestCase {
public function testAddTransitionMissingToException() {
$this->setExpectedException(\InvalidArgumentException::class, "The state 'published' does not exist in workflow.");
$workflow = new Workflow(['id' => 'test', 'type' => 'test_type'], 'workflow');
$workflow->addState('draft', 'Draft');
$workflow->addTransition('publish', 'Publish', ['draft'], 'published');
$workflow->getTypePlugin()->addState('draft', 'Draft');
$workflow->getTypePlugin()->addTransition('publish', 'Publish', ['draft'], 'published');
}
/**
@ -341,11 +345,12 @@ class WorkflowTest extends UnitTestCase {
$workflow = new Workflow(['id' => 'test', 'type' => 'test_type'], 'workflow');
// Getting transitions works when there are none.
$this->assertArrayEquals([], array_keys($workflow->getTransitions()));
$this->assertArrayEquals([], array_keys($workflow->getTransitions([])));
$this->assertArrayEquals([], array_keys($workflow->getTypePlugin()->getTransitions()));
$this->assertArrayEquals([], array_keys($workflow->getTypePlugin()->getTransitions([])));
// By default states are ordered in the order added.
$workflow
->getTypePlugin()
->addState('a', 'A')
->addState('b', 'B')
->addTransition('a_b', 'A to B', ['a'], 'b')
@ -355,26 +360,26 @@ class WorkflowTest extends UnitTestCase {
$this->assertArrayEquals(['a_a', 'a_b'], array_keys($workflow->getTypePlugin()->getConfiguration()['transitions']));
// Ensure we're returning transition objects.
$this->assertInstanceOf(Transition::class, $workflow->getTransitions()['a_a']);
$this->assertInstanceOf(Transition::class, $workflow->getTypePlugin()->getTransitions()['a_a']);
// Passing in no IDs returns all transitions.
$this->assertArrayEquals(['a_b', 'a_a'], array_keys($workflow->getTransitions()));
$this->assertArrayEquals(['a_b', 'a_a'], array_keys($workflow->getTypePlugin()->getTransitions()));
// The order of states is by weight.
$workflow->setTransitionWeight('a_a', -1);
$this->assertArrayEquals(['a_a', 'a_b'], array_keys($workflow->getTransitions()));
$workflow->getTypePlugin()->setTransitionWeight('a_a', -1);
$this->assertArrayEquals(['a_a', 'a_b'], array_keys($workflow->getTypePlugin()->getTransitions()));
// If all weights are equal it will fallback to labels.
$workflow->setTransitionWeight('a_a', 0);
$this->assertArrayEquals(['a_a', 'a_b'], array_keys($workflow->getTransitions()));
$workflow->setTransitionLabel('a_b', 'A B');
$this->assertArrayEquals(['a_b', 'a_a'], array_keys($workflow->getTransitions()));
$workflow->getTypePlugin()->setTransitionWeight('a_a', 0);
$this->assertArrayEquals(['a_a', 'a_b'], array_keys($workflow->getTypePlugin()->getTransitions()));
$workflow->getTypePlugin()->setTransitionLabel('a_b', 'A B');
$this->assertArrayEquals(['a_b', 'a_a'], array_keys($workflow->getTypePlugin()->getTransitions()));
// You can limit the states returned by passing in states IDs.
$this->assertArrayEquals(['a_a'], array_keys($workflow->getTransitions(['a_a'])));
$this->assertArrayEquals(['a_a'], array_keys($workflow->getTypePlugin()->getTransitions(['a_a'])));
// An empty array does not load all states.
$this->assertArrayEquals([], array_keys($workflow->getTransitions([])));
$this->assertArrayEquals([], array_keys($workflow->getTypePlugin()->getTransitions([])));
}
@ -385,6 +390,7 @@ class WorkflowTest extends UnitTestCase {
$workflow = new Workflow(['id' => 'test', 'type' => 'test_type'], 'workflow');
// By default states are ordered in the order added.
$workflow
->getTypePlugin()
->addState('draft', 'Draft')
->addState('published', 'Published')
->addState('archived', 'Archived')
@ -392,13 +398,13 @@ class WorkflowTest extends UnitTestCase {
->addTransition('publish', 'Publish', ['draft'], 'published');
// Ensure we're returning state objects and they are set up correctly
$this->assertInstanceOf(Transition::class, $workflow->getTransition('create_new_draft'));
$this->assertEquals('publish', $workflow->getTransition('publish')->id());
$this->assertEquals('Publish', $workflow->getTransition('publish')->label());
$this->assertInstanceOf(Transition::class, $workflow->getTypePlugin()->getTransition('create_new_draft'));
$this->assertEquals('publish', $workflow->getTypePlugin()->getTransition('publish')->id());
$this->assertEquals('Publish', $workflow->getTypePlugin()->getTransition('publish')->label());
$transition = $workflow->getTransition('publish');
$this->assertEquals($workflow->getState('draft'), $transition->from()['draft']);
$this->assertEquals($workflow->getState('published'), $transition->to());
$transition = $workflow->getTypePlugin()->getTransition('publish');
$this->assertEquals($workflow->getTypePlugin()->getState('draft'), $transition->from()['draft']);
$this->assertEquals($workflow->getTypePlugin()->getState('published'), $transition->to());
}
/**
@ -407,7 +413,7 @@ class WorkflowTest extends UnitTestCase {
public function testGetTransitionException() {
$this->setExpectedException(\InvalidArgumentException::class, "The transition 'transition_that_does_not_exist' does not exist in workflow.");
$workflow = new Workflow(['id' => 'test', 'type' => 'test_type'], 'workflow');
$workflow->getTransition('transition_that_does_not_exist');
$workflow->getTypePlugin()->getTransition('transition_that_does_not_exist');
}
/**
@ -417,6 +423,7 @@ class WorkflowTest extends UnitTestCase {
$workflow = new Workflow(['id' => 'test', 'type' => 'test_type'], 'workflow');
// By default states are ordered in the order added.
$workflow
->getTypePlugin()
->addState('draft', 'Draft')
->addState('published', 'Published')
->addState('archived', 'Archived')
@ -424,12 +431,12 @@ class WorkflowTest extends UnitTestCase {
->addTransition('publish', 'Publish', ['draft', 'published'], 'published')
->addTransition('archive', 'Archive', ['published'], 'archived');
$this->assertEquals(['create_new_draft', 'publish'], array_keys($workflow->getTransitionsForState('draft')));
$this->assertEquals(['create_new_draft'], array_keys($workflow->getTransitionsForState('draft', 'to')));
$this->assertEquals(['publish', 'archive'], array_keys($workflow->getTransitionsForState('published')));
$this->assertEquals(['publish'], array_keys($workflow->getTransitionsForState('published', 'to')));
$this->assertEquals(['create_new_draft'], array_keys($workflow->getTransitionsForState('archived', 'from')));
$this->assertEquals(['archive'], array_keys($workflow->getTransitionsForState('archived', 'to')));
$this->assertEquals(['create_new_draft', 'publish'], array_keys($workflow->getTypePlugin()->getTransitionsForState('draft')));
$this->assertEquals(['create_new_draft'], array_keys($workflow->getTypePlugin()->getTransitionsForState('draft', 'to')));
$this->assertEquals(['publish', 'archive'], array_keys($workflow->getTypePlugin()->getTransitionsForState('published')));
$this->assertEquals(['publish'], array_keys($workflow->getTypePlugin()->getTransitionsForState('published', 'to')));
$this->assertEquals(['create_new_draft'], array_keys($workflow->getTypePlugin()->getTransitionsForState('archived', 'from')));
$this->assertEquals(['archive'], array_keys($workflow->getTypePlugin()->getTransitionsForState('archived', 'to')));
}
@ -441,6 +448,7 @@ class WorkflowTest extends UnitTestCase {
$workflow = new Workflow(['id' => 'test', 'type' => 'test_type'], 'workflow');
// By default states are ordered in the order added.
$workflow
->getTypePlugin()
->addState('draft', 'Draft')
->addState('published', 'Published')
->addState('archived', 'Archived')
@ -448,9 +456,9 @@ class WorkflowTest extends UnitTestCase {
->addTransition('publish', 'Publish', ['draft', 'published'], 'published')
->addTransition('archive', 'Archive', ['published'], 'archived');
$this->assertTrue($workflow->hasTransitionFromStateToState('draft', 'published'));
$this->assertFalse($workflow->hasTransitionFromStateToState('archived', 'archived'));
$transition = $workflow->getTransitionFromStateToState('published', 'archived');
$this->assertTrue($workflow->getTypePlugin()->hasTransitionFromStateToState('draft', 'published'));
$this->assertFalse($workflow->getTypePlugin()->hasTransitionFromStateToState('archived', 'archived'));
$transition = $workflow->getTypePlugin()->getTransitionFromStateToState('published', 'archived');
$this->assertEquals('Archive', $transition->label());
}
@ -462,6 +470,7 @@ class WorkflowTest extends UnitTestCase {
$workflow = new Workflow(['id' => 'test', 'type' => 'test_type'], 'workflow');
// By default states are ordered in the order added.
$workflow
->getTypePlugin()
->addState('draft', 'Draft')
->addState('published', 'Published')
->addState('archived', 'Archived')
@ -469,7 +478,7 @@ class WorkflowTest extends UnitTestCase {
->addTransition('publish', 'Publish', ['draft', 'published'], 'published')
->addTransition('archive', 'Archive', ['published'], 'archived');
$workflow->getTransitionFromStateToState('archived', 'archived');
$workflow->getTypePlugin()->getTransitionFromStateToState('archived', 'archived');
}
/**
@ -478,12 +487,13 @@ class WorkflowTest extends UnitTestCase {
public function testSetTransitionLabel() {
$workflow = new Workflow(['id' => 'test', 'type' => 'test_type'], 'workflow');
$workflow
->getTypePlugin()
->addState('draft', 'Draft')
->addState('published', 'Published')
->addTransition('publish', 'Publish', ['draft'], 'published');
$this->assertEquals('Publish', $workflow->getTransition('publish')->label());
$workflow->setTransitionLabel('publish', 'Publish!');
$this->assertEquals('Publish!', $workflow->getTransition('publish')->label());
$this->assertEquals('Publish', $workflow->getTypePlugin()->getTransition('publish')->label());
$workflow->getTypePlugin()->setTransitionLabel('publish', 'Publish!');
$this->assertEquals('Publish!', $workflow->getTypePlugin()->getTransition('publish')->label());
}
/**
@ -492,8 +502,8 @@ class WorkflowTest extends UnitTestCase {
public function testSetTransitionLabelException() {
$this->setExpectedException(\InvalidArgumentException::class, "The transition 'draft-published' does not exist in workflow.");
$workflow = new Workflow(['id' => 'test', 'type' => 'test_type'], 'workflow');
$workflow->addState('published', 'Published');
$workflow->setTransitionLabel('draft-published', 'Publish');
$workflow->getTypePlugin()->addState('published', 'Published');
$workflow->getTypePlugin()->setTransitionLabel('draft-published', 'Publish');
}
/**
@ -502,12 +512,13 @@ class WorkflowTest extends UnitTestCase {
public function testSetTransitionWeight() {
$workflow = new Workflow(['id' => 'test', 'type' => 'test_type'], 'workflow');
$workflow
->getTypePlugin()
->addState('draft', 'Draft')
->addState('published', 'Published')
->addTransition('publish', 'Publish', ['draft'], 'published');
$this->assertEquals(0, $workflow->getTransition('publish')->weight());
$workflow->setTransitionWeight('publish', 10);
$this->assertEquals(10, $workflow->getTransition('publish')->weight());
$this->assertEquals(0, $workflow->getTypePlugin()->getTransition('publish')->weight());
$workflow->getTypePlugin()->setTransitionWeight('publish', 10);
$this->assertEquals(10, $workflow->getTypePlugin()->getTransition('publish')->weight());
}
/**
@ -516,8 +527,8 @@ class WorkflowTest extends UnitTestCase {
public function testSetTransitionWeightException() {
$this->setExpectedException(\InvalidArgumentException::class, "The transition 'draft-published' does not exist in workflow.");
$workflow = new Workflow(['id' => 'test', 'type' => 'test_type'], 'workflow');
$workflow->addState('published', 'Published');
$workflow->setTransitionWeight('draft-published', 10);
$workflow->getTypePlugin()->addState('published', 'Published');
$workflow->getTypePlugin()->setTransitionWeight('draft-published', 10);
}
/**
@ -526,22 +537,23 @@ class WorkflowTest extends UnitTestCase {
public function testSetTransitionFromStates() {
$workflow = new Workflow(['id' => 'test', 'type' => 'test_type'], 'workflow');
$workflow
->getTypePlugin()
->addState('draft', 'Draft')
->addState('published', 'Published')
->addState('archived', 'Archived')
->addTransition('test', 'Test', ['draft'], 'draft');
$this->assertTrue($workflow->hasTransitionFromStateToState('draft', 'draft'));
$this->assertFalse($workflow->hasTransitionFromStateToState('published', 'draft'));
$this->assertFalse($workflow->hasTransitionFromStateToState('archived', 'draft'));
$workflow->setTransitionFromStates('test', ['draft', 'published', 'archived']);
$this->assertTrue($workflow->hasTransitionFromStateToState('draft', 'draft'));
$this->assertTrue($workflow->hasTransitionFromStateToState('published', 'draft'));
$this->assertTrue($workflow->hasTransitionFromStateToState('archived', 'draft'));
$workflow->setTransitionFromStates('test', ['published', 'archived']);
$this->assertFalse($workflow->hasTransitionFromStateToState('draft', 'draft'));
$this->assertTrue($workflow->hasTransitionFromStateToState('published', 'draft'));
$this->assertTrue($workflow->hasTransitionFromStateToState('archived', 'draft'));
$this->assertTrue($workflow->getTypePlugin()->hasTransitionFromStateToState('draft', 'draft'));
$this->assertFalse($workflow->getTypePlugin()->hasTransitionFromStateToState('published', 'draft'));
$this->assertFalse($workflow->getTypePlugin()->hasTransitionFromStateToState('archived', 'draft'));
$workflow->getTypePlugin()->setTransitionFromStates('test', ['draft', 'published', 'archived']);
$this->assertTrue($workflow->getTypePlugin()->hasTransitionFromStateToState('draft', 'draft'));
$this->assertTrue($workflow->getTypePlugin()->hasTransitionFromStateToState('published', 'draft'));
$this->assertTrue($workflow->getTypePlugin()->hasTransitionFromStateToState('archived', 'draft'));
$workflow->getTypePlugin()->setTransitionFromStates('test', ['published', 'archived']);
$this->assertFalse($workflow->getTypePlugin()->hasTransitionFromStateToState('draft', 'draft'));
$this->assertTrue($workflow->getTypePlugin()->hasTransitionFromStateToState('published', 'draft'));
$this->assertTrue($workflow->getTypePlugin()->hasTransitionFromStateToState('archived', 'draft'));
}
/**
@ -551,12 +563,13 @@ class WorkflowTest extends UnitTestCase {
$this->setExpectedException(\InvalidArgumentException::class, "The transition 'test' does not exist in workflow.");
$workflow = new Workflow(['id' => 'test', 'type' => 'test_type'], 'workflow');
$workflow
->getTypePlugin()
->addState('draft', 'Draft')
->addState('published', 'Published')
->addState('archived', 'Archived')
->addTransition('create_new_draft', 'Create new draft', ['draft'], 'draft');
$workflow->setTransitionFromStates('test', ['draft', 'published', 'archived']);
$workflow->getTypePlugin()->setTransitionFromStates('test', ['draft', 'published', 'archived']);
}
/**
@ -566,11 +579,12 @@ class WorkflowTest extends UnitTestCase {
$this->setExpectedException(\InvalidArgumentException::class, "The state 'published' does not exist in workflow.");
$workflow = new Workflow(['id' => 'test', 'type' => 'test_type'], 'workflow');
$workflow
->getTypePlugin()
->addState('draft', 'Draft')
->addState('archived', 'Archived')
->addTransition('create_new_draft', 'Create new draft', ['draft'], 'draft');
$workflow->setTransitionFromStates('create_new_draft', ['draft', 'published', 'archived']);
$workflow->getTypePlugin()->setTransitionFromStates('create_new_draft', ['draft', 'published', 'archived']);
}
/**
@ -580,13 +594,14 @@ class WorkflowTest extends UnitTestCase {
$this->setExpectedException(\InvalidArgumentException::class, "The 'create_new_draft' transition already allows 'draft' to 'draft' transitions in workflow.");
$workflow = new Workflow(['id' => 'test', 'type' => 'test_type'], 'workflow');
$workflow
->getTypePlugin()
->addState('draft', 'Draft')
->addState('archived', 'Archived')
->addState('needs_review', 'Needs Review')
->addTransition('create_new_draft', 'Create new draft', ['draft'], 'draft')
->addTransition('needs_review', 'Needs review', ['needs_review'], 'draft');
$workflow->setTransitionFromStates('needs_review', ['draft']);
$workflow->getTypePlugin()->setTransitionFromStates('needs_review', ['draft']);
}
/**
@ -611,17 +626,17 @@ class WorkflowTest extends UnitTestCase {
public function testDeleteTransitionException() {
$this->setExpectedException(\InvalidArgumentException::class, "The transition 'draft-published' does not exist in workflow.");
$workflow = new Workflow(['id' => 'test', 'type' => 'test_type'], 'workflow');
$workflow->addState('published', 'Published');
$workflow->deleteTransition('draft-published');
$workflow->getTypePlugin()->addState('published', 'Published');
$workflow->getTypePlugin()->deleteTransition('draft-published');
}
/**
* @covers ::status
* @covers \Drupal\workflows\Entity\Workflow::status
*/
public function testStatus() {
$workflow = new Workflow(['id' => 'test', 'type' => 'test_type'], 'workflow');
$this->assertFalse($workflow->status());
$workflow->addState('published', 'Published');
$workflow->getTypePlugin()->addState('published', 'Published');
$this->assertTrue($workflow->status());
}