class ContentModerationRouteSubscriberTest

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

@coversDefaultClass \Drupal\content_moderation\Routing\ContentModerationRouteSubscriber

@group content_moderation

Hierarchy

Expanded class hierarchy of ContentModerationRouteSubscriberTest

File

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

Namespace

Drupal\Tests\content_moderation\Unit
View source
class ContentModerationRouteSubscriberTest extends UnitTestCase {
    
    /**
     * The test content moderation route subscriber.
     *
     * @var \Drupal\content_moderation\Routing\ContentModerationRouteSubscriber
     */
    protected $routeSubscriber;
    
    /**
     * {@inheritdoc}
     */
    protected function setUp() : void {
        parent::setUp();
        
        /** @var \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager */
        $entity_type_manager = $this->createMock(EntityTypeManagerInterface::class);
        $this->routeSubscriber = new ContentModerationRouteSubscriber($entity_type_manager);
        $this->setupEntityTypes();
    }
    
    /**
     * Creates the entity type manager mock returning entity type objects.
     */
    protected function setupEntityTypes() : void {
        $definition = $this->createMock(EntityTypeInterface::class);
        $definition->expects($this->any())
            ->method('getClass')
            ->willReturn(TestEntity::class);
        $definition->expects($this->any())
            ->method('isRevisionable')
            ->willReturn(FALSE);
        $revisionable_definition = $this->createMock(EntityTypeInterface::class);
        $revisionable_definition->expects($this->any())
            ->method('getClass')
            ->willReturn(TestEntity::class);
        $revisionable_definition->expects($this->any())
            ->method('isRevisionable')
            ->willReturn(TRUE);
        $entity_types = [
            'entity_test' => $definition,
            'entity_test_rev' => $revisionable_definition,
        ];
        $reflector = new \ReflectionProperty($this->routeSubscriber, 'moderatedEntityTypes');
        $reflector->setValue($this->routeSubscriber, $entity_types);
    }
    
    /**
     * Data provider for ::testSetLatestRevisionFlag.
     */
    public static function setLatestRevisionFlagTestCases() {
        return [
            'Entity parameter not on an entity form' => [
                [],
                [
                    'entity_test' => [
                        'type' => 'entity:entity_test_rev',
                    ],
                ],
            ],
            'Entity parameter on an entity form' => [
                [
                    '_entity_form' => 'entity_test_rev.edit',
                ],
                [
                    'entity_test_rev' => [
                        'type' => 'entity:entity_test_rev',
                    ],
                ],
                [
                    'entity_test_rev' => [
                        'type' => 'entity:entity_test_rev',
                        'load_latest_revision' => TRUE,
                    ],
                ],
            ],
            'Entity form with no operation' => [
                [
                    '_entity_form' => 'entity_test_rev',
                ],
                [
                    'entity_test_rev' => [
                        'type' => 'entity:entity_test_rev',
                    ],
                ],
                [
                    'entity_test_rev' => [
                        'type' => 'entity:entity_test_rev',
                        'load_latest_revision' => TRUE,
                    ],
                ],
            ],
            'Non-moderated entity form' => [
                [
                    '_entity_form' => 'entity_test_mulrev',
                ],
                [
                    'entity_test_mulrev' => [
                        'type' => 'entity:entity_test_mulrev',
                    ],
                ],
            ],
            'Multiple entity parameters on an entity form' => [
                [
                    '_entity_form' => 'entity_test_rev.edit',
                ],
                [
                    'entity_test_rev' => [
                        'type' => 'entity:entity_test_rev',
                    ],
                    'node' => [
                        'type' => 'entity:node',
                    ],
                ],
                [
                    'entity_test_rev' => [
                        'type' => 'entity:entity_test_rev',
                        'load_latest_revision' => TRUE,
                    ],
                    'node' => [
                        'type' => 'entity:node',
                    ],
                ],
            ],
            'Overridden load_latest_revision flag does not change' => [
                [
                    '_entity_form' => 'entity_test_rev.edit',
                ],
                [
                    'entity_test_rev' => [
                        'type' => 'entity:entity_test_rev',
                        'load_latest_revision' => FALSE,
                    ],
                ],
            ],
            'Non-revisionable entity type will not change' => [
                [
                    '_entity_form' => 'entity_test.edit',
                ],
                [
                    'entity_test' => [
                        'type' => 'entity:entity_test',
                    ],
                ],
                FALSE,
                FALSE,
            ],
            'Overridden load_latest_revision flag does not change with multiple parameters' => [
                [
                    '_entity_form' => 'entity_test_rev.edit',
                ],
                [
                    'entity_test_rev' => [
                        'type' => 'entity:entity_test_rev',
                    ],
                    'node' => [
                        'type' => 'entity:node',
                        'load_latest_revision' => FALSE,
                    ],
                ],
                [
                    'entity_test_rev' => [
                        'type' => 'entity:entity_test_rev',
                        'load_latest_revision' => TRUE,
                    ],
                    'node' => [
                        'type' => 'entity:node',
                        'load_latest_revision' => FALSE,
                    ],
                ],
            ],
            'Parameter without type is unchanged' => [
                [
                    '_entity_form' => 'entity_test_rev.edit',
                ],
                [
                    'entity_test_rev' => [
                        'type' => 'entity:entity_test_rev',
                    ],
                    'unrelated_param' => [
                        'foo' => 'bar',
                    ],
                ],
                [
                    'entity_test_rev' => [
                        'type' => 'entity:entity_test_rev',
                        'load_latest_revision' => TRUE,
                    ],
                    'unrelated_param' => [
                        'foo' => 'bar',
                    ],
                ],
            ],
        ];
    }
    
    /**
     * Tests that the "load_latest_revision" flag is handled correctly.
     *
     * @param array $defaults
     *   The route defaults.
     * @param array $parameters
     *   The route parameters.
     * @param array|bool $expected_parameters
     *   (optional) The expected route parameters. Defaults to FALSE.
     *
     * @covers ::setLatestRevisionFlag
     *
     * @dataProvider setLatestRevisionFlagTestCases
     */
    public function testSetLatestRevisionFlag($defaults, $parameters, $expected_parameters = FALSE) : void {
        $route = new Route('/foo/{entity_test}', $defaults, [], [
            'parameters' => $parameters,
        ]);
        $route_collection = new RouteCollection();
        $route_collection->add('test', $route);
        $event = new RouteBuildEvent($route_collection);
        $this->routeSubscriber
            ->onAlterRoutes($event);
        // If expected parameters have not been provided, assert they are unchanged.
        $this->assertEquals($expected_parameters ?: $parameters, $route->getOption('parameters'));
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
ContentModerationRouteSubscriberTest::$routeSubscriber protected property The test content moderation route subscriber.
ContentModerationRouteSubscriberTest::setLatestRevisionFlagTestCases public static function Data provider for ::testSetLatestRevisionFlag.
ContentModerationRouteSubscriberTest::setUp protected function Overrides UnitTestCase::setUp
ContentModerationRouteSubscriberTest::setupEntityTypes protected function Creates the entity type manager mock returning entity type objects.
ContentModerationRouteSubscriberTest::testSetLatestRevisionFlag public function Tests that the "load_latest_revision" flag is handled correctly.
ExpectDeprecationTrait::expectDeprecation public function Adds an expected deprecation.
ExpectDeprecationTrait::getCallableName private static function Returns a callable as a string suitable for inclusion in a message.
ExpectDeprecationTrait::setUpErrorHandler public function Sets up the test error handler.
ExpectDeprecationTrait::tearDownErrorHandler public function Tears down the test error handler.
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers.
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
UnitTestCase::$root protected property The app root.
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::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setUpBeforeClass public static function

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