View source
<?php
namespace Drupal\Tests\ckeditor5\Functional;
use Drupal\Core\Url;
use Drupal\editor\Entity\Editor;
use Drupal\filter\Entity\FilterFormat;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\ckeditor5\Traits\SynchronizeCsrfTokenSeedTrait;
use Drupal\Tests\jsonapi\Functional\JsonApiRequestTestTrait;
use Drupal\Tests\TestFileCreationTrait;
use Drupal\user\RoleInterface;
use GuzzleHttp\RequestOptions;
class ImageUploadTest extends BrowserTestBase {
use JsonApiRequestTestTrait;
use TestFileCreationTrait;
use SynchronizeCsrfTokenSeedTrait;
protected static $modules = [
'editor',
'filter',
'ckeditor5',
];
protected $user;
protected $defaultTheme = 'stark';
protected function setUp() : void {
parent::setUp();
$this->user = $this
->drupalCreateUser();
$this
->drupalLogin($this->user);
}
public function testUploadFileExtension() {
$this
->createBasicFormat();
$this
->createEditorWithUpload([
'status' => TRUE,
'scheme' => 'public',
'directory' => 'inline-images',
'max_size' => '',
'max_dimensions' => [
'width' => 0,
'height' => 0,
],
]);
$url = $this
->getUploadUrl();
$image_file = file_get_contents(current($this
->getTestFiles('image'))->uri);
$non_image_file = file_get_contents(current($this
->getTestFiles('php'))->uri);
$response = $this
->uploadRequest($url, $non_image_file, 'test.php');
$this
->assertSame(422, $response
->getStatusCode());
$response = $this
->uploadRequest($url, $image_file, 'test.jpg');
$this
->assertSame(201, $response
->getStatusCode());
}
public function testFileUploadLargerFileSize() {
$this
->createBasicFormat();
$this
->createEditorWithUpload([
'status' => TRUE,
'scheme' => 'public',
'directory' => 'inline-images',
'max_size' => 30000,
'max_dimensions' => [
'width' => 0,
'height' => 0,
],
]);
$url = $this
->getUploadUrl();
$images = $this
->getTestFiles('image');
$large_image = $this
->getTestImageByStat($images, 'size', function ($size) {
return $size > 30000;
});
$small_image = $this
->getTestImageByStat($images, 'size', function ($size) {
return $size < 30000;
});
$response = $this
->uploadRequest($url, file_get_contents($large_image->uri), 'large.jpg');
$this
->assertSame(422, $response
->getStatusCode());
$response = $this
->uploadRequest($url, file_get_contents($small_image->uri), 'small.jpg');
$this
->assertSame(201, $response
->getStatusCode());
}
public function testLockAfterFailedValidation() {
$this
->createBasicFormat();
$this
->createEditorWithUpload([
'status' => TRUE,
'scheme' => 'public',
'directory' => 'inline-images',
'max_size' => 30000,
'max_dimensions' => [
'width' => 0,
'height' => 0,
],
]);
$url = $this
->getUploadUrl();
$images = $this
->getTestFiles('image');
$large_image = $this
->getTestImageByStat($images, 'size', function ($size) {
return $size > 30000;
});
$small_image = $this
->getTestImageByStat($images, 'size', function ($size) {
return $size < 30000;
});
$response = $this
->uploadRequest($url, file_get_contents($large_image->uri), 'same.jpg');
$this
->assertSame(422, $response
->getStatusCode());
$response = $this
->uploadRequest($url, file_get_contents($small_image->uri), 'same.jpg');
$this
->assertSame(201, $response
->getStatusCode());
}
protected function uploadRequest(Url $url, string $file_contents, string $file_name) {
$request_options[RequestOptions::HEADERS] = [
'Accept' => 'application/json',
];
$request_options[RequestOptions::MULTIPART] = [
[
'name' => 'upload',
'filename' => $file_name,
'contents' => $file_contents,
],
];
return $this
->request('POST', $url, $request_options);
}
protected function getUploadUrl() {
$token = $this->container
->get('csrf_token')
->get('ckeditor5/upload-image/basic_html');
return Url::fromRoute('ckeditor5.upload_image', [
'editor' => 'basic_html',
], [
'query' => [
'token' => $token,
],
]);
}
protected function createBasicFormat() {
$basic_html_format = FilterFormat::create([
'format' => 'basic_html',
'name' => 'Basic HTML',
'weight' => 1,
'filters' => [
'filter_html_escape' => [
'status' => 1,
],
],
'roles' => [
RoleInterface::AUTHENTICATED_ID,
],
]);
$basic_html_format
->save();
}
protected function createEditorWithUpload(array $upload_config) {
$editor = Editor::create([
'editor' => 'ckeditor5',
'format' => 'basic_html',
'settings' => [
'toolbar' => [
'items' => [
'uploadImage',
],
],
'plugins' => [],
],
'image_upload' => $upload_config,
]);
$editor
->save();
return $editor;
}
protected function getTestImageByStat(array $images, string $stat, callable $condition) {
return current(array_filter($images, function ($image) use ($condition, $stat) {
$stats = stat($image->uri);
return $condition($stats[$stat]);
}));
}
}