3 namespace Drupal\Tests\filter\Functional;
5 use Drupal\filter\Entity\FilterFormat;
6 use Drupal\Tests\BrowserTestBase;
9 * Tests the default text formats for different users.
13 class FilterDefaultFormatTest extends BrowserTestBase {
20 public static $modules = ['filter'];
23 * Tests if the default text format is accessible to users.
25 public function testDefaultTextFormats() {
26 // Create two text formats, and two users. The first user has access to
27 // both formats, but the second user only has access to the second one.
28 $admin_user = $this->drupalCreateUser(['administer filters']);
29 $this->drupalLogin($admin_user);
31 for ($i = 0; $i < 2; $i++) {
33 'format' => mb_strtolower($this->randomMachineName()),
34 'name' => $this->randomMachineName(),
36 $this->drupalPostForm('admin/config/content/formats/add', $edit, t('Save configuration'));
37 $this->resetFilterCaches();
38 $formats[] = FilterFormat::load($edit['format']);
40 list($first_format, $second_format) = $formats;
41 $second_format_permission = $second_format->getPermissionName();
42 $first_user = $this->drupalCreateUser([$first_format->getPermissionName(), $second_format_permission]);
43 $second_user = $this->drupalCreateUser([$second_format_permission]);
45 // Adjust the weights so that the first and second formats (in that order)
46 // are the two lowest weighted formats available to any user.
48 $edit['formats[' . $first_format->id() . '][weight]'] = -2;
49 $edit['formats[' . $second_format->id() . '][weight]'] = -1;
50 $this->drupalPostForm('admin/config/content/formats', $edit, t('Save'));
51 $this->resetFilterCaches();
53 // Check that each user's default format is the lowest weighted format that
54 // the user has access to.
55 $actual = filter_default_format($first_user);
56 $expected = $first_format->id();
57 $this->assertEqual($actual, $expected, "First user's default format $actual is the expected lowest weighted format $expected that the user has access to.");
58 $actual = filter_default_format($second_user);
59 $expected = $second_format->id();
60 $this->assertEqual($actual, $expected, "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.");
62 // Reorder the two formats, and check that both users now have the same
65 $edit['formats[' . $second_format->id() . '][weight]'] = -3;
66 $this->drupalPostForm('admin/config/content/formats', $edit, t('Save'));
67 $this->resetFilterCaches();
68 $this->assertEqual(filter_default_format($first_user), filter_default_format($second_user), 'After the formats are reordered, both users have the same default format.');
72 * Rebuilds text format and permission caches in the thread running the tests.
74 protected function resetFilterCaches() {
75 filter_formats_reset();