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