View source
<?php
namespace Drupal\Tests\image\Functional;
use Drupal\Component\Serialization\Json;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\image\Kernel\ImageFieldCreationTrait;
use Drupal\Tests\TestFileCreationTrait;
class QuickEditImageControllerTest extends BrowserTestBase {
use ImageFieldCreationTrait;
use TestFileCreationTrait {
getTestFiles as drupalGetTestFiles;
}
public static $modules = [
'node',
'image',
'quickedit',
];
protected $defaultTheme = 'stark';
protected $fieldName;
protected $contentAuthorUser;
protected function setUp() {
parent::setUp();
$this
->drupalCreateContentType([
'type' => 'article',
'name' => 'Article',
]);
$this->contentAuthorUser = $this
->drupalCreateUser([
'access contextual links',
'access in-place editing',
'access content',
'create article content',
'edit any article content',
'delete any article content',
]);
$this
->drupalLogin($this->contentAuthorUser);
$this->fieldName = strtolower($this
->randomMachineName());
$field_settings = [
'max_resolution' => '100x',
'min_resolution' => '50x',
];
$this
->createImageField($this->fieldName, 'article', [], $field_settings);
}
public function testAccess() {
$user = $this
->createUser();
$this
->drupalLogin($user);
$node = $this
->drupalCreateNode([
'type' => 'article',
'title' => t('Test Node'),
]);
$this
->drupalGet('quickedit/image/info/node/' . $node
->id() . '/' . $this->fieldName . '/' . $node
->language()
->getId() . '/default');
$this
->assertSession()
->statusCodeEquals(403);
$client = $this
->getSession()
->getDriver()
->getClient();
$client
->request('POST', '/quickedit/image/upload/node/' . $node
->id() . '/' . $this->fieldName . '/' . $node
->language()
->getId() . '/default');
$this
->assertEquals('403', $client
->getResponse()
->getStatus());
}
public function testFieldInfo() {
$node = $this
->drupalCreateNode([
'type' => 'article',
'title' => t('Test Node'),
]);
$json = $this
->drupalGet('quickedit/image/info/node/' . $node
->id() . '/' . $this->fieldName . '/' . $node
->language()
->getId() . '/default', [
'query' => [
'_format' => 'json',
],
]);
$info = Json::decode($json);
$this
->assertTrue($info['alt_field']);
$this
->assertFalse($info['title_field']);
}
public function testValidImageUpload() {
$node = $this
->drupalCreateNode([
'type' => 'article',
'title' => t('Test Node'),
]);
$valid_image = FALSE;
$image_factory = $this->container
->get('image.factory');
foreach ($this
->drupalGetTestFiles('image') as $image) {
$image_file = $image_factory
->get($image->uri);
if ($image_file
->getWidth() > 50 && $image_file
->getWidth() < 100) {
$valid_image = $image;
break;
}
}
$this
->assertNotFalse($valid_image);
$this
->drupalLogin($this->contentAuthorUser);
$this
->uploadImage($valid_image, $node
->id(), $this->fieldName, $node
->language()
->getId());
$this
->assertStringContainsString('"fid":"1"', $this
->getSession()
->getPage()
->getContent(), 'Valid upload completed successfully.');
}
public function testInvalidUpload() {
$node = $this
->drupalCreateNode([
'type' => 'article',
'title' => t('Test Node'),
]);
$invalid_image = FALSE;
$image_factory = $this->container
->get('image.factory');
foreach ($this
->drupalGetTestFiles('image') as $image) {
$image_file = $image_factory
->get($image->uri);
if ($image_file
->getWidth() < 50 || $image_file
->getWidth() > 100) {
$invalid_image = $image;
break;
}
}
$this
->assertNotFalse($invalid_image);
$this
->drupalLogin($this->contentAuthorUser);
$this
->uploadImage($invalid_image, $node
->id(), $this->fieldName, $node
->language()
->getId());
$this
->assertStringContainsString('"main_error":"The image failed validation."', $this
->getSession()
->getPage()
->getContent(), 'Invalid upload returned errors.');
}
public function uploadImage($image, $nid, $field_name, $langcode) {
$filepath = $this->container
->get('file_system')
->realpath($image->uri);
$path = 'quickedit/image/upload/node/' . $nid . '/' . $field_name . '/' . $langcode . '/default';
$this
->prepareRequest();
$client = $this
->getSession()
->getDriver()
->getClient();
$client
->request('POST', $this
->buildUrl($path, []), [], [
'files[image]' => $filepath,
]);
}
}