Skip to content
Snippets Groups Projects
Select Git revision
  • fa2724a4cdfbc07a9b4d9a33cb407ff6b5232dcb
  • 11.x default protected
  • 11.2.x protected
  • 10.6.x protected
  • 10.5.x protected
  • 11.1.x protected
  • 10.4.x protected
  • 11.0.x protected
  • 10.3.x protected
  • 7.x protected
  • 10.2.x protected
  • 10.1.x protected
  • 9.5.x protected
  • 10.0.x protected
  • 9.4.x protected
  • 9.3.x protected
  • 9.2.x protected
  • 9.1.x protected
  • 8.9.x protected
  • 9.0.x protected
  • 8.8.x protected
  • 10.5.1 protected
  • 11.2.2 protected
  • 11.2.1 protected
  • 11.2.0 protected
  • 10.5.0 protected
  • 11.2.0-rc2 protected
  • 10.5.0-rc1 protected
  • 11.2.0-rc1 protected
  • 10.4.8 protected
  • 11.1.8 protected
  • 10.5.0-beta1 protected
  • 11.2.0-beta1 protected
  • 11.2.0-alpha1 protected
  • 10.4.7 protected
  • 11.1.7 protected
  • 10.4.6 protected
  • 11.1.6 protected
  • 10.3.14 protected
  • 10.4.5 protected
  • 11.0.13 protected
41 results

RawStringFormatterTest.php

Blame
  • webchick's avatar
    Issue #2547851 by alexpott, joelpittet, dawehner, stefan.r, xjm:...
    Angie Byron authored
    Issue #2547851 by alexpott, joelpittet, dawehner, stefan.r, xjm: SafeMarkup::format() should require arguments without them it is just SafeMarkup::set() in disguise
    fa2724a4
    History
    Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    RawStringFormatterTest.php 3.35 KiB
    <?php
    
    /**
     * @file
     * Contains \Drupal\field\Tests\String\RawStringFormatterTest.
     */
    
    namespace Drupal\field\Tests\String;
    
    use Drupal\Component\Utility\SafeMarkup;
    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;
    
    /**
     * Tests the raw string formatter
     *
     * @group field
     */
    class RawStringFormatterTest extends KernelTestBase {
    
      /**
       * Modules to enable.
       *
       * @var array
       */
      public static $modules = array('field', 'text', 'entity_test', 'system', 'filter', 'user');
    
      /**
       * @var string
       */
      protected $entityType;
    
      /**
       * @var string
       */
      protected $bundle;
    
      /**
       * @var string
       */
      protected $fieldName;
    
      /**
       * @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface
       */
      protected $display;
    
      /**
       * {@inheritdoc}
       */
      protected function setUp() {
        parent::setUp();
    
        // Configure the theme system.
        $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();
      }
    
      /**
       * 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<strong>" . $this->randomString() . '</strong>';
        $value .= "\n\n" . $this->randomString();
    
        $entity = EntityTest::create(array());
        $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(SafeMarkup::checkPlain($value)));
    
        // Verify the cache tags.
        $build = $entity->{$this->fieldName}->view();
        $this->assertTrue(!isset($build[0]['#cache']), 'The string formatter has no cache tags.');
      }
    
    }