X-Git-Url: http://www.aleph1.co.uk/gitweb/?a=blobdiff_plain;f=web%2Fcore%2Fmodules%2Ffield%2Ftests%2Fsrc%2FKernel%2FString%2FStringFormatterTest.php;fp=web%2Fcore%2Fmodules%2Ffield%2Ftests%2Fsrc%2FKernel%2FString%2FStringFormatterTest.php;h=b09eb58c09133e89aee5d8b06e75c7412d12f071;hb=a2bd1bf0c2c1f1a17d188f4dc0726a45494cefae;hp=0000000000000000000000000000000000000000;hpb=57c063afa3f66b07c4bbddc2d6129a96d90f0aad;p=yaffs-website diff --git a/web/core/modules/field/tests/src/Kernel/String/StringFormatterTest.php b/web/core/modules/field/tests/src/Kernel/String/StringFormatterTest.php new file mode 100644 index 000000000..b09eb58c0 --- /dev/null +++ b/web/core/modules/field/tests/src/Kernel/String/StringFormatterTest.php @@ -0,0 +1,160 @@ +installConfig(['system', 'field']); + \Drupal::service('router.builder')->rebuild(); + $this->installEntitySchema('entity_test_rev'); + + $this->entityType = 'entity_test_rev'; + $this->bundle = $this->entityType; + $this->fieldName = Unicode::strtolower($this->randomMachineName()); + + $field_storage = FieldStorageConfig::create([ + 'field_name' => $this->fieldName, + 'entity_type' => $this->entityType, + 'type' => 'string', + ]); + $field_storage->save(); + + $instance = FieldConfig::create([ + 'field_storage' => $field_storage, + 'bundle' => $this->bundle, + 'label' => $this->randomMachineName(), + ]); + $instance->save(); + + $this->display = entity_get_display($this->entityType, $this->bundle, 'default') + ->setComponent($this->fieldName, [ + 'type' => 'string', + 'settings' => [], + ]); + $this->display->save(); + } + + /** + * Renders fields of a given entity with a given display. + * + * @param \Drupal\Core\Entity\FieldableEntityInterface $entity + * The entity object with attached fields to render. + * @param \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display + * The display to render the fields in. + * + * @return string + * The rendered entity fields. + */ + protected function renderEntityFields(FieldableEntityInterface $entity, EntityViewDisplayInterface $display) { + $content = $display->build($entity); + $content = $this->render($content); + return $content; + } + + /** + * Tests string formatter output. + */ + public function testStringFormatter() { + $value = $this->randomString(); + $value .= "\n\n" . $this->randomString() . ''; + $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->url()); + + // $entity->url('revision') falls back to the canonical URL if this is no + // revision. + $this->assertLinkByHref($entity->url('revision')); + + // 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 = \Drupal::entityManager()->getStorage('entity_test_rev')->loadRevision($old_revision_id); + + $this->renderEntityFields($entity, $this->display); + $this->assertLink($value2, 0); + $this->assertLinkByHref($entity->url('revision')); + + $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'); + } + +}