3 namespace Drupal\image\Tests;
5 use Drupal\Tests\image\Kernel\ImageFieldCreationTrait;
6 use Drupal\simpletest\WebTestBase;
9 * Tests the endpoints used by the "image" in-place editor.
13 class QuickEditImageControllerTest extends WebTestBase {
15 use ImageFieldCreationTrait;
20 public static $modules = ['node', 'image', 'quickedit'];
23 * The machine name of our image field.
30 * A user with permissions to edit articles and use Quick Edit.
32 * @var \Drupal\user\UserInterface
34 protected $contentAuthorUser;
39 protected function setUp() {
42 // Create the Article node type.
43 $this->drupalCreateContentType(['type' => 'article', 'name' => 'Article']);
45 // Log in as a content author who can use Quick Edit and edit Articles.
46 $this->contentAuthorUser = $this->drupalCreateUser([
47 'access contextual links',
48 'access in-place editing',
50 'create article content',
51 'edit any article content',
52 'delete any article content',
54 $this->drupalLogin($this->contentAuthorUser);
56 // Create a field with basic resolution validators.
57 $this->fieldName = strtolower($this->randomMachineName());
59 'max_resolution' => '100x',
60 'min_resolution' => '50x',
62 $this->createImageField($this->fieldName, 'article', [], $field_settings);
66 * Tests that routes restrict access for un-privileged users.
68 public function testAccess() {
69 // Create an anonymous user.
70 $user = $this->createUser();
71 $this->drupalLogin($user);
73 // Create a test Node.
74 $node = $this->drupalCreateNode([
76 'title' => t('Test Node'),
78 $this->drupalGet('quickedit/image/info/node/' . $node->id() . '/' . $this->fieldName . '/' . $node->language()->getId() . '/default');
79 $this->assertResponse('403');
80 $this->drupalPost('quickedit/image/upload/node/' . $node->id() . '/' . $this->fieldName . '/' . $node->language()->getId() . '/default', 'application/json', []);
81 $this->assertResponse('403');
85 * Tests that the field info route returns expected data.
87 public function testFieldInfo() {
88 // Create a test Node.
89 $node = $this->drupalCreateNode([
91 'title' => t('Test Node'),
93 $info = $this->drupalGetJSON('quickedit/image/info/node/' . $node->id() . '/' . $this->fieldName . '/' . $node->language()->getId() . '/default');
94 // Assert that the default settings for our field are respected by our JSON
96 $this->assertTrue($info['alt_field']);
97 $this->assertFalse($info['title_field']);
101 * Tests that uploading a valid image works.
103 public function testValidImageUpload() {
104 // Create a test Node.
105 $node = $this->drupalCreateNode([
107 'title' => t('Test Node'),
110 // We want a test image that is a valid size.
111 $valid_image = FALSE;
112 $image_factory = $this->container->get('image.factory');
113 foreach ($this->drupalGetTestFiles('image') as $image) {
114 $image_file = $image_factory->get($image->uri);
115 if ($image_file->getWidth() > 50 && $image_file->getWidth() < 100) {
116 $valid_image = $image;
120 $this->assertTrue($valid_image);
121 $this->uploadImage($valid_image, $node->id(), $this->fieldName, $node->language()->getId());
122 $this->assertText('fid', t('Valid upload completed successfully.'));
126 * Tests that uploading a invalid image does not work.
128 public function testInvalidUpload() {
129 // Create a test Node.
130 $node = $this->drupalCreateNode([
132 'title' => t('Test Node'),
135 // We want a test image that will fail validation.
136 $invalid_image = FALSE;
137 /** @var \Drupal\Core\Image\ImageFactory $image_factory */
138 $image_factory = $this->container->get('image.factory');
139 foreach ($this->drupalGetTestFiles('image') as $image) {
140 /** @var \Drupal\Core\Image\ImageInterface $image_file */
141 $image_file = $image_factory->get($image->uri);
142 if ($image_file->getWidth() < 50 || $image_file->getWidth() > 100) {
143 $invalid_image = $image;
147 $this->assertTrue($invalid_image);
148 $this->uploadImage($invalid_image, $node->id(), $this->fieldName, $node->language()->getId());
149 $this->assertText('main_error', t('Invalid upload returned errors.'));
153 * Uploads an image using the image module's Quick Edit route.
155 * @param object $image
156 * The image to upload.
158 * The target node ID.
159 * @param string $field_name
160 * The target field machine name.
161 * @param string $langcode
162 * The langcode to use when setting the field's value.
165 * The content returned from the call to $this->curlExec().
167 public function uploadImage($image, $nid, $field_name, $langcode) {
168 $filepath = $this->container->get('file_system')->realpath($image->uri);
170 'files[image]' => curl_file_create($filepath),
172 $path = 'quickedit/image/upload/node/' . $nid . '/' . $field_name . '/' . $langcode . '/default';
173 // We assemble the curl request ourselves as drupalPost cannot process file
174 // uploads, and drupalPostForm only works with typical Drupal forms.
175 return $this->curlExec([
176 CURLOPT_URL => $this->buildUrl($path, []),
177 CURLOPT_POST => TRUE,
178 CURLOPT_POSTFIELDS => $data,
179 CURLOPT_HTTPHEADER => [
180 'Accept: application/json',
181 'Content-Type: multipart/form-data',