Version 1
[yaffs-website] / web / core / modules / inline_form_errors / tests / src / FunctionalJavascript / FormErrorHandlerCKEditorTest.php
1 <?php
2
3 namespace Drupal\Tests\inline_form_errors\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\JavascriptTestBase;
11 use Drupal\node\Entity\NodeType;
12
13 /**
14  * Tests the inline errors fragment link to a CKEditor-enabled textarea.
15  *
16  * @group ckeditor
17  */
18 class FormErrorHandlerCKEditorTest extends JavascriptTestBase {
19
20   /**
21    * {@inheritdoc}
22    */
23   public static $modules = ['node', 'ckeditor', 'inline_form_errors', 'filter'];
24
25   /**
26    * {@inheritdoc}
27    */
28   protected function setUp() {
29     parent::setUp();
30
31     // Create a text format and associate CKEditor.
32     $filtered_html_format = FilterFormat::create([
33       'format' => 'filtered_html',
34       'name' => 'Filtered HTML',
35       'weight' => 0,
36     ]);
37     $filtered_html_format->save();
38
39     Editor::create([
40       'format' => 'filtered_html',
41       'editor' => 'ckeditor',
42     ])->save();
43
44     // Create a node type for testing.
45     NodeType::create(['type' => 'page', 'name' => 'page'])->save();
46
47     $field_storage = FieldStorageConfig::loadByName('node', 'body');
48
49     // Create a body field instance for the 'page' node type.
50     FieldConfig::create([
51       'field_storage' => $field_storage,
52       'bundle' => 'page',
53       'label' => 'Body',
54       'settings' => ['display_summary' => TRUE],
55       'required' => TRUE,
56     ])->save();
57
58     // Assign widget settings for the 'default' form mode.
59     EntityFormDisplay::create([
60       'targetEntityType' => 'node',
61       'bundle' => 'page',
62       'mode' => 'default',
63       'status' => TRUE,
64     ])->setComponent('body', ['type' => 'text_textarea_with_summary'])
65       ->save();
66
67     $account = $this->drupalCreateUser([
68       'administer nodes',
69       'create page content',
70       'use text format filtered_html',
71     ]);
72     $this->drupalLogin($account);
73   }
74
75   /**
76    * Tests if the fragment link to a textarea works with CKEditor enabled.
77    */
78   public function testFragmentLink() {
79     $session = $this->getSession();
80     $web_assert = $this->assertSession();
81     $ckeditor_id = '#cke_edit-body-0-value';
82
83     $this->drupalGet('node/add/page');
84
85     // Only enter a title in the node add form and leave the body field empty.
86     $edit = ['edit-title-0-value' => 'Test inline form error with CKEditor'];
87
88     $this->submitForm($edit, 'Save and publish');
89
90     // Add a bottom margin to the title field to be sure the body field is not
91     // visible. PhantomJS runs with a resolution of 1024x768px.
92     $session->executeScript("document.getElementById('edit-title-0-value').style.marginBottom = '800px';");
93
94     // Check that the CKEditor-enabled body field is currently not visible in
95     // the viewport.
96     $web_assert->assertNotVisibleInViewport('css', $ckeditor_id, 'topLeft', 'CKEditor-enabled body field is not visible.');
97
98     // Check if we can find the error fragment link within the errors summary
99     // message.
100     $errors_link = $this->assertSession()->waitForElementVisible('css', '.messages--error a[href="#edit-body-0-value"]');
101     $this->assertNotEmpty($errors_link, 'Error fragment link is visible.');
102
103     $errors_link->click();
104
105     // Check that the CKEditor-enabled body field is visible in the viewport.
106     $web_assert->assertVisibleInViewport('css', $ckeditor_id, 'topLeft', 'CKEditor-enabled body field is visible.');
107   }
108
109 }