Chris@16: drupalCreateContentType(['type' => 'article', 'name' => 'Article']); Chris@16: Chris@16: // Log in as a content author who can use Quick Edit and edit Articles. Chris@16: $this->contentAuthorUser = $this->drupalCreateUser([ Chris@16: 'access contextual links', Chris@16: 'access in-place editing', Chris@16: 'access content', Chris@16: 'create article content', Chris@16: 'edit any article content', Chris@16: 'delete any article content', Chris@16: ]); Chris@16: $this->drupalLogin($this->contentAuthorUser); Chris@16: Chris@16: // Create a field with basic resolution validators. Chris@16: $this->fieldName = strtolower($this->randomMachineName()); Chris@16: $field_settings = [ Chris@16: 'max_resolution' => '100x', Chris@16: 'min_resolution' => '50x', Chris@16: ]; Chris@16: $this->createImageField($this->fieldName, 'article', [], $field_settings); Chris@16: } Chris@16: Chris@16: /** Chris@16: * Tests that routes restrict access for un-privileged users. Chris@16: */ Chris@16: public function testAccess() { Chris@16: // Create an anonymous user. Chris@16: $user = $this->createUser(); Chris@16: $this->drupalLogin($user); Chris@16: Chris@16: // Create a test Node. Chris@16: $node = $this->drupalCreateNode([ Chris@16: 'type' => 'article', Chris@16: 'title' => t('Test Node'), Chris@16: ]); Chris@16: $this->drupalGet('quickedit/image/info/node/' . $node->id() . '/' . $this->fieldName . '/' . $node->language()->getId() . '/default'); Chris@16: $this->assertResponse('403'); Chris@16: Chris@16: /** @var \Symfony\Component\BrowserKit\Client $client */ Chris@16: $client = $this->getSession()->getDriver()->getClient(); Chris@16: $client->request('POST', '/quickedit/image/upload/node/' . $node->id() . '/' . $this->fieldName . '/' . $node->language()->getId() . '/default'); Chris@16: $this->assertEquals('403', $client->getResponse()->getStatus()); Chris@16: } Chris@16: Chris@16: /** Chris@16: * Tests that the field info route returns expected data. Chris@16: */ Chris@16: public function testFieldInfo() { Chris@16: // Create a test Node. Chris@16: $node = $this->drupalCreateNode([ Chris@16: 'type' => 'article', Chris@16: 'title' => t('Test Node'), Chris@16: ]); Chris@16: $json = $this->drupalGet('quickedit/image/info/node/' . $node->id() . '/' . $this->fieldName . '/' . $node->language()->getId() . '/default', ['query' => ['_format' => 'json']]); Chris@16: $info = Json::decode($json); Chris@16: // Assert that the default settings for our field are respected by our JSON Chris@16: // endpoint. Chris@16: $this->assertTrue($info['alt_field']); Chris@16: $this->assertFalse($info['title_field']); Chris@16: } Chris@16: Chris@16: /** Chris@16: * Tests that uploading a valid image works. Chris@16: */ Chris@16: public function testValidImageUpload() { Chris@16: // Create a test Node. Chris@16: $node = $this->drupalCreateNode([ Chris@16: 'type' => 'article', Chris@16: 'title' => t('Test Node'), Chris@16: ]); Chris@16: Chris@16: // We want a test image that is a valid size. Chris@16: $valid_image = FALSE; Chris@16: $image_factory = $this->container->get('image.factory'); Chris@16: foreach ($this->drupalGetTestFiles('image') as $image) { Chris@16: $image_file = $image_factory->get($image->uri); Chris@16: if ($image_file->getWidth() > 50 && $image_file->getWidth() < 100) { Chris@16: $valid_image = $image; Chris@16: break; Chris@16: } Chris@16: } Chris@16: $this->assertTrue($valid_image); Chris@16: Chris@16: $this->drupalLogin($this->contentAuthorUser); Chris@16: $this->uploadImage($valid_image, $node->id(), $this->fieldName, $node->language()->getId()); Chris@16: $this->assertContains('"fid":"1"', $this->getSession()->getPage()->getContent(), 'Valid upload completed successfully.'); Chris@16: } Chris@16: Chris@16: /** Chris@16: * Tests that uploading a invalid image does not work. Chris@16: */ Chris@16: public function testInvalidUpload() { Chris@16: // Create a test Node. Chris@16: $node = $this->drupalCreateNode([ Chris@16: 'type' => 'article', Chris@16: 'title' => t('Test Node'), Chris@16: ]); Chris@16: Chris@16: // We want a test image that will fail validation. Chris@16: $invalid_image = FALSE; Chris@16: /** @var \Drupal\Core\Image\ImageFactory $image_factory */ Chris@16: $image_factory = $this->container->get('image.factory'); Chris@16: foreach ($this->drupalGetTestFiles('image') as $image) { Chris@16: /** @var \Drupal\Core\Image\ImageInterface $image_file */ Chris@16: $image_file = $image_factory->get($image->uri); Chris@16: if ($image_file->getWidth() < 50 || $image_file->getWidth() > 100) { Chris@16: $invalid_image = $image; Chris@16: break; Chris@16: } Chris@16: } Chris@16: $this->assertTrue($invalid_image); Chris@16: Chris@16: $this->drupalLogin($this->contentAuthorUser); Chris@16: $this->uploadImage($invalid_image, $node->id(), $this->fieldName, $node->language()->getId()); Chris@16: $this->assertContains('"main_error":"The image failed validation."', $this->getSession()->getPage()->getContent(), 'Invalid upload returned errors.'); Chris@16: } Chris@16: Chris@16: /** Chris@16: * Uploads an image using the image module's Quick Edit route. Chris@16: * Chris@16: * @param object $image Chris@16: * The image to upload. Chris@16: * @param int $nid Chris@16: * The target node ID. Chris@16: * @param string $field_name Chris@16: * The target field machine name. Chris@16: * @param string $langcode Chris@16: * The langcode to use when setting the field's value. Chris@16: */ Chris@16: public function uploadImage($image, $nid, $field_name, $langcode) { Chris@16: $filepath = $this->container->get('file_system')->realpath($image->uri); Chris@16: $path = 'quickedit/image/upload/node/' . $nid . '/' . $field_name . '/' . $langcode . '/default'; Chris@16: Chris@16: $this->prepareRequest(); Chris@16: $client = $this->getSession()->getDriver()->getClient(); Chris@16: $client->request('POST', $this->buildUrl($path, []), [], ['files[image]' => $filepath]); Chris@16: } Chris@16: Chris@16: }