comparison core/modules/image/tests/src/FunctionalJavascript/QuickEditImageTest.php @ 17:129ea1e6d783

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:21:36 +0000
parents 4c8ae668cc8c
children
comparison
equal deleted inserted replaced
16:c2387f117808 17:129ea1e6d783
1 <?php 1 <?php
2 2
3 namespace Drupal\Tests\image\FunctionalJavascript; 3 namespace Drupal\Tests\image\FunctionalJavascript;
4 4
5 use Drupal\file\Entity\File; 5 use Drupal\file\Entity\File;
6 use Drupal\FunctionalJavascriptTests\JavascriptTestBase;
7 use Drupal\Tests\image\Kernel\ImageFieldCreationTrait; 6 use Drupal\Tests\image\Kernel\ImageFieldCreationTrait;
7 use Drupal\Tests\quickedit\FunctionalJavascript\QuickEditJavascriptTestBase;
8 use Drupal\Tests\TestFileCreationTrait; 8 use Drupal\Tests\TestFileCreationTrait;
9 9
10 /** 10 /**
11 * Tests the JavaScript functionality of the "image" in-place editor. 11 * @coversDefaultClass \Drupal\image\Plugin\InPlaceEditor\Image
12 *
13 * @group image 12 * @group image
14 */ 13 */
15 class QuickEditImageTest extends JavascriptTestBase { 14 class QuickEditImageTest extends QuickEditJavascriptTestBase {
16 15
17 use ImageFieldCreationTrait; 16 use ImageFieldCreationTrait;
18 use TestFileCreationTrait; 17 use TestFileCreationTrait;
18 use QuickEditImageEditorTestTrait;
19 19
20 /** 20 /**
21 * {@inheritdoc} 21 * {@inheritdoc}
22 */ 22 */
23 public static $modules = ['node', 'image', 'field_ui', 'contextual', 'quickedit', 'toolbar']; 23 public static $modules = ['node', 'image', 'field_ui'];
24 24
25 /** 25 /**
26 * A user with permissions to edit Articles and use Quick Edit. 26 * A user with permissions to edit Articles and use Quick Edit.
27 * 27 *
28 * @var \Drupal\user\UserInterface 28 * @var \Drupal\user\UserInterface
50 ]); 50 ]);
51 $this->drupalLogin($this->contentAuthorUser); 51 $this->drupalLogin($this->contentAuthorUser);
52 } 52 }
53 53
54 /** 54 /**
55 * Tests if an image can be uploaded inline with Quick Edit. 55 * Test that quick editor works correctly with images.
56 */ 56 *
57 public function testUpload() { 57 * @covers ::isCompatible
58 * @covers ::getAttachments
59 */
60 public function testImageInPlaceEditor() {
58 // Create a field with a basic filetype restriction. 61 // Create a field with a basic filetype restriction.
59 $field_name = strtolower($this->randomMachineName()); 62 $field_name = strtolower($this->randomMachineName());
60 $field_settings = [ 63 $field_settings = [
61 'file_extensions' => 'png', 64 'file_extensions' => 'png',
62 ]; 65 ];
112 $new_image_selector = 'img[src*="' . $valid_images[1]->filename . '"][alt="New text"]'; 115 $new_image_selector = 'img[src*="' . $valid_images[1]->filename . '"][alt="New text"]';
113 116
114 // Assert that the initial image is present. 117 // Assert that the initial image is present.
115 $this->assertSession()->elementExists('css', $entity_selector . ' ' . $field_selector . ' ' . $original_image_selector); 118 $this->assertSession()->elementExists('css', $entity_selector . ' ' . $field_selector . ' ' . $original_image_selector);
116 119
117 // Wait until Quick Edit loads. 120 // Initial state.
118 $condition = "jQuery('" . $entity_selector . " .quickedit').length > 0"; 121 $this->awaitQuickEditForEntity('node', 1);
119 $this->assertJsCondition($condition, 10000); 122 $this->assertEntityInstanceStates([
120 123 'node/1[0]' => 'closed',
121 // Initiate Quick Editing. 124 ]);
122 $this->click('.contextual-toolbar-tab button'); 125 $this->assertEntityInstanceFieldStates('node', 1, 0, [
123 $this->click($entity_selector . ' [data-contextual-id] > button'); 126 'node/1/title/en/full' => 'inactive',
124 $this->click($entity_selector . ' [data-contextual-id] .quickedit > a'); 127 'node/1/uid/en/full' => 'inactive',
128 'node/1/created/en/full' => 'inactive',
129 'node/1/body/en/full' => 'inactive',
130 'node/1/' . $field_name . '/en/full' => 'inactive',
131 ]);
132
133 // Start in-place editing of the article node.
134 $this->startQuickEditViaToolbar('node', 1, 0);
135 $this->assertEntityInstanceStates([
136 'node/1[0]' => 'opened',
137 ]);
138 $this->assertQuickEditEntityToolbar((string) $node->label(), NULL);
139 $this->assertEntityInstanceFieldStates('node', 1, 0, [
140 'node/1/title/en/full' => 'candidate',
141 'node/1/uid/en/full' => 'candidate',
142 'node/1/created/en/full' => 'candidate',
143 'node/1/body/en/full' => 'candidate',
144 'node/1/' . $field_name . '/en/full' => 'candidate',
145 ]);
146
147 // Click the image field.
125 $this->click($field_selector); 148 $this->click($field_selector);
126 149 $this->awaitImageEditor();
127 // Wait for the field info to load and set new alt text.
128 $condition = "jQuery('.quickedit-image-field-info').length > 0";
129 $this->assertJsCondition($condition, 10000);
130 $input = $this->assertSession()->elementExists('css', '.quickedit-image-field-info input[name="alt"]');
131 $input->setValue('New text');
132
133 // Check that our Dropzone element exists.
134 $this->assertSession()->elementExists('css', $field_selector . ' .quickedit-image-dropzone'); 150 $this->assertSession()->elementExists('css', $field_selector . ' .quickedit-image-dropzone');
135 151 $this->assertEntityInstanceFieldStates('node', 1, 0, [
136 // Our headless browser can't drag+drop files, but we can mock the event. 152 'node/1/title/en/full' => 'candidate',
137 // Append a hidden upload element to the DOM. 153 'node/1/uid/en/full' => 'candidate',
138 $script = 'jQuery("<input id=\"quickedit-image-test-input\" type=\"file\" />").appendTo("body")'; 154 'node/1/created/en/full' => 'candidate',
139 $this->getSession()->executeScript($script); 155 'node/1/body/en/full' => 'candidate',
140 156 'node/1/' . $field_name . '/en/full' => 'active',
141 // Find the element, and set its value to our new image. 157 ]);
142 $input = $this->assertSession()->elementExists('css', '#quickedit-image-test-input'); 158
143 $filepath = $this->container->get('file_system')->realpath($valid_images[1]->uri); 159 // Type new 'alt' text.
144 $input->attachFile($filepath); 160 $this->typeInImageEditorAltTextInput('New text');
145 161 $this->assertEntityInstanceFieldStates('node', 1, 0, [
146 // Trigger the upload logic with a mock "drop" event. 162 'node/1/title/en/full' => 'candidate',
147 $script = 'var e = jQuery.Event("drop");' 163 'node/1/uid/en/full' => 'candidate',
148 . 'e.originalEvent = {dataTransfer: {files: jQuery("#quickedit-image-test-input").get(0).files}};' 164 'node/1/created/en/full' => 'candidate',
149 . 'e.preventDefault = e.stopPropagation = function () {};' 165 'node/1/body/en/full' => 'candidate',
150 . 'jQuery(".quickedit-image-dropzone").trigger(e);'; 166 'node/1/' . $field_name . '/en/full' => 'changed',
151 $this->getSession()->executeScript($script); 167 ]);
152 168
153 // Wait for the dropzone element to be removed (i.e. loading is done). 169 // Drag and drop an image.
154 $condition = "jQuery('" . $field_selector . " .quickedit-image-dropzone').length == 0"; 170 $this->dropImageOnImageEditor($valid_images[1]->uri);
155 $this->assertJsCondition($condition, 20000);
156 171
157 // To prevent 403s on save, we re-set our request (cookie) state. 172 // To prevent 403s on save, we re-set our request (cookie) state.
158 $this->prepareRequest(); 173 $this->prepareRequest();
159 174
160 // Save the change. 175 // Click 'Save'.
161 $this->click('.quickedit-button.action-save'); 176 $this->saveQuickEdit();
162 $this->assertSession()->assertWaitOnAjaxRequest(); 177 $this->assertEntityInstanceStates([
178 'node/1[0]' => 'committing',
179 ]);
180 $this->assertEntityInstanceFieldStates('node', 1, 0, [
181 'node/1/title/en/full' => 'candidate',
182 'node/1/uid/en/full' => 'candidate',
183 'node/1/created/en/full' => 'candidate',
184 'node/1/body/en/full' => 'candidate',
185 'node/1/' . $field_name . '/en/full' => 'saving',
186 ]);
187 $this->assertEntityInstanceFieldMarkup('node', 1, 0, [
188 'node/1/' . $field_name . '/en/full' => '.quickedit-changed',
189 ]);
190
191 // Wait for the saving of the image field to complete.
192 $this->assertJsCondition("Drupal.quickedit.collections.entities.get('node/1[0]').get('state') === 'closed'");
193 $this->assertEntityInstanceStates([
194 'node/1[0]' => 'closed',
195 ]);
163 196
164 // Re-visit the page to make sure the edit worked. 197 // Re-visit the page to make sure the edit worked.
165 $this->drupalGet('node/' . $node->id()); 198 $this->drupalGet('node/' . $node->id());
166 199
167 // Check that the new image appears as expected. 200 // Check that the new image appears as expected.