annotate core/tests/Drupal/FunctionalJavascriptTests/Ajax/AjaxFormPageCacheTest.php @ 14:1fec387a4317

Update Drupal core to 8.5.2 via Composer
author Chris Cannam
date Mon, 23 Apr 2018 09:46:53 +0100
parents 4c8ae668cc8c
children 129ea1e6d783
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\FunctionalJavascriptTests\Ajax;
Chris@0 4
Chris@0 5 use Drupal\FunctionalJavascriptTests\JavascriptTestBase;
Chris@0 6
Chris@0 7 /**
Chris@0 8 * Performs tests on AJAX forms in cached pages.
Chris@0 9 *
Chris@0 10 * @group Ajax
Chris@0 11 */
Chris@0 12 class AjaxFormPageCacheTest extends JavascriptTestBase {
Chris@0 13
Chris@0 14 /**
Chris@0 15 * {@inheritdoc}
Chris@0 16 */
Chris@0 17 public static $modules = ['ajax_test', 'ajax_forms_test'];
Chris@0 18
Chris@0 19 /**
Chris@0 20 * {@inheritdoc}
Chris@0 21 */
Chris@0 22 protected function setUp() {
Chris@0 23 parent::setUp();
Chris@0 24
Chris@0 25 $config = $this->config('system.performance');
Chris@0 26 $config->set('cache.page.max_age', 300);
Chris@0 27 $config->save();
Chris@0 28 }
Chris@0 29
Chris@0 30 /**
Chris@0 31 * Return the build id of the current form.
Chris@0 32 */
Chris@0 33 protected function getFormBuildId() {
Chris@0 34 $build_id_fields = $this->xpath('//input[@name="form_build_id"]');
Chris@0 35 $this->assertEquals(count($build_id_fields), 1, 'One form build id field on the page');
Chris@0 36 return $build_id_fields[0]->getValue();
Chris@0 37 }
Chris@0 38
Chris@0 39 /**
Chris@0 40 * Create a simple form, then submit the form via AJAX to change to it.
Chris@0 41 */
Chris@0 42 public function testSimpleAJAXFormValue() {
Chris@0 43 $this->drupalGet('ajax_forms_test_get_form');
Chris@0 44 $this->assertEquals($this->drupalGetHeader('X-Drupal-Cache'), 'MISS', 'Page was not cached.');
Chris@0 45 $build_id_initial = $this->getFormBuildId();
Chris@0 46
Chris@0 47 // Changing the value of a select input element, triggers a AJAX
Chris@0 48 // request/response. The callback on the form responds with three AJAX
Chris@0 49 // commands:
Chris@0 50 // - UpdateBuildIdCommand
Chris@0 51 // - HtmlCommand
Chris@0 52 // - DataCommand
Chris@0 53 $session = $this->getSession();
Chris@0 54 $session->getPage()->selectFieldOption('select', 'green');
Chris@0 55
Chris@0 56 // Wait for the DOM to update. The HtmlCommand will update
Chris@0 57 // #ajax_selected_color to reflect the color change.
Chris@0 58 $green_div = $this->assertSession()->waitForElement('css', "#ajax_selected_color div:contains('green')");
Chris@0 59 $this->assertNotNull($green_div, 'DOM update: The selected color DIV is green.');
Chris@0 60
Chris@0 61 // Confirm the operation of the UpdateBuildIdCommand.
Chris@0 62 $build_id_first_ajax = $this->getFormBuildId();
Chris@0 63 $this->assertNotEquals($build_id_initial, $build_id_first_ajax, 'Build id is changed in the form_build_id element on first AJAX submission');
Chris@0 64
Chris@0 65 // Changing the value of a select input element, triggers a AJAX
Chris@0 66 // request/response.
Chris@0 67 $session->getPage()->selectFieldOption('select', 'red');
Chris@0 68
Chris@0 69 // Wait for the DOM to update.
Chris@0 70 $red_div = $this->assertSession()->waitForElement('css', "#ajax_selected_color div:contains('red')");
Chris@0 71 $this->assertNotNull($red_div, 'DOM update: The selected color DIV is red.');
Chris@0 72
Chris@0 73 // Confirm the operation of the UpdateBuildIdCommand.
Chris@0 74 $build_id_second_ajax = $this->getFormBuildId();
Chris@0 75 $this->assertNotEquals($build_id_first_ajax, $build_id_second_ajax, 'Build id changes on subsequent AJAX submissions');
Chris@0 76
Chris@0 77 // Emulate a push of the reload button and then repeat the test sequence
Chris@0 78 // this time with a page loaded from the cache.
Chris@0 79 $session->reload();
Chris@0 80 $this->assertEquals($this->drupalGetHeader('X-Drupal-Cache'), 'HIT', 'Page was cached.');
Chris@0 81 $build_id_from_cache_initial = $this->getFormBuildId();
Chris@0 82 $this->assertEquals($build_id_initial, $build_id_from_cache_initial, 'Build id is the same as on the first request');
Chris@0 83
Chris@0 84 // Changing the value of a select input element, triggers a AJAX
Chris@0 85 // request/response.
Chris@0 86 $session->getPage()->selectFieldOption('select', 'green');
Chris@0 87
Chris@0 88 // Wait for the DOM to update.
Chris@0 89 $green_div2 = $this->assertSession()->waitForElement('css', "#ajax_selected_color div:contains('green')");
Chris@0 90 $this->assertNotNull($green_div2, 'DOM update: After reload - the selected color DIV is green.');
Chris@0 91
Chris@0 92 $build_id_from_cache_first_ajax = $this->getFormBuildId();
Chris@0 93 $this->assertNotEquals($build_id_from_cache_initial, $build_id_from_cache_first_ajax, 'Build id is changed in the simpletest-DOM on first AJAX submission');
Chris@0 94 $this->assertNotEquals($build_id_first_ajax, $build_id_from_cache_first_ajax, 'Build id from first user is not reused');
Chris@0 95
Chris@0 96 // Changing the value of a select input element, triggers a AJAX
Chris@0 97 // request/response.
Chris@0 98 $session->getPage()->selectFieldOption('select', 'red');
Chris@0 99
Chris@0 100 // Wait for the DOM to update.
Chris@0 101 $red_div2 = $this->assertSession()->waitForElement('css', "#ajax_selected_color div:contains('red')");
Chris@0 102 $this->assertNotNull($red_div2, 'DOM update: After reload - the selected color DIV is red.');
Chris@0 103
Chris@0 104 $build_id_from_cache_second_ajax = $this->getFormBuildId();
Chris@0 105 $this->assertNotEquals($build_id_from_cache_first_ajax, $build_id_from_cache_second_ajax, 'Build id changes on subsequent AJAX submissions');
Chris@0 106
Chris@0 107 }
Chris@0 108
Chris@0 109 /**
Chris@0 110 * Tests that updating the text field trigger an AJAX request/response.
Chris@0 111 *
Chris@0 112 * @see \Drupal\system\Tests\Ajax\ElementValidationTest::testAjaxElementValidation()
Chris@0 113 */
Chris@0 114 public function testAjaxElementValidation() {
Chris@0 115 $this->drupalGet('ajax_validation_test');
Chris@0 116 // Changing the value of the textfield will trigger an AJAX
Chris@0 117 // request/response.
Chris@0 118 $this->getSession()->getPage()->fillField('drivertext', 'some dumb text');
Chris@0 119
Chris@0 120 // When the AJAX command updates the DOM a <ul> unsorted list
Chris@0 121 // "message__list" structure will appear on the page echoing back the
Chris@0 122 // "some dumb text" message.
Chris@0 123 $placeholder = $this->assertSession()->waitForElement('css', "ul.messages__list li.messages__item em:contains('some dumb text')");
Chris@0 124 $this->assertNotNull($placeholder, 'Message structure containing input data located.');
Chris@0 125 }
Chris@0 126
Chris@0 127 }