View source
<?php
namespace Drupal\ckeditor\Tests;
use Drupal\simpletest\WebTestBase;
class CKEditorLoadingTest extends WebTestBase {
public static $modules = array(
'filter',
'editor',
'ckeditor',
'node',
);
protected $untrustedUser;
protected $normalUser;
protected function setUp() {
parent::setUp();
$filtered_html_format = entity_create('filter_format', array(
'format' => 'filtered_html',
'name' => 'Filtered HTML',
'weight' => 0,
'filters' => array(),
));
$filtered_html_format
->save();
$editor = entity_create('editor', array(
'format' => 'filtered_html',
'editor' => 'ckeditor',
));
$editor
->save();
$full_html_format = entity_create('filter_format', array(
'format' => 'full_html',
'name' => 'Full HTML',
'weight' => 1,
'filters' => array(),
));
$full_html_format
->save();
$this
->drupalCreateContentType(array(
'type' => 'article',
'name' => 'Article',
));
$this->untrustedUser = $this
->drupalCreateUser(array(
'create article content',
'edit any article content',
));
$this->normalUser = $this
->drupalCreateUser(array(
'create article content',
'edit any article content',
'use text format filtered_html',
'use text format full_html',
));
}
function testLoading() {
$this
->drupalLogin($this->untrustedUser);
$this
->drupalGet('node/add/article');
list($settings, $editor_settings_present, $editor_js_present, $body, $format_selector) = $this
->getThingsToCheck();
$this
->assertFalse($editor_settings_present, 'No Text Editor module settings.');
$this
->assertFalse($editor_js_present, 'No Text Editor JavaScript.');
$this
->assertTrue(count($body) === 1, 'A body field exists.');
$this
->assertTrue(count($format_selector) === 0, 'No text format selector exists on the page.');
$hidden_input = $this
->xpath('//input[@type="hidden" and contains(@class, "editor")]');
$this
->assertTrue(count($hidden_input) === 0, 'A single text format hidden input does not exist on the page.');
$this
->assertNoRaw(drupal_get_path('module', 'ckeditor') . '/js/ckeditor.js', 'CKEditor glue JS is absent.');
$this
->drupalGet('user');
$this
->assertNoRaw(drupal_get_path('module', 'ckeditor') . '/js/ckeditor.js', 'CKEditor glue JS is absent.');
$this
->drupalLogin($this->normalUser);
$this
->drupalGet('node/add/article');
list($settings, $editor_settings_present, $editor_js_present, $body, $format_selector) = $this
->getThingsToCheck();
$ckeditor_plugin = $this->container
->get('plugin.manager.editor')
->createInstance('ckeditor');
$editor = entity_load('editor', 'filtered_html');
$expected = array(
'formats' => array(
'filtered_html' => array(
'format' => 'filtered_html',
'editor' => 'ckeditor',
'editorSettings' => $this
->castSafeStrings($ckeditor_plugin
->getJSSettings($editor)),
'editorSupportsContentFiltering' => TRUE,
'isXssSafe' => FALSE,
),
),
);
$this
->assertTrue($editor_settings_present, "Text Editor module's JavaScript settings are on the page.");
$this
->assertIdentical($expected, $this
->castSafeStrings($settings['editor']), "Text Editor module's JavaScript settings on the page are correct.");
$this
->assertTrue($editor_js_present, 'Text Editor JavaScript is present.');
$this
->assertTrue(count($body) === 1, 'A body field exists.');
$this
->assertTrue(count($format_selector) === 1, 'A single text format selector exists on the page.');
$specific_format_selector = $this
->xpath('//select[contains(@class, "filter-list") and @data-editor-for="edit-body-0-value"]');
$this
->assertTrue(count($specific_format_selector) === 1, 'A single text format selector exists on the page and has a "data-editor-for" attribute with the correct value.');
$this
->assertTrue(in_array('ckeditor/drupal.ckeditor', explode(',', $settings['ajaxPageState']['libraries'])), 'CKEditor glue library is present.');
\Drupal::service('module_installer')
->install(array(
'ckeditor_test',
));
$this->container
->get('plugin.manager.ckeditor.plugin')
->clearCachedDefinitions();
$editor_settings = $editor
->getSettings();
$editor_settings['toolbar']['rows'][0][0]['items'][] = 'Llama';
$editor
->setSettings($editor_settings);
$editor
->save();
$this
->drupalGet('node/add/article');
list($settings, $editor_settings_present, $editor_js_present, $body, $format_selector) = $this
->getThingsToCheck();
$expected = array(
'formats' => array(
'filtered_html' => array(
'format' => 'filtered_html',
'editor' => 'ckeditor',
'editorSettings' => $this
->castSafeStrings($ckeditor_plugin
->getJSSettings($editor)),
'editorSupportsContentFiltering' => TRUE,
'isXssSafe' => FALSE,
),
),
);
$this
->assertTrue($editor_settings_present, "Text Editor module's JavaScript settings are on the page.");
$this
->assertIdentical($expected, $this
->castSafeStrings($settings['editor']), "Text Editor module's JavaScript settings on the page are correct.");
$this
->assertTrue($editor_js_present, 'Text Editor JavaScript is present.');
$this
->assertTrue(in_array('ckeditor/drupal.ckeditor', explode(',', $settings['ajaxPageState']['libraries'])), 'CKEditor glue library is present.');
}
protected function getThingsToCheck() {
$settings = $this
->getDrupalSettings();
return array(
$settings,
isset($settings['editor']),
isset($settings['ajaxPageState']['libraries']) && in_array('ckeditor/drupal.ckeditor', explode(',', $settings['ajaxPageState']['libraries'])),
$this
->xpath('//textarea[@id="edit-body-0-value"]'),
$this
->xpath('//select[contains(@class, "filter-list")]'),
);
}
}