45c37117113334112cb1ba509bd7a5505b75c801
[yaffs-website] / web / core / modules / filter / tests / src / Functional / FilterDefaultFormatTest.php
1 <?php
2
3 namespace Drupal\Tests\filter\Functional;
4
5 use Drupal\Component\Utility\Unicode;
6 use Drupal\filter\Entity\FilterFormat;
7 use Drupal\Tests\BrowserTestBase;
8
9 /**
10  * Tests the default text formats for different users.
11  *
12  * @group filter
13  */
14 class FilterDefaultFormatTest extends BrowserTestBase {
15
16   /**
17    * Modules to enable.
18    *
19    * @var array
20    */
21   public static $modules = ['filter'];
22
23   /**
24    * Tests if the default text format is accessible to users.
25    */
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);
31     $formats = [];
32     for ($i = 0; $i < 2; $i++) {
33       $edit = [
34         'format' => Unicode::strtolower($this->randomMachineName()),
35         'name' => $this->randomMachineName(),
36       ];
37       $this->drupalPostForm('admin/config/content/formats/add', $edit, t('Save configuration'));
38       $this->resetFilterCaches();
39       $formats[] = FilterFormat::load($edit['format']);
40     }
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]);
45
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.
48     $edit = [];
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();
53
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.");
62
63     // Reorder the two formats, and check that both users now have the same
64     // default.
65     $edit = [];
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.');
70   }
71
72   /**
73    * Rebuilds text format and permission caches in the thread running the tests.
74    */
75   protected function resetFilterCaches() {
76     filter_formats_reset();
77   }
78
79 }