class CKEditorTest

Tests for the 'CKEditor' text editor plugin.

Hierarchy

Expanded class hierarchy of CKEditorTest

File

drupal/core/modules/ckeditor/lib/Drupal/ckeditor/Tests/CKEditorTest.php, line 17
Definition of \Drupal\ckeditor\Tests\CKEditorTest.

Namespace

Drupal\ckeditor\Tests
View source
class CKEditorTest extends DrupalUnitTestBase {

  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = array(
    'system',
    'editor',
    'ckeditor',
  );

  /**
   * An instance of the "CKEditor" text editor plugin.
   *
   * @var \Drupal\ckeditor\Plugin\Editor\CKEditor;
   */
  protected $ckeditor;
  public static function getInfo() {
    return array(
      'name' => 'CKEditor text editor plugin',
      'description' => 'Tests all aspects of the CKEditor text editor plugin.',
      'group' => 'CKEditor',
    );
  }
  function setUp() {
    parent::setUp();

    // Install the Filter module.
    $this
      ->installSchema('system', 'url_alias');
    $this
      ->enableModules(array(
      'user',
      'filter',
    ));

    // Create text format, associate CKEditor.
    $filtered_html_format = entity_create('filter_format', array(
      'format' => 'filtered_html',
      'name' => 'Filtered HTML',
      'weight' => 0,
      'filters' => array(
        'filter_html' => array(
          'status' => 1,
        ),
      ),
    ));
    $filtered_html_format
      ->save();
    $editor = entity_create('editor', array(
      'format' => 'filtered_html',
      'editor' => 'ckeditor',
    ));
    $editor
      ->save();

    // Create "CKEditor" text editor plugin instance.
    $manager = $this->container
      ->get('plugin.manager.editor');
    $this->ckeditor = $manager
      ->createInstance('ckeditor');
  }

  /**
   * Tests CKEditor::getJSSettings().
   */
  function testGetJSSettings() {
    $editor = entity_load('editor', 'filtered_html');

    // Default toolbar.
    $expected_config = $this
      ->getDefaultInternalConfig() + array(
      'toolbar' => $this
        ->getDefaultToolbarConfig(),
      'contentsCss' => $this
        ->getDefaultContentsCssConfig(),
      'extraPlugins' => '',
      'language' => 'en',
      'stylesSet' => FALSE,
      'drupalExternalPlugins' => array(),
    );
    $this
      ->assertIdentical($expected_config, $this->ckeditor
      ->getJSSettings($editor), 'Generated JS settings are correct for default configuration.');

    // Customize the configuration: add button, have two contextually enabled
    // buttons, and configure a CKEditor plugin setting.
    $this
      ->enableModules(array(
      'ckeditor_test',
    ));
    drupal_container()
      ->get('plugin.manager.ckeditor.plugin')
      ->clearCachedDefinitions();
    $editor->settings['toolbar']['buttons'][0][] = 'Strike';
    $editor->settings['toolbar']['buttons'][1][] = 'Format';
    $editor->settings['plugins']['internal']['link_shortcut'] = 'CTRL+K';
    $editor
      ->save();
    $expected_config['toolbar'][count($expected_config['toolbar']) - 2]['items'][] = 'Strike';
    $expected_config['toolbar'][]['items'][] = 'Format';
    $expected_config['toolbar'][] = '/';
    $expected_config['format_tags'] = 'p;h4;h5;h6';
    $expected_config['extraPlugins'] = 'llama_contextual,llama_contextual_and_button';
    $expected_config['drupalExternalPlugins']['llama_contextual'] = file_create_url('core/modules/ckeditor/tests/modules/js/llama_contextual.js');
    $expected_config['drupalExternalPlugins']['llama_contextual_and_button'] = file_create_url('core/modules/ckeditor/tests/modules/js/llama_contextual_and_button.js');
    $expected_config['contentsCss'][] = file_create_url('core/modules/ckeditor/tests/modules/ckeditor_test.css');
    $expected_config['keystrokes'] = array(
      array(
        1114187,
        'link',
      ),
      array(
        1114188,
        NULL,
      ),
    );
    $this
      ->assertEqual($expected_config, $this->ckeditor
      ->getJSSettings($editor), 'Generated JS settings are correct for customized configuration.');

    // Change the allowed HTML tags; the "format_tags" setting for CKEditor
    // should automatically be updated as well.
    $format = entity_load('filter_format', 'filtered_html');
    $format
      ->filters('filter_html')->settings['allowed_html'] .= '<pre> <h3>';
    $format
      ->save();
    $expected_config['format_tags'] = 'p;h3;h4;h5;h6;pre';
    $this
      ->assertEqual($expected_config, $this->ckeditor
      ->getJSSettings($editor), 'Generated JS settings are correct for customized configuration.');
  }

  /**
   * Tests CKEditor::buildToolbarJSSetting().
   */
  function testBuildToolbarJSSetting() {
    $editor = entity_load('editor', 'filtered_html');

    // Default toolbar.
    $expected = $this
      ->getDefaultToolbarConfig();
    $this
      ->assertIdentical($expected, $this->ckeditor
      ->buildToolbarJSSetting($editor), '"toolbar" configuration part of JS settings built correctly for default toolbar.');

    // Customize the configuration.
    $editor->settings['toolbar']['buttons'][0][] = 'Strike';
    $editor
      ->save();
    $expected[count($expected) - 2]['items'][] = 'Strike';
    $this
      ->assertIdentical($expected, $this->ckeditor
      ->buildToolbarJSSetting($editor), '"toolbar" configuration part of JS settings built correctly for customized toolbar.');

    // Enable the editor_test module, customize further.
    $this
      ->enableModules(array(
      'ckeditor_test',
    ));
    drupal_container()
      ->get('plugin.manager.ckeditor.plugin')
      ->clearCachedDefinitions();
    $editor->settings['toolbar']['buttons'][0][] = 'Llama';
    $editor
      ->save();
    $expected[count($expected) - 2]['items'][] = 'Llama';
    $this
      ->assertIdentical($expected, $this->ckeditor
      ->buildToolbarJSSetting($editor), '"toolbar" configuration part of JS settings built correctly for customized toolbar with contrib module-provided CKEditor plugin.');
  }

  /**
   * Tests CKEditor::buildContentsCssJSSetting().
   */
  function testBuildContentsCssJSSetting() {
    $editor = entity_load('editor', 'filtered_html');

    // Default toolbar.
    $expected = $this
      ->getDefaultContentsCssConfig();
    $this
      ->assertIdentical($expected, $this->ckeditor
      ->buildContentsCssJSSetting($editor), '"contentsCss" configuration part of JS settings built correctly for default toolbar.');

    // Enable the editor_test module, which implements hook_ckeditor_css_alter().
    $this
      ->enableModules(array(
      'ckeditor_test',
    ));
    $expected[] = file_create_url('core/modules/ckeditor/tests/modules/ckeditor_test.css');
    $this
      ->assertIdentical($expected, $this->ckeditor
      ->buildContentsCssJSSetting($editor), '"contentsCss" configuration part of JS settings built correctly while a hook_ckeditor_css_alter() implementation exists.');

    // @todo test coverage for _ckeditor_theme_css(), by including a custom theme in this test with a "ckeditor_stylesheets" entry in its .info file.
  }

  /**
   * Tests Internal::getConfig().
   */
  function testInternalGetConfig() {
    $editor = entity_load('editor', 'filtered_html');
    $manager = drupal_container()
      ->get('plugin.manager.ckeditor.plugin');
    $internal_plugin = $manager
      ->createInstance('internal');

    // Default toolbar.
    $expected = $this
      ->getDefaultInternalConfig();
    $this
      ->assertIdentical($expected, $internal_plugin
      ->getConfig($editor), '"Internal" plugin configuration built correctly for default toolbar.');

    // Format dropdown/button enabled: new setting should be present.
    $editor->settings['toolbar']['buttons'][0][] = 'Format';
    $expected['format_tags'] = 'p;h4;h5;h6';
    $this
      ->assertIdentical($expected, $internal_plugin
      ->getConfig($editor), '"Internal" plugin configuration built correctly for customized toolbar.');
  }

  /**
   * Tests StylesCombo::getConfig().
   */
  function testStylesComboGetConfig() {
    $editor = entity_load('editor', 'filtered_html');
    $manager = drupal_container()
      ->get('plugin.manager.ckeditor.plugin');
    $stylescombo_plugin = $manager
      ->createInstance('stylescombo');

    // Styles dropdown/button enabled: new setting should be present.
    $editor->settings['toolbar']['buttons'][0][] = 'Styles';
    $editor->settings['plugins']['stylescombo']['styles'] = '';
    $editor
      ->save();
    $expected['stylesSet'] = array();
    $this
      ->assertIdentical($expected, $stylescombo_plugin
      ->getConfig($editor), '"StylesCombo" plugin configuration built correctly for customized toolbar.');

    // Configure the optional "styles" setting in odd ways that shouldn't affect
    // the end result.
    $editor->settings['plugins']['stylescombo']['styles'] = "   \n";
    $editor
      ->save();
    $this
      ->assertIdentical($expected, $stylescombo_plugin
      ->getConfig($editor));
    $editor->settings['plugins']['stylescombo']['styles'] = "\r\n  \n  \r  \n ";
    $editor
      ->save();
    $this
      ->assertIdentical($expected, $stylescombo_plugin
      ->getConfig($editor), '"StylesCombo" plugin configuration built correctly for customized toolbar.');

    // Now configure it properly, the end result should change.
    $editor->settings['plugins']['stylescombo']['styles'] = "h1.title|Title\np.mAgical.Callout|Callout";
    $editor
      ->save();
    $expected['stylesSet'] = array(
      array(
        'name' => 'Title',
        'element' => 'h1',
        'attributes' => array(
          'class' => 'title',
        ),
      ),
      array(
        'name' => 'Callout',
        'element' => 'p',
        'attributes' => array(
          'class' => 'mAgical Callout',
        ),
      ),
    );
    $this
      ->assertIdentical($expected, $stylescombo_plugin
      ->getConfig($editor), '"StylesCombo" plugin configuration built correctly for customized toolbar.');

    // Same configuration, but now interspersed with nonsense. Should yield the
    // same result.
    $editor->settings['plugins']['stylescombo']['styles'] = "  h1 .title   |  Title \r \n\r  \np.mAgical  .Callout|Callout\r";
    $editor
      ->save();
    $this
      ->assertIdentical($expected, $stylescombo_plugin
      ->getConfig($editor), '"StylesCombo" plugin configuration built correctly for customized toolbar.');

    // Invalid syntax should cause stylesSet to be set to FALSE.
    $editor->settings['plugins']['stylescombo']['styles'] = "h1|Title";
    $editor
      ->save();
    $expected['stylesSet'] = FALSE;
    $this
      ->assertIdentical($expected, $stylescombo_plugin
      ->getConfig($editor), '"StylesCombo" plugin configuration built correctly for customized toolbar.');
  }
  protected function getDefaultInternalConfig() {
    return array(
      'customConfig' => '',
      'pasteFromWordPromptCleanup' => TRUE,
      'removeDialogTabs' => 'image:Link;image:advanced;link:advanced',
      'resize_dir' => 'vertical',
      'keystrokes' => array(
        array(
          0x110000 + 75,
          'link',
        ),
        array(
          0x110000 + 76,
          NULL,
        ),
      ),
    );
  }
  protected function getDefaultToolbarConfig() {
    return array(
      0 => array(
        'items' => array(
          'Bold',
          'Italic',
        ),
      ),
      1 => array(
        'items' => array(
          'Link',
          'Unlink',
        ),
      ),
      2 => array(
        'items' => array(
          'BulletedList',
          'NumberedList',
        ),
      ),
      3 => array(
        'items' => array(
          'Blockquote',
          'Image',
        ),
      ),
      4 => array(
        'items' => array(
          'Source',
        ),
      ),
      5 => '/',
    );
  }
  protected function getDefaultContentsCssConfig() {
    return array(
      file_create_url('core/modules/ckeditor/css/ckeditor-iframe.css'),
    );
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CKEditorTest::$ckeditor protected property An instance of the "CKEditor" text editor plugin.
CKEditorTest::$modules public static property Modules to enable. Overrides DrupalUnitTestBase::$modules
CKEditorTest::getDefaultContentsCssConfig protected function
CKEditorTest::getDefaultInternalConfig protected function
CKEditorTest::getDefaultToolbarConfig protected function
CKEditorTest::getInfo public static function
CKEditorTest::setUp function Sets up Drupal unit test environment. Overrides DrupalUnitTestBase::setUp
CKEditorTest::testBuildContentsCssJSSetting function Tests CKEditor::buildContentsCssJSSetting().
CKEditorTest::testBuildToolbarJSSetting function Tests CKEditor::buildToolbarJSSetting().
CKEditorTest::testGetJSSettings function Tests CKEditor::getJSSettings().
CKEditorTest::testInternalGetConfig function Tests Internal::getConfig().
CKEditorTest::testStylesComboGetConfig function Tests StylesCombo::getConfig().
DrupalUnitTestBase::$keyValueFactory protected property A KeyValueMemoryFactory instance to use when building the container.
DrupalUnitTestBase::$moduleFiles private property
DrupalUnitTestBase::$themeData private property
DrupalUnitTestBase::$themeFiles private property
DrupalUnitTestBase::containerBuild public function Sets up the base service container for this test. 1
DrupalUnitTestBase::disableModules protected function Disables modules for this test.
DrupalUnitTestBase::enableModules protected function Enables modules for this test.
DrupalUnitTestBase::installConfig protected function Installs default configuration for a given list of modules.
DrupalUnitTestBase::installSchema protected function Installs a specific table from a module schema definition.
DrupalUnitTestBase::tearDown protected function Deletes created files, database tables, and reverts all environment changes. Overrides TestBase::tearDown 2
DrupalUnitTestBase::__construct function Overrides \Drupal\simpletest\UnitTestBase::__construct(). Overrides UnitTestBase::__construct
TestBase::$assertions protected property Assertions thrown in that test case.
TestBase::$configImporter protected property The config importer that can used in a test. 1
TestBase::$container protected property The dependency injection container used in the test. 1
TestBase::$databasePrefix protected property The database prefix of this test run.
TestBase::$dieOnFail public property Whether to die in case any test assertion fails.
TestBase::$originalFileDirectory protected property The original file directory, before it was changed for testing purposes.
TestBase::$originalPrefix protected property The original database prefix when running inside Simpletest.
TestBase::$originalSettings protected property The settings array.
TestBase::$public_files_directory protected property The public file directory for the test environment.
TestBase::$results public property Current results of this test case.
TestBase::$setup protected property Flag to indicate whether the test has been set up.
TestBase::$setupDatabasePrefix protected property
TestBase::$setupEnvironment protected property
TestBase::$skipClasses protected property This class is skipped when looking for the source of an assertion.
TestBase::$testId protected property The test run ID.
TestBase::$timeLimit protected property Time limit for the test.
TestBase::$verbose protected property TRUE if verbose debugging is enabled.
TestBase::$verboseClassName protected property Safe class name for use in verbose output filenames.
TestBase::$verboseDirectory protected property Directory where verbose output files are put.
TestBase::$verboseDirectoryUrl protected property URL to the verbose output file directory.
TestBase::$verboseId protected property Incrementing identifier for verbose output filenames.
TestBase::assert protected function Internal helper: stores the assert.
TestBase::assertEqual protected function Check to see if two values are equal.
TestBase::assertFalse protected function Check to see if a value is false (an empty string, 0, NULL, or FALSE).
TestBase::assertIdentical protected function Check to see if two values are identical.
TestBase::assertIdenticalObject protected function Checks to see if two objects are identical.
TestBase::assertNotEqual protected function Check to see if two values are not equal.
TestBase::assertNotIdentical protected function Check to see if two values are not identical.
TestBase::assertNotNull protected function Check to see if a value is not NULL.
TestBase::assertNull protected function Check to see if a value is NULL.
TestBase::assertTrue protected function Check to see if a value is not false (not an empty string, 0, NULL, or FALSE).
TestBase::changeDatabasePrefix protected function Changes the database connection to the prefixed one.
TestBase::checkRequirements protected function Checks the matching requirements for Test. 4
TestBase::configImporter public function Returns a ConfigImporter object to import test importing of configuration. 1
TestBase::copyConfig public function Copies configuration objects from source storage to target storage.
TestBase::deleteAssert public static function Delete an assertion record by message ID.
TestBase::error protected function Fire an error assertion. 1
TestBase::errorHandler public function Handle errors during test runs.
TestBase::exceptionHandler protected function Handle exceptions.
TestBase::fail protected function Fire an assertion that is always negative.
TestBase::filePreDeleteCallback public static function Ensures test files are deletable within file_unmanaged_delete_recursive().
TestBase::generatePermutations public static function Converts a list of possible parameters into a stack of permutations.
TestBase::getAssertionCall protected function Cycles through backtrace until the first non-assertion method is found.
TestBase::getDatabaseConnection public static function Returns the database connection to the site running Simpletest.
TestBase::insertAssert public static function Store an assertion from outside the testing context.
TestBase::pass protected function Fire an assertion that is always positive.
TestBase::prepareConfigDirectories protected function Create and set new configuration directories. 1
TestBase::prepareDatabasePrefix protected function Generates a database prefix for running tests.
TestBase::prepareEnvironment protected function Prepares the current environment for running the test.
TestBase::randomName public static function Generates a random string containing letters and numbers.
TestBase::randomObject public static function Generates a random PHP object.
TestBase::randomString public static function Generates a random string of ASCII characters of codes 32 to 126.
TestBase::rebuildContainer protected function Rebuild drupal_container(). 1
TestBase::run public function Run all tests in this class.
TestBase::settingsSet protected function Changes in memory settings.
TestBase::verbose protected function Logs verbose message in a text file.
UnitTestBase::$configDirectories protected property