View source
<?php
namespace Drupal\Tests\quickedit\FunctionalJavascript;
use Drupal\file\Entity\File;
use Drupal\Tests\image\Kernel\ImageFieldCreationTrait;
use Drupal\Tests\TestFileCreationTrait;
class QuickEditImageTest extends QuickEditJavascriptTestBase {
use ImageFieldCreationTrait;
use TestFileCreationTrait;
use QuickEditImageEditorTestTrait;
protected static $modules = [
'node',
'image',
'field_ui',
'hold_test',
];
protected $defaultTheme = 'stark';
protected $contentAuthorUser;
protected function setUp() : void {
parent::setUp();
$this
->drupalCreateContentType([
'type' => 'article',
'name' => 'Article',
]);
}
public function testImageInPlaceEditor($admin_permission = FALSE) {
$permissions = [
'access contextual links',
'access toolbar',
'access in-place editing',
'access content',
'create article content',
'edit any article content',
'delete any article content',
];
if ($admin_permission) {
$permissions[] = 'administer nodes';
}
$this->contentAuthorUser = $this
->drupalCreateUser($permissions);
$this
->drupalLogin($this->contentAuthorUser);
$field_name = strtolower($this
->randomMachineName());
$field_settings = [
'file_extensions' => 'png',
];
$formatter_settings = [
'image_style' => 'large',
'image_link' => '',
];
$this
->createImageField($field_name, 'article', [], $field_settings, [], $formatter_settings);
$valid_images = [];
foreach ($this
->getTestFiles('image') as $image) {
$regex = '/\\.(' . preg_replace('/ +/', '|', preg_quote($field_settings['file_extensions'])) . ')$/i';
if (preg_match($regex, $image->filename)) {
$valid_images[] = $image;
}
}
$this
->assertGreaterThanOrEqual(2, count($valid_images));
$file = File::create([
'uri' => $valid_images[0]->uri,
'uid' => $this->contentAuthorUser
->id(),
]);
$file
->setPermanent();
$file
->save();
$image_factory = $this->container
->get('image.factory');
$image = $image_factory
->get($valid_images[0]->uri);
$node = $this
->drupalCreateNode([
'type' => 'article',
'title' => 'Test Node',
$field_name => [
'target_id' => $file
->id(),
'alt' => 'Hello world',
'title' => '',
'width' => $image
->getWidth(),
'height' => $image
->getHeight(),
],
]);
$this
->drupalGet('node/' . $node
->id());
$entity_selector = '[data-quickedit-entity-id="node/' . $node
->id() . '"]';
$field_selector = '[data-quickedit-field-id="node/' . $node
->id() . '/' . $field_name . '/' . $node
->language()
->getId() . '/full"]';
$original_image_selector = 'img[src*="' . $valid_images[0]->filename . '"][alt="Hello world"]';
$new_image_selector = 'img[src*="' . $valid_images[1]->filename . '"][alt="New text"]';
$this
->assertSession()
->elementExists('css', $entity_selector . ' ' . $field_selector . ' ' . $original_image_selector);
$this
->awaitQuickEditForEntity('node', 1);
$this
->assertEntityInstanceStates([
'node/1[0]' => 'closed',
]);
$admin_inactive = [];
$admin_candidate = [];
if ($admin_permission) {
$admin_inactive = [
'node/1/uid/en/full' => 'inactive',
'node/1/created/en/full' => 'inactive',
];
$admin_candidate = [
'node/1/uid/en/full' => 'candidate',
'node/1/created/en/full' => 'candidate',
];
}
$this
->assertEntityInstanceFieldStates('node', 1, 0, [
'node/1/title/en/full' => 'inactive',
'node/1/body/en/full' => 'inactive',
'node/1/' . $field_name . '/en/full' => 'inactive',
] + $admin_inactive);
$this
->startQuickEditViaToolbar('node', 1, 0);
$this
->assertEntityInstanceStates([
'node/1[0]' => 'opened',
]);
$this
->assertQuickEditEntityToolbar((string) $node
->label(), NULL);
$this
->assertEntityInstanceFieldStates('node', 1, 0, [
'node/1/title/en/full' => 'candidate',
'node/1/body/en/full' => 'candidate',
'node/1/' . $field_name . '/en/full' => 'candidate',
] + $admin_candidate);
$this
->click($field_selector);
$this
->awaitImageEditor();
$this
->assertSession()
->elementExists('css', $field_selector . ' .quickedit-image-dropzone');
$this
->assertEntityInstanceFieldStates('node', 1, 0, [
'node/1/title/en/full' => 'candidate',
'node/1/body/en/full' => 'candidate',
'node/1/' . $field_name . '/en/full' => 'active',
] + $admin_candidate);
$this
->typeInImageEditorAltTextInput('New text');
$this
->assertEntityInstanceFieldStates('node', 1, 0, [
'node/1/title/en/full' => 'candidate',
'node/1/body/en/full' => 'candidate',
'node/1/' . $field_name . '/en/full' => 'changed',
] + $admin_candidate);
$this
->dropImageOnImageEditor($valid_images[1]->uri);
$this
->prepareRequest();
hold_test_response(TRUE);
$this
->saveQuickEdit();
$this
->assertEntityInstanceStates([
'node/1[0]' => 'committing',
]);
$this
->assertEntityInstanceFieldStates('node', 1, 0, [
'node/1/title/en/full' => 'candidate',
'node/1/body/en/full' => 'candidate',
'node/1/' . $field_name . '/en/full' => 'saving',
] + $admin_candidate);
$this
->assertEntityInstanceFieldMarkup([
'node/1/' . $field_name . '/en/full' => '.quickedit-changed',
]);
hold_test_response(FALSE);
$this
->assertJsCondition("Drupal.quickedit.collections.entities.get('node/1[0]').get('state') === 'closed'");
$this
->assertEntityInstanceStates([
'node/1[0]' => 'closed',
]);
$this
->drupalGet('node/' . $node
->id());
$this
->assertSession()
->elementNotExists('css', $entity_selector . ' ' . $field_selector . ' ' . $original_image_selector);
$this
->assertSession()
->elementExists('css', $entity_selector . ' ' . $field_selector . ' ' . $new_image_selector);
}
public function providerTestImageInPlaceEditor() : array {
return [
'with permission' => [
TRUE,
],
'without permission' => [
FALSE,
],
];
}
}