FilterDefaultFormatTest.php in Drupal 9
File
core/modules/filter/tests/src/Functional/FilterDefaultFormatTest.php
View source
<?php
namespace Drupal\Tests\filter\Functional;
use Drupal\filter\Entity\FilterFormat;
use Drupal\Tests\BrowserTestBase;
class FilterDefaultFormatTest extends BrowserTestBase {
protected static $modules = [
'filter',
];
protected $defaultTheme = 'stark';
public function testDefaultTextFormats() {
$admin_user = $this
->drupalCreateUser([
'administer filters',
]);
$this
->drupalLogin($admin_user);
$formats = [];
for ($i = 0; $i < 2; $i++) {
$edit = [
'format' => mb_strtolower($this
->randomMachineName()),
'name' => $this
->randomMachineName(),
];
$this
->drupalGet('admin/config/content/formats/add');
$this
->submitForm($edit, 'Save configuration');
$this
->resetFilterCaches();
$formats[] = FilterFormat::load($edit['format']);
}
list($first_format, $second_format) = $formats;
$second_format_permission = $second_format
->getPermissionName();
$first_user = $this
->drupalCreateUser([
$first_format
->getPermissionName(),
$second_format_permission,
]);
$second_user = $this
->drupalCreateUser([
$second_format_permission,
]);
$edit = [];
$edit['formats[' . $first_format
->id() . '][weight]'] = -2;
$edit['formats[' . $second_format
->id() . '][weight]'] = -1;
$this
->drupalGet('admin/config/content/formats');
$this
->submitForm($edit, 'Save');
$this
->resetFilterCaches();
$actual = filter_default_format($first_user);
$expected = $first_format
->id();
$this
->assertEquals($expected, $actual, "First user's default format {$actual} is the expected lowest weighted format {$expected} that the user has access to.");
$actual = filter_default_format($second_user);
$expected = $second_format
->id();
$this
->assertEquals($expected, $actual, "Second user's default format {$actual} is the expected lowest weighted format {$expected} that the user has access to, and different to the first user's.");
$edit = [];
$edit['formats[' . $second_format
->id() . '][weight]'] = -3;
$this
->drupalGet('admin/config/content/formats');
$this
->submitForm($edit, 'Save');
$this
->resetFilterCaches();
$this
->assertEquals(filter_default_format($first_user), filter_default_format($second_user), 'After the formats are reordered, both users have the same default format.');
}
protected function resetFilterCaches() {
filter_formats_reset();
}
}