class LinkFormatterTest
Same name in this branch
- main core/modules/link/tests/src/Kernel/LinkFormatterTest.php \Drupal\Tests\link\Kernel\LinkFormatterTest
Same name and namespace in other branches
- 11.x core/modules/link/tests/src/Unit/LinkFormatterTest.php \Drupal\Tests\link\Unit\LinkFormatterTest
- 11.x core/modules/link/tests/src/Kernel/LinkFormatterTest.php \Drupal\Tests\link\Kernel\LinkFormatterTest
- 10 core/modules/link/tests/src/Unit/LinkFormatterTest.php \Drupal\Tests\link\Unit\LinkFormatterTest
- 9 core/modules/link/tests/src/Unit/LinkFormatterTest.php \Drupal\Tests\link\Unit\LinkFormatterTest
Tests the Field Formatter for the link field type.
Attributes
#[Group('link')]
Hierarchy
- class \Drupal\Tests\DrupalTestCase uses \Drupal\Tests\DrupalTestCaseTrait extends \PHPUnit\Framework\TestCase
- class \Drupal\Tests\UnitTestCase uses \Prophecy\PhpUnit\ProphecyTrait, \Drupal\Tests\RandomGeneratorTrait extends \Drupal\Tests\DrupalTestCase
- class \Drupal\Tests\link\Unit\LinkFormatterTest extends \Drupal\Tests\UnitTestCase
- class \Drupal\Tests\UnitTestCase uses \Prophecy\PhpUnit\ProphecyTrait, \Drupal\Tests\RandomGeneratorTrait extends \Drupal\Tests\DrupalTestCase
Expanded class hierarchy of LinkFormatterTest
File
-
core/
modules/ link/ tests/ src/ Unit/ LinkFormatterTest.php, line 23
Namespace
Drupal\Tests\link\UnitView source
class LinkFormatterTest extends UnitTestCase {
/**
* Tests when LinkItem::getUrl with malformed URL renders empty link.
*
* LinkItem::getUrl will throw \InvalidArgumentException.
*/
public function testFormatterLinkItemUrlMalformed() : void {
$linkItem = $this->createMock(LinkItemInterface::class);
$linkItem->expects($this->once())
->method('getUrl')
->willThrowException(new \InvalidArgumentException());
$fieldDefinition = $this->createStub(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->createStub(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 {
$linkItem = $this->createMock(LinkItemInterface::class);
$linkItem->expects($this->once())
->method('getUrl')
->willThrowException(new \Exception('Unexpected!!!'));
$fieldDefinition = $this->createStub(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->createStub(PathValidatorInterface::class);
$linkFormatter = new LinkFormatter('', [], $fieldDefinition, [], '', '', [], $pathValidator);
$this->expectException(\Exception::class);
$this->expectExceptionMessageIs('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);
$linkItem->expects($this->once())
->method('getUrl')
->willReturn($expectedUrl);
$fieldDefinition = $this->createStub(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->createStub(PathValidatorInterface::class);
$linkFormatter = new LinkFormatter('', [], $fieldDefinition, [], '', '', [], $pathValidator);
$elements = $linkFormatter->viewElements($fieldList, 'zh');
$this->assertEquals([
[
'#type' => 'link',
'#title' => 'http://example.com',
'#url' => $expectedUrl,
],
], $elements);
}
/**
* Tests settings summary messages.
*/
public function testSettingsSummary(array $settings, array $expected_summaries) : void {
$container = new ContainerBuilder();
$container->set('string_translation', $this->getStringTranslationStub());
\Drupal::setContainer($container);
$mock = $this->getMockBuilder(LinkFormatter::class)
->disableOriginalConstructor()
->onlyMethods([
'getPluginId',
'getSettings',
])
->getMock();
$mock->expects($this->once())
->method('getPluginId')
->willReturn('link');
$mock->expects($this->once())
->method('getSettings')
->willReturn($settings);
$summaries = $mock->settingsSummary();
// Summaries are translated so cast them to strings before comparison.
foreach ($summaries as $key => $summary) {
$summaries[$key] = (string) $summary;
}
$this->assertEqualsCanonicalizing($expected_summaries, $summaries);
}
/**
* Provides test cases for ::testSettingsSummary().
*/
public static function providerSettingsSummary() : array {
return [
[
[],
[
'Link text not trimmed',
],
],
[
[
'trim_length' => 80,
],
[
'Link text trimmed to 80 characters',
],
],
[
[
'trim_length' => 20,
],
[
'Link text trimmed to 20 characters',
],
],
[
[
'trim_length' => 20,
'url_only' => TRUE,
'url_plain' => TRUE,
],
[
'Link text not trimmed',
'Show URL only as plain-text',
],
],
[
[
'trim_length' => 20,
'url_only' => TRUE,
'url_plain' => FALSE,
],
[
'Link text not trimmed',
'Show URL only',
],
],
[
[
'rel' => 'nofollow',
],
[
'Link text not trimmed',
'Add rel="nofollow"',
],
],
[
[
'target' => '_blank',
],
[
'Link text not trimmed',
'Open link in new window',
],
],
];
}
}
Members
| Title Sort descending | Modifiers | Object type | Summary | Overrides |
|---|---|---|---|---|
| DrupalTestCaseTrait::checkErrorHandlerOnTearDown | public | function | Checks the test error handler after test execution. | 1 |
| DrupalTestCaseTrait::setDebugDumpHandler | public static | function | Registers the dumper CLI handler when the DebugDump extension is enabled. | |
| 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::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::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 | 358 | |
| UnitTestCase::setupMockIterator | protected | function | Set up a traversable class mock to return specific items when iterated. |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.