Chris@0: drupalCreateContentType(['type' => 'article', 'name' => 'Article']); Chris@0: Chris@0: // Log in as a content author who can use Quick Edit and edit Articles. Chris@0: $this->contentAuthorUser = $this->drupalCreateUser([ Chris@0: 'access contextual links', Chris@0: 'access in-place editing', Chris@0: 'access content', Chris@0: 'create article content', Chris@0: 'edit any article content', Chris@0: 'delete any article content', Chris@0: ]); Chris@0: $this->drupalLogin($this->contentAuthorUser); Chris@0: Chris@0: // Create a field with basic resolution validators. Chris@0: $this->fieldName = strtolower($this->randomMachineName()); Chris@0: $field_settings = [ Chris@0: 'max_resolution' => '100x', Chris@0: 'min_resolution' => '50x', Chris@0: ]; Chris@0: $this->createImageField($this->fieldName, 'article', [], $field_settings); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests that routes restrict access for un-privileged users. Chris@0: */ Chris@0: public function testAccess() { Chris@0: // Create an anonymous user. Chris@0: $user = $this->createUser(); Chris@0: $this->drupalLogin($user); Chris@0: Chris@0: // Create a test Node. Chris@0: $node = $this->drupalCreateNode([ Chris@0: 'type' => 'article', Chris@0: 'title' => t('Test Node'), Chris@0: ]); Chris@0: $this->drupalGet('quickedit/image/info/node/' . $node->id() . '/' . $this->fieldName . '/' . $node->language()->getId() . '/default'); Chris@0: $this->assertResponse('403'); Chris@0: $this->drupalPost('quickedit/image/upload/node/' . $node->id() . '/' . $this->fieldName . '/' . $node->language()->getId() . '/default', 'application/json', []); Chris@0: $this->assertResponse('403'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests that the field info route returns expected data. Chris@0: */ Chris@0: public function testFieldInfo() { Chris@0: // Create a test Node. Chris@0: $node = $this->drupalCreateNode([ Chris@0: 'type' => 'article', Chris@0: 'title' => t('Test Node'), Chris@0: ]); Chris@0: $info = $this->drupalGetJSON('quickedit/image/info/node/' . $node->id() . '/' . $this->fieldName . '/' . $node->language()->getId() . '/default'); Chris@0: // Assert that the default settings for our field are respected by our JSON Chris@0: // endpoint. Chris@0: $this->assertTrue($info['alt_field']); Chris@0: $this->assertFalse($info['title_field']); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests that uploading a valid image works. Chris@0: */ Chris@0: public function testValidImageUpload() { Chris@0: // Create a test Node. Chris@0: $node = $this->drupalCreateNode([ Chris@0: 'type' => 'article', Chris@0: 'title' => t('Test Node'), Chris@0: ]); Chris@0: Chris@0: // We want a test image that is a valid size. Chris@0: $valid_image = FALSE; Chris@0: $image_factory = $this->container->get('image.factory'); Chris@0: foreach ($this->drupalGetTestFiles('image') as $image) { Chris@0: $image_file = $image_factory->get($image->uri); Chris@0: if ($image_file->getWidth() > 50 && $image_file->getWidth() < 100) { Chris@0: $valid_image = $image; Chris@0: break; Chris@0: } Chris@0: } Chris@0: $this->assertTrue($valid_image); Chris@0: $this->uploadImage($valid_image, $node->id(), $this->fieldName, $node->language()->getId()); Chris@0: $this->assertText('fid', t('Valid upload completed successfully.')); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests that uploading a invalid image does not work. Chris@0: */ Chris@0: public function testInvalidUpload() { Chris@0: // Create a test Node. Chris@0: $node = $this->drupalCreateNode([ Chris@0: 'type' => 'article', Chris@0: 'title' => t('Test Node'), Chris@0: ]); Chris@0: Chris@0: // We want a test image that will fail validation. Chris@0: $invalid_image = FALSE; Chris@0: /** @var \Drupal\Core\Image\ImageFactory $image_factory */ Chris@0: $image_factory = $this->container->get('image.factory'); Chris@0: foreach ($this->drupalGetTestFiles('image') as $image) { Chris@0: /** @var \Drupal\Core\Image\ImageInterface $image_file */ Chris@0: $image_file = $image_factory->get($image->uri); Chris@0: if ($image_file->getWidth() < 50 || $image_file->getWidth() > 100) { Chris@0: $invalid_image = $image; Chris@0: break; Chris@0: } Chris@0: } Chris@0: $this->assertTrue($invalid_image); Chris@0: $this->uploadImage($invalid_image, $node->id(), $this->fieldName, $node->language()->getId()); Chris@0: $this->assertText('main_error', t('Invalid upload returned errors.')); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Uploads an image using the image module's Quick Edit route. Chris@0: * Chris@0: * @param object $image Chris@0: * The image to upload. Chris@0: * @param int $nid Chris@0: * The target node ID. Chris@0: * @param string $field_name Chris@0: * The target field machine name. Chris@0: * @param string $langcode Chris@0: * The langcode to use when setting the field's value. Chris@0: * Chris@0: * @return mixed Chris@0: * The content returned from the call to $this->curlExec(). Chris@0: */ Chris@0: public function uploadImage($image, $nid, $field_name, $langcode) { Chris@0: $filepath = $this->container->get('file_system')->realpath($image->uri); Chris@0: $data = [ Chris@0: 'files[image]' => curl_file_create($filepath), Chris@0: ]; Chris@0: $path = 'quickedit/image/upload/node/' . $nid . '/' . $field_name . '/' . $langcode . '/default'; Chris@0: // We assemble the curl request ourselves as drupalPost cannot process file Chris@0: // uploads, and drupalPostForm only works with typical Drupal forms. Chris@0: return $this->curlExec([ Chris@0: CURLOPT_URL => $this->buildUrl($path, []), Chris@0: CURLOPT_POST => TRUE, Chris@0: CURLOPT_POSTFIELDS => $data, Chris@0: CURLOPT_HTTPHEADER => [ Chris@0: 'Accept: application/json', Chris@0: 'Content-Type: multipart/form-data', Chris@0: ], Chris@0: ]); Chris@0: } Chris@0: Chris@0: }