View source
<?php
namespace Drupal\Tests\allowed_formats\Functional;
use Drupal\filter\Entity\FilterFormat;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\taxonomy\Traits\TaxonomyTestTrait;
use Drupal\Core\StringTranslation\StringTranslationTrait;
class AllowedFormatsTest extends BrowserTestBase {
use TaxonomyTestTrait;
use StringTranslationTrait;
protected $defaultTheme = 'stark';
public static $modules = [
'entity_test',
'allowed_formats',
'field_ui',
'taxonomy',
];
protected $adminUser;
protected $webUser;
protected function setUp() {
parent::setUp();
$this->adminUser = $this
->drupalCreateUser([
'administer filters',
'administer entity_test fields',
]);
$this->webUser = $this
->drupalCreateUser([
'administer entity_test content',
'administer taxonomy',
]);
}
public function testAllowedFormats() {
$format1 = FilterFormat::create([
'format' => mb_strtolower($this
->randomMachineName()),
'name' => $this
->randomString(),
'roles' => [
$this->webUser
->getRoles()[0],
],
]);
$format1
->save();
$format2 = FilterFormat::create([
'format' => mb_strtolower($this
->randomMachineName()),
'name' => $this
->randomString(),
'roles' => [
$this->webUser
->getRoles()[0],
],
]);
$format2
->save();
$this
->drupalLogin($this->adminUser);
$this
->drupalPostForm('entity_test/structure/entity_test/fields/entity_test.entity_test.field_test_text', [
'third_party_settings[allowed_formats][' . $format1
->id() . ']' => TRUE,
'third_party_settings[allowed_formats][' . $format2
->id() . ']' => TRUE,
], $this
->t('Save settings'));
$this
->drupalLogin($this->webUser);
$this
->drupalGet('entity_test/add');
$this
->assertFieldByName("field_test_text[0][value]", NULL, 'Widget is displayed');
$this
->assertFieldByName("field_test_text[0][format]", NULL, 'Format selector is displayed');
$this
->drupalLogin($this->adminUser);
$this
->drupalPostForm('entity_test/structure/entity_test/fields/entity_test.entity_test.field_test_text', [
'third_party_settings[allowed_formats][' . $format2
->id() . ']' => FALSE,
], $this
->t('Save settings'));
$this
->drupalLogin($this->webUser);
$this
->drupalGet('entity_test/add');
$this
->assertFieldByName("field_test_text[0][value]", NULL, 'Widget is displayed');
$this
->assertNoFieldByName("field_test_text[0][format]", NULL, 'Format selector is not displayed');
}
public function testBaseFields() {
$vocabulary = $this
->createVocabulary();
$roles = [
$this->webUser
->getRoles()[0],
];
$format1 = FilterFormat::create([
'format' => 'basic_html',
'name' => 'basic_html',
'roles' => $roles,
]);
$format1
->save();
$format2 = FilterFormat::create([
'format' => 'restricted_html',
'name' => 'restricted_html',
'roles' => $roles,
]);
$format2
->save();
$format3 = FilterFormat::create([
'format' => 'full_html',
'name' => 'full_html',
'roles' => $roles,
]);
$format3
->save();
$this
->drupalLogin($this->webUser);
$this
->drupalGet('admin/structure/taxonomy/manage/' . $vocabulary
->id() . '/add');
$this
->assertFieldByName("description[0][value]", NULL, 'Widget is displayed');
$this
->assertFieldByName("description[0][format]", NULL, 'Format selector is displayed');
$this
->assertOption('edit-description-0-format--2', 'basic_html');
$this
->assertOption('edit-description-0-format--2', 'restricted_html');
$this
->assertOption('edit-description-0-format--2', 'full_html');
\Drupal::service('module_installer')
->install([
'allowed_formats_base_fields_test',
]);
$this
->drupalGet('admin/structure/taxonomy/manage/' . $vocabulary
->id() . '/add');
$this
->assertFieldByName("description[0][value]", NULL, 'Widget is displayed');
$this
->assertFieldByName("description[0][format]", NULL, 'Format selector is displayed');
$this
->assertOption('edit-description-0-format--2', 'basic_html');
$this
->assertOption('edit-description-0-format--2', 'restricted_html');
$this
->assertNoOption('edit-description-0-format--2', 'full_html');
}
}