View source
<?php
namespace Drupal\Tests\filter\Kernel;
use Drupal\Core\Form\FormInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\filter\Entity\FilterFormat;
use Drupal\KernelTests\KernelTestBase;
use Drupal\user\Entity\Role;
use Drupal\user\Entity\User;
class TextFormatElementFormTest extends KernelTestBase implements FormInterface {
protected $testUser;
protected static $modules = [
'system',
'user',
'filter',
'filter_test',
'editor',
];
protected function setUp() : void {
parent::setUp();
$this
->installEntitySchema('user');
$this
->installSchema('system', [
'sequences',
]);
$this
->installConfig([
'filter',
'filter_test',
]);
User::create([
'uid' => 1,
'name' => 'user1',
])
->save();
$manager = \Drupal::service('plugin.manager.element_info');
$manager
->clearCachedDefinitions();
$manager
->getDefinitions();
$filter_test_format = FilterFormat::load('filter_test');
$full_html_format = FilterFormat::load('full_html');
$filtered_html_format = FilterFormat::load('filtered_html');
$role = Role::create([
'id' => 'admin',
'label' => 'admin',
]);
$role
->grantPermission($filter_test_format
->getPermissionName());
$role
->grantPermission($full_html_format
->getPermissionName());
$role
->grantPermission($filtered_html_format
->getPermissionName());
$role
->save();
$this->testUser = User::create([
'name' => 'foobar',
'mail' => 'foobar@example.com',
]);
$this->testUser
->addRole($role
->id());
$this->testUser
->save();
\Drupal::service('current_user')
->setAccount($this->testUser);
}
public function getFormId() {
return 'test_text_area_element';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form['textformat'] = [
'#type' => 'text_format',
'#required' => TRUE,
'#title' => 'Text',
'#base_type' => 'textfield',
'#format' => NULL,
'#default_value' => 'test value',
];
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
}
public function validateForm(array &$form, FormStateInterface $form_state) {
}
public function testTextFormatElement() {
$form_builder = $this->container
->get('form_builder');
$form = $form_builder
->getForm($this);
$output = $this
->render($form);
$this
->setRawContent($output);
$this
->assertFieldByName('textformat[value]');
$this
->assertRaw('<h4>Full HTML</h4>');
$this
->assertRaw('<h4>Filtered HTML</h4>');
$this
->assertRaw('<h4>Test format</h4>');
$this
->assertNoPattern('|<h4[^>]*></h4>|', 'No empty H4 element found.');
}
}