You are here

class DefaultHtmlRouteProviderTest in Drupal 8

Same name and namespace in other branches
  1. 9 core/tests/Drupal/Tests/Core/Entity/Routing/DefaultHtmlRouteProviderTest.php \Drupal\Tests\Core\Entity\Routing\DefaultHtmlRouteProviderTest
  2. 10 core/tests/Drupal/Tests/Core/Entity/Routing/DefaultHtmlRouteProviderTest.php \Drupal\Tests\Core\Entity\Routing\DefaultHtmlRouteProviderTest

@coversDefaultClass \Drupal\Core\Entity\Routing\DefaultHtmlRouteProvider @group Entity

Hierarchy

Expanded class hierarchy of DefaultHtmlRouteProviderTest

File

core/tests/Drupal/Tests/Core/Entity/Routing/DefaultHtmlRouteProviderTest.php, line 27
Contains \Drupal\Tests\Core\Entity\Routing\DefaultHtmlRouteProviderTest.

Namespace

Drupal\Tests\Core\Entity\Routing
View source
class DefaultHtmlRouteProviderTest extends UnitTestCase {

  /**
   * The entity type manager prophecy used in the test.
   *
   * @var \Prophecy\Prophecy\ProphecyInterface|\Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The entity field manager prophecy used in the test.
   *
   * @var \Prophecy\Prophecy\ProphecyInterface|\Drupal\Core\Entity\EntityFieldManagerInterface
   */
  protected $entityFieldManager;

  /**
   * The HTML route provider used in the test.
   *
   * @var \Drupal\Tests\Core\Entity\Routing\TestDefaultHtmlRouteProvider
   */
  protected $routeProvider;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    $this->entityTypeManager = $this
      ->prophesize(EntityTypeManagerInterface::class);
    $this->entityFieldManager = $this
      ->prophesize(EntityFieldManagerInterface::class);
    $this->routeProvider = new TestDefaultHtmlRouteProvider($this->entityTypeManager
      ->reveal(), $this->entityFieldManager
      ->reveal());
  }

  /**
   * @covers ::getAddPageRoute
   * @dataProvider providerTestGetAddPageRoute
   */
  public function testGetAddPageRoute(Route $expected = NULL, EntityTypeInterface $entity_type) {
    $route = $this->routeProvider
      ->getAddPageRoute($entity_type);
    $this
      ->assertEquals($expected, $route);
  }
  public function providerTestGetAddPageRoute() {
    $data = [];
    $entity_type1 = $this
      ->getEntityType();
    $entity_type1
      ->hasLinkTemplate('add-page')
      ->willReturn(FALSE);
    $data['no_add_page_link_template'] = [
      NULL,
      $entity_type1
        ->reveal(),
    ];
    $entity_type2 = $this
      ->getEntityType();
    $entity_type2
      ->hasLinkTemplate('add-page')
      ->willReturn(TRUE);
    $entity_type2
      ->getKey('bundle')
      ->willReturn(NULL);
    $data['no_bundle'] = [
      NULL,
      $entity_type2
        ->reveal(),
    ];
    $entity_type3 = $this
      ->getEntityType();
    $entity_type3
      ->hasLinkTemplate('add-page')
      ->willReturn(TRUE);
    $entity_type3
      ->getLinkTemplate('add-page')
      ->willReturn('/the/add/page/link/template');
    $entity_type3
      ->id()
      ->willReturn('the_entity_type_id');
    $entity_type3
      ->getKey('bundle')
      ->willReturn('type');
    $route = new Route('/the/add/page/link/template');
    $route
      ->setDefaults([
      '_controller' => 'Drupal\\Core\\Entity\\Controller\\EntityController::addPage',
      '_title_callback' => 'Drupal\\Core\\Entity\\Controller\\EntityController::addTitle',
      'entity_type_id' => 'the_entity_type_id',
    ]);
    $route
      ->setRequirement('_entity_create_any_access', 'the_entity_type_id');
    $data['add_page'] = [
      clone $route,
      $entity_type3
        ->reveal(),
    ];
    return $data;
  }

  /**
   * @covers ::getAddFormRoute
   * @dataProvider providerTestGetAddFormRoute
   */
  public function testGetAddFormRoute(Route $expected = NULL, EntityTypeInterface $entity_type, EntityTypeInterface $bundle_entity_type = NULL, FieldStorageDefinitionInterface $field_storage_definition = NULL) {
    if ($bundle_entity_type) {
      $this->entityTypeManager
        ->getDefinition('the_bundle_entity_type_id')
        ->willReturn($bundle_entity_type);
      if ($field_storage_definition) {
        $this->entityFieldManager
          ->getFieldStorageDefinitions('the_bundle_entity_type_id')
          ->willReturn([
          'id' => $field_storage_definition,
        ]);
      }
    }
    $route = $this->routeProvider
      ->getAddFormRoute($entity_type);
    $this
      ->assertEquals($expected, $route);
  }
  public function providerTestGetAddFormRoute() {
    $data = [];
    $entity_type1 = $this
      ->getEntityType();
    $entity_type1
      ->hasLinkTemplate('add-form')
      ->willReturn(FALSE);
    $data['no_add_form_link_template'] = [
      NULL,
      $entity_type1
        ->reveal(),
    ];
    $entity_type2 = $this
      ->getEntityType();
    $entity_type2
      ->getBundleEntityType()
      ->willReturn(NULL);
    $entity_type2
      ->hasLinkTemplate('add-form')
      ->willReturn(TRUE);
    $entity_type2
      ->id()
      ->willReturn('the_entity_type_id');
    $entity_type2
      ->getLinkTemplate('add-form')
      ->willReturn('/the/add/form/link/template');
    $entity_type2
      ->getFormClass('add')
      ->willReturn(NULL);
    $entity_type2
      ->getKey('bundle')
      ->willReturn(NULL);
    $route = (new Route('/the/add/form/link/template'))
      ->setDefaults([
      '_entity_form' => 'the_entity_type_id.default',
      'entity_type_id' => 'the_entity_type_id',
      '_title_callback' => 'Drupal\\Core\\Entity\\Controller\\EntityController::addTitle',
    ])
      ->setRequirement('_entity_create_access', 'the_entity_type_id');
    $data['no_add_form_no_bundle'] = [
      clone $route,
      $entity_type2
        ->reveal(),
    ];
    $entity_type3 = $this
      ->getEntityType($entity_type2);
    $entity_type3
      ->getFormClass('add')
      ->willReturn('Drupal\\Core\\Entity\\EntityForm');
    $route
      ->setDefault('_entity_form', 'the_entity_type_id.add');
    $data['add_form_no_bundle'] = [
      clone $route,
      $entity_type3
        ->reveal(),
    ];
    $entity_type4 = $this
      ->getEntityType($entity_type3);
    $entity_type4
      ->getKey('bundle')
      ->willReturn('the_bundle_key');
    $entity_type4
      ->getBundleEntityType()
      ->willReturn(NULL);
    $entity_type4
      ->getLinkTemplate('add-form')
      ->willReturn('/the/add/form/link/template/{the_bundle_key}');
    $route
      ->setPath('/the/add/form/link/template/{the_bundle_key}');
    $route
      ->setDefault('_title_callback', 'Drupal\\Core\\Entity\\Controller\\EntityController::addBundleTitle')
      ->setDefault('bundle_parameter', 'the_bundle_key')
      ->setRequirement('_entity_create_access', 'the_entity_type_id:{the_bundle_key}');
    $data['add_form_bundle_static'] = [
      clone $route,
      $entity_type4
        ->reveal(),
    ];
    $entity_type5 = $this
      ->getEntityType($entity_type4);
    $entity_type5
      ->getBundleEntityType()
      ->willReturn('the_bundle_entity_type_id');
    $entity_type5
      ->getLinkTemplate('add-form')
      ->willReturn('/the/add/form/link/template/{the_bundle_entity_type_id}');
    $bundle_entity_type = $this
      ->getEntityType();
    $bundle_entity_type
      ->entityClassImplements(FieldableEntityInterface::class)
      ->willReturn(FALSE);
    $route
      ->setPath('/the/add/form/link/template/{the_bundle_entity_type_id}');
    $route
      ->setDefault('bundle_parameter', 'the_bundle_entity_type_id')
      ->setRequirement('_entity_create_access', 'the_entity_type_id:{the_bundle_entity_type_id}')
      ->setOption('parameters', [
      'the_bundle_entity_type_id' => [
        'type' => 'entity:the_bundle_entity_type_id',
      ],
    ]);
    $data['add_form_bundle_entity_id_key_type_null'] = [
      clone $route,
      $entity_type5
        ->reveal(),
      $bundle_entity_type
        ->reveal(),
    ];
    $entity_type6 = $this
      ->getEntityType($entity_type5);
    $bundle_entity_type = $this
      ->getEntityType();
    $bundle_entity_type
      ->entityClassImplements(FieldableEntityInterface::class)
      ->willReturn(TRUE);
    $field_storage_definition = $this
      ->prophesize(FieldStorageDefinitionInterface::class);
    $field_storage_definition
      ->getType()
      ->willReturn('integer');
    $route
      ->setRequirement('the_entity_type_id', '\\d+');
    $data['add_form_bundle_entity_id_key_type_integer'] = [
      clone $route,
      $entity_type6
        ->reveal(),
      $bundle_entity_type
        ->reveal(),
      $field_storage_definition
        ->reveal(),
    ];
    $entity_type7 = $this
      ->getEntityType($entity_type6);
    $bundle_entity_type = $this
      ->prophesize(ConfigEntityTypeInterface::class);
    $bundle_entity_type
      ->entityClassImplements(FieldableEntityInterface::class)
      ->willReturn(FALSE);
    $field_storage_definition = $this
      ->prophesize(FieldStorageDefinitionInterface::class);
    $route
      ->setRequirements([
      '_entity_create_access' => $route
        ->getRequirement('_entity_create_access'),
    ])
      ->setOption('parameters', [
      'the_bundle_entity_type_id' => [
        'type' => 'entity:the_bundle_entity_type_id',
        'with_config_overrides' => TRUE,
      ],
    ]);
    $data['add_form_bundle_entity_id_key_type_integer'] = [
      clone $route,
      $entity_type7
        ->reveal(),
      $bundle_entity_type
        ->reveal(),
      $field_storage_definition
        ->reveal(),
    ];
    return $data;
  }

  /**
   * @covers ::getCanonicalRoute
   * @dataProvider providerTestGetCanonicalRoute
   */
  public function testGetCanonicalRoute(Route $expected = NULL, EntityTypeInterface $entity_type, FieldStorageDefinitionInterface $field_storage_definition = NULL) {
    if ($field_storage_definition) {
      $this->entityFieldManager
        ->getFieldStorageDefinitions($entity_type
        ->id())
        ->willReturn([
        $entity_type
          ->getKey('id') => $field_storage_definition,
      ]);
    }
    $route = $this->routeProvider
      ->getCanonicalRoute($entity_type);
    $this
      ->assertEquals($expected, $route);
  }
  public function providerTestGetCanonicalRoute() {
    $data = [];
    $entity_type1 = $this
      ->getEntityType();
    $entity_type1
      ->hasLinkTemplate('canonical')
      ->willReturn(FALSE);
    $data['no_canonical_link_template'] = [
      NULL,
      $entity_type1
        ->reveal(),
    ];
    $entity_type2 = $this
      ->getEntityType();
    $entity_type2
      ->hasLinkTemplate('canonical')
      ->willReturn(TRUE);
    $entity_type2
      ->hasViewBuilderClass()
      ->willReturn(FALSE);
    $data['no_view_builder'] = [
      NULL,
      $entity_type2
        ->reveal(),
    ];
    $entity_type3 = $this
      ->getEntityType($entity_type2);
    $entity_type3
      ->hasViewBuilderClass()
      ->willReturn(TRUE);
    $entity_type3
      ->id()
      ->willReturn('the_entity_type_id');
    $entity_type3
      ->getLinkTemplate('canonical')
      ->willReturn('/the/canonical/link/template');
    $entity_type3
      ->entityClassImplements(FieldableEntityInterface::class)
      ->willReturn(FALSE);
    $route = (new Route('/the/canonical/link/template'))
      ->setDefaults([
      '_entity_view' => 'the_entity_type_id.full',
      '_title_callback' => '\\Drupal\\Core\\Entity\\Controller\\EntityController::title',
    ])
      ->setRequirements([
      '_entity_access' => 'the_entity_type_id.view',
    ])
      ->setOptions([
      'parameters' => [
        'the_entity_type_id' => [
          'type' => 'entity:the_entity_type_id',
        ],
      ],
    ]);
    $data['id_key_type_null'] = [
      clone $route,
      $entity_type3
        ->reveal(),
    ];
    $entity_type4 = $this
      ->getEntityType($entity_type3);
    $entity_type4
      ->entityClassImplements(FieldableEntityInterface::class)
      ->willReturn(TRUE);
    $entity_type4
      ->getKey('id')
      ->willReturn('id');
    $route
      ->setRequirement('the_entity_type_id', '\\d+');
    $field_storage_definition = $this
      ->prophesize(FieldStorageDefinitionInterface::class);
    $field_storage_definition
      ->getType()
      ->willReturn('integer');
    $data['id_key_type_integer'] = [
      clone $route,
      $entity_type4
        ->reveal(),
      $field_storage_definition
        ->reveal(),
    ];
    return $data;
  }

  /**
   * @covers ::getCollectionRoute
   * @dataProvider providerTestGetCollectionRoute
   */
  public function testGetCollectionRoute(Route $expected = NULL, EntityTypeInterface $entity_type) {
    $route = $this->routeProvider
      ->getCollectionRoute($entity_type);
    $this
      ->assertEquals($expected, $route);
  }
  public function providerTestGetCollectionRoute() {
    $data = [];
    $entity_type1 = $this
      ->getEntityType();
    $entity_type1
      ->hasLinkTemplate('collection')
      ->willReturn(FALSE);
    $data['no_collection_link_template'] = [
      NULL,
      $entity_type1
        ->reveal(),
    ];
    $entity_type2 = $this
      ->getEntityType();
    $entity_type2
      ->hasLinkTemplate('collection')
      ->willReturn(TRUE);
    $entity_type2
      ->hasListBuilderClass()
      ->willReturn(FALSE);
    $data['no_list_builder'] = [
      NULL,
      $entity_type2
        ->reveal(),
    ];
    $entity_type3 = $this
      ->getEntityType($entity_type2);
    $entity_type3
      ->hasListBuilderClass()
      ->willReturn(TRUE);
    $entity_type3
      ->getAdminPermission()
      ->willReturn(FALSE);
    $data['no_admin_permission'] = [
      NULL,
      $entity_type3
        ->reveal(),
    ];
    $entity_type4 = $this
      ->getEntityType($entity_type3);
    $entity_type4
      ->getAdminPermission()
      ->willReturn('administer the entity type');
    $entity_type4
      ->id()
      ->willReturn('the_entity_type_id');
    $entity_type4
      ->getLabel()
      ->willReturn('The entity type');
    $entity_type4
      ->getCollectionLabel()
      ->willReturn(new TranslatableMarkup('Test entities'));
    $entity_type4
      ->getLinkTemplate('collection')
      ->willReturn('/the/collection/link/template');
    $entity_type4
      ->entityClassImplements(FieldableEntityInterface::class)
      ->willReturn(FALSE);
    $route = (new Route('/the/collection/link/template'))
      ->setDefaults([
      '_entity_list' => 'the_entity_type_id',
      '_title' => 'Test entities',
      '_title_arguments' => [],
      '_title_context' => '',
    ])
      ->setRequirements([
      '_permission' => 'administer the entity type',
    ]);
    $data['collection_route'] = [
      clone $route,
      $entity_type4
        ->reveal(),
    ];
    return $data;
  }

  /**
   * @covers ::getEntityTypeIdKeyType
   */
  public function testGetEntityTypeIdKeyType() {
    $entity_type = $this
      ->prophesize(EntityTypeInterface::class);
    $entity_type
      ->entityClassImplements(FieldableEntityInterface::class)
      ->willReturn(TRUE);
    $entity_type
      ->id()
      ->willReturn('the_entity_type_id');
    $entity_type
      ->getKey('id')
      ->willReturn('id');
    $field_storage_definition = $this
      ->prophesize(FieldStorageDefinitionInterface::class);
    $field_storage_definition
      ->getType()
      ->willReturn('integer');
    $this->entityFieldManager
      ->getFieldStorageDefinitions('the_entity_type_id')
      ->willReturn([
      'id' => $field_storage_definition,
    ]);
    $type = $this->routeProvider
      ->getEntityTypeIdKeyType($entity_type
      ->reveal());
    $this
      ->assertSame('integer', $type);
  }

  /**
   * @covers ::getEntityTypeIdKeyType
   */
  public function testGetEntityTypeIdKeyTypeNotFieldable() {
    $entity_type = $this
      ->prophesize(EntityTypeInterface::class);
    $entity_type
      ->entityClassImplements(FieldableEntityInterface::class)
      ->willReturn(FALSE);
    $this->entityFieldManager
      ->getFieldStorageDefinitions(Argument::any())
      ->shouldNotBeCalled();
    $type = $this->routeProvider
      ->getEntityTypeIdKeyType($entity_type
      ->reveal());
    $this
      ->assertNull($type);
  }

  /**
   * @param \Prophecy\Prophecy\ObjectProphecy $base_entity_type
   * @return \Prophecy\Prophecy\ObjectProphecy
   */
  protected function getEntityType(ObjectProphecy $base_entity_type = NULL) {
    $entity_type = $this
      ->prophesize(EntityTypeInterface::class);
    if ($base_entity_type) {
      foreach ($base_entity_type
        ->getMethodProphecies() as $method => $prophecies) {
        foreach ($prophecies as $prophecy) {
          $entity_type
            ->addMethodProphecy(clone $prophecy);
        }
      }
    }
    return $entity_type;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DefaultHtmlRouteProviderTest::$entityFieldManager protected property The entity field manager prophecy used in the test.
DefaultHtmlRouteProviderTest::$entityTypeManager protected property The entity type manager prophecy used in the test.
DefaultHtmlRouteProviderTest::$routeProvider protected property The HTML route provider used in the test.
DefaultHtmlRouteProviderTest::getEntityType protected function
DefaultHtmlRouteProviderTest::providerTestGetAddFormRoute public function
DefaultHtmlRouteProviderTest::providerTestGetAddPageRoute public function
DefaultHtmlRouteProviderTest::providerTestGetCanonicalRoute public function
DefaultHtmlRouteProviderTest::providerTestGetCollectionRoute public function
DefaultHtmlRouteProviderTest::setUp protected function Overrides UnitTestCase::setUp
DefaultHtmlRouteProviderTest::testGetAddFormRoute public function @covers ::getAddFormRoute @dataProvider providerTestGetAddFormRoute
DefaultHtmlRouteProviderTest::testGetAddPageRoute public function @covers ::getAddPageRoute @dataProvider providerTestGetAddPageRoute
DefaultHtmlRouteProviderTest::testGetCanonicalRoute public function @covers ::getCanonicalRoute @dataProvider providerTestGetCanonicalRoute
DefaultHtmlRouteProviderTest::testGetCollectionRoute public function @covers ::getCollectionRoute @dataProvider providerTestGetCollectionRoute
DefaultHtmlRouteProviderTest::testGetEntityTypeIdKeyType public function @covers ::getEntityTypeIdKeyType
DefaultHtmlRouteProviderTest::testGetEntityTypeIdKeyTypeNotFieldable public function @covers ::getEntityTypeIdKeyType
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.
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.