/** * Verify moderation state methods based on entity properties. * * @covers ::isPublishedState * @covers ::isDefaultRevisionState * * @dataProvider moderationStateProvider */ public function testModerationStateProperties($published, $default_revision, $is_published, $is_default) { $moderation_state_id = $this->randomMachineName(); $moderation_state = ModerationState::create(['id' => $moderation_state_id, 'label' => $this->randomString(), 'published' => $published, 'default_revision' => $default_revision]); $moderation_state->save(); $moderation_state = ModerationState::load($moderation_state_id); $this->assertEquals($is_published, $moderation_state->isPublishedState()); $this->assertEquals($is_default, $moderation_state->isDefaultRevisionState()); }
/** * Returns an array of transition permissions. * * @return array * The transition permissions. */ public function transitionPermissions() { // @todo write a test for this. $perms = []; /* @var \Drupal\workbench_moderation\ModerationStateInterface[] $states */ $states = ModerationState::loadMultiple(); /* @var \Drupal\workbench_moderation\ModerationStateTransitionInterface $transition */ foreach (ModerationStateTransition::loadMultiple() as $id => $transition) { $perms['use ' . $id . ' transition'] = ['title' => $this->t('Use the %transition_name transition', ['%transition_name' => $transition->label()]), 'description' => $this->t('Move content from %from state to %to state.', ['%from' => $states[$transition->getFromState()]->label(), '%to' => $states[$transition->getToState()]->label()])]; } return $perms; }
/** * Tests workbench moderation third party schema for block content types. */ public function testWorkbenchModerationBlockContentTypeConfig() { $this->installEntitySchema('block_content'); $this->installEntitySchema('user'); $this->installConfig(['workbench_moderation']); $typed_config = \Drupal::service('config.typed'); $moderation_states = ModerationState::loadMultiple(); $block_content_type = BlockContentType::create(['id' => 'basic', 'label' => 'basic', 'revision' => TRUE]); $block_content_type->setThirdPartySetting('workbench_moderation', 'enabled', TRUE); $block_content_type->setThirdPartySetting('workbench_moderation', 'allowed_moderation_states', array_keys($moderation_states)); $block_content_type->setThirdPartySetting('workbench_moderation', 'default_moderation_state', ''); $block_content_type->save(); $this->assertConfigSchema($typed_config, $block_content_type->getEntityType()->getConfigPrefix() . '.' . $block_content_type->id(), $block_content_type->toArray()); }
/** * {@inheritdoc} */ public function form(array $form, FormStateInterface $form_state) { $form = parent::form($form, $form_state); /* @var \Drupal\workbench_moderation\ModerationStateTransitionInterface $moderation_state_transition */ $moderation_state_transition = $this->entity; $form['label'] = ['#type' => 'textfield', '#title' => $this->t('Label'), '#maxlength' => 255, '#default_value' => $moderation_state_transition->label(), '#description' => $this->t("Label for the Moderation state transition."), '#required' => TRUE]; $form['id'] = ['#type' => 'machine_name', '#default_value' => $moderation_state_transition->id(), '#machine_name' => ['exists' => '\\Drupal\\workbench_moderation\\Entity\\ModerationStateTransition::load'], '#disabled' => !$moderation_state_transition->isNew()]; $options = []; foreach (ModerationState::loadMultiple() as $moderation_state) { $options[$moderation_state->id()] = $moderation_state->label(); } $form['container'] = ['#type' => 'container', '#attributes' => ['class' => ['container-inline']]]; $form['container']['stateFrom'] = ['#type' => 'select', '#title' => $this->t('Transition from'), '#options' => $options, '#required' => TRUE, '#empty_option' => $this->t('-- Select --'), '#default_value' => $moderation_state_transition->getFromState()]; $form['container']['stateTo'] = ['#type' => 'select', '#options' => $options, '#required' => TRUE, '#title' => $this->t('Transition to'), '#empty_option' => $this->t('-- Select --'), '#default_value' => $moderation_state_transition->getToState()]; return $form; }
/** * {@inheritdoc} */ public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) { $entity = $items->getEntity(); /* @var \Drupal\Core\Config\Entity\ConfigEntityInterface $bundle_entity */ $bundle_entity = $this->entityTypeManager->getStorage($entity->getEntityType()->getBundleEntityType())->load($entity->bundle()); if (!$this->moderationInformation->isModeratableEntity($entity)) { // @todo write a test for this. return $element + ['#access' => FALSE]; } $options = $this->fieldDefinition->getFieldStorageDefinition()->getOptionsProvider($this->column, $entity)->getSettableOptions($this->currentUser); $default = $items->get($delta)->target_id ?: $bundle_entity->getThirdPartySetting('workbench_moderation', 'default_moderation_state', FALSE); /** @var \Drupal\workbench_moderation\ModerationStateInterface $default_state */ $default_state = ModerationState::load($default); if (!$default || !$default_state) { throw new \UnexpectedValueException(sprintf('The %s bundle has an invalid moderation state configuration, moderation states are enabled but no default is set.', $bundle_entity->label())); } // @todo write a test for this. $from = $this->moderationStateTransitionEntityQuery->condition('stateFrom', $default)->execute(); // Can always keep this one as is. $to[$default] = $default; // @todo write a test for this. $allowed = $bundle_entity->getThirdPartySetting('workbench_moderation', 'allowed_moderation_states', []); if ($from) { /* @var \Drupal\workbench_moderation\ModerationStateTransitionInterface $transition */ foreach ($this->moderationStateTransitionStorage->loadMultiple($from) as $id => $transition) { $to_state = $transition->getToState(); if ($this->currentUser->hasPermission('use ' . $id . ' transition') && in_array($to_state, $allowed, TRUE)) { $to[$to_state] = $to_state; } } } $options = array_intersect_key($options, $to); // @todo write a test for this. $element += ['#access' => count($options), '#type' => 'select', '#options' => $options, '#default_value' => $default, '#published' => $default ? $default_state->isPublishedState() : FALSE]; if ($this->currentUser->hasPermission($this->getAdminPermission($entity->getEntityType())) && count($options)) { // Use the dropbutton. $element['#process'][] = [get_called_class(), 'processActions']; // Don't show in sidebar/body. $element['#access'] = FALSE; } else { // Place the field as a details element in the advanced tab-set in e.g. // the sidebar. $element = ['#type' => 'details', '#group' => 'advanced', '#open' => TRUE, '#weight' => -10, '#title' => t('Moderation state'), 'target_id' => $element]; } return $element; }
/** * Verifies that an unpublished state may be made the default revision. */ public function testArchive() { $published_id = $this->randomMachineName(); $published_state = ModerationState::create(['id' => $published_id, 'label' => $this->randomString(), 'published' => TRUE, 'default_revision' => TRUE]); $published_state->save(); $archived_id = $this->randomMachineName(); $archived_state = ModerationState::create(['id' => $archived_id, 'label' => $this->randomString(), 'published' => FALSE, 'default_revision' => TRUE]); $archived_state->save(); $page = Node::create(['type' => 'page', 'title' => $this->randomString()]); $page->moderation_state->target_id = $published_id; $page->save(); $id = $page->id(); // The newly-created page should already be published. $page = Node::load($id); $this->assertTrue($page->isPublished()); // When the page is moderated to the archived state, then the latest // revision should be the default revision, and it should be unpublished. $page->moderation_state->target_id = $archived_id; $page->save(); $new_revision_id = $page->getRevisionId(); $storage = \Drupal::entityTypeManager()->getStorage('node'); $new_revision = $storage->loadRevision($new_revision_id); $this->assertFalse($new_revision->isPublished()); $this->assertTrue($new_revision->isDefaultRevision()); }