RawStringFormatterTest.php in Zircon Profile 8
File
core/modules/field/src/Tests/String/RawStringFormatterTest.php
View source
<?php
namespace Drupal\field\Tests\String;
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\simpletest\KernelTestBase;
class RawStringFormatterTest extends KernelTestBase {
public static $modules = array(
'field',
'text',
'entity_test',
'system',
'filter',
'user',
);
protected $entityType;
protected $bundle;
protected $fieldName;
protected $display;
protected function setUp() {
parent::setUp();
$this
->installConfig(array(
'system',
'field',
));
$this
->installSchema('system', 'router');
\Drupal::service('router.builder')
->rebuild();
$this
->installEntitySchema('entity_test');
$this->entityType = 'entity_test';
$this->bundle = $this->entityType;
$this->fieldName = Unicode::strtolower($this
->randomMachineName());
$field_storage = FieldStorageConfig::create(array(
'field_name' => $this->fieldName,
'entity_type' => $this->entityType,
'type' => 'string_long',
));
$field_storage
->save();
$instance = FieldConfig::create(array(
'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, array(
'type' => 'string',
'settings' => array(),
));
$this->display
->save();
}
protected function renderEntityFields(FieldableEntityInterface $entity, EntityViewDisplayInterface $display) {
$content = $display
->build($entity);
$content = $this
->render($content);
return $content;
}
public function testStringFormatter() {
$value = $this
->randomString();
$value .= "\n\n<strong>" . $this
->randomString() . '</strong>';
$value .= "\n\n" . $this
->randomString();
$entity = EntityTest::create(array());
$entity->{$this->fieldName}->value = $value;
$this
->renderEntityFields($entity, $this->display);
$this
->assertNoRaw($value);
$this
->assertRaw(nl2br(Html::escape($value)));
$build = $entity->{$this->fieldName}
->view();
$this
->assertTrue(!isset($build[0]['#cache']), 'The string formatter has no cache tags.');
}
}