Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / image / src / Tests / QuickEditImageControllerTest.php
1 <?php
2
3 namespace Drupal\image\Tests;
4
5 use Drupal\Tests\image\Kernel\ImageFieldCreationTrait;
6 use Drupal\simpletest\WebTestBase;
7
8 /**
9  * Tests the endpoints used by the "image" in-place editor.
10  *
11  * @group image
12  */
13 class QuickEditImageControllerTest extends WebTestBase {
14
15   use ImageFieldCreationTrait;
16
17   /**
18    * {@inheritdoc}
19    */
20   public static $modules = ['node', 'image', 'quickedit'];
21
22   /**
23    * The machine name of our image field.
24    *
25    * @var string
26    */
27   protected $fieldName;
28
29   /**
30    * A user with permissions to edit articles and use Quick Edit.
31    *
32    * @var \Drupal\user\UserInterface
33    */
34   protected $contentAuthorUser;
35
36   /**
37    * {@inheritdoc}
38    */
39   protected function setUp() {
40     parent::setUp();
41
42     // Create the Article node type.
43     $this->drupalCreateContentType(['type' => 'article', 'name' => 'Article']);
44
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',
49       'access content',
50       'create article content',
51       'edit any article content',
52       'delete any article content',
53     ]);
54     $this->drupalLogin($this->contentAuthorUser);
55
56     // Create a field with basic resolution validators.
57     $this->fieldName = strtolower($this->randomMachineName());
58     $field_settings = [
59       'max_resolution' => '100x',
60       'min_resolution' => '50x',
61     ];
62     $this->createImageField($this->fieldName, 'article', [], $field_settings);
63   }
64
65   /**
66    * Tests that routes restrict access for un-privileged users.
67    */
68   public function testAccess() {
69     // Create an anonymous user.
70     $user = $this->createUser();
71     $this->drupalLogin($user);
72
73     // Create a test Node.
74     $node = $this->drupalCreateNode([
75       'type' => 'article',
76       'title' => t('Test Node'),
77     ]);
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');
82   }
83
84   /**
85    * Tests that the field info route returns expected data.
86    */
87   public function testFieldInfo() {
88     // Create a test Node.
89     $node = $this->drupalCreateNode([
90       'type' => 'article',
91       'title' => t('Test Node'),
92     ]);
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
95     // endpoint.
96     $this->assertTrue($info['alt_field']);
97     $this->assertFalse($info['title_field']);
98   }
99
100   /**
101    * Tests that uploading a valid image works.
102    */
103   public function testValidImageUpload() {
104     // Create a test Node.
105     $node = $this->drupalCreateNode([
106       'type' => 'article',
107       'title' => t('Test Node'),
108     ]);
109
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;
117         break;
118       }
119     }
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.'));
123   }
124
125   /**
126    * Tests that uploading a invalid image does not work.
127    */
128   public function testInvalidUpload() {
129     // Create a test Node.
130     $node = $this->drupalCreateNode([
131       'type' => 'article',
132       'title' => t('Test Node'),
133     ]);
134
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;
144         break;
145       }
146     }
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.'));
150   }
151
152   /**
153    * Uploads an image using the image module's Quick Edit route.
154    *
155    * @param object $image
156    *   The image to upload.
157    * @param int $nid
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.
163    *
164    * @return mixed
165    *   The content returned from the call to $this->curlExec().
166    */
167   public function uploadImage($image, $nid, $field_name, $langcode) {
168     $filepath = $this->container->get('file_system')->realpath($image->uri);
169     $data = [
170       'files[image]' => curl_file_create($filepath),
171     ];
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',
182       ],
183     ]);
184   }
185
186 }