class LinkFormatterTest

Same name and namespace in other branches
  1. 10 core/modules/link/tests/src/Unit/LinkFormatterTest.php \Drupal\Tests\link\Unit\LinkFormatterTest
  2. 11.x 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 22

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() {
        $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')
            ->will($this->returnValue($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() {
        $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')
            ->will($this->returnValue($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() {
        $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')
            ->will($this->returnValue($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',
                '#options' => [],
                '#url' => $expectedUrl,
            ],
        ], $elements);
    }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overrides
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.
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::setUp protected function 338
UnitTestCase::setUpBeforeClass public static function

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