3 namespace Drupal\Tests\filter\Kernel;
5 use Drupal\filter\Entity\FilterFormat;
6 use Drupal\KernelTests\KernelTestBase;
9 * Tests creation, loading, updating, deleting of text formats and filters.
13 class FilterCrudTest extends KernelTestBase {
20 public static $modules = ['filter', 'filter_test', 'system', 'user'];
23 * Tests CRUD operations for text formats and filters.
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',
32 $this->verifyTextFormat($format);
34 // Add another text format specifying all possible properties.
35 $format = FilterFormat::create([
36 'format' => 'custom_format',
37 'name' => 'Custom format',
39 $format->setFilterConfig('filter_url', [
42 'filter_url_length' => 30,
46 $this->verifyTextFormat($format);
48 // Alter some text format properties and save again.
49 $format->set('name', 'Altered format');
50 $format->setFilterConfig('filter_url', [
53 $format->setFilterConfig('filter_autop', [
57 $this->verifyTextFormat($format);
59 // Add a filter_test_replace filter and save again.
60 $format->setFilterConfig('filter_test_replace', [
64 $this->verifyTextFormat($format);
66 // Disable the text format.
67 $format->disable()->save();
69 $formats = filter_formats();
70 $this->assertTrue(!isset($formats[$format->id()]), 'filter_formats: Disabled text format no longer exists.');
74 * Tests disabling the fallback text format.
76 public function testDisableFallbackFormat() {
77 $this->installConfig(['filter']);
78 $message = '\LogicException with message "The fallback text format \'plain_text\' cannot be disabled." was thrown.';
80 FilterFormat::load('plain_text')->disable();
81 $this->fail($message);
83 catch (\LogicException $e) {
84 $this->assertIdentical($e->getMessage(), "The fallback text format 'plain_text' cannot be disabled.", $message);
89 * Verifies that a text format is properly stored.
91 public function verifyTextFormat($format) {
92 $t_args = ['%format' => $format->label()];
93 $default_langcode = \Drupal::languageManager()->getDefaultLanguage()->getId();
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));