class LinkFormatterTest

Same name in other branches
  1. 9 core/modules/link/tests/src/Unit/LinkFormatterTest.php \Drupal\Tests\link\Unit\LinkFormatterTest
  2. 10 core/modules/link/tests/src/Unit/LinkFormatterTest.php \Drupal\Tests\link\Unit\LinkFormatterTest

Tests the Field Formatter for the link field type.

@group link

Hierarchy

Expanded class hierarchy of LinkFormatterTest

File

core/modules/link/tests/src/Unit/LinkFormatterTest.php, line 24

Namespace

Drupal\Tests\link\Unit
View source
class LinkFormatterTest extends UnitTestCase {
    
    /**
     * Tests when LinkItem::getUrl with malformed URL renders empty link.
     *
     * LinkItem::getUrl will throw \InvalidArgumentException.
     */
    public function testFormatterLinkItemUrlMalformed() : void {
        $entity = $this->createMock(EntityInterface::class);
        $linkItem = $this->createMock(LinkItemInterface::class);
        $exception = new \InvalidArgumentException();
        $linkItem->expects($this->any())
            ->method('getParent')
            ->willReturn($entity);
        $linkItem->expects($this->once())
            ->method('getUrl')
            ->willThrowException($exception);
        $linkItem->expects($this->any())
            ->method('__get')
            ->with('options')
            ->willReturn([]);
        $fieldDefinition = $this->createMock(FieldDefinitionInterface::class);
        $fieldList = new FieldItemList($fieldDefinition, '', $linkItem);
        $fieldTypePluginManager = $this->createMock(FieldTypePluginManagerInterface::class);
        $fieldTypePluginManager->expects($this->once())
            ->method('createFieldItem')
            ->willReturn($linkItem);
        $urlGenerator = $this->createMock(UrlGenerator::class);
        $urlGenerator->expects($this->once())
            ->method('generateFromRoute')
            ->with('<none>', [], [], FALSE)
            ->willReturn('http://example.com');
        $container = new ContainerBuilder();
        $container->set('plugin.manager.field.field_type', $fieldTypePluginManager);
        $container->set('url_generator', $urlGenerator);
        \Drupal::setContainer($container);
        $fieldList->setValue([
            $linkItem,
        ]);
        $pathValidator = $this->createMock(PathValidatorInterface::class);
        $linkFormatter = new LinkFormatter('', [], $fieldDefinition, [], '', '', [], $pathValidator);
        $elements = $linkFormatter->viewElements($fieldList, 'es');
        $this->assertEquals('link', $elements[0]['#type']);
    }
    
    /**
     * Tests when LinkItem::getUrl throws an unexpected exception.
     */
    public function testFormatterLinkItemUrlUnexpectedException() : void {
        $exception = new \Exception('Unexpected!!!');
        $linkItem = $this->createMock(LinkItemInterface::class);
        $entity = $this->createMock(EntityInterface::class);
        $linkItem->expects($this->any())
            ->method('getParent')
            ->willReturn($entity);
        $linkItem->expects($this->once())
            ->method('getUrl')
            ->willThrowException($exception);
        $linkItem->expects($this->any())
            ->method('__get')
            ->with('options')
            ->willReturn([]);
        $fieldDefinition = $this->createMock(FieldDefinitionInterface::class);
        $fieldList = new FieldItemList($fieldDefinition, '', $linkItem);
        $fieldTypePluginManager = $this->createMock(FieldTypePluginManagerInterface::class);
        $fieldTypePluginManager->expects($this->once())
            ->method('createFieldItem')
            ->willReturn($linkItem);
        $container = new ContainerBuilder();
        $container->set('plugin.manager.field.field_type', $fieldTypePluginManager);
        \Drupal::setContainer($container);
        $fieldList->setValue([
            $linkItem,
        ]);
        $pathValidator = $this->createMock(PathValidatorInterface::class);
        $linkFormatter = new LinkFormatter('', [], $fieldDefinition, [], '', '', [], $pathValidator);
        $this->expectException(\Exception::class);
        $this->expectExceptionMessage('Unexpected!!!');
        $linkFormatter->viewElements($fieldList, 'fr');
    }
    
    /**
     * Tests when LinkItem::getUrl returns a functional URL.
     */
    public function testFormatterLinkItem() : void {
        $expectedUrl = Url::fromUri('route:<front>');
        $linkItem = $this->createMock(LinkItemInterface::class);
        $entity = $this->createMock(EntityInterface::class);
        $linkItem->expects($this->any())
            ->method('getParent')
            ->willReturn($entity);
        $linkItem->expects($this->once())
            ->method('getUrl')
            ->willReturn($expectedUrl);
        $linkItem->expects($this->any())
            ->method('__get')
            ->with('options')
            ->willReturn([]);
        $fieldDefinition = $this->createMock(FieldDefinitionInterface::class);
        $fieldList = new FieldItemList($fieldDefinition, '', $linkItem);
        $fieldTypePluginManager = $this->createMock(FieldTypePluginManagerInterface::class);
        $fieldTypePluginManager->expects($this->once())
            ->method('createFieldItem')
            ->willReturn($linkItem);
        $urlGenerator = $this->createMock(UrlGenerator::class);
        $urlGenerator->expects($this->once())
            ->method('generateFromRoute')
            ->with('<front>', [], [], FALSE)
            ->willReturn('http://example.com');
        $container = new ContainerBuilder();
        $container->set('plugin.manager.field.field_type', $fieldTypePluginManager);
        $container->set('url_generator', $urlGenerator);
        \Drupal::setContainer($container);
        $fieldList->setValue([
            $linkItem,
        ]);
        $pathValidator = $this->createMock(PathValidatorInterface::class);
        $linkFormatter = new LinkFormatter('', [], $fieldDefinition, [], '', '', [], $pathValidator);
        $elements = $linkFormatter->viewElements($fieldList, 'zh');
        $this->assertEquals([
            [
                '#type' => 'link',
                '#title' => 'http://example.com',
                '#url' => $expectedUrl,
            ],
        ], $elements);
    }

}

Members

Title Sort descending Modifiers Object type Summary Overrides
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.
LinkFormatterTest::testFormatterLinkItem public function Tests when LinkItem::getUrl returns a functional URL.
LinkFormatterTest::testFormatterLinkItemUrlMalformed public function Tests when LinkItem::getUrl with malformed URL renders empty link.
LinkFormatterTest::testFormatterLinkItemUrlUnexpectedException public function Tests when LinkItem::getUrl throws an unexpected exception.
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.