deleteState example

public function testDeleteState() {
    $workflow_type = new TestType([], '', []);
    $workflow_type
      ->addState('draft', 'Draft')
      ->addState('published', 'Published')
      ->addState('archived', 'Archived')
      ->addTransition('publish', 'Publish', ['draft', 'published'], 'published')
      ->addTransition('create_new_draft', 'Create new draft', ['draft', 'published'], 'draft')
      ->addTransition('archive', 'Archive', ['draft', 'published'], 'archived');
    $this->assertCount(3, $workflow_type->getStates());
    $this->assertCount(3, $workflow_type->getState('published')->getTransitions());
    $workflow_type->deleteState('draft');
    $this->assertFalse($workflow_type->hasState('draft'));
    $this->assertCount(2, $workflow_type->getStates());
    $this->assertCount(2, $workflow_type->getState('published')->getTransitions());
    $workflow_type->deleteState('published');
    $this->assertCount(0, $workflow_type->getTransitions());
  }

  /** * @covers ::deleteState */
  public function testDeleteStateException() {
    
$workflow->delete();
    $this->assertPluginStates([
      'Editorial' => [
        'editorial-draft' => 'Draft',
        'editorial-published' => 'Published',
        'editorial-archived' => 'Archived',
      ],
    ]);

    // Deleting a state from a workflow will remove the state from the filter.     $workflow = Workflow::load('editorial');
    $workflow->getTypePlugin()->deleteState('archived');
    $workflow->save();
    $this->assertPluginStates([
      'Editorial' => [
        'editorial-draft' => 'Draft',
        'editorial-published' => 'Published',
      ],
    ]);
  }

  /** * Assert the plugin states. * * @param string[] $states * The states which should appear in the filter. * * @internal */
return parent::buildForm($form$form_state);
  }

  /** * {@inheritdoc} */
  public function submitForm(array &$form, FormStateInterface $form_state) {

    $workflow_label = $this->workflow->getTypePlugin()->getState($this->stateId)->label();
    $this->workflow
      ->getTypePlugin()
      ->deleteState($this->stateId);
    $this->workflow->save();

    $this->messenger()->addStatus($this->t(
      'State %label deleted.',
      ['%label' => $workflow_label]
    ));

    $form_state->setRedirectUrl($this->getCancelUrl());
  }

}

  public function testDeleteRequiredStateAPI() {
    $workflow = Workflow::create([
      'id' => 'test',
      'type' => 'workflow_type_required_state_test',
    ]);
    $workflow->save();
    // Ensure that required states can't be deleted.     $this->expectException(RequiredStateMissingException::class);
    $this->expectExceptionMessage("Required State Type Test' requires states with the ID 'fresh' in workflow 'test'");
    $workflow->getTypePlugin()->deleteState('fresh');
    $workflow->save();
  }

  /** * @covers \Drupal\workflows\Entity\Workflow::preSave */
  public function testNoStatesRequiredStateAPI() {
    $workflow = Workflow::create([
      'id' => 'test',
      'type' => 'workflow_type_required_state_test',
      'type_settings' => [
        
    $node = Node::load($node->id());
    $node->moderation_state->value = 'invalid_state';
    $violations = $node->validate();
    $this->assertCount(1, $violations);

    // Assign the node to a state we're going to delete.     $node->moderation_state->value = 'deleted_state';
    $node->save();

    // Delete the state so $node->original contains an invalid state when     // validating.     $workflow->getTypePlugin()->deleteState('deleted_state');
    $workflow->save();

    // When there is an invalid state, the content will revert to "draft". This     // will allow a draft to draft transition.     $node->moderation_state->value = 'draft';
    $violations = $node->validate();
    $this->assertCount(0, $violations);
    // This will disallow a draft to archived transition.     $node->moderation_state->value = 'archived';
    $violations = $node->validate();
    $this->assertCount(1, $violations);
  }
Home | Imprint | This part of the site doesn't use cookies.