+ /**
+ * @dataProvider transitionAccessValidationTestCases
+ */
+ public function testTransitionAccessValidation($permissions, $target_state, $messages) {
+ $node_type = NodeType::create([
+ 'type' => 'example',
+ ]);
+ $node_type->save();
+ $workflow = $this->createEditorialWorkflow();
+ $workflow->getTypePlugin()->addState('foo', 'Foo');
+ $workflow->getTypePlugin()->addTransition('draft_to_foo', 'Draft to foo', ['draft'], 'foo');
+ $workflow->getTypePlugin()->addTransition('foo_to_foo', 'Foo to foo', ['foo'], 'foo');
+ $workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'example');
+ $workflow->save();
+
+ $this->setCurrentUser($this->createUser($permissions));
+
+ $node = Node::create([
+ 'type' => 'example',
+ 'title' => 'Test content',
+ 'moderation_state' => $target_state,
+ ]);
+ $this->assertTrue($node->isNew());
+ $violations = $node->validate();
+ $this->assertCount(count($messages), $violations);
+ foreach ($messages as $i => $message) {
+ $this->assertEquals($message, $violations->get($i)->getMessage());
+ }
+ }
+
+ /**
+ * Test cases for ::testTransitionAccessValidation.
+ */
+ public function transitionAccessValidationTestCases() {
+ return [
+ 'Invalid transition, no permissions validated' => [
+ [],
+ 'archived',
+ ['Invalid state transition from <em class="placeholder">Draft</em> to <em class="placeholder">Archived</em>'],
+ ],
+ 'Valid transition, missing permission' => [
+ [],
+ 'published',
+ ['You do not have access to transition from <em class="placeholder">Draft</em> to <em class="placeholder">Published</em>'],
+ ],
+ 'Valid transition, granted published permission' => [
+ ['use editorial transition publish'],
+ 'published',
+ [],
+ ],
+ 'Valid transition, granted draft permission' => [
+ ['use editorial transition create_new_draft'],
+ 'draft',
+ [],
+ ],
+ 'Valid transition, incorrect permission granted' => [
+ ['use editorial transition create_new_draft'],
+ 'published',
+ ['You do not have access to transition from <em class="placeholder">Draft</em> to <em class="placeholder">Published</em>'],
+ ],
+ // Test with an additional state and set of transitions, since the
+ // "published" transition can start from either "draft" or "published", it
+ // does not capture bugs that fail to correctly distinguish the initial
+ // workflow state from the set state of a new entity.
+ 'Valid transition, granted foo permission' => [
+ ['use editorial transition draft_to_foo'],
+ 'foo',
+ [],
+ ],
+ 'Valid transition, incorrect foo permission granted' => [
+ ['use editorial transition foo_to_foo'],
+ 'foo',
+ ['You do not have access to transition from <em class="placeholder">Draft</em> to <em class="placeholder">Foo</em>'],
+ ],
+ ];
+ }
+