comparison core/modules/quickedit/tests/src/Functional/QuickEditCustomPipelineTest.php @ 18:af1871eacc83

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:33:08 +0100
parents
children
comparison
equal deleted inserted replaced
17:129ea1e6d783 18:af1871eacc83
1 <?php
2
3 namespace Drupal\Tests\quickedit\Functional;
4
5 use Drupal\Component\Serialization\Json;
6 use Drupal\Core\EventSubscriber\MainContentViewSubscriber;
7 use Drupal\Tests\BrowserTestBase;
8
9 /**
10 * Tests using a custom pipeline with Quick Edit.
11 *
12 * @group quickedit
13 */
14 class QuickEditCustomPipelineTest extends BrowserTestBase {
15
16 /**
17 * {@inheritdoc}
18 */
19 protected static $modules = [
20 'quickedit',
21 'quickedit_test',
22 'node',
23 ];
24
25 /**
26 * Tests that Quick Edit works with custom render pipelines.
27 */
28 public function testCustomPipeline() {
29 // Create a node type.
30 $this->drupalCreateContentType([
31 'type' => 'article',
32 'name' => 'Article',
33 ]);
34 $node = $this->createNode(['type' => 'article']);
35 $editor_user = $this->drupalCreateUser([
36 'access content',
37 'create article content',
38 'edit any article content',
39 'access in-place editing',
40 ]);
41 $this->drupalLogin($editor_user);
42
43 $custom_render_url = $this->buildUrl('quickedit/form/node/' . $node->id() . '/body/en/quickedit_test-custom-render-data');
44
45 $client = $this->getHttpClient();
46 $post = ['nocssjs' => 'true'];
47 $response = $client->post($custom_render_url, [
48 'body' => http_build_query($post),
49 'query' => [MainContentViewSubscriber::WRAPPER_FORMAT => 'drupal_ajax'],
50 'cookies' => $this->getSessionCookies(),
51 'headers' => [
52 'Accept' => 'application/json',
53 'Content-Type' => 'application/x-www-form-urlencoded',
54 ],
55 'http_errors' => FALSE,
56 ]);
57
58 $this->assertEquals(200, $response->getStatusCode());
59
60 $ajax_commands = Json::decode($response->getBody());
61 // Request editing to render results with the custom render pipeline.
62
63 // Prepare form values for submission. drupalPostAJAX() is not suitable for
64 // handling pages with JSON responses, so we need our own solution here.
65 $form_tokens_found = preg_match('/\sname="form_token" value="([^"]+)"/', $ajax_commands[0]['data'], $token_match) && preg_match('/\sname="form_build_id" value="([^"]+)"/', $ajax_commands[0]['data'], $build_id_match);
66 $this->assertTrue($form_tokens_found, 'Form tokens found in output.');
67
68 $post = [
69 'form_id' => 'quickedit_field_form',
70 'form_token' => $token_match[1],
71 'form_build_id' => $build_id_match[1],
72 'body[0][summary]' => '',
73 'body[0][value]' => '<p>Fine thanks.</p>',
74 'body[0][format]' => 'filtered_html',
75 'op' => t('Save'),
76 ];
77 // Assume there is another field on this page, which doesn't use a custom
78 // render pipeline, but the default one, and it uses the "full" view mode.
79 $post += ['other_view_modes[]' => 'full'];
80
81 // Submit field form and check response. Should render with the custom
82 // render pipeline.
83 $response = $client->post($custom_render_url, [
84 'body' => http_build_query($post),
85 'query' => [MainContentViewSubscriber::WRAPPER_FORMAT => 'drupal_ajax'],
86 'cookies' => $this->getSessionCookies(),
87 'headers' => [
88 'Accept' => 'application/json',
89 'Content-Type' => 'application/x-www-form-urlencoded',
90 ],
91 'http_errors' => FALSE,
92 ]);
93 $ajax_commands = Json::decode($response->getBody());
94 $this->assertIdentical(1, count($ajax_commands), 'The field form HTTP request results in one AJAX command.');
95 $this->assertIdentical('quickeditFieldFormSaved', $ajax_commands[0]['command'], 'The first AJAX command is a quickeditFieldFormSaved command.');
96 $this->assertTrue(strpos($ajax_commands[0]['data'], 'Fine thanks.'), 'Form value saved and printed back.');
97 $this->assertTrue(strpos($ajax_commands[0]['data'], '<div class="quickedit-test-wrapper">') !== FALSE, 'Custom render pipeline used to render the value.');
98 $this->assertIdentical(array_keys($ajax_commands[0]['other_view_modes']), ['full'], 'Field was also rendered in the "full" view mode.');
99 $this->assertTrue(strpos($ajax_commands[0]['other_view_modes']['full'], 'Fine thanks.'), '"full" version of field contains the form value.');
100 }
101
102 }