class StringFormatterTest

Same name in this branch
  1. 11.x core/modules/field/tests/src/Kernel/KernelString/StringFormatterTest.php \Drupal\Tests\field\Kernel\KernelString\StringFormatterTest
Same name in other branches
  1. 9 core/modules/field/tests/src/Kernel/String/StringFormatterTest.php \Drupal\Tests\field\Kernel\String\StringFormatterTest
  2. 8.9.x core/modules/field/tests/src/Kernel/String/StringFormatterTest.php \Drupal\Tests\field\Kernel\String\StringFormatterTest
  3. 10 core/modules/field/tests/src/Kernel/KernelString/StringFormatterTest.php \Drupal\Tests\field\Kernel\KernelString\StringFormatterTest

Tests the string field formatter.

@group field

@coversDefaultClass \Drupal\Core\Field\Plugin\Field\FieldFormatter\StringFormatter

Hierarchy

  • class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses \Drupal\Tests\PhpUnitCompatibilityTrait, \Prophecy\PhpUnit\ProphecyTrait, \Drupal\TestTools\Extension\DeprecationBridge\ExpectDeprecationTrait, \Drupal\Tests\RandomGeneratorTrait
    • class \Drupal\Tests\field\Unit\Plugin\Field\FieldFormatter\StringFormatterTest extends \Drupal\Tests\UnitTestCase

Expanded class hierarchy of StringFormatterTest

File

core/modules/field/tests/src/Unit/Plugin/Field/FieldFormatter/StringFormatterTest.php, line 26

Namespace

Drupal\Tests\field\Unit\Plugin\Field\FieldFormatter
View source
final class StringFormatterTest extends UnitTestCase {
    
    /**
     * Checks link visibility depending on link templates and access.
     *
     * @param bool $hasUrl
     *   Whether the entity type has a canonical link template.
     * @param string|null $accessClass
     *   The access result for the current user.
     * @param bool $expectIsLinkElement
     *   Whether to expect the text to be wrapped in a link element.
     *
     * @phpstan-param class-string<\Drupal\Core\Access\AccessResultInterface>|null $accessClass
     *
     * @dataProvider providerAccessLinkToEntity
     */
    public function testLinkToEntity(bool $hasUrl, ?string $accessClass, bool $expectIsLinkElement) : void {
        $fieldDefinition = $this->prophesize(FieldDefinitionInterface::class);
        $entityTypeManager = $this->prophesize(EntityTypeManagerInterface::class);
        $fieldFormatter = new StringFormatter('foobar', [], $fieldDefinition->reveal(), [], 'TestLabel', 'default', [], $entityTypeManager->reveal());
        $fieldFormatter->setSetting('link_to_entity', TRUE);
        $entityType = $this->prophesize(EntityTypeInterface::class);
        $entityType->hasLinkTemplate('canonical')
            ->willReturn($hasUrl)
            ->shouldBeCalledTimes(1);
        $entityType->hasLinkTemplate('revision')
            ->willReturn(FALSE)
            ->shouldBeCalledTimes($hasUrl ? 1 : 0);
        $entity = $this->prophesize(EntityInterface::class);
        $entity->isNew()
            ->willReturn(FALSE);
        $entity->getEntityType()
            ->willReturn($entityType->reveal());
        if ($hasUrl) {
            $url = $this->prophesize(Url::class);
            $url->access(NULL, TRUE)
                ->willReturn(new $accessClass());
            $entity->toUrl('canonical')
                ->willReturn($url);
        }
        $item = $this->getMockBuilder(StringItem::class)
            ->disableOriginalConstructor()
            ->onlyMethods([])
            ->getMock();
        $item->setValue([
            'value' => 'FooText',
        ]);
        $items = $this->prophesize(FieldItemListInterface::class);
        $items->getEntity()
            ->willReturn($entity->reveal());
        $items->valid()
            ->willReturn(TRUE, FALSE);
        $items->next();
        $items->rewind();
        $items->current()
            ->willReturn($item);
        $items->key()
            ->willReturn(0);
        $elements = $fieldFormatter->viewElements($items->reveal(), 'en');
        if ($expectIsLinkElement) {
            $this->assertEquals('link', $elements[0]['#type']);
            $this->assertEquals('FooText', $elements[0]['#title']['#context']['value']);
        }
        else {
            $this->assertEquals('inline_template', $elements[0]['#type']);
            $this->assertEquals('FooText', $elements[0]['#context']['value']);
        }
    }
    
    /**
     * Data provider.
     *
     * @return \Generator
     *   Test scenarios.
     */
    public static function providerAccessLinkToEntity() : \Generator {
        (yield 'entity with no URL' => [
            FALSE,
            NULL,
            FALSE,
        ]);
        (yield 'entity with url, with access' => [
            TRUE,
            AccessResultAllowed::class,
            TRUE,
        ]);
        (yield 'entity with url, no access' => [
            TRUE,
            AccessResultForbidden::class,
            FALSE,
        ]);
    }

}

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.
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.
StringFormatterTest::providerAccessLinkToEntity public static function Data provider.
StringFormatterTest::testLinkToEntity public function Checks link visibility depending on link templates and access.
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 358
UnitTestCase::setUpBeforeClass public static function

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