function EntityStateChangeValidationTest::testInvalidStateWithoutExisting

Same name in other branches
  1. 8.9.x core/modules/content_moderation/tests/src/Kernel/EntityStateChangeValidationTest.php \Drupal\Tests\content_moderation\Kernel\EntityStateChangeValidationTest::testInvalidStateWithoutExisting()
  2. 10 core/modules/content_moderation/tests/src/Kernel/EntityStateChangeValidationTest.php \Drupal\Tests\content_moderation\Kernel\EntityStateChangeValidationTest::testInvalidStateWithoutExisting()
  3. 11.x 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 143

Class

EntityStateChangeValidationTest
@coversDefaultClass \Drupal\content_moderation\Plugin\Validation\Constraint\ModerationStateConstraintValidator @group content_moderation

Namespace

Drupal\Tests\content_moderation\Kernel

Code

public function testInvalidStateWithoutExisting() {
    $this->setCurrentUser($this->adminUser);
    // Create content without moderation enabled for the content type.
    $node_type = NodeType::create([
        'type' => '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.