You are here

class DefaultHtmlRouteProviderTest in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 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 23
Contains \Drupal\Tests\Core\Entity\Routing\DefaultHtmlRouteProviderTest.

Namespace

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

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

  /**
   * @covers ::getEntityTypeIdKeyType
   */
  public function testGetEntityTypeIdKeyTypeNotFieldable() {
    $entity_manager = $this
      ->prophesize(EntityManagerInterface::class);
    $route_provider = new TestDefaultHtmlRouteProvider($entity_manager
      ->reveal());
    $entity_type = $this
      ->prophesize(EntityTypeInterface::class);
    $entity_type
      ->isSubclassOf(FieldableEntityInterface::class)
      ->willReturn(FALSE);
    $entity_manager
      ->getFieldStorageDefinitions(Argument::any())
      ->shouldNotBeCalled();
    $type = $route_provider
      ->getEntityTypeIdKeyType($entity_type
      ->reveal());
    $this
      ->assertNull($type);
  }

  /**
   * @covers ::getCanonicalRoute
   * @dataProvider providerTestGetCanonicalRoute
   */
  public function testGetCanonicalRoute($entity_type_prophecy, $expected, $field_storage_definition = NULL) {
    $entity_manager = $this
      ->prophesize(EntityManagerInterface::class);
    $route_provider = new TestDefaultHtmlRouteProvider($entity_manager
      ->reveal());
    $entity_type = $entity_type_prophecy
      ->reveal();
    if ($field_storage_definition) {
      $entity_manager
        ->getFieldStorageDefinitions($entity_type
        ->id())
        ->willReturn([
        $entity_type
          ->getKey('id') => $field_storage_definition,
      ]);
    }
    $route = $route_provider
      ->getCanonicalRoute($entity_type);
    $this
      ->assertEquals($expected, $route);
  }
  public function providerTestGetCanonicalRoute() {
    $data = [];
    $entity_type1 = $this
      ->prophesize(EntityTypeInterface::class);
    $entity_type1
      ->hasLinkTemplate('canonical')
      ->willReturn(FALSE);
    $data['no_canonical_link_template'] = [
      $entity_type1,
      NULL,
    ];
    $entity_type2 = $this
      ->prophesize(EntityTypeInterface::class);
    $entity_type2
      ->hasLinkTemplate('canonical')
      ->willReturn(TRUE);
    $entity_type2
      ->hasViewBuilderClass()
      ->willReturn(FALSE);
    $data['no_view_builder'] = [
      $entity_type2,
      NULL,
    ];
    $entity_type3 = $this
      ->prophesize(EntityTypeInterface::class);
    $entity_type3
      ->hasLinkTemplate('canonical')
      ->willReturn(TRUE);
    $entity_type3
      ->hasViewBuilderClass()
      ->willReturn(TRUE);
    $entity_type3
      ->id()
      ->willReturn('the_entity_type_id');
    $entity_type3
      ->getLinkTemplate('canonical')
      ->willReturn('/the/canonical/link/template');
    $entity_type3
      ->isSubclassOf(FieldableEntityInterface::class)
      ->willReturn(FALSE);
    $route3 = (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'] = [
      $entity_type3,
      $route3,
    ];
    $entity_type4 = $this
      ->prophesize(EntityTypeInterface::class);
    $entity_type4
      ->hasLinkTemplate('canonical')
      ->willReturn(TRUE);
    $entity_type4
      ->hasViewBuilderClass()
      ->willReturn(TRUE);
    $entity_type4
      ->id()
      ->willReturn('the_entity_type_id');
    $entity_type4
      ->getLinkTemplate('canonical')
      ->willReturn('/the/canonical/link/template');
    $entity_type4
      ->isSubclassOf(FieldableEntityInterface::class)
      ->willReturn(TRUE);
    $entity_type4
      ->getKey('id')
      ->willReturn('id');
    $route4 = (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',
      'the_entity_type_id' => '\\d+',
    ])
      ->setOptions([
      'parameters' => [
        'the_entity_type_id' => [
          'type' => 'entity:the_entity_type_id',
        ],
      ],
    ]);
    $field_storage_definition = $this
      ->prophesize(FieldStorageDefinitionInterface::class);
    $field_storage_definition
      ->getType()
      ->willReturn('integer');
    $data['id_key_type_integer'] = [
      $entity_type4,
      $route4,
      $field_storage_definition,
    ];
    return $data;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DefaultHtmlRouteProviderTest::providerTestGetCanonicalRoute public function
DefaultHtmlRouteProviderTest::testGetCanonicalRoute public function @covers ::getCanonicalRoute @dataProvider providerTestGetCanonicalRoute
DefaultHtmlRouteProviderTest::testGetEntityTypeIdKeyType public function @covers ::getEntityTypeIdKeyType
DefaultHtmlRouteProviderTest::testGetEntityTypeIdKeyTypeNotFieldable public function @covers ::getEntityTypeIdKeyType
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root.
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName protected function Mocks a block with a block plugin.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed in 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::setUp protected function 259