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