Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\editor\Tests;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Component\Serialization\Json;
|
Chris@0
|
6 use Drupal\Core\EventSubscriber\MainContentViewSubscriber;
|
Chris@0
|
7 use Drupal\simpletest\WebTestBase;
|
Chris@0
|
8 use Drupal\filter\Entity\FilterFormat;
|
Chris@0
|
9
|
Chris@0
|
10 /**
|
Chris@0
|
11 * Tests Quick Edit module integration endpoints.
|
Chris@0
|
12 *
|
Chris@0
|
13 * @group editor
|
Chris@0
|
14 */
|
Chris@0
|
15 class QuickEditIntegrationLoadingTest extends WebTestBase {
|
Chris@0
|
16
|
Chris@0
|
17 /**
|
Chris@0
|
18 * Modules to enable.
|
Chris@0
|
19 *
|
Chris@0
|
20 * @var array
|
Chris@0
|
21 */
|
Chris@0
|
22 public static $modules = ['quickedit', 'filter', 'node', 'editor'];
|
Chris@0
|
23
|
Chris@0
|
24 /**
|
Chris@0
|
25 * The basic permissions necessary to view content and use in-place editing.
|
Chris@0
|
26 *
|
Chris@0
|
27 * @var array
|
Chris@0
|
28 */
|
Chris@0
|
29 protected static $basicPermissions = ['access content', 'create article content', 'use text format filtered_html', 'access contextual links'];
|
Chris@0
|
30
|
Chris@0
|
31 protected function setUp() {
|
Chris@0
|
32 parent::setUp();
|
Chris@0
|
33
|
Chris@0
|
34 // Create a text format.
|
Chris@0
|
35 $filtered_html_format = FilterFormat::create([
|
Chris@0
|
36 'format' => 'filtered_html',
|
Chris@0
|
37 'name' => 'Filtered HTML',
|
Chris@0
|
38 'weight' => 0,
|
Chris@0
|
39 'filters' => [
|
Chris@0
|
40 'filter_caption' => [
|
Chris@0
|
41 'status' => 1,
|
Chris@0
|
42 ],
|
Chris@0
|
43 ],
|
Chris@0
|
44 ]);
|
Chris@0
|
45 $filtered_html_format->save();
|
Chris@0
|
46
|
Chris@0
|
47 // Create a node type.
|
Chris@0
|
48 $this->drupalCreateContentType([
|
Chris@0
|
49 'type' => 'article',
|
Chris@0
|
50 'name' => 'Article',
|
Chris@0
|
51 ]);
|
Chris@0
|
52
|
Chris@0
|
53 // Create one node of the above node type using the above text format.
|
Chris@0
|
54 $this->drupalCreateNode([
|
Chris@0
|
55 'type' => 'article',
|
Chris@0
|
56 'body' => [
|
Chris@0
|
57 0 => [
|
Chris@0
|
58 'value' => '<p>Do you also love Drupal?</p><img src="druplicon.png" data-caption="Druplicon" />',
|
Chris@0
|
59 'format' => 'filtered_html',
|
Chris@0
|
60 ]
|
Chris@0
|
61 ]
|
Chris@0
|
62 ]);
|
Chris@0
|
63 }
|
Chris@0
|
64
|
Chris@0
|
65 /**
|
Chris@0
|
66 * Test loading of untransformed text when a user doesn't have access to it.
|
Chris@0
|
67 */
|
Chris@0
|
68 public function testUsersWithoutPermission() {
|
Chris@0
|
69 // Create 3 users, each with insufficient permissions, i.e. without either
|
Chris@0
|
70 // or both of the following permissions:
|
Chris@0
|
71 // - the 'access in-place editing' permission
|
Chris@0
|
72 // - the 'edit any article content' permission (necessary to edit node 1)
|
Chris@0
|
73 $users = [
|
Chris@0
|
74 $this->drupalCreateUser(static::$basicPermissions),
|
Chris@0
|
75 $this->drupalCreateUser(array_merge(static::$basicPermissions, ['edit any article content'])),
|
Chris@0
|
76 $this->drupalCreateUser(array_merge(static::$basicPermissions, ['access in-place editing']))
|
Chris@0
|
77 ];
|
Chris@0
|
78
|
Chris@0
|
79 // Now test with each of the 3 users with insufficient permissions.
|
Chris@0
|
80 foreach ($users as $user) {
|
Chris@0
|
81 $this->drupalLogin($user);
|
Chris@0
|
82 $this->drupalGet('node/1');
|
Chris@0
|
83
|
Chris@0
|
84 // Ensure the text is transformed.
|
Chris@0
|
85 $this->assertRaw('<p>Do you also love Drupal?</p><figure role="group" class="caption caption-img"><img src="druplicon.png" /><figcaption>Druplicon</figcaption></figure>');
|
Chris@0
|
86
|
Chris@0
|
87 // Retrieving the untransformed text should result in an 403 response and
|
Chris@0
|
88 // return a different error message depending of the missing permission.
|
Chris@0
|
89 $response = $this->drupalPost('editor/' . 'node/1/body/en/full', '', [], ['query' => [MainContentViewSubscriber::WRAPPER_FORMAT => 'drupal_ajax']]);
|
Chris@0
|
90 $this->assertResponse(403);
|
Chris@0
|
91 if (!$user->hasPermission('access in-place editing')) {
|
Chris@0
|
92 $message = "The 'access in-place editing' permission is required.";
|
Chris@0
|
93 }
|
Chris@0
|
94 else {
|
Chris@0
|
95 $message = '';
|
Chris@0
|
96 }
|
Chris@0
|
97 $this->assertIdentical(Json::encode(['message' => $message]), $response);
|
Chris@0
|
98 }
|
Chris@0
|
99 }
|
Chris@0
|
100
|
Chris@0
|
101 /**
|
Chris@0
|
102 * Test loading of untransformed text when a user does have access to it.
|
Chris@0
|
103 */
|
Chris@0
|
104 public function testUserWithPermission() {
|
Chris@0
|
105 $user = $this->drupalCreateUser(array_merge(static::$basicPermissions, ['edit any article content', 'access in-place editing']));
|
Chris@0
|
106 $this->drupalLogin($user);
|
Chris@0
|
107 $this->drupalGet('node/1');
|
Chris@0
|
108
|
Chris@0
|
109 // Ensure the text is transformed.
|
Chris@0
|
110 $this->assertRaw('<p>Do you also love Drupal?</p><figure role="group" class="caption caption-img"><img src="druplicon.png" /><figcaption>Druplicon</figcaption></figure>');
|
Chris@0
|
111
|
Chris@0
|
112 $response = $this->drupalPost('editor/' . 'node/1/body/en/full', '', [], ['query' => [MainContentViewSubscriber::WRAPPER_FORMAT => 'drupal_ajax']]);
|
Chris@0
|
113 $this->assertResponse(200);
|
Chris@0
|
114 $ajax_commands = Json::decode($response);
|
Chris@0
|
115 $this->assertIdentical(1, count($ajax_commands), 'The untransformed text POST request results in one AJAX command.');
|
Chris@0
|
116 $this->assertIdentical('editorGetUntransformedText', $ajax_commands[0]['command'], 'The first AJAX command is an editorGetUntransformedText command.');
|
Chris@0
|
117 $this->assertIdentical('<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.');
|
Chris@0
|
118 }
|
Chris@0
|
119
|
Chris@0
|
120 }
|