class StateTransitionValidationTest

Same name in other branches
  1. 9 core/modules/content_moderation/tests/src/Unit/StateTransitionValidationTest.php \Drupal\Tests\content_moderation\Unit\StateTransitionValidationTest
  2. 10 core/modules/content_moderation/tests/src/Unit/StateTransitionValidationTest.php \Drupal\Tests\content_moderation\Unit\StateTransitionValidationTest
  3. 11.x core/modules/content_moderation/tests/src/Unit/StateTransitionValidationTest.php \Drupal\Tests\content_moderation\Unit\StateTransitionValidationTest

@coversDefaultClass \Drupal\content_moderation\StateTransitionValidation @group content_moderation

Hierarchy

Expanded class hierarchy of StateTransitionValidationTest

File

core/modules/content_moderation/tests/src/Unit/StateTransitionValidationTest.php, line 21

Namespace

Drupal\Tests\content_moderation\Unit
View source
class StateTransitionValidationTest extends UnitTestCase {
    
    /**
     * A test workflow.
     *
     * @var \Drupal\workflows\WorkflowInterface
     */
    protected $workflow;
    
    /**
     * {@inheritdoc}
     */
    protected function setUp() {
        parent::setUp();
        // Create a container so that the plugin manager and workflow type can be
        // mocked.
        $container = new ContainerBuilder();
        $workflow_manager = $this->prophesize(WorkflowTypeManager::class);
        $workflow_manager->createInstance('content_moderation', Argument::any())
            ->willReturn(new TestType([], '', []));
        $container->set('plugin.manager.workflows.type', $workflow_manager->reveal());
        \Drupal::setContainer($container);
        $this->workflow = new Workflow([
            'id' => 'process',
            'type' => 'content_moderation',
        ], 'workflow');
        $this->workflow
            ->getTypePlugin()
            ->addState('draft', 'draft')
            ->addState('needs_review', 'needs_review')
            ->addState('published', 'published')
            ->addTransition('draft', 'draft', [
            'draft',
        ], 'draft')
            ->addTransition('review', 'review', [
            'draft',
        ], 'needs_review')
            ->addTransition('publish', 'publish', [
            'needs_review',
            'published',
        ], 'published');
    }
    
    /**
     * Verifies user-aware transition validation.
     *
     * @param string $from_id
     *   The state to transition from.
     * @param string $to_id
     *   The state to transition to.
     * @param string $permission
     *   The permission to give the user, or not.
     * @param bool $allowed
     *   Whether or not to grant a user this permission.
     * @param bool $result
     *   Whether getValidTransitions() is expected to have the.
     *
     * @dataProvider userTransitionsProvider
     */
    public function testUserSensitiveValidTransitions($from_id, $to_id, $permission, $allowed, $result) {
        $user = $this->prophesize(AccountInterface::class);
        // The one listed permission will be returned as instructed; Any others are
        // always denied.
        $user->hasPermission($permission)
            ->willReturn($allowed);
        $user->hasPermission(Argument::type('string'))
            ->willReturn(FALSE);
        $entity = $this->prophesize(ContentEntityInterface::class);
        $entity = $entity->reveal();
        $entity->moderation_state = new \stdClass();
        $entity->moderation_state->value = $from_id;
        $moderation_info = $this->prophesize(ModerationInformationInterface::class);
        $moderation_info->getWorkflowForEntity($entity)
            ->willReturn($this->workflow);
        $validator = new StateTransitionValidation($moderation_info->reveal());
        $has_transition = FALSE;
        foreach ($validator->getValidTransitions($entity, $user->reveal()) as $transition) {
            if ($transition->to()
                ->id() === $to_id) {
                $has_transition = TRUE;
                break;
            }
        }
        $this->assertSame($result, $has_transition);
    }
    
    /**
     * @expectedDeprecation Omitting the $entity parameter from Drupal\content_moderation\StateTransitionValidation::isTransitionValid is deprecated and will be required in Drupal 9.0.0.
     * @group legacy
     */
    public function testDeprecatedEntityParameter() {
        $moderation_info = $this->prophesize(ModerationInformationInterface::class);
        $state = new State($this->workflow
            ->getTypePlugin(), 'draft', 'draft');
        $user = $this->prophesize(AccountInterface::class);
        $validator = new StateTransitionValidation($moderation_info->reveal());
        $validator->isTransitionValid($this->workflow, $state, $state, $user->reveal());
    }
    
    /**
     * Data provider for the user transition test.
     */
    public function userTransitionsProvider() {
        // The user has the right permission, so let it through.
        $ret[] = [
            'draft',
            'draft',
            'use process transition draft',
            TRUE,
            TRUE,
        ];
        // The user doesn't have the right permission, block it.
        $ret[] = [
            'draft',
            'draft',
            'use process transition draft',
            FALSE,
            FALSE,
        ];
        // The user has some other permission that doesn't matter.
        $ret[] = [
            'draft',
            'draft',
            'use process transition review',
            TRUE,
            FALSE,
        ];
        return $ret;
    }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overriden Title Overrides
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
StateTransitionValidationTest::$workflow protected property A test workflow.
StateTransitionValidationTest::setUp protected function Overrides UnitTestCase::setUp
StateTransitionValidationTest::testDeprecatedEntityParameter public function @expectedDeprecation Omitting the $entity parameter from Drupal\content_moderation\StateTransitionValidation::isTransitionValid is deprecated and will be required in Drupal 9.0.0.
@group legacy
StateTransitionValidationTest::testUserSensitiveValidTransitions public function Verifies user-aware transition validation.
StateTransitionValidationTest::userTransitionsProvider public function Data provider for the user transition test.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.

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