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