Version 1
[yaffs-website] / web / core / modules / text / tests / src / Kernel / TextFormatterTest.php
1 <?php
2
3 namespace Drupal\Tests\text\Kernel;
4
5 use Drupal\field\Entity\FieldConfig;
6 use Drupal\filter\Entity\FilterFormat;
7 use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
8 use Drupal\field\Entity\FieldStorageConfig;
9
10 /**
11  * Tests the text formatters functionality.
12  *
13  * @group text
14  */
15 class TextFormatterTest extends EntityKernelTestBase {
16
17   /**
18    * The entity type used in this test.
19    *
20    * @var string
21    */
22   protected $entityType = 'entity_test';
23
24   /**
25    * The bundle used in this test.
26    *
27    * @var string
28    */
29   protected $bundle = 'entity_test';
30
31   /**
32    * Modules to enable.
33    *
34    * @var array
35    */
36   public static $modules = ['text'];
37
38   /**
39    * {@inheritdoc}
40    */
41   protected function setUp() {
42     parent::setUp();
43
44     FilterFormat::create([
45       'format' => 'my_text_format',
46       'name' => 'My text format',
47       'filters' => [
48         'filter_autop' => [
49           'module' => 'filter',
50           'status' => TRUE,
51         ],
52       ],
53     ])->save();
54
55     FieldStorageConfig::create([
56       'field_name' => 'formatted_text',
57       'entity_type' => $this->entityType,
58       'type' => 'text',
59       'settings' => [],
60     ])->save();
61     FieldConfig::create([
62       'entity_type' => $this->entityType,
63       'bundle' => $this->bundle,
64       'field_name' => 'formatted_text',
65       'label' => 'Filtered text',
66     ])->save();
67   }
68
69   /**
70    * Tests all text field formatters.
71    */
72   public function testFormatters() {
73     $formatters = [
74       'text_default',
75       'text_trimmed',
76       'text_summary_or_trimmed',
77     ];
78
79     // Create the entity to be referenced.
80     $entity = $this->container->get('entity_type.manager')
81       ->getStorage($this->entityType)
82       ->create(['name' => $this->randomMachineName()]);
83     $entity->formatted_text = [
84       'value' => 'Hello, world!',
85       'format' => 'my_text_format',
86     ];
87     $entity->save();
88
89     foreach ($formatters as $formatter) {
90       // Verify the text field formatter's render array.
91       $build = $entity->get('formatted_text')->view(['type' => $formatter]);
92       \Drupal::service('renderer')->renderRoot($build[0]);
93       $this->assertEqual($build[0]['#markup'], "<p>Hello, world!</p>\n");
94       $this->assertEqual($build[0]['#cache']['tags'], FilterFormat::load('my_text_format')->getCacheTags(), format_string('The @formatter formatter has the expected cache tags when formatting a formatted text field.', ['@formatter' => $formatter]));
95     }
96   }
97
98 }