class EntityLinkTest

Same name in this branch
  1. 10 core/modules/comment/tests/src/Unit/Plugin/views/field/EntityLinkTest.php \Drupal\Tests\comment\Unit\Plugin\views\field\EntityLinkTest
Same name and namespace in other branches
  1. 9 core/tests/Drupal/Tests/Core/Entity/EntityLinkTest.php \Drupal\Tests\Core\Entity\EntityLinkTest
  2. 8.9.x core/tests/Drupal/Tests/Core/Entity/EntityLinkTest.php \Drupal\Tests\Core\Entity\EntityLinkTest
  3. 11.x core/modules/comment/tests/src/Unit/Plugin/views/field/EntityLinkTest.php \Drupal\Tests\comment\Unit\Plugin\views\field\EntityLinkTest
  4. 11.x core/tests/Drupal/Tests/Core/Entity/EntityLinkTest.php \Drupal\Tests\Core\Entity\EntityLinkTest

@coversDefaultClass \Drupal\Core\Entity\EntityBase
@group Entity

Hierarchy

Expanded class hierarchy of EntityLinkTest

File

core/tests/Drupal/Tests/Core/Entity/EntityLinkTest.php, line 18

Namespace

Drupal\Tests\Core\Entity
View source
class EntityLinkTest extends UnitTestCase {
  
  /**
   * The mocked entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $entityTypeManager;
  
  /**
   * The tested link generator.
   *
   * @var \Drupal\Core\Utility\LinkGeneratorInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $linkGenerator;
  
  /**
   * The mocked language manager.
   *
   * @var \Drupal\Core\Language\LanguageManagerInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $languageManager;
  
  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    $this->entityTypeManager = $this->createMock(EntityTypeManagerInterface::class);
    $this->linkGenerator = $this->createMock('Drupal\\Core\\Utility\\LinkGeneratorInterface');
    $this->languageManager = $this->createMock('Drupal\\Core\\Language\\LanguageManagerInterface');
    $container = new ContainerBuilder();
    $container->set('entity_type.manager', $this->entityTypeManager);
    $container->set('link_generator', $this->linkGenerator);
    $container->set('language_manager', $this->languageManager);
    \Drupal::setContainer($container);
  }
  
  /**
   * Tests for the Entity::toLink() method.
   *
   * @covers ::toLink
   *
   * @dataProvider providerTestLink
   */
  public function testToLink($entity_label, $link_text, $expected_text, $link_rel = 'canonical', array $link_options = []) : void {
    $language = new Language([
      'id' => 'es',
    ]);
    $link_options += [
      'language' => $language,
    ];
    $this->languageManager
      ->expects($this->any())
      ->method('getLanguage')
      ->with('es')
      ->willReturn($language);
    $route_name_map = [
      'canonical' => 'entity.test_entity_type.canonical',
      'edit-form' => 'entity.test_entity_type.edit_form',
    ];
    $route_name = $route_name_map[$link_rel];
    $entity_id = 'test_entity_id';
    $entity_type_id = 'test_entity_type';
    $entity_type = $this->createMock('Drupal\\Core\\Entity\\EntityTypeInterface');
    $entity_type->expects($this->once())
      ->method('getLinkTemplates')
      ->willReturn($route_name_map);
    $entity_type->expects($this->any())
      ->method('getKey')
      ->willReturnMap([
      [
        'label',
        'label',
      ],
      [
        'langcode',
        'langcode',
      ],
    ]);
    $this->entityTypeManager
      ->expects($this->any())
      ->method('getDefinition')
      ->with($entity_type_id)
      ->willReturn($entity_type);
    /** @var \Drupal\Core\Entity\Entity $entity */
    $entity = $this->getMockForAbstractClass(ConfigEntityBase::class, [
      [
        'id' => $entity_id,
        'label' => $entity_label,
        'langcode' => 'es',
      ],
      $entity_type_id,
    ]);
    $expected_link = Link::createFromRoute($expected_text, $route_name, [
      $entity_type_id => $entity_id,
    ], [
      'entity_type' => $entity_type_id,
      'entity' => $entity,
    ] + $link_options);
    $result_link = $entity->toLink($link_text, $link_rel, $link_options);
    $this->assertEquals($expected_link, $result_link);
  }
  
  /**
   * Provides test data for testLink().
   */
  public static function providerTestLink() {
    $data = [];
    $data[] = [
      'some_entity_label',
      'link text',
      'link text',
    ];
    $data[] = [
      'some_entity_label',
      NULL,
      'some_entity_label',
    ];
    $data[] = [
      'some_entity_label',
      '0',
      '0',
    ];
    $data[] = [
      'some_entity_label',
      'link text',
      'link text',
      'edit-form',
    ];
    $data[] = [
      'some_entity_label',
      'link text',
      'link text',
      'edit-form',
    ];
    $data[] = [
      'some_entity_label',
      'link text',
      'link text',
      'edit-form',
      [
        'foo' => 'qwer',
      ],
    ];
    return $data;
  }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overriden Title Overrides
EntityLinkTest::$entityTypeManager protected property The mocked entity type manager.
EntityLinkTest::$languageManager protected property The mocked language manager.
EntityLinkTest::$linkGenerator protected property The tested link generator.
EntityLinkTest::providerTestLink public static function Provides test data for testLink().
EntityLinkTest::setUp protected function Overrides UnitTestCase::setUp
EntityLinkTest::testToLink public function Tests for the Entity::toLink() method.
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.
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.
RandomGeneratorTrait::randomStringValidate Deprecated public function Callback for random string validation.
UnitTestCase::$root protected property The app root. 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::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setUpBeforeClass public static function
UnitTestCase::__get public function

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