9e7b8df53a6ee2d22d166da6ff8175f62a7e922f
[yaffs-website] / web / core / modules / ckeditor / tests / src / FunctionalJavascript / CKEditorIntegrationTest.php
1 <?php
2
3 namespace Drupal\Tests\ckeditor\FunctionalJavascript;
4
5 use Drupal\Core\Entity\Entity\EntityFormDisplay;
6 use Drupal\editor\Entity\Editor;
7 use Drupal\field\Entity\FieldConfig;
8 use Drupal\field\Entity\FieldStorageConfig;
9 use Drupal\filter\Entity\FilterFormat;
10 use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
11 use Drupal\node\Entity\NodeType;
12
13 /**
14  * Tests the integration of CKEditor.
15  *
16  * @group ckeditor
17  */
18 class CKEditorIntegrationTest extends WebDriverTestBase {
19
20   /**
21    * The account.
22    *
23    * @var \Drupal\user\UserInterface
24    */
25   protected $account;
26
27   /**
28    * The FilterFormat config entity used for testing.
29    *
30    * @var \Drupal\filter\FilterFormatInterface
31    */
32   protected $filterFormat;
33
34   /**
35    * {@inheritdoc}
36    */
37   public static $modules = ['node', 'ckeditor', 'filter'];
38
39   /**
40    * {@inheritdoc}
41    */
42   protected function setUp() {
43     parent::setUp();
44
45     // Create a text format and associate CKEditor.
46     $this->filterFormat = FilterFormat::create([
47       'format' => 'filtered_html',
48       'name' => 'Filtered HTML',
49       'weight' => 0,
50     ]);
51     $this->filterFormat->save();
52
53     Editor::create([
54       'format' => 'filtered_html',
55       'editor' => 'ckeditor',
56     ])->save();
57
58     // Create a node type for testing.
59     NodeType::create(['type' => 'page', 'name' => 'page'])->save();
60
61     $field_storage = FieldStorageConfig::loadByName('node', 'body');
62
63     // Create a body field instance for the 'page' node type.
64     FieldConfig::create([
65       'field_storage' => $field_storage,
66       'bundle' => 'page',
67       'label' => 'Body',
68       'settings' => ['display_summary' => TRUE],
69       'required' => TRUE,
70     ])->save();
71
72     // Assign widget settings for the 'default' form mode.
73     EntityFormDisplay::create([
74       'targetEntityType' => 'node',
75       'bundle' => 'page',
76       'mode' => 'default',
77       'status' => TRUE,
78     ])->setComponent('body', ['type' => 'text_textarea_with_summary'])
79       ->save();
80
81     $this->account = $this->drupalCreateUser([
82       'administer nodes',
83       'create page content',
84       'use text format filtered_html',
85     ]);
86     $this->drupalLogin($this->account);
87   }
88
89   /**
90    * Tests if the fragment link to a textarea works with CKEditor enabled.
91    */
92   public function testFragmentLink() {
93     $session = $this->getSession();
94     $web_assert = $this->assertSession();
95     $ckeditor_id = '#cke_edit-body-0-value';
96
97     $this->drupalGet('node/add/page');
98
99     $session->getPage();
100
101     // Add a bottom margin to the title field to be sure the body field is not
102     // visible.
103     $session->executeScript("document.getElementById('edit-title-0-value').style.marginBottom = window.innerHeight*2 +'px';");
104
105     $this->assertSession()->waitForElementVisible('css', $ckeditor_id);
106     // Check that the CKEditor-enabled body field is currently not visible in
107     // the viewport.
108     $web_assert->assertNotVisibleInViewport('css', $ckeditor_id, 'topLeft', 'CKEditor-enabled body field is visible.');
109
110     $before_url = $session->getCurrentUrl();
111
112     // Trigger a hash change with as target the hidden textarea.
113     $session->executeScript("location.hash = '#edit-body-0-value';");
114
115     // Check that the CKEditor-enabled body field is visible in the viewport.
116     $web_assert->assertVisibleInViewport('css', $ckeditor_id, 'topLeft', 'CKEditor-enabled body field is not visible.');
117
118     // Use JavaScript to go back in the history instead of
119     // \Behat\Mink\Session::back() because that function doesn't work after a
120     // hash change.
121     $session->executeScript("history.back();");
122
123     $after_url = $session->getCurrentUrl();
124
125     // Check that going back in the history worked.
126     self::assertEquals($before_url, $after_url, 'History back works.');
127   }
128
129   /**
130    * Tests if the Image button appears and works as expected.
131    */
132   public function testDrupalImageDialog() {
133     $session = $this->getSession();
134     $web_assert = $this->assertSession();
135
136     $this->drupalGet('node/add/page');
137     $session->getPage();
138
139     // Asserts the Image button is present in the toolbar.
140     $web_assert->elementExists('css', '#cke_edit-body-0-value .cke_button__drupalimage');
141
142     // Asserts the image dialog opens when clicking the Image button.
143     $this->click('.cke_button__drupalimage');
144     $this->assertNotEmpty($web_assert->waitForElement('css', '.ui-dialog'));
145
146     $web_assert->elementContains('css', '.ui-dialog .ui-dialog-titlebar', 'Insert Image');
147   }
148
149   /**
150    * Tests if the Drupal Image Caption plugin appears and works as expected.
151    */
152   public function testDrupalImageCaptionDialog() {
153     $web_assert = $this->assertSession();
154
155     // Disable the caption filter.
156     $this->filterFormat->setFilterConfig('filter_caption', [
157       'status' => FALSE,
158     ]);
159     $this->filterFormat->save();
160
161     // If the caption filter is disabled, its checkbox should be absent.
162     $this->drupalGet('node/add/page');
163     $this->click('.cke_button__drupalimage');
164     $this->assertNotEmpty($web_assert->waitForElement('css', '.ui-dialog'));
165     $web_assert->elementNotExists('css', '.ui-dialog input[name="attributes[hasCaption]"]');
166
167     // Enable the caption filter again.
168     $this->filterFormat->setFilterConfig('filter_caption', [
169       'status' => TRUE,
170     ]);
171     $this->filterFormat->save();
172
173     // If the caption filter is enabled,  its checkbox should be present.
174     $this->drupalGet('node/add/page');
175     $this->click('.cke_button__drupalimage');
176     $this->assertNotEmpty($web_assert->waitForElement('css', '.ui-dialog'));
177     $web_assert->elementExists('css', '.ui-dialog input[name="attributes[hasCaption]"]');
178   }
179
180 }