You are here

public function EditorAdminTest::testDisableFormatWithEditor in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/editor/tests/src/Functional/EditorAdminTest.php \Drupal\Tests\editor\Functional\EditorAdminTest::testDisableFormatWithEditor()

Tests format disabling.

File

core/modules/editor/tests/src/Functional/EditorAdminTest.php, line 109

Class

EditorAdminTest
Tests administration of text editors.

Namespace

Drupal\Tests\editor\Functional

Code

public function testDisableFormatWithEditor() {
  $formats = [
    'monoceros' => 'Monoceros',
    'tattoo' => 'Tattoo',
  ];

  // Install the node module.
  $this->container
    ->get('module_installer')
    ->install([
    'node',
  ]);
  $this
    ->resetAll();

  // Create a new node type and attach the 'body' field to it.
  $node_type = NodeType::create([
    'type' => mb_strtolower($this
      ->randomMachineName()),
  ]);
  $node_type
    ->save();
  node_add_body_field($node_type, $this
    ->randomString());
  $permissions = [
    'administer filters',
    "edit any {$node_type->id()} content",
  ];
  foreach ($formats as $format => $name) {

    // Create a format and add an editor to this format.
    $this
      ->addEditorToNewFormat($format, $name);

    // Add permission for this format.
    $permissions[] = "use text format {$format}";
  }

  // Create a node having the body format value 'monoceros'.
  $node = Node::create([
    'type' => $node_type
      ->id(),
    'title' => $this
      ->randomString(),
  ]);
  $node->body->value = $this
    ->randomString(100);
  $node->body->format = 'monoceros';
  $node
    ->save();

  // Log in as a user able to use both formats and edit nodes of created type.
  $account = $this
    ->drupalCreateUser($permissions);
  $this
    ->drupalLogin($account);

  // The node edit page header.
  $text = (string) new FormattableMarkup('<em>Edit @type</em> @title', [
    '@type' => $node_type
      ->label(),
    '@title' => $node
      ->label(),
  ]);

  // Go to node edit form.
  $this
    ->drupalGet('node/' . $node
    ->id() . '/edit');
  $this
    ->assertSession()
    ->responseContains($text);

  // Disable the format assigned to the 'body' field of the node.
  FilterFormat::load('monoceros')
    ->disable()
    ->save();

  // Edit again the node.
  $this
    ->drupalGet('node/' . $node
    ->id() . '/edit');
  $this
    ->assertSession()
    ->responseContains($text);
}