View source
<?php
namespace Drupal\Tests\nbsp\FunctionalJavascript;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\editor\Entity\Editor;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\filter\Entity\FilterFormat;
use Drupal\node\Entity\NodeType;
use Drupal\Tests\ckeditor\Traits\CKEditorTestTrait;
use Drupal\Component\Utility\Html;
class DrupalNbspTest extends WebDriverTestBase {
use CKEditorTestTrait;
protected static $modules = [
'filter',
'editor',
'ckeditor',
'nbsp',
];
protected $profile = 'minimal';
protected $defaultTheme = 'classy';
protected $adminUser;
protected $editor;
protected $editorFilterFormat;
protected function setUp() : void {
parent::setUp();
$this->editorFilterFormat = FilterFormat::create([
'format' => 'full_html',
'name' => 'Full HTML',
'weight' => 0,
'filters' => [],
]);
$this->editorFilterFormat
->save();
$this->editor = Editor::create([
'format' => 'full_html',
'editor' => 'ckeditor',
]);
$settings = [
'toolbar' => [
'rows' => [
[
[
'name' => 'All the things',
'items' => [
'Source',
'Bold',
'Italic',
'DrupalNbsp',
],
],
],
],
],
'plugins' => [],
];
$this->editor
->setSettings($settings);
$this->editor
->save();
NodeType::create([
'type' => 'page',
'name' => 'page',
])
->save();
$field_storage = FieldStorageConfig::loadByName('node', 'body');
FieldConfig::create([
'field_storage' => $field_storage,
'bundle' => 'page',
'label' => 'Body',
'settings' => [
'display_summary' => TRUE,
],
'required' => TRUE,
])
->save();
EntityFormDisplay::create([
'targetEntityType' => 'node',
'bundle' => 'page',
'mode' => 'default',
'status' => TRUE,
])
->setComponent('body', [
'type' => 'text_textarea_with_summary',
])
->save();
$this->adminUser = $this
->drupalCreateUser([
'administer nodes',
'create page content',
'use text format full_html',
]);
$this
->drupalLogin($this->adminUser);
}
public function testEditorWorksWhenNbspEnabled() {
$this
->drupalGet('node/add/page');
$this
->waitForEditor();
$this
->assignNameToCkeditorIframe();
$this
->getSession()
->switchToIFrame('ckeditor');
$assert_session = $this
->assertSession();
$this
->assertNotEmpty($assert_session
->waitForElementVisible('css', '.cke_editable', 1000));
$this
->getSession()
->switchToIFrame();
$this
->assertNotEmpty($this
->assertSession()
->waitForElementVisible('css', 'a.cke_button__drupalnbsp'));
}
public function testEditorWorksWhenNbspNotEnabled() {
$settings = [
'toolbar' => [
'rows' => [
[
[
'name' => 'All the things',
'items' => [
'Bold',
'Italic',
],
],
],
],
],
'plugins' => [],
];
$this->editor
->setSettings($settings);
$this->editor
->save();
$this
->drupalGet('node/add/page');
$this
->waitForEditor();
$this
->assignNameToCkeditorIframe();
$this
->getSession()
->switchToIFrame('ckeditor');
$assert_session = $this
->assertSession();
$this
->assertNotEmpty($assert_session
->waitForElementVisible('css', '.cke_editable', 1000));
$this
->getSession()
->switchToIFrame();
$this
->assertEmpty($this
->assertSession()
->waitForElementVisible('css', 'a.cke_button__drupalnbsp'));
}
public function testButton() {
$this
->drupalGet('node/add/page');
$this
->waitForEditor();
$this
->pressEditorButton('drupalnbsp');
$this
->pressEditorButton('source');
$assert_session = $this
->assertSession();
$value = $assert_session
->elementExists('css', 'textarea.cke_source')
->getValue();
$dom = Html::load($value);
$xpath = new \DOMXPath($dom);
$nbsp = $xpath
->query('//span')[0];
$expected_attributes = [
'class' => 'nbsp',
];
foreach ($expected_attributes as $name => $expected) {
$this
->assertSame($expected, $nbsp
->getAttribute($name));
}
$this
->assertEquals(" ", $nbsp->firstChild->nodeValue);
}
}