Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\image\Tests;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Tests\image\Kernel\ImageFieldCreationTrait;
|
Chris@0
|
6 use Drupal\simpletest\WebTestBase;
|
Chris@0
|
7
|
Chris@0
|
8 /**
|
Chris@0
|
9 * Tests the endpoints used by the "image" in-place editor.
|
Chris@0
|
10 *
|
Chris@0
|
11 * @group image
|
Chris@0
|
12 */
|
Chris@0
|
13 class QuickEditImageControllerTest extends WebTestBase {
|
Chris@0
|
14
|
Chris@0
|
15 use ImageFieldCreationTrait;
|
Chris@0
|
16
|
Chris@0
|
17 /**
|
Chris@0
|
18 * {@inheritdoc}
|
Chris@0
|
19 */
|
Chris@0
|
20 public static $modules = ['node', 'image', 'quickedit'];
|
Chris@0
|
21
|
Chris@0
|
22 /**
|
Chris@0
|
23 * The machine name of our image field.
|
Chris@0
|
24 *
|
Chris@0
|
25 * @var string
|
Chris@0
|
26 */
|
Chris@0
|
27 protected $fieldName;
|
Chris@0
|
28
|
Chris@0
|
29 /**
|
Chris@0
|
30 * A user with permissions to edit articles and use Quick Edit.
|
Chris@0
|
31 *
|
Chris@0
|
32 * @var \Drupal\user\UserInterface
|
Chris@0
|
33 */
|
Chris@0
|
34 protected $contentAuthorUser;
|
Chris@0
|
35
|
Chris@0
|
36 /**
|
Chris@0
|
37 * {@inheritdoc}
|
Chris@0
|
38 */
|
Chris@0
|
39 protected function setUp() {
|
Chris@0
|
40 parent::setUp();
|
Chris@0
|
41
|
Chris@0
|
42 // Create the Article node type.
|
Chris@0
|
43 $this->drupalCreateContentType(['type' => 'article', 'name' => 'Article']);
|
Chris@0
|
44
|
Chris@0
|
45 // Log in as a content author who can use Quick Edit and edit Articles.
|
Chris@0
|
46 $this->contentAuthorUser = $this->drupalCreateUser([
|
Chris@0
|
47 'access contextual links',
|
Chris@0
|
48 'access in-place editing',
|
Chris@0
|
49 'access content',
|
Chris@0
|
50 'create article content',
|
Chris@0
|
51 'edit any article content',
|
Chris@0
|
52 'delete any article content',
|
Chris@0
|
53 ]);
|
Chris@0
|
54 $this->drupalLogin($this->contentAuthorUser);
|
Chris@0
|
55
|
Chris@0
|
56 // Create a field with basic resolution validators.
|
Chris@0
|
57 $this->fieldName = strtolower($this->randomMachineName());
|
Chris@0
|
58 $field_settings = [
|
Chris@0
|
59 'max_resolution' => '100x',
|
Chris@0
|
60 'min_resolution' => '50x',
|
Chris@0
|
61 ];
|
Chris@0
|
62 $this->createImageField($this->fieldName, 'article', [], $field_settings);
|
Chris@0
|
63 }
|
Chris@0
|
64
|
Chris@0
|
65 /**
|
Chris@0
|
66 * Tests that routes restrict access for un-privileged users.
|
Chris@0
|
67 */
|
Chris@0
|
68 public function testAccess() {
|
Chris@0
|
69 // Create an anonymous user.
|
Chris@0
|
70 $user = $this->createUser();
|
Chris@0
|
71 $this->drupalLogin($user);
|
Chris@0
|
72
|
Chris@0
|
73 // Create a test Node.
|
Chris@0
|
74 $node = $this->drupalCreateNode([
|
Chris@0
|
75 'type' => 'article',
|
Chris@0
|
76 'title' => t('Test Node'),
|
Chris@0
|
77 ]);
|
Chris@0
|
78 $this->drupalGet('quickedit/image/info/node/' . $node->id() . '/' . $this->fieldName . '/' . $node->language()->getId() . '/default');
|
Chris@0
|
79 $this->assertResponse('403');
|
Chris@0
|
80 $this->drupalPost('quickedit/image/upload/node/' . $node->id() . '/' . $this->fieldName . '/' . $node->language()->getId() . '/default', 'application/json', []);
|
Chris@0
|
81 $this->assertResponse('403');
|
Chris@0
|
82 }
|
Chris@0
|
83
|
Chris@0
|
84 /**
|
Chris@0
|
85 * Tests that the field info route returns expected data.
|
Chris@0
|
86 */
|
Chris@0
|
87 public function testFieldInfo() {
|
Chris@0
|
88 // Create a test Node.
|
Chris@0
|
89 $node = $this->drupalCreateNode([
|
Chris@0
|
90 'type' => 'article',
|
Chris@0
|
91 'title' => t('Test Node'),
|
Chris@0
|
92 ]);
|
Chris@0
|
93 $info = $this->drupalGetJSON('quickedit/image/info/node/' . $node->id() . '/' . $this->fieldName . '/' . $node->language()->getId() . '/default');
|
Chris@0
|
94 // Assert that the default settings for our field are respected by our JSON
|
Chris@0
|
95 // endpoint.
|
Chris@0
|
96 $this->assertTrue($info['alt_field']);
|
Chris@0
|
97 $this->assertFalse($info['title_field']);
|
Chris@0
|
98 }
|
Chris@0
|
99
|
Chris@0
|
100 /**
|
Chris@0
|
101 * Tests that uploading a valid image works.
|
Chris@0
|
102 */
|
Chris@0
|
103 public function testValidImageUpload() {
|
Chris@0
|
104 // Create a test Node.
|
Chris@0
|
105 $node = $this->drupalCreateNode([
|
Chris@0
|
106 'type' => 'article',
|
Chris@0
|
107 'title' => t('Test Node'),
|
Chris@0
|
108 ]);
|
Chris@0
|
109
|
Chris@0
|
110 // We want a test image that is a valid size.
|
Chris@0
|
111 $valid_image = FALSE;
|
Chris@0
|
112 $image_factory = $this->container->get('image.factory');
|
Chris@0
|
113 foreach ($this->drupalGetTestFiles('image') as $image) {
|
Chris@0
|
114 $image_file = $image_factory->get($image->uri);
|
Chris@0
|
115 if ($image_file->getWidth() > 50 && $image_file->getWidth() < 100) {
|
Chris@0
|
116 $valid_image = $image;
|
Chris@0
|
117 break;
|
Chris@0
|
118 }
|
Chris@0
|
119 }
|
Chris@0
|
120 $this->assertTrue($valid_image);
|
Chris@0
|
121 $this->uploadImage($valid_image, $node->id(), $this->fieldName, $node->language()->getId());
|
Chris@0
|
122 $this->assertText('fid', t('Valid upload completed successfully.'));
|
Chris@0
|
123 }
|
Chris@0
|
124
|
Chris@0
|
125 /**
|
Chris@0
|
126 * Tests that uploading a invalid image does not work.
|
Chris@0
|
127 */
|
Chris@0
|
128 public function testInvalidUpload() {
|
Chris@0
|
129 // Create a test Node.
|
Chris@0
|
130 $node = $this->drupalCreateNode([
|
Chris@0
|
131 'type' => 'article',
|
Chris@0
|
132 'title' => t('Test Node'),
|
Chris@0
|
133 ]);
|
Chris@0
|
134
|
Chris@0
|
135 // We want a test image that will fail validation.
|
Chris@0
|
136 $invalid_image = FALSE;
|
Chris@0
|
137 /** @var \Drupal\Core\Image\ImageFactory $image_factory */
|
Chris@0
|
138 $image_factory = $this->container->get('image.factory');
|
Chris@0
|
139 foreach ($this->drupalGetTestFiles('image') as $image) {
|
Chris@0
|
140 /** @var \Drupal\Core\Image\ImageInterface $image_file */
|
Chris@0
|
141 $image_file = $image_factory->get($image->uri);
|
Chris@0
|
142 if ($image_file->getWidth() < 50 || $image_file->getWidth() > 100) {
|
Chris@0
|
143 $invalid_image = $image;
|
Chris@0
|
144 break;
|
Chris@0
|
145 }
|
Chris@0
|
146 }
|
Chris@0
|
147 $this->assertTrue($invalid_image);
|
Chris@0
|
148 $this->uploadImage($invalid_image, $node->id(), $this->fieldName, $node->language()->getId());
|
Chris@0
|
149 $this->assertText('main_error', t('Invalid upload returned errors.'));
|
Chris@0
|
150 }
|
Chris@0
|
151
|
Chris@0
|
152 /**
|
Chris@0
|
153 * Uploads an image using the image module's Quick Edit route.
|
Chris@0
|
154 *
|
Chris@0
|
155 * @param object $image
|
Chris@0
|
156 * The image to upload.
|
Chris@0
|
157 * @param int $nid
|
Chris@0
|
158 * The target node ID.
|
Chris@0
|
159 * @param string $field_name
|
Chris@0
|
160 * The target field machine name.
|
Chris@0
|
161 * @param string $langcode
|
Chris@0
|
162 * The langcode to use when setting the field's value.
|
Chris@0
|
163 *
|
Chris@0
|
164 * @return mixed
|
Chris@0
|
165 * The content returned from the call to $this->curlExec().
|
Chris@0
|
166 */
|
Chris@0
|
167 public function uploadImage($image, $nid, $field_name, $langcode) {
|
Chris@0
|
168 $filepath = $this->container->get('file_system')->realpath($image->uri);
|
Chris@0
|
169 $data = [
|
Chris@0
|
170 'files[image]' => curl_file_create($filepath),
|
Chris@0
|
171 ];
|
Chris@0
|
172 $path = 'quickedit/image/upload/node/' . $nid . '/' . $field_name . '/' . $langcode . '/default';
|
Chris@0
|
173 // We assemble the curl request ourselves as drupalPost cannot process file
|
Chris@0
|
174 // uploads, and drupalPostForm only works with typical Drupal forms.
|
Chris@0
|
175 return $this->curlExec([
|
Chris@0
|
176 CURLOPT_URL => $this->buildUrl($path, []),
|
Chris@0
|
177 CURLOPT_POST => TRUE,
|
Chris@0
|
178 CURLOPT_POSTFIELDS => $data,
|
Chris@0
|
179 CURLOPT_HTTPHEADER => [
|
Chris@0
|
180 'Accept: application/json',
|
Chris@0
|
181 'Content-Type: multipart/form-data',
|
Chris@0
|
182 ],
|
Chris@0
|
183 ]);
|
Chris@0
|
184 }
|
Chris@0
|
185
|
Chris@0
|
186 }
|