function EntityStateChangeValidationTest::testInvalidStateWithoutExisting

Same name and namespace in other branches
  1. 9 core/modules/content_moderation/tests/src/Kernel/EntityStateChangeValidationTest.php \Drupal\Tests\content_moderation\Kernel\EntityStateChangeValidationTest::testInvalidStateWithoutExisting()
  2. 8.9.x core/modules/content_moderation/tests/src/Kernel/EntityStateChangeValidationTest.php \Drupal\Tests\content_moderation\Kernel\EntityStateChangeValidationTest::testInvalidStateWithoutExisting()
  3. 10 core/modules/content_moderation/tests/src/Kernel/EntityStateChangeValidationTest.php \Drupal\Tests\content_moderation\Kernel\EntityStateChangeValidationTest::testInvalidStateWithoutExisting()

Tests validation with no initial state or an invalid state.

File

core/modules/content_moderation/tests/src/Kernel/EntityStateChangeValidationTest.php, line 155

Class

EntityStateChangeValidationTest
@coversDefaultClass <a href="/api/drupal/core%21modules%21content_moderation%21src%21Plugin%21Validation%21Constraint%21ModerationStateConstraintValidator.php/class/ModerationStateConstraintValidator/11.x" title="Checks if a moderation state transition is valid." class="local">\Drupal\content_moderation\Plugin\Validation\Constraint\ModerationStateConstraintValidator</a> @group content_moderation

Namespace

Drupal\Tests\content_moderation\Kernel

Code

public function testInvalidStateWithoutExisting() : void {
    $this->setCurrentUser($this->adminUser);
    // Create content without moderation enabled for the content type.
    $node_type = NodeType::create([
        'type' => 'example',
        'name' => 'Example',
    ]);
    $node_type->save();
    $node = Node::create([
        'type' => 'example',
        'title' => 'Test title',
    ]);
    $node->save();
    // Enable moderation to test validation on existing content, with no
    // explicit state.
    $workflow = $this->createEditorialWorkflow();
    $workflow->getTypePlugin()
        ->addState('deleted_state', 'Deleted state');
    $workflow->getTypePlugin()
        ->addEntityTypeAndBundle('node', 'example');
    $workflow->save();
    // Validate the invalid state.
    $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);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.