Updated to Drupal 8.6.4, which is PHP 7.3 friendly. Also updated HTMLaw library....
[yaffs-website] / web / core / modules / filter / tests / src / Kernel / FilterCrudTest.php
1 <?php
2
3 namespace Drupal\Tests\filter\Kernel;
4
5 use Drupal\filter\Entity\FilterFormat;
6 use Drupal\KernelTests\KernelTestBase;
7
8 /**
9  * Tests creation, loading, updating, deleting of text formats and filters.
10  *
11  * @group filter
12  */
13 class FilterCrudTest extends KernelTestBase {
14
15   /**
16    * Modules to enable.
17    *
18    * @var array
19    */
20   public static $modules = ['filter', 'filter_test', 'system', 'user'];
21
22   /**
23    * Tests CRUD operations for text formats and filters.
24    */
25   public function testTextFormatCrud() {
26     // Add a text format with minimum data only.
27     $format = FilterFormat::create([
28       'format' => 'empty_format',
29       'name' => 'Empty format',
30     ]);
31     $format->save();
32     $this->verifyTextFormat($format);
33
34     // Add another text format specifying all possible properties.
35     $format = FilterFormat::create([
36       'format' => 'custom_format',
37       'name' => 'Custom format',
38     ]);
39     $format->setFilterConfig('filter_url', [
40       'status' => 1,
41       'settings' => [
42         'filter_url_length' => 30,
43       ],
44     ]);
45     $format->save();
46     $this->verifyTextFormat($format);
47
48     // Alter some text format properties and save again.
49     $format->set('name', 'Altered format');
50     $format->setFilterConfig('filter_url', [
51       'status' => 0,
52     ]);
53     $format->setFilterConfig('filter_autop', [
54       'status' => 1,
55     ]);
56     $format->save();
57     $this->verifyTextFormat($format);
58
59     // Add a filter_test_replace  filter and save again.
60     $format->setFilterConfig('filter_test_replace', [
61       'status' => 1,
62     ]);
63     $format->save();
64     $this->verifyTextFormat($format);
65
66     // Disable the text format.
67     $format->disable()->save();
68
69     $formats = filter_formats();
70     $this->assertTrue(!isset($formats[$format->id()]), 'filter_formats: Disabled text format no longer exists.');
71   }
72
73   /**
74    * Tests disabling the fallback text format.
75    */
76   public function testDisableFallbackFormat() {
77     $this->installConfig(['filter']);
78     $message = '\LogicException with message "The fallback text format \'plain_text\' cannot be disabled." was thrown.';
79     try {
80       FilterFormat::load('plain_text')->disable();
81       $this->fail($message);
82     }
83     catch (\LogicException $e) {
84       $this->assertIdentical($e->getMessage(), "The fallback text format 'plain_text' cannot be disabled.", $message);
85     }
86   }
87
88   /**
89    * Verifies that a text format is properly stored.
90    */
91   public function verifyTextFormat($format) {
92     $t_args = ['%format' => $format->label()];
93     $default_langcode = \Drupal::languageManager()->getDefaultLanguage()->getId();
94
95     // Verify the loaded filter has all properties.
96     $filter_format = FilterFormat::load($format->id());
97     $this->assertEqual($filter_format->id(), $format->id(), format_string('filter_format_load: Proper format id for text format %format.', $t_args));
98     $this->assertEqual($filter_format->label(), $format->label(), format_string('filter_format_load: Proper title for text format %format.', $t_args));
99     $this->assertEqual($filter_format->get('weight'), $format->get('weight'), format_string('filter_format_load: Proper weight for text format %format.', $t_args));
100     // Check that the filter was created in site default language.
101     $this->assertEqual($format->language()->getId(), $default_langcode, format_string('filter_format_load: Proper language code for text format %format.', $t_args));
102   }
103
104 }