View source
<?php
namespace Drupal\Tests\linkit\FunctionalJavascript;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\editor\Entity\Editor;
use Drupal\entity_test\Entity\EntityTestMul;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\filter\Entity\FilterFormat;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\linkit\Tests\ProfileCreationTrait;
use Drupal\node\Entity\NodeType;
class LinkitDialogTest extends WebDriverTestBase {
use ProfileCreationTrait;
public static $modules = [
'node',
'ckeditor',
'filter',
'linkit',
'entity_test',
'language',
];
protected $defaultTheme = 'stark';
protected $ckeditor;
protected $linkitProfile;
protected function setUp() {
parent::setUp();
$matcherManager = $this->container
->get('plugin.manager.linkit.matcher');
$this->linkitProfile = $this
->createProfile();
$plugin = $matcherManager
->createInstance('entity:entity_test_mul');
$this->linkitProfile
->addMatcher($plugin
->getConfiguration());
$this->linkitProfile
->save();
$llama_format = FilterFormat::create([
'format' => 'llama',
'name' => 'Llama',
'weight' => 0,
'filters' => [],
]);
$llama_format
->save();
$editor = Editor::create([
'format' => 'llama',
'editor' => 'ckeditor',
]);
$editor
->save();
$this->ckeditor = $this->container
->get('plugin.manager.editor')
->createInstance('ckeditor');
NodeType::create([
'type' => 'page',
'name' => 'page',
])
->save();
FieldConfig::create([
'field_storage' => FieldStorageConfig::loadByName('node', 'body'),
'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->container
->get('plugin.manager.editor')
->clearCachedDefinitions();
$this->ckeditor = $this->container
->get('plugin.manager.editor')
->createInstance('ckeditor');
$this->container
->get('plugin.manager.ckeditor.plugin')
->clearCachedDefinitions();
$settings = $editor
->getSettings();
$settings['plugins']['drupallink']['linkit_enabled'] = TRUE;
$settings['plugins']['drupallink']['linkit_profile'] = $this->linkitProfile
->id();
$editor
->setSettings($settings);
$editor
->save();
$account = $this
->drupalCreateUser([
'administer nodes',
'create page content',
'edit own page content',
'use text format llama',
'view test entity',
]);
$this
->drupalLogin($account);
}
public function testLinkDialog() {
$session = $this
->getSession();
$web_assert = $this
->assertSession();
$page = $session
->getPage();
$langcodes = [
'sv',
'da',
'fi',
];
foreach ($langcodes as $langcode) {
ConfigurableLanguage::createFromLangcode($langcode)
->save();
}
$entity = EntityTestMul::create([
'name' => 'Foo',
]);
$entity
->save();
$this
->drupalGet('node/add/page');
$ckeditor_loaded = $this
->getSession()
->wait(5000, "jQuery('.cke_contents').length > 0");
$this
->assertTrue($ckeditor_loaded, 'The editor has been loaded.');
$page
->find('css', 'a.cke_button__drupallink')
->click();
$web_assert
->assertWaitOnAjaxRequest();
$href_field = $page
->findField('attributes[href]');
$href_field
->hasAttribute('data-autocomplete-path');
$href_field
->hasClass('form-linkit-autocomplete');
$href_field
->hasClass('ui-autocomplete-input');
$this
->assertEmpty($href_field
->getValue(), 'Href field is empty.');
$this
->assertEmptyWithJs('attributes[data-entity-type]');
$this
->assertEmptyWithJs('attributes[data-entity-uuid]');
$this
->assertEmptyWithJs('attributes[data-entity-substitution]');
$this
->assertEmptyWithJs('href_dirty_check');
$autocomplete_container = $page
->find('css', 'ul.linkit-ui-autocomplete');
$this
->assertFalse($autocomplete_container
->isVisible());
$href_field
->setValue('f');
$this
->getSession()
->getDriver()
->keyDown($href_field
->getXpath(), ' ');
$this
->getSession()
->wait(5000, "jQuery('.linkit-result-line.ui-menu-item').length > 0");
$this
->assertTrue($autocomplete_container
->isVisible());
$results = $page
->findAll('css', '.linkit-result-line.ui-menu-item');
$this
->assertEquals(1, count($results), 'Found autocomplete result');
$page
->find('xpath', '//li[contains(@class, "linkit-result-line") and contains(@class, "ui-menu-item")][1]')
->click();
$this
->assertEquals($entity
->toUrl()
->toString(), $href_field
->getValue(), 'The href field is populated with the node url.');
$this
->assertEqualsWithJs('attributes[data-entity-type]', $entity
->getEntityTypeId());
$this
->assertEqualsWithJs('attributes[data-entity-uuid]', $entity
->uuid());
$this
->assertEqualsWithJs('attributes[data-entity-substitution]', 'canonical');
$this
->assertEqualsWithJs('href_dirty_check', $entity
->toUrl()
->toString());
$this
->click('.editor-link-dialog button:contains("Save")');
$web_assert
->assertWaitOnAjaxRequest();
$fields = [
'data-entity-type' => $entity
->getEntityTypeId(),
'data-entity-uuid' => $entity
->uuid(),
'data-entity-substitution' => 'canonical',
'href' => $entity
->toUrl()
->toString(),
];
foreach ($fields as $attribute => $value) {
$link_attribute = $this
->getLinkAttributeFromEditor($attribute);
$this
->assertEquals($value, $link_attribute, 'The link contain an attribute by the name of "' . $attribute . '" with a value of "' . $value . '"');
}
$javascript = <<<JS
(function(){
var editor = window.CKEDITOR.instances['edit-body-0-value'];
console.log(editor);
var element = editor.document.findOne( 'a' );
editor.getSelection().selectElement( element );
})()
JS;
$session
->executeScript($javascript);
$page
->find('css', 'a.cke_button__drupallink')
->click();
$web_assert
->assertWaitOnAjaxRequest();
$href_field = $page
->findField('attributes[href]');
$this
->assertEquals($entity
->toUrl()
->toString(), $href_field
->getValue(), 'Href field contains the node url when edit.');
$this
->assertEqualsWithJs('attributes[data-entity-type]', $entity
->getEntityTypeId());
$this
->assertEqualsWithJs('attributes[data-entity-uuid]', $entity
->uuid());
$this
->assertEqualsWithJs('attributes[data-entity-substitution]', 'canonical');
$this
->assertEqualsWithJs('href_dirty_check', $entity
->toUrl()
->toString());
$href_field
->setValue('http://example.com');
$this
->click('.editor-link-dialog button:contains("Save")');
$web_assert
->assertWaitOnAjaxRequest();
$fields = [
'data-entity-type',
'data-entity-uuid',
'data-entity-substitution',
];
foreach ($fields as $attribute) {
$link_attribute = $this
->getLinkAttributeFromEditor($attribute);
$this
->assertNull($link_attribute, 'The link does not contain an attribute by the name of "' . $attribute . '"');
}
$href_attribute = $this
->getLinkAttributeFromEditor('href');
$this
->assertEquals('http://example.com', $href_attribute, 'The link href is correct.');
}
private function assertEmptyWithJs($field_name) {
$javascript = "(function (){ return jQuery('input[name=\"" . $field_name . "\"]').val(); })()";
$field_value = $this
->getSession()
->evaluateScript($javascript);
$this
->assertEmpty($field_value, 'The "' . $field_name . '" field is empty.');
}
private function assertEqualsWithJs($field_name, $expected) {
$javascript = "(function (){ return jQuery('input[name=\"" . $field_name . "\"]').val(); })()";
$field_value = $this
->getSession()
->evaluateScript($javascript);
$this
->assertEquals($expected, $field_value, 'The "' . $field_name . '" field has a value of "' . $expected . '".');
}
private function getLinkAttributeFromEditor($attribute) {
$javascript = <<<JS
(function(){
var iframes = document.getElementsByClassName('cke_wysiwyg_frame');
if (iframes.length) {
var doc = iframes[0].contentDocument || iframes[0].contentWindow.document;
var link = doc.getElementsByTagName('a')[0];
return link.getAttribute("{<span class="php-variable">$attribute</span>}");
}
})()
JS;
return $this
->getSession()
->evaluateScript($javascript);
}
}