class EntityRouteEnhancerTest

Same name in other branches
  1. 9 core/tests/Drupal/Tests/Core/Entity/Enhancer/EntityRouteEnhancerTest.php \Drupal\Tests\Core\Entity\Enhancer\EntityRouteEnhancerTest
  2. 8.9.x core/tests/Drupal/Tests/Core/Entity/Enhancer/EntityRouteEnhancerTest.php \Drupal\Tests\Core\Entity\Enhancer\EntityRouteEnhancerTest
  3. 10 core/tests/Drupal/Tests/Core/Entity/Enhancer/EntityRouteEnhancerTest.php \Drupal\Tests\Core\Entity\Enhancer\EntityRouteEnhancerTest

@coversDefaultClass \Drupal\Core\Entity\Enhancer\EntityRouteEnhancer @group Entity

Hierarchy

Expanded class hierarchy of EntityRouteEnhancerTest

File

core/tests/Drupal/Tests/Core/Entity/Enhancer/EntityRouteEnhancerTest.php, line 17

Namespace

Drupal\Tests\Core\Entity\Enhancer
View source
class EntityRouteEnhancerTest extends UnitTestCase {
    
    /**
     * Tests the enhancer method.
     *
     * @see \Drupal\Core\Entity\Enhancer\EntityRouteEnhancer::enhancer()
     */
    public function testEnhancer() : void {
        $route_enhancer = new EntityRouteEnhancer();
        // Set a controller to ensure it is not overridden.
        $request = new Request();
        $defaults = [];
        $defaults['_controller'] = 'Drupal\\Tests\\Core\\Controller\\TestController::content';
        $defaults['_entity_form'] = 'entity_test.default';
        $defaults['_route_object'] = new Route('/test', $defaults);
        $new_defaults = $route_enhancer->enhance($defaults, $request);
        $this->assertEquals($defaults['_controller'], $new_defaults['_controller'], '_controller did not get overridden.');
        // Set _entity_form and ensure that the form is set.
        $defaults = [];
        $defaults['_entity_form'] = 'entity_test.default';
        $defaults['_route_object'] = new Route('/test', $defaults);
        $new_defaults = $route_enhancer->enhance($defaults, $request);
        $this->assertEquals('controller.entity_form:getContentResult', $new_defaults['_controller']);
        // Set _entity_list and ensure that the entity list controller is set.
        $defaults = [];
        $defaults['_entity_list'] = 'entity_test.default';
        $defaults['_route_object'] = new Route('/test', $defaults);
        $new_defaults = $route_enhancer->enhance($defaults, $request);
        $this->assertEquals('\\Drupal\\Core\\Entity\\Controller\\EntityListController::listing', $new_defaults['_controller'], 'The entity list controller was not set.');
        $this->assertEquals('entity_test.default', $new_defaults['entity_type']);
        $this->assertFalse(isset($new_defaults['_entity_list']));
        // Set _entity_view and ensure that the entity view controller is set.
        $defaults = [];
        $defaults['_entity_view'] = 'entity_test.full';
        $defaults['entity_test'] = 'Mock entity';
        $defaults['_route_object'] = new Route('/test', $defaults);
        $defaults = $route_enhancer->enhance($defaults, $request);
        $this->assertEquals('\\Drupal\\Core\\Entity\\Controller\\EntityViewController::view', $defaults['_controller'], 'The entity view controller was not set.');
        $this->assertEquals('Mock entity', $defaults['_entity']);
        $this->assertEquals('full', $defaults['view_mode']);
        $this->assertFalse(isset($defaults['_entity_view']));
        // Set _entity_view and ensure that the entity view controller is set using
        // a converter.
        $defaults = [];
        $defaults['_entity_view'] = 'entity_test.full';
        $defaults['foo'] = 'Mock entity';
        // Add a converter.
        $options['parameters']['foo'] = [
            'type' => 'entity:entity_test',
        ];
        // Set the route.
        $route = new Route('/test');
        $route->setOptions($options);
        $route->setDefaults($defaults);
        $defaults[RouteObjectInterface::ROUTE_OBJECT] = $route;
        $defaults = $route_enhancer->enhance($defaults, $request);
        $this->assertEquals('\\Drupal\\Core\\Entity\\Controller\\EntityViewController::view', $defaults['_controller'], 'The entity view controller was not set.');
        $this->assertEquals('Mock entity', $defaults['_entity']);
        $this->assertEquals('full', $defaults['view_mode']);
        $this->assertFalse(isset($defaults['_entity_view']));
        // Set _entity_view without a view mode.
        $defaults = [];
        $defaults['_entity_view'] = 'entity_test';
        $defaults['entity_test'] = 'Mock entity';
        $defaults['_route_object'] = new Route('/test', $defaults);
        $defaults = $route_enhancer->enhance($defaults, $request);
        $this->assertEquals('\\Drupal\\Core\\Entity\\Controller\\EntityViewController::view', $defaults['_controller'], 'The entity view controller was not set.');
        $this->assertEquals('Mock entity', $defaults['_entity']);
        $this->assertArrayNotHasKey('view_mode', $defaults);
        $this->assertFalse(isset($defaults['_entity_view']));
    }

}

Members

Title Sort descending Modifiers Object type Summary Overrides
EntityRouteEnhancerTest::testEnhancer public function Tests the enhancer method.
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::setUp protected function 367
UnitTestCase::setUpBeforeClass public static function

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