trait CKEditorTestTrait

Same name in other branches
  1. 9 core/modules/ckeditor/tests/src/Traits/CKEditorTestTrait.php \Drupal\Tests\ckeditor\Traits\CKEditorTestTrait

Provides methods to test CKEditor.

This trait is meant to be used only by functional JavaScript test classes.

Hierarchy

3 files declare their use of CKEditorTestTrait
CKEditorIntegrationTest.php in core/modules/media/tests/src/FunctionalJavascript/CKEditorIntegrationTest.php
CKEditorIntegrationTest.php in core/modules/media_library/tests/src/FunctionalJavascript/CKEditorIntegrationTest.php
CKEditorIntegrationTest.php in core/modules/ckeditor/tests/src/FunctionalJavascript/CKEditorIntegrationTest.php

File

core/modules/ckeditor/tests/src/Traits/CKEditorTestTrait.php, line 10

Namespace

Drupal\Tests\ckeditor\Traits
View source
trait CKEditorTestTrait {
    
    /**
     * Waits for CKEditor to initialize.
     *
     * @param string $instance_id
     *   (optional) The CKEditor instance ID. Defaults to 'edit-body-0-value'.
     * @param int $timeout
     *   (optional) Timeout in milliseconds, defaults to 10000.
     */
    protected function waitForEditor($instance_id = 'edit-body-0-value', $timeout = 10000) {
        $condition = <<<JS
      (function() {
        return (
          typeof CKEDITOR !== 'undefined'
          && typeof CKEDITOR.instances["{<span class="php-variable">$instance_id</span>}"] !== 'undefined'
          && CKEDITOR.instances["{<span class="php-variable">$instance_id</span>}"].instanceReady
        );
      }());
JS;
        $this->assertJsCondition($condition, $timeout);
    }
    
    /**
     * Assigns a name to the CKEditor iframe.
     *
     * @param string $id
     *   (optional) The id to assign the iframe element. Defaults to 'ckeditor'.
     * @param string $instance_id
     *   (optional) The CKEditor instance ID. Defaults to 'edit-body-0-value'.
     *
     * @see \Behat\Mink\Session::switchToIFrame()
     */
    protected function assignNameToCkeditorIframe($id = 'ckeditor', $instance_id = 'edit-body-0-value') {
        $javascript = <<<JS
(function(){
  CKEDITOR.instances['{<span class="php-variable">$instance_id</span>}'].element.getParent().find('.cke_wysiwyg_frame').\$[0].id = '{<span class="php-variable">$id</span>}';
})()
JS;
        $this->getSession()
            ->evaluateScript($javascript);
    }
    
    /**
     * Clicks a CKEditor button.
     *
     * @param string $name
     *   The name of the button, such as `drupallink`, `source`, etc.
     * @param string $instance_id
     *   (optional) The CKEditor instance ID. Defaults to 'edit-body-0-value'.
     */
    protected function pressEditorButton($name, $instance_id = 'edit-body-0-value') {
        $this->getEditorButton($name, $instance_id)
            ->click();
    }
    
    /**
     * Waits for a CKEditor button and returns it when available and visible.
     *
     * @param string $name
     *   The name of the button, such as `drupallink`, `source`, etc.
     * @param string $instance_id
     *   (optional) The CKEditor instance ID. Defaults to 'edit-body-0-value'.
     *
     * @return \Behat\Mink\Element\NodeElement|null
     *   The page element node if found, NULL if not.
     */
    protected function getEditorButton($name, $instance_id = 'edit-body-0-value') {
        $this->getSession()
            ->switchToIFrame();
        $button = $this->assertSession()
            ->waitForElementVisible('css', "#cke_{$instance_id} a.cke_button__" . $name);
        $this->assertNotEmpty($button);
        return $button;
    }
    
    /**
     * Asserts a CKEditor button is disabled.
     *
     * @param string $name
     *   The name of the button, such as `drupallink`, `source`, etc.
     * @param string $instance_id
     *   (optional) The CKEditor instance ID. Defaults to 'edit-body-0-value'.
     */
    protected function assertEditorButtonDisabled($name, $instance_id = 'edit-body-0-value') {
        $button = $this->getEditorButton($name, $instance_id);
        $this->assertTrue($button->hasClass('cke_button_disabled'));
        $this->assertSame('true', $button->getAttribute('aria-disabled'));
    }
    
    /**
     * Asserts a CKEditor button is enabled.
     *
     * @param string $name
     *   The name of the button, such as `drupallink`, `source`, etc.
     * @param string $instance_id
     *   (optional) The CKEditor instance ID. Defaults to 'edit-body-0-value'.
     */
    protected function assertEditorButtonEnabled($name, $instance_id = 'edit-body-0-value') {
        $button = $this->getEditorButton($name, $instance_id);
        $this->assertFalse($button->hasClass('cke_button_disabled'));
        $this->assertSame('false', $button->getAttribute('aria-disabled'));
    }

}

Members

Title Sort descending Modifiers Object type Summary
CKEditorTestTrait::assertEditorButtonDisabled protected function Asserts a CKEditor button is disabled.
CKEditorTestTrait::assertEditorButtonEnabled protected function Asserts a CKEditor button is enabled.
CKEditorTestTrait::assignNameToCkeditorIframe protected function Assigns a name to the CKEditor iframe.
CKEditorTestTrait::getEditorButton protected function Waits for a CKEditor button and returns it when available and visible.
CKEditorTestTrait::pressEditorButton protected function Clicks a CKEditor button.
CKEditorTestTrait::waitForEditor protected function Waits for CKEditor to initialize.

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