View source
<?php
namespace Drupal\Tests\linkit_media_library\FunctionalJavascript;
use Drupal\editor\Entity\Editor;
use Drupal\file\Entity\File;
use Drupal\filter\Entity\FilterFormat;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
use Drupal\linkit\Tests\ProfileCreationTrait;
use Drupal\media\Entity\Media;
use Drupal\Tests\ckeditor\Traits\CKEditorTestTrait;
use Drupal\Tests\media\Traits\MediaTypeCreationTrait;
use Drupal\Tests\TestFileCreationTrait;
class LinkitMediaLibraryTest extends WebDriverTestBase {
use CKEditorTestTrait;
use MediaTypeCreationTrait;
use ProfileCreationTrait;
use TestFileCreationTrait;
protected $filter;
protected $editor;
protected $testUser;
protected static $modules = [
'ckeditor',
'linkit_media_library',
'node',
];
protected $defaultTheme = 'stark';
protected function setUp() : void {
parent::setUp();
$this->filter = FilterFormat::create([
'format' => 'test_format',
'name' => 'Filtered HTML',
'weight' => 0,
'filters' => [
'linkit' => [
'status' => TRUE,
'settings' => [
'title' => FALSE,
],
],
],
]);
$this->filter
->save();
$this->editor = Editor::create([
'format' => 'test_format',
'editor' => 'ckeditor',
]);
$this->editor
->setSettings([
'plugins' => [
'drupallink' => [
'linkit_enabled' => TRUE,
'linkit_profile' => 'default',
],
],
]);
$this->editor
->save();
$this
->createMediaType('file', [
'id' => 'document',
]);
File::create([
'uri' => $this
->getTestFiles('text')[0]->uri,
])
->save();
Media::create([
'bundle' => 'document',
'name' => 'Test document',
'field_media_document' => [
[
'target_id' => 1,
],
],
])
->save();
$this
->drupalCreateContentType([
'type' => 'page',
]);
$this->testUser = $this
->drupalCreateUser([
'use text format test_format',
'bypass node access',
]);
$this
->drupalLogin($this->testUser);
}
public function testLinkitMediaLibraryInsertion() {
$assert_session = $this
->assertSession();
$page = $this
->getSession()
->getPage();
$this
->drupalGet('node/add/page');
$this
->waitForEditor();
$this
->pressEditorButton('drupallink');
$assert_session
->waitForId('drupal-modal');
$assert_session
->elementExists('xpath', '//div[@id="drupal-modal"]//input[@name="linkit_media_library_opener"]');
$page
->pressButton('Media Library');
$assert_session
->waitForElement('css', '.media-library-widget-modal');
$page
->checkField('Select Test document');
$assert_session
->elementExists('css', '.ui-dialog-buttonpane')
->pressButton('Insert selected');
$assert_session
->waitForElementRemoved('css', '.media-library-widget-modal');
$this
->assignNameToCkeditorIframe();
$this
->getSession()
->switchToIFrame('ckeditor');
$link = $assert_session
->elementExists('css', 'a[href="/media/1"]');
$this
->assertNotEmpty($link);
}
public function testButtonRendering() {
$assert_session = $this
->assertSession();
$this
->drupalGet('node/add/page');
$this
->waitForEditor();
$this
->pressEditorButton('drupallink');
$assert_session
->waitForId('drupal-modal');
$assert_session
->elementExists('xpath', '//div[@id="drupal-modal"]//input[@name="linkit_media_library_opener"]');
$this->editor
->setSettings([
'plugins' => [
'drupallink' => [
'linkit_enabled' => FALSE,
'linkit_profile' => '',
],
],
]);
$this->editor
->save();
$this
->drupalGet('node/add/page');
$this
->waitForEditor();
$this
->pressEditorButton('drupallink');
$assert_session
->waitForId('drupal-modal');
$assert_session
->elementNotExists('xpath', '//div[@id="drupal-modal"]//input[@name="linkit_media_library_opener"]');
$this->editor
->setSettings([
'plugins' => [
'drupallink' => [
'linkit_enabled' => TRUE,
'linkit_profile' => 'default',
],
],
]);
$this->editor
->save();
$this->filter
->removeFilter('linkit');
$this->filter
->save();
$this
->drupalGet('node/add/page');
$this
->waitForEditor();
$this
->pressEditorButton('drupallink');
$assert_session
->waitForId('drupal-modal');
$assert_session
->elementNotExists('xpath', '//div[@id="drupal-modal"]//input[@name="linkit_media_library_opener"]');
}
}