function MediaTest::testLinkManualDecorator

Ensure that manual link decorators work with linkable media.

@dataProvider providerLinkability

File

core/modules/ckeditor5/tests/src/FunctionalJavascript/MediaTest.php, line 1040

Class

MediaTest
@coversDefaultClass \Drupal\ckeditor5\Plugin\CKEditor5Plugin\Media @group ckeditor5 @internal

Namespace

Drupal\Tests\ckeditor5\FunctionalJavascript

Code

public function testLinkManualDecorator(bool $unrestricted) {
    \Drupal::service('module_installer')->install([
        'ckeditor5_manual_decorator_test',
    ]);
    $this->resetAll();
    $decorator = 'Open in a new tab';
    $decorator_attributes = '[@target="_blank"][@rel="noopener noreferrer"][@class="link-new-tab"]';
    // Disable filter_html.
    if ($unrestricted) {
        FilterFormat::load('test_format')->setFilterConfig('filter_html', [
            'status' => FALSE,
        ])
            ->save();
        $decorator = 'Pink color';
        $decorator_attributes = '[@style="color:pink;"]';
    }
    $this->drupalGet($this->host
        ->toUrl('edit-form'));
    $this->waitForEditor();
    $assert_session = $this->assertSession();
    $page = $this->getSession()
        ->getPage();
    $this->assertNotEmpty($drupalmedia = $assert_session->waitForElementVisible('css', '.ck-content .ck-widget.drupal-media'));
    $drupalmedia->click();
    $this->assertVisibleBalloon('.ck-toolbar[aria-label="Drupal Media toolbar"]');
    // Turn off caption, so we don't accidentally put our link in that text
    // field instead of on the actual media.
    $this->getBalloonButton('Toggle caption off')
        ->click();
    $assert_session->assertNoElementAfterWait('css', 'figure.drupal-media > figcaption');
    $this->assertVisibleBalloon('.ck-toolbar[aria-label="Drupal Media toolbar"]');
    $this->getBalloonButton('Link media')
        ->click();
    $balloon = $this->assertVisibleBalloon('.ck-link-form');
    $url_input = $balloon->find('css', '.ck-labeled-field-view__input-wrapper .ck-input-text');
    $url_input->setValue('http://linking-embedded-media.com');
    $this->getBalloonButton($decorator)
        ->click();
    $balloon->pressButton('Save');
    $this->assertNotEmpty($assert_session->waitForElementVisible('css', '.drupal-media a'));
    $this->assertVisibleBalloon('.ck-link-actions');
    $xpath = new \DOMXPath($this->getEditorDataAsDom());
    $this->assertNotEmpty($xpath->query("//a[@href='http://linking-embedded-media.com']{$decorator_attributes}"));
    $this->assertNotEmpty($xpath->query("//a[@href='http://linking-embedded-media.com']{$decorator_attributes}/drupal-media"));
    // Ensure that manual decorators upcast correctly.
    $page->pressButton('Save');
    $this->drupalGet($this->host
        ->toUrl('edit-form'));
    $this->assertNotEmpty($drupalmedia = $assert_session->waitForElementVisible('css', '.ck-content .ck-widget.drupal-media'));
    $xpath = new \DOMXPath($this->getEditorDataAsDom());
    $this->assertNotEmpty($xpath->query("//a[@href='http://linking-embedded-media.com']{$decorator_attributes}"));
    $this->assertNotEmpty($xpath->query("//a[@href='http://linking-embedded-media.com']{$decorator_attributes}/drupal-media"));
    // Finally, ensure that media can be unlinked.
    $drupalmedia->click();
    $this->assertVisibleBalloon('.ck-toolbar[aria-label="Drupal Media toolbar"]');
    $this->getBalloonButton('Link media')
        ->click();
    $this->assertVisibleBalloon('.ck-link-actions');
    $this->getBalloonButton('Unlink')
        ->click();
    $this->assertTrue($assert_session->waitForElementRemoved('css', '.drupal-media a'));
    $xpath = new \DOMXPath($this->getEditorDataAsDom());
    $this->assertEmpty($xpath->query('//a'));
    $this->assertNotEmpty($xpath->query('//drupal-media'));
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.