View source  
  <?php
namespace Drupal\Tests\ckeditor\FunctionalJavascript;
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\FunctionalJavascriptTests\WebDriverTestBase;
use Drupal\node\Entity\NodeType;
use Drupal\Tests\ckeditor\Traits\CKEditorTestTrait;
class CKEditorIntegrationTest extends WebDriverTestBase {
  use CKEditorTestTrait;
  
  protected $defaultTheme = 'stark';
  
  protected $account;
  
  protected $filterFormat;
  
  protected static $modules = [
    'node',
    'ckeditor',
    'filter',
    'ckeditor_test',
  ];
  
  protected function setUp() : void {
    parent::setUp();
    
    $this->filterFormat = FilterFormat::create([
      'format' => 'filtered_html',
      'name' => 'Filtered HTML',
      'weight' => 0,
    ]);
    $this->filterFormat
      ->save();
    Editor::create([
      'format' => 'filtered_html',
      'editor' => 'ckeditor',
    ])
      ->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->account = $this
      ->drupalCreateUser([
      'administer nodes',
      'create page content',
      'use text format filtered_html',
    ]);
    $this
      ->drupalLogin($this->account);
  }
  
  public function testFragmentLink() {
    $session = $this
      ->getSession();
    $web_assert = $this
      ->assertSession();
    $ckeditor_id = '#cke_edit-body-0-value';
    $this
      ->drupalGet('node/add/page');
    $session
      ->getPage();
    
    $session
      ->executeScript("document.getElementById('edit-title-0-value').style.marginBottom = window.innerHeight*2 +'px';");
    $this
      ->assertSession()
      ->waitForElementVisible('css', $ckeditor_id);
    
    $web_assert
      ->assertNotVisibleInViewport('css', $ckeditor_id, 'topLeft', 'CKEditor-enabled body field is visible.');
    $before_url = $session
      ->getCurrentUrl();
    
    $session
      ->executeScript("location.hash = '#edit-body-0-value';");
    
    $web_assert
      ->assertVisibleInViewport('css', $ckeditor_id, 'topLeft', 'CKEditor-enabled body field is not visible.');
    
    $session
      ->executeScript("history.back();");
    $after_url = $session
      ->getCurrentUrl();
    
    self::assertEquals($before_url, $after_url, 'History back works.');
  }
  
  public function testDrupalImageDialog() {
    $session = $this
      ->getSession();
    $web_assert = $this
      ->assertSession();
    $this
      ->drupalGet('node/add/page');
    $session
      ->getPage();
    
    $web_assert
      ->elementExists('css', '#cke_edit-body-0-value .cke_button__drupalimage');
    
    $this
      ->click('.cke_button__drupalimage');
    $this
      ->assertNotEmpty($web_assert
      ->waitForElement('css', '.ui-dialog'));
    $web_assert
      ->elementContains('css', '.ui-dialog .ui-dialog-titlebar', 'Insert Image');
  }
  
  public function testDrupalImageCaptionDialog() {
    $web_assert = $this
      ->assertSession();
    
    $this->filterFormat
      ->setFilterConfig('filter_caption', [
      'status' => FALSE,
    ]);
    $this->filterFormat
      ->save();
    
    $this
      ->drupalGet('node/add/page');
    $this
      ->waitForEditor();
    $this
      ->pressEditorButton('drupalimage');
    $this
      ->assertNotEmpty($web_assert
      ->waitForElement('css', '.ui-dialog'));
    $web_assert
      ->elementNotExists('css', '.ui-dialog input[name="attributes[hasCaption]"]');
    
    $this->filterFormat
      ->setFilterConfig('filter_caption', [
      'status' => TRUE,
    ]);
    $this->filterFormat
      ->save();
    
    $this
      ->drupalGet('node/add/page');
    $this
      ->waitForEditor();
    $this
      ->pressEditorButton('drupalimage');
    $this
      ->assertNotEmpty($web_assert
      ->waitForElement('css', '.ui-dialog'));
    $web_assert
      ->elementExists('css', '.ui-dialog input[name="attributes[hasCaption]"]');
  }
  
  public function testOffCanvasStyles() {
    $assert_session = $this
      ->assertSession();
    $page = $this
      ->getSession()
      ->getPage();
    $this
      ->drupalGet('/ckeditor_test/off_canvas');
    
    $page
      ->clickLink('Add Node');
    $assert_session
      ->waitForElementVisible('css', '#drupal-off-canvas');
    $assert_session
      ->assertWaitOnAjaxRequest();
    
    $assert_session
      ->elementExists('css', '.cke_top');
    $ckeditor_top_bg_color = $this
      ->getSession()
      ->evaluateScript('window.getComputedStyle(document.getElementsByClassName(\'cke_top\')[0]).backgroundColor');
    $this
      ->assertEquals('rgb(248, 248, 248)', $ckeditor_top_bg_color);
    $assert_session
      ->elementExists('css', '.cke_button__source');
    $ckeditor_source_button_bg_color = $this
      ->getSession()
      ->evaluateScript('window.getComputedStyle(document.getElementsByClassName(\'cke_button__source\')[0]).backgroundColor');
    $this
      ->assertEquals('rgba(0, 0, 0, 0)', $ckeditor_source_button_bg_color);
    
    $get_cache_keys = 'Object.keys(window.localStorage).filter(function (i) {return i.indexOf(\'Drupal.off-canvas.css.\') === 0})';
    $old_keys = $this
      ->getSession()
      ->evaluateScript($get_cache_keys);
    
    $this
      ->resetAll();
    
    \Drupal::state()
      ->set('system.css_js_query_string', '0');
    $this
      ->drupalGet('/ckeditor_test/off_canvas');
    $page
      ->clickLink('Add Node');
    $assert_session
      ->waitForElementVisible('css', '#drupal-off-canvas');
    $assert_session
      ->assertWaitOnAjaxRequest();
    $new_keys = $this
      ->getSession()
      ->evaluateScript($get_cache_keys);
    $this
      ->assertCount(1, $old_keys, 'Only one off-canvas style was cached before clearing caches.');
    $this
      ->assertCount(1, $new_keys, 'Only one off-canvas style was cached after clearing caches.');
    $this
      ->assertNotEquals($old_keys, $new_keys, 'Clearing caches changed the off-canvas style cache key.');
  }
}