View source
<?php
namespace Drupal\Tests\quickedit\Functional;
use Drupal\Component\Serialization\Json;
use Drupal\Core\EventSubscriber\MainContentViewSubscriber;
use Drupal\filter\Entity\FilterFormat;
use Drupal\Tests\BrowserTestBase;
class EditorIntegrationLoadingTest extends BrowserTestBase {
protected static $modules = [
'quickedit',
'filter',
'node',
'editor',
];
protected $defaultTheme = 'classy';
protected static $basicPermissions = [
'access content',
'create article content',
'use text format filtered_html',
'access contextual links',
];
protected function setUp() : void {
parent::setUp();
$filtered_html_format = FilterFormat::create([
'format' => 'filtered_html',
'name' => 'Filtered HTML',
'weight' => 0,
'filters' => [
'filter_caption' => [
'status' => 1,
],
],
]);
$filtered_html_format
->save();
$this
->drupalCreateContentType([
'type' => 'article',
'name' => 'Article',
]);
$this
->drupalCreateNode([
'type' => 'article',
'body' => [
0 => [
'value' => '<p>Do you also love Drupal?</p><img src="druplicon.png" data-caption="Druplicon" />',
'format' => 'filtered_html',
],
],
]);
}
public function testUsersWithoutPermission() {
$users = [
$this
->drupalCreateUser(static::$basicPermissions),
$this
->drupalCreateUser(array_merge(static::$basicPermissions, [
'edit any article content',
])),
$this
->drupalCreateUser(array_merge(static::$basicPermissions, [
'access in-place editing',
])),
];
foreach ($users as $user) {
$this
->drupalLogin($user);
$this
->drupalGet('node/1');
$this
->assertSession()
->responseContains('<p>Do you also love Drupal?</p><figure role="group" class="caption caption-img"><img src="druplicon.png" /><figcaption>Druplicon</figcaption></figure>');
$client = $this
->getHttpClient();
$response = $client
->post($this
->buildUrl('editor/node/1/body/en/full'), [
'query' => http_build_query([
MainContentViewSubscriber::WRAPPER_FORMAT => 'drupal_ajax',
]),
'cookies' => $this
->getSessionCookies(),
'headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/x-www-form-urlencoded',
],
'http_errors' => FALSE,
]);
$this
->assertEquals(403, $response
->getStatusCode());
if (!$user
->hasPermission('access in-place editing')) {
$message = "The 'access in-place editing' permission is required.";
}
else {
$message = "The 'edit any article content' permission is required.";
}
$body = Json::decode($response
->getBody());
$this
->assertSame($message, $body['message']);
}
}
public function testUserWithPermission() {
$user = $this
->drupalCreateUser(array_merge(static::$basicPermissions, [
'edit any article content',
'access in-place editing',
]));
$this
->drupalLogin($user);
$this
->drupalGet('node/1');
$this
->assertSession()
->responseContains('<p>Do you also love Drupal?</p><figure role="group" class="caption caption-img"><img src="druplicon.png" /><figcaption>Druplicon</figcaption></figure>');
$client = $this
->getHttpClient();
$response = $client
->post($this
->buildUrl('editor/node/1/body/en/full'), [
'query' => http_build_query([
MainContentViewSubscriber::WRAPPER_FORMAT => 'drupal_ajax',
]),
'cookies' => $this
->getSessionCookies(),
'headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/x-www-form-urlencoded',
],
'http_errors' => FALSE,
]);
$this
->assertEquals(200, $response
->getStatusCode());
$ajax_commands = Json::decode($response
->getBody());
$this
->assertCount(1, $ajax_commands, 'The untransformed text POST request results in one AJAX command.');
$this
->assertSame('editorGetUntransformedText', $ajax_commands[0]['command'], 'The first AJAX command is an editorGetUntransformedText command.');
$this
->assertSame('<p>Do you also love Drupal?</p><img src="druplicon.png" data-caption="Druplicon" />', $ajax_commands[0]['data'], 'The editorGetUntransformedText command contains the expected data.');
}
}