comparison core/modules/image/tests/src/Functional/QuickEditImageControllerTest.php @ 16:c2387f117808

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