You are here

class EntityResolverManagerTest in Drupal 9

Same name and namespace in other branches
  1. 8 core/tests/Drupal/Tests/Core/Entity/EntityResolverManagerTest.php \Drupal\Tests\Core\Entity\EntityResolverManagerTest

@coversDefaultClass \Drupal\Core\Entity\EntityResolverManager @group Entity

Hierarchy

Expanded class hierarchy of EntityResolverManagerTest

File

core/tests/Drupal/Tests/Core/Entity/EntityResolverManagerTest.php, line 23
Contains \Drupal\Tests\Core\Entity\EntityResolverManagerTest.

Namespace

Drupal\Tests\Core\Entity
View source
class EntityResolverManagerTest extends UnitTestCase {

  /**
   * The tested entity resolver manager.
   *
   * @var \Drupal\Core\Entity\EntityResolverManager
   */
  protected $entityResolverManager;

  /**
   * The mocked entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $entityTypeManager;

  /**
   * The mocked class resolver.
   *
   * @var \Drupal\Core\DependencyInjection\ClassResolverInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $classResolver;

  /**
   * The mocked dependency injection container.
   *
   * @var \Symfony\Component\DependencyInjection\ContainerInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $container;

  /**
   * {@inheritdoc}
   *
   * @covers ::__construct
   */
  protected function setUp() : void {
    $this->entityTypeManager = $this
      ->createMock('Drupal\\Core\\Entity\\EntityTypeManagerInterface');
    $this->container = $this
      ->createMock('Symfony\\Component\\DependencyInjection\\ContainerInterface');
    $this->classResolver = $this
      ->getClassResolverStub();
    $this->entityResolverManager = new EntityResolverManager($this->entityTypeManager, $this->classResolver);
  }

  /**
   * Tests setRouteOptions() with no parameter.
   *
   * We don't have any entity type involved, so we don't need any upcasting.
   *
   * @covers ::setRouteOptions
   * @covers ::getControllerClass
   *
   * @dataProvider providerTestSetRouteOptionsWithStandardRoute
   */
  public function testSetRouteOptionsWithStandardRoute($controller) {
    $route = new Route('/example', [
      '_controller' => $controller,
    ]);
    $defaults = $route
      ->getDefaults();
    $this->entityResolverManager
      ->setRouteOptions($route);
    $this
      ->assertEquals($defaults, $route
      ->getDefaults());
    $this
      ->assertEmpty($route
      ->getOption('parameters'));
  }

  /**
   * Data provider for testSetRouteOptionsWithStandardRoute.
   */
  public function providerTestSetRouteOptionsWithStandardRoute() {
    return [
      [
        'Drupal\\Tests\\Core\\Entity\\BasicControllerClass::exampleControllerMethod',
      ],
      [
        'Drupal\\Tests\\Core\\Entity\\test_function_controller',
      ],
    ];
  }

  /**
   * Tests setRouteOptions() with a controller with a non entity argument.
   *
   * @covers ::setRouteOptions
   * @covers ::getControllerClass
   *
   * @dataProvider providerTestSetRouteOptionsWithStandardRouteWithArgument
   */
  public function testSetRouteOptionsWithStandardRouteWithArgument($controller) {
    $route = new Route('/example/{argument}', [
      '_controller' => $controller,
      'argument' => 'test',
    ]);
    $defaults = $route
      ->getDefaults();
    $this->entityResolverManager
      ->setRouteOptions($route);
    $this
      ->assertEquals($defaults, $route
      ->getDefaults());
    $this
      ->assertEmpty($route
      ->getOption('parameters'));
  }

  /**
   * Data provider for testSetRouteOptionsWithStandardRouteWithArgument.
   */
  public function providerTestSetRouteOptionsWithStandardRouteWithArgument() {
    return [
      [
        'Drupal\\Tests\\Core\\Entity\\BasicControllerClass::exampleControllerMethodWithArgument',
      ],
      [
        'Drupal\\Tests\\Core\\Entity\\test_function_controller_with_argument',
      ],
    ];
  }

  /**
   * Tests setRouteOptions() with a _content default.
   *
   * @covers ::setRouteOptions
   * @covers ::getControllerClass
   *
   * @dataProvider providerTestSetRouteOptionsWithContentController
   */
  public function testSetRouteOptionsWithContentController($controller) {
    $route = new Route('/example/{argument}', [
      '_controller' => $controller,
      'argument' => 'test',
    ]);
    $defaults = $route
      ->getDefaults();
    $this->entityResolverManager
      ->setRouteOptions($route);
    $this
      ->assertEquals($defaults, $route
      ->getDefaults());
    $this
      ->assertEmpty($route
      ->getOption('parameters'));
  }

  /**
   * Data provider for testSetRouteOptionsWithContentController.
   */
  public function providerTestSetRouteOptionsWithContentController() {
    return [
      [
        'Drupal\\Tests\\Core\\Entity\\BasicControllerClass::exampleControllerMethodWithArgument',
      ],
      [
        'Drupal\\Tests\\Core\\Entity\\test_function_controller_with_argument',
      ],
    ];
  }

  /**
   * Tests setRouteOptions() with an entity type parameter.
   *
   * @covers ::setRouteOptions
   * @covers ::getControllerClass
   * @covers ::getEntityTypes
   * @covers ::setParametersFromReflection
   *
   * @dataProvider providerTestSetRouteOptionsWithEntityTypeNoUpcasting
   */
  public function testSetRouteOptionsWithEntityTypeNoUpcasting($controller) {
    $this
      ->setupEntityTypes();
    $route = new Route('/example/{entity_test}', [
      '_controller' => $controller,
    ]);
    $defaults = $route
      ->getDefaults();
    $this->entityResolverManager
      ->setRouteOptions($route);
    $this
      ->assertEquals($defaults, $route
      ->getDefaults());
    $this
      ->assertEmpty($route
      ->getOption('parameters'));
  }

  /**
   * Data provider for testSetRouteOptionsWithEntityTypeNoUpcasting.
   */
  public function providerTestSetRouteOptionsWithEntityTypeNoUpcasting() {
    return [
      [
        'Drupal\\Tests\\Core\\Entity\\BasicControllerClass::exampleControllerWithEntityNoUpcasting',
      ],
      [
        'Drupal\\Tests\\Core\\Entity\\test_function_controller_no_upcasting',
      ],
    ];
  }

  /**
   * Tests setRouteOptions() with an entity type parameter, upcasting.
   *
   * @covers ::setRouteOptions
   * @covers ::getControllerClass
   * @covers ::getEntityTypes
   * @covers ::setParametersFromReflection
   *
   * @dataProvider providerTestSetRouteOptionsWithEntityTypeUpcasting
   */
  public function testSetRouteOptionsWithEntityTypeUpcasting($controller) {
    $this
      ->setupEntityTypes();
    $route = new Route('/example/{entity_test}', [
      '_controller' => $controller,
    ]);
    $defaults = $route
      ->getDefaults();
    $this->entityResolverManager
      ->setRouteOptions($route);
    $this
      ->assertEquals($defaults, $route
      ->getDefaults());
    $parameters = $route
      ->getOption('parameters');
    $this
      ->assertEquals([
      'entity_test' => [
        'type' => 'entity:entity_test',
      ],
    ], $parameters);
  }

  /**
   * Data provider for testSetRouteOptionsWithEntityTypeUpcasting.
   */
  public function providerTestSetRouteOptionsWithEntityTypeUpcasting() {
    return [
      [
        'Drupal\\Tests\\Core\\Entity\\BasicControllerClass::exampleControllerWithEntityUpcasting',
      ],
      [
        'Drupal\\Tests\\Core\\Entity\\test_function_controller_entity_upcasting',
      ],
    ];
  }

  /**
   * Tests setRouteOptions() with an entity type parameter form.
   *
   * @covers ::setRouteOptions
   * @covers ::getControllerClass
   * @covers ::getEntityTypes
   * @covers ::setParametersFromReflection
   */
  public function testSetRouteOptionsWithEntityFormUpcasting() {
    $this
      ->setupEntityTypes();
    $route = new Route('/example/{entity_test}', [
      '_form' => 'Drupal\\Tests\\Core\\Entity\\BasicForm',
    ]);
    $defaults = $route
      ->getDefaults();
    $this->entityResolverManager
      ->setRouteOptions($route);
    $this
      ->assertEquals($defaults, $route
      ->getDefaults());
    $parameters = $route
      ->getOption('parameters');
    $this
      ->assertEquals([
      'entity_test' => [
        'type' => 'entity:entity_test',
      ],
    ], $parameters);
  }

  /**
   * Tests setRouteOptions() with entity form upcasting, no create method.
   *
   * @covers ::setRouteOptions
   * @covers ::getControllerClass
   * @covers ::getEntityTypes
   * @covers ::setParametersFromReflection
   */
  public function testSetRouteOptionsWithEntityUpcastingNoCreate() {
    $this
      ->setupEntityTypes();
    $route = new Route('/example/{entity_test}', [
      '_form' => 'Drupal\\Tests\\Core\\Entity\\BasicFormNoContainerInjectionInterface',
    ]);
    $defaults = $route
      ->getDefaults();
    $this->entityResolverManager
      ->setRouteOptions($route);
    $this
      ->assertEquals($defaults, $route
      ->getDefaults());
    $parameters = $route
      ->getOption('parameters');
    $this
      ->assertEquals([
      'entity_test' => [
        'type' => 'entity:entity_test',
      ],
    ], $parameters);
  }

  /**
   * Tests setRouteOptions() with a form parameter without interface.
   *
   * @covers ::setRouteOptions
   * @covers ::getControllerClass
   * @covers ::getEntityTypes
   * @covers ::setParametersFromReflection
   */
  public function testSetRouteOptionsWithEntityFormNoUpcasting() {
    $this
      ->setupEntityTypes();
    $route = new Route('/example/{entity_test}', [
      '_form' => 'Drupal\\Tests\\Core\\Entity\\BasicFormNoUpcasting',
    ]);
    $defaults = $route
      ->getDefaults();
    $this->entityResolverManager
      ->setRouteOptions($route);
    $this
      ->assertEquals($defaults, $route
      ->getDefaults());
    $this
      ->assertEmpty($route
      ->getOption('parameters'));
  }

  /**
   * Tests setRouteOptions() with an _entity_view route.
   *
   * @covers ::setRouteOptions
   * @covers ::getControllerClass
   * @covers ::getEntityTypes
   * @covers ::setParametersFromReflection
   * @covers ::setParametersFromEntityInformation
   */
  public function testSetRouteOptionsWithEntityViewRouteAndManualParameters() {
    $this
      ->setupEntityTypes();
    $route = new Route('/example/{foo}', [
      '_entity_view' => 'entity_test.view',
    ], [], [
      'parameters' => [
        'foo' => [
          'type' => 'entity:entity_test',
        ],
      ],
    ]);
    $defaults = $route
      ->getDefaults();
    $this->entityResolverManager
      ->setRouteOptions($route);
    $this
      ->assertEquals($defaults, $route
      ->getDefaults());
    $parameters = $route
      ->getOption('parameters');
    $this
      ->assertEquals([
      'foo' => [
        'type' => 'entity:entity_test',
      ],
    ], $parameters);
  }

  /**
   * Tests setRouteOptions() with an _entity_view route.
   *
   * @covers ::setRouteOptions
   * @covers ::getControllerClass
   * @covers ::getEntityTypes
   * @covers ::setParametersFromReflection
   * @covers ::setParametersFromEntityInformation
   */
  public function testSetRouteOptionsWithEntityViewRoute() {
    $this
      ->setupEntityTypes();
    $route = new Route('/example/{entity_test}', [
      '_entity_view' => 'entity_test.view',
    ]);
    $defaults = $route
      ->getDefaults();
    $this->entityResolverManager
      ->setRouteOptions($route);
    $this
      ->assertEquals($defaults, $route
      ->getDefaults());
    $parameters = $route
      ->getOption('parameters');
    $this
      ->assertEquals([
      'entity_test' => [
        'type' => 'entity:entity_test',
      ],
    ], $parameters);
  }

  /**
   * Tests setRouteOptions() with an _entity_list route.
   *
   * @covers ::setRouteOptions
   * @covers ::getControllerClass
   * @covers ::getEntityTypes
   * @covers ::setParametersFromReflection
   * @covers ::setParametersFromEntityInformation
   */
  public function testSetRouteOptionsWithEntityListRoute() {
    $this
      ->setupEntityTypes();
    $route = new Route('/example/{entity_test}', [
      '_entity_list' => 'entity_test',
    ]);
    $defaults = $route
      ->getDefaults();
    $this->entityResolverManager
      ->setRouteOptions($route);
    $this
      ->assertEquals($defaults, $route
      ->getDefaults());
    $parameters = $route
      ->getOption('parameters');
    $this
      ->assertNull($parameters);
  }

  /**
   * Tests setRouteOptions() with an _entity_form route.
   *
   * @covers ::setRouteOptions
   * @covers ::getControllerClass
   * @covers ::getEntityTypes
   * @covers ::setParametersFromReflection
   * @covers ::setParametersFromEntityInformation
   */
  public function testSetRouteOptionsWithEntityFormRoute() {
    $this
      ->setupEntityTypes();
    $route = new Route('/example/{entity_test}', [
      '_entity_form' => 'entity_test.edit',
    ]);
    $defaults = $route
      ->getDefaults();
    $this->entityResolverManager
      ->setRouteOptions($route);
    $this
      ->assertEquals($defaults, $route
      ->getDefaults());
    $parameters = $route
      ->getOption('parameters');
    $this
      ->assertEquals([
      'entity_test' => [
        'type' => 'entity:entity_test',
      ],
    ], $parameters);
  }

  /**
   * Tests an _entity_form route where a non-entity parameter is first.
   *
   * The {argument} preceding {entity_test} in route path, is upcasting with a
   * custom param converter.
   *
   * @covers ::setRouteOptions
   * @covers ::getControllerClass
   * @covers ::getEntityTypes
   * @covers ::setParametersFromReflection
   * @covers ::setParametersFromEntityInformation
   */
  public function testSetRouteOptionsWithEntityFormRouteAndArgument() {
    $this
      ->setupEntityTypes();
    $route = new Route('/example/{argument}/{entity_test}', [
      '_entity_form' => 'entity_test.edit',
    ]);

    // Add {argument} parameter configuration. In this case {argument} is
    // upcasted by a custom param converter 'argument_type'.
    $route
      ->setOption('parameters', [
      'argument' => [
        'type' => 'argument_type',
      ],
    ]);
    $defaults = $route
      ->getDefaults();
    $this->entityResolverManager
      ->setRouteOptions($route);
    $this
      ->assertEquals($defaults, $route
      ->getDefaults());
    $parameters = $route
      ->getOption('parameters');
    $expect = [
      'argument' => [
        'type' => 'argument_type',
      ],
      'entity_test' => [
        'type' => 'entity:entity_test',
      ],
    ];
    $this
      ->assertEquals($expect, $parameters);
  }

  /**
   * Tests setRouteOptions() with an _entity_form route for an add form.
   *
   * @covers ::setRouteOptions
   * @covers ::getControllerClass
   * @covers ::getEntityTypes
   * @covers ::setParametersFromReflection
   * @covers ::setParametersFromEntityInformation
   */
  public function testSetRouteOptionsWithEntityAddFormRoute() {
    $this
      ->setupEntityTypes();
    $route = new Route('/example/add', [
      '_entity_form' => 'entity_test.add',
    ]);
    $defaults = $route
      ->getDefaults();
    $this->entityResolverManager
      ->setRouteOptions($route);
    $this
      ->assertEquals($defaults, $route
      ->getDefaults());
    $this
      ->assertFalse($route
      ->hasOption('parameters'));
  }

  /**
   * Creates the entity type manager mock returning entity type objects.
   */
  protected function setupEntityTypes() {
    $definition = $this
      ->createMock('Drupal\\Core\\Entity\\EntityTypeInterface');
    $definition
      ->expects($this
      ->any())
      ->method('getClass')
      ->will($this
      ->returnValue('Drupal\\Tests\\Core\\Entity\\SimpleTestEntity'));
    $definition
      ->expects($this
      ->any())
      ->method('isRevisionable')
      ->willReturn(FALSE);
    $revisionable_definition = $this
      ->createMock('Drupal\\Core\\Entity\\EntityTypeInterface');
    $revisionable_definition
      ->expects($this
      ->any())
      ->method('getClass')
      ->will($this
      ->returnValue('Drupal\\Tests\\Core\\Entity\\SimpleTestEntity'));
    $revisionable_definition
      ->expects($this
      ->any())
      ->method('isRevisionable')
      ->willReturn(TRUE);
    $this->entityTypeManager
      ->expects($this
      ->any())
      ->method('getDefinitions')
      ->will($this
      ->returnValue([
      'entity_test' => $definition,
      'entity_test_rev' => $revisionable_definition,
    ]));
    $this->entityTypeManager
      ->expects($this
      ->any())
      ->method('getDefinition')
      ->willReturnCallback(function ($entity_type) use ($definition, $revisionable_definition) {
      if ($entity_type == 'entity_test') {
        return $definition;
      }
      elseif ($entity_type === 'entity_test_rev') {
        return $revisionable_definition;
      }
      else {
        return NULL;
      }
    });
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityResolverManagerTest::$classResolver protected property The mocked class resolver.
EntityResolverManagerTest::$container protected property The mocked dependency injection container.
EntityResolverManagerTest::$entityResolverManager protected property The tested entity resolver manager.
EntityResolverManagerTest::$entityTypeManager protected property The mocked entity type manager.
EntityResolverManagerTest::providerTestSetRouteOptionsWithContentController public function Data provider for testSetRouteOptionsWithContentController.
EntityResolverManagerTest::providerTestSetRouteOptionsWithEntityTypeNoUpcasting public function Data provider for testSetRouteOptionsWithEntityTypeNoUpcasting.
EntityResolverManagerTest::providerTestSetRouteOptionsWithEntityTypeUpcasting public function Data provider for testSetRouteOptionsWithEntityTypeUpcasting.
EntityResolverManagerTest::providerTestSetRouteOptionsWithStandardRoute public function Data provider for testSetRouteOptionsWithStandardRoute.
EntityResolverManagerTest::providerTestSetRouteOptionsWithStandardRouteWithArgument public function Data provider for testSetRouteOptionsWithStandardRouteWithArgument.
EntityResolverManagerTest::setUp protected function @covers ::__construct Overrides UnitTestCase::setUp
EntityResolverManagerTest::setupEntityTypes protected function Creates the entity type manager mock returning entity type objects.
EntityResolverManagerTest::testSetRouteOptionsWithContentController public function Tests setRouteOptions() with a _content default.
EntityResolverManagerTest::testSetRouteOptionsWithEntityAddFormRoute public function Tests setRouteOptions() with an _entity_form route for an add form.
EntityResolverManagerTest::testSetRouteOptionsWithEntityFormNoUpcasting public function Tests setRouteOptions() with a form parameter without interface.
EntityResolverManagerTest::testSetRouteOptionsWithEntityFormRoute public function Tests setRouteOptions() with an _entity_form route.
EntityResolverManagerTest::testSetRouteOptionsWithEntityFormRouteAndArgument public function Tests an _entity_form route where a non-entity parameter is first.
EntityResolverManagerTest::testSetRouteOptionsWithEntityFormUpcasting public function Tests setRouteOptions() with an entity type parameter form.
EntityResolverManagerTest::testSetRouteOptionsWithEntityListRoute public function Tests setRouteOptions() with an _entity_list route.
EntityResolverManagerTest::testSetRouteOptionsWithEntityTypeNoUpcasting public function Tests setRouteOptions() with an entity type parameter.
EntityResolverManagerTest::testSetRouteOptionsWithEntityTypeUpcasting public function Tests setRouteOptions() with an entity type parameter, upcasting.
EntityResolverManagerTest::testSetRouteOptionsWithEntityUpcastingNoCreate public function Tests setRouteOptions() with entity form upcasting, no create method.
EntityResolverManagerTest::testSetRouteOptionsWithEntityViewRoute public function Tests setRouteOptions() with an _entity_view route.
EntityResolverManagerTest::testSetRouteOptionsWithEntityViewRouteAndManualParameters public function Tests setRouteOptions() with an _entity_view route.
EntityResolverManagerTest::testSetRouteOptionsWithStandardRoute public function Tests setRouteOptions() with no parameter.
EntityResolverManagerTest::testSetRouteOptionsWithStandardRouteWithArgument public function Tests setRouteOptions() with a controller with a non entity argument.
PhpUnitWarnings::$deprecationWarnings private static property Deprecation warnings from PHPUnit to raise with @trigger_error().
PhpUnitWarnings::addWarning public function Converts PHPUnit deprecation warnings to E_USER_DEPRECATED.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals Deprecated protected function Asserts if two arrays are equal by sorting them first.
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.
UnitTestCase::setUpBeforeClass public static function