3 namespace Drupal\Tests\filter\Functional;
5 use Drupal\Tests\BrowserTestBase;
6 use Drupal\user\RoleInterface;
9 * Tests hooks for text formats insert/update/disable.
13 class FilterHooksTest extends BrowserTestBase {
20 public static $modules = ['node', 'filter_test'];
23 * Tests hooks on format management.
25 * Tests that hooks run correctly on creating, editing, and deleting a text
28 public function testFilterHooks() {
29 // Create content type, with underscores.
30 $type_name = 'test_' . strtolower($this->randomMachineName());
31 $type = $this->drupalCreateContentType(['name' => $type_name, 'type' => $type_name]);
32 $node_permission = "create $type_name content";
34 $admin_user = $this->drupalCreateUser(['administer filters', 'administer nodes', $node_permission]);
35 $this->drupalLogin($admin_user);
38 $name = $this->randomMachineName();
40 $edit['format'] = mb_strtolower($this->randomMachineName());
41 $edit['name'] = $name;
42 $edit['roles[' . RoleInterface::ANONYMOUS_ID . ']'] = 1;
43 $this->drupalPostForm('admin/config/content/formats/add', $edit, t('Save configuration'));
44 $this->assertRaw(t('Added text format %format.', ['%format' => $name]));
45 $this->assertText('hook_filter_format_insert invoked.');
47 $format_id = $edit['format'];
49 // Update text format.
51 $edit['roles[' . RoleInterface::AUTHENTICATED_ID . ']'] = 1;
52 $this->drupalPostForm('admin/config/content/formats/manage/' . $format_id, $edit, t('Save configuration'));
53 $this->assertRaw(t('The text format %format has been updated.', ['%format' => $name]));
54 $this->assertText('hook_filter_format_update invoked.');
56 // Use the format created.
57 $title = $this->randomMachineName(8);
59 $edit['title[0][value]'] = $title;
60 $edit['body[0][value]'] = $this->randomMachineName(32);
61 $edit['body[0][format]'] = $format_id;
62 $this->drupalPostForm("node/add/{$type->id()}", $edit, t('Save'));
63 $this->assertText(t('@type @title has been created.', ['@type' => $type_name, '@title' => $title]));
65 // Disable the text format.
66 $this->drupalPostForm('admin/config/content/formats/manage/' . $format_id . '/disable', [], t('Disable'));
67 $this->assertRaw(t('Disabled text format %format.', ['%format' => $name]));
68 $this->assertText('hook_filter_format_disable invoked.');