function EditorLoadingTest::testLoading

Same name and namespace in other branches
  1. 9 core/modules/editor/tests/src/Functional/EditorLoadingTest.php \Drupal\Tests\editor\Functional\EditorLoadingTest::testLoading()
  2. 8.9.x core/modules/editor/tests/src/Functional/EditorLoadingTest.php \Drupal\Tests\editor\Functional\EditorLoadingTest::testLoading()
  3. 10 core/modules/editor/tests/src/Functional/EditorLoadingTest.php \Drupal\Tests\editor\Functional\EditorLoadingTest::testLoading()

Tests loading of text editors.

File

core/modules/editor/tests/src/Functional/EditorLoadingTest.php, line 135

Class

EditorLoadingTest
Tests loading of text editors.

Namespace

Drupal\Tests\editor\Functional

Code

public function testLoading() : void {
    // Only associate a text editor with the "Full HTML" text format.
    $editor = Editor::create([
        'format' => 'full_html',
        'editor' => 'unicorn',
        'image_upload' => [
            'status' => FALSE,
        ],
    ]);
    $editor->save();
    // The normal user:
    // - has access to 2 text formats;
    // - doesn't have access to the full_html text format, so: no text editor.
    $this->drupalLogin($this->normalUser);
    $this->drupalGet('node/add/article');
    [
        ,
        $editor_settings_present,
        $editor_js_present,
        $body,
    ] = $this->getThingsToCheck('body');
    $this->assertFalse($editor_settings_present, 'No Text Editor module settings.');
    $this->assertFalse($editor_js_present, 'No Text Editor JavaScript.');
    $this->assertSession()
        ->elementsCount('xpath', $body, 1);
    $this->assertSession()
        ->elementNotExists('css', 'select.js-filter-list');
    $this->drupalLogout();
    // The privileged user:
    // - has access to 2 text formats (and the fallback format);
    // - does have access to the full_html text format, so: Unicorn text editor.
    $this->drupalLogin($this->privilegedUser);
    $this->drupalGet('node/add/article');
    [
        $settings,
        $editor_settings_present,
        $editor_js_present,
        $body,
    ] = $this->getThingsToCheck('body');
    $expected = [
        'formats' => [
            'full_html' => [
                'format' => 'full_html',
                'editor' => 'unicorn',
                'editorSettings' => [
                    'ponyModeEnabled' => TRUE,
                ],
                'editorSupportsContentFiltering' => TRUE,
                'isXssSafe' => FALSE,
            ],
        ],
    ];
    $this->assertTrue($editor_settings_present, "Text Editor module's JavaScript settings are on the page.");
    $this->assertSame($expected, $settings['editor'], "Text Editor module's JavaScript settings on the page are correct.");
    $this->assertTrue($editor_js_present, 'Text Editor JavaScript is present.');
    $this->assertSession()
        ->elementsCount('xpath', $body, 1);
    $this->assertSession()
        ->elementsCount('css', 'select.js-filter-list', 1);
    $select = $this->assertSession()
        ->elementExists('css', 'select.js-filter-list');
    $this->assertSame('edit-body-0-value', $select->getAttribute('data-editor-for'));
    $this->drupalLogout();
    // Also associate a text editor with the "Plain Text" text format.
    $editor = Editor::create([
        'format' => 'plain_text',
        'editor' => 'unicorn',
        'image_upload' => [
            'status' => FALSE,
        ],
    ]);
    $editor->save();
    // The untrusted user:
    // - has access to 1 text format (plain_text);
    // - has access to the plain_text text format, so: Unicorn text editor.
    $this->drupalLogin($this->untrustedUser);
    $this->drupalGet('node/add/article');
    [
        $settings,
        $editor_settings_present,
        $editor_js_present,
        $body,
    ] = $this->getThingsToCheck('body');
    $expected = [
        'formats' => [
            'plain_text' => [
                'format' => 'plain_text',
                'editor' => 'unicorn',
                'editorSettings' => [
                    'ponyModeEnabled' => TRUE,
                ],
                'editorSupportsContentFiltering' => TRUE,
                'isXssSafe' => FALSE,
            ],
        ],
    ];
    $this->assertTrue($editor_settings_present, "Text Editor module's JavaScript settings are on the page.");
    $this->assertSame($expected, $settings['editor'], "Text Editor module's JavaScript settings on the page are correct.");
    $this->assertTrue($editor_js_present, 'Text Editor JavaScript is present.');
    $this->assertSession()
        ->elementsCount('xpath', $body, 1);
    $this->assertSession()
        ->elementNotExists('css', 'select.js-filter-list');
    // Verify that a single text format hidden input exists on the page and has
    // a "data-editor-for" attribute with the correct value.
    $hidden_input = $this->assertSession()
        ->hiddenFieldExists('body[0][format]');
    $this->assertSame('plain_text', $hidden_input->getValue());
    $this->assertSame('edit-body-0-value', $hidden_input->getAttribute('data-editor-for'));
    // Create an "article" node that uses the full_html text format, then try
    // to let the untrusted user edit it.
    $this->drupalCreateNode([
        'type' => 'article',
        'body' => [
            [
                'value' => $this->randomMachineName(32),
                'format' => 'full_html',
            ],
        ],
    ]);
    // The untrusted user tries to edit content that is written in a text format
    // that they are not allowed to use. The editor is still loaded. CKEditor,
    // for example, supports being loaded in a disabled state.
    $this->drupalGet('node/1/edit');
    [
        ,
        $editor_settings_present,
        $editor_js_present,
        $body,
    ] = $this->getThingsToCheck('body');
    $this->assertTrue($editor_settings_present, 'Text Editor module settings.');
    $this->assertTrue($editor_js_present, 'Text Editor JavaScript.');
    $this->assertSession()
        ->elementsCount('xpath', $body, 1);
    $this->assertSession()
        ->fieldDisabled("edit-body-0-value");
    $this->assertSession()
        ->fieldValueEquals("edit-body-0-value", 'This field has been disabled because you do not have sufficient permissions to edit it.');
    $this->assertSession()
        ->elementNotExists('css', 'select.js-filter-list');
    // Verify that no single text format hidden input exists on the page.
    $this->assertSession()
        ->elementNotExists('xpath', '//input[@type="hidden" and contains(@class, "editor")]');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.