3 namespace Drupal\Tests\filter\Functional;
5 use Drupal\Component\Utility\Unicode;
6 use Drupal\Tests\BrowserTestBase;
7 use Drupal\user\RoleInterface;
10 * Tests hooks for text formats insert/update/disable.
14 class FilterHooksTest extends BrowserTestBase {
21 public static $modules = ['node', 'filter_test'];
24 * Tests hooks on format management.
26 * Tests that hooks run correctly on creating, editing, and deleting a text
29 public function testFilterHooks() {
30 // Create content type, with underscores.
31 $type_name = 'test_' . strtolower($this->randomMachineName());
32 $type = $this->drupalCreateContentType(['name' => $type_name, 'type' => $type_name]);
33 $node_permission = "create $type_name content";
35 $admin_user = $this->drupalCreateUser(['administer filters', 'administer nodes', $node_permission]);
36 $this->drupalLogin($admin_user);
39 $name = $this->randomMachineName();
41 $edit['format'] = Unicode::strtolower($this->randomMachineName());
42 $edit['name'] = $name;
43 $edit['roles[' . RoleInterface::ANONYMOUS_ID . ']'] = 1;
44 $this->drupalPostForm('admin/config/content/formats/add', $edit, t('Save configuration'));
45 $this->assertRaw(t('Added text format %format.', ['%format' => $name]));
46 $this->assertText('hook_filter_format_insert invoked.');
48 $format_id = $edit['format'];
50 // Update text format.
52 $edit['roles[' . RoleInterface::AUTHENTICATED_ID . ']'] = 1;
53 $this->drupalPostForm('admin/config/content/formats/manage/' . $format_id, $edit, t('Save configuration'));
54 $this->assertRaw(t('The text format %format has been updated.', ['%format' => $name]));
55 $this->assertText('hook_filter_format_update invoked.');
57 // Use the format created.
58 $title = $this->randomMachineName(8);
60 $edit['title[0][value]'] = $title;
61 $edit['body[0][value]'] = $this->randomMachineName(32);
62 $edit['body[0][format]'] = $format_id;
63 $this->drupalPostForm("node/add/{$type->id()}", $edit, t('Save'));
64 $this->assertText(t('@type @title has been created.', ['@type' => $type_name, '@title' => $title]));
66 // Disable the text format.
67 $this->drupalPostForm('admin/config/content/formats/manage/' . $format_id . '/disable', [], t('Disable'));
68 $this->assertRaw(t('Disabled text format %format.', ['%format' => $name]));
69 $this->assertText('hook_filter_format_disable invoked.');