function StringFormatterTest::testStringFormatter

Same name in other branches
  1. 8.9.x core/modules/field/tests/src/Kernel/String/StringFormatterTest.php \Drupal\Tests\field\Kernel\String\StringFormatterTest::testStringFormatter()
  2. 10 core/modules/field/tests/src/Kernel/KernelString/StringFormatterTest.php \Drupal\Tests\field\Kernel\KernelString\StringFormatterTest::testStringFormatter()
  3. 11.x core/modules/field/tests/src/Kernel/KernelString/StringFormatterTest.php \Drupal\Tests\field\Kernel\KernelString\StringFormatterTest::testStringFormatter()

Tests string formatter output.

File

core/modules/field/tests/src/Kernel/String/StringFormatterTest.php, line 122

Class

StringFormatterTest
Tests the creation of text fields.

Namespace

Drupal\Tests\field\Kernel\String

Code

public function testStringFormatter() {
    $value = $this->randomString();
    $value .= "\n\n<strong>" . $this->randomString() . '</strong>';
    $value .= "\n\n" . $this->randomString();
    $entity = EntityTestRev::create([]);
    $entity->{$this->fieldName}->value = $value;
    // Verify that all HTML is escaped and newlines are retained.
    $this->renderEntityFields($entity, $this->display);
    $this->assertNoRaw($value);
    $this->assertRaw(nl2br(Html::escape($value)));
    // Verify the cache tags.
    $build = $entity->{$this->fieldName}
        ->view();
    $this->assertTrue(!isset($build[0]['#cache']), 'The string formatter has no cache tags.');
    $value = $this->randomMachineName();
    $entity->{$this->fieldName}->value = $value;
    $entity->save();
    // Set the formatter to link to the entity.
    $this->display
        ->setComponent($this->fieldName, [
        'type' => 'string',
        'settings' => [
            'link_to_entity' => TRUE,
        ],
    ]);
    $this->display
        ->save();
    $this->renderEntityFields($entity, $this->display);
    $this->assertLink($value, 0);
    $this->assertLinkByHref($entity->toUrl()
        ->toString());
    // $entity->toUrl('revision') falls back to the canonical URL if this is no
    // revision.
    $this->assertLinkByHref($entity->toUrl('revision')
        ->toString());
    // Make the entity a new revision.
    $old_revision_id = $entity->getRevisionId();
    $entity->setNewRevision(TRUE);
    $value2 = $this->randomMachineName();
    $entity->{$this->fieldName}->value = $value2;
    $entity->save();
    $entity_new_revision = $this->entityTypeManager
        ->getStorage('entity_test_rev')
        ->loadRevision($old_revision_id);
    $this->renderEntityFields($entity, $this->display);
    $this->assertLink($value2, 0);
    $this->assertLinkByHref($entity->toUrl('revision')
        ->toString());
    $this->renderEntityFields($entity_new_revision, $this->display);
    $this->assertLink($value, 0);
    $this->assertLinkByHref('/entity_test_rev/' . $entity_new_revision->id() . '/revision/' . $entity_new_revision->getRevisionId() . '/view');
    // Check that linking to a revisionable entity works if the entity type does
    // not specify a 'revision' link template.
    $entity_type = clone $this->entityTypeManager
        ->getDefinition('entity_test_rev');
    $link_templates = $entity_type->getLinkTemplates();
    unset($link_templates['revision']);
    $entity_type->set('links', $link_templates);
    \Drupal::state()->set('entity_test_rev.entity_type', $entity_type);
    $this->entityTypeManager
        ->clearCachedDefinitions();
    $this->renderEntityFields($entity_new_revision, $this->display);
    $this->assertLink($value, 0);
    $this->assertLinkByHref($entity->toUrl('canonical')
        ->toString());
}

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