Updated to Drupal 8.6.4, which is PHP 7.3 friendly. Also updated HTMLaw library....
[yaffs-website] / web / core / modules / filter / tests / src / Kernel / TextFormatElementFormTest.php
1 <?php
2
3 namespace Drupal\Tests\filter\Kernel;
4
5 use Drupal\Core\Form\FormInterface;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\filter\Entity\FilterFormat;
8 use Drupal\KernelTests\KernelTestBase;
9 use Drupal\user\Entity\Role;
10 use Drupal\user\Entity\User;
11
12 /**
13  * Tests PathElement validation and conversion functionality.
14  *
15  * @group Form
16  */
17 class TextFormatElementFormTest extends KernelTestBase implements FormInterface {
18
19   /**
20    * User for testing.
21    *
22    * @var \Drupal\user\UserInterface
23    */
24   protected $testUser;
25
26   /**
27    * Modules to enable.
28    *
29    * @var array
30    */
31   public static $modules = ['system', 'user', 'filter', 'filter_test', 'editor'];
32
33   /**
34    * Sets up the test.
35    */
36   protected function setUp() {
37     parent::setUp();
38     $this->installEntitySchema('user');
39     $this->installSchema('system', ['sequences']);
40     $this->installConfig(['filter', 'filter_test']);
41     // Filter tips link to the full-page.
42     \Drupal::service('router.builder')->rebuild();
43     /* @var \Drupal\Core\Render\ElementInfoManager $manager */
44     $manager = \Drupal::service('plugin.manager.element_info');
45     $manager->clearCachedDefinitions();
46     $manager->getDefinitions();
47     /* @var \Drupal\filter\FilterFormatInterface $filter_test_format */
48     $filter_test_format = FilterFormat::load('filter_test');
49
50     /* @var \Drupal\user\RoleInterface $role */
51     $role = Role::create([
52       'id' => 'admin',
53       'label' => 'admin',
54     ]);
55     $role->grantPermission($filter_test_format->getPermissionName());
56     $role->save();
57     $this->testUser = User::create([
58       'name' => 'foobar',
59       'mail' => 'foobar@example.com',
60     ]);
61     $this->testUser->addRole($role->id());
62     $this->testUser->save();
63     \Drupal::service('current_user')->setAccount($this->testUser);
64   }
65
66   /**
67    * {@inheritdoc}
68    */
69   public function getFormId() {
70     return 'test_text_area_element';
71   }
72
73   /**
74    * {@inheritdoc}
75    */
76   public function buildForm(array $form, FormStateInterface $form_state) {
77     // A textformat field.
78     $form['textformat'] = [
79       '#type' => 'text_format',
80       '#required' => TRUE,
81       '#title' => 'Text',
82       '#base_type' => 'textfield',
83       '#format' => NULL,
84       '#default_value' => 'test value',
85     ];
86
87     return $form;
88   }
89
90   /**
91    * {@inheritdoc}
92    */
93   public function submitForm(array &$form, FormStateInterface $form_state) {}
94
95   /**
96    * Form validation handler.
97    *
98    * @param array $form
99    *   An associative array containing the structure of the form.
100    * @param \Drupal\Core\Form\FormStateInterface $form_state
101    *   The current state of the form.
102    */
103   public function validateForm(array &$form, FormStateInterface $form_state) {}
104
105   /**
106    * Tests that values are returned.
107    */
108   public function testTextFormatElement() {
109     /* @var \Drupal\Core\Form\FormBuilder $form_builder */
110     $form_builder = $this->container->get('form_builder');
111     $form = $form_builder->getForm($this);
112     $output = $this->render($form);
113     $this->setRawContent($output);
114     $this->assertFieldByName('textformat[value]');
115     $this->assertRaw('<h4>Full HTML</h4>');
116     $this->assertRaw('<h4>Filtered HTML</h4>');
117     $this->assertRaw('<h4>Test format</h4>');
118     $this->assertNoPattern('|<h4[^>]*></h4>|', 'No empty H4 element found.');
119   }
120
121   /**
122    * {@inheritdoc}
123    */
124   protected function getUrl() {
125     // \Drupal\simpletest\AssertContentTrait needs this for ::assertFieldByName
126     // to work.
127     return 'Internal rendering';
128   }
129
130 }