annotate core/modules/quickedit/tests/src/Functional/QuickEditCustomPipelineTest.php @ 19:fa3358dc1485 tip

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