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