annotate core/tests/Drupal/FunctionalJavascriptTests/Ajax/AjaxFormPageCacheTest.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\FunctionalJavascriptTests\Ajax;
Chris@0 4
Chris@17 5 use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
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@17 12 class AjaxFormPageCacheTest extends WebDriverTestBase {
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 $build_id_initial = $this->getFormBuildId();
Chris@0 45
Chris@0 46 // Changing the value of a select input element, triggers a AJAX
Chris@0 47 // request/response. The callback on the form responds with three AJAX
Chris@0 48 // commands:
Chris@0 49 // - UpdateBuildIdCommand
Chris@0 50 // - HtmlCommand
Chris@0 51 // - DataCommand
Chris@0 52 $session = $this->getSession();
Chris@0 53 $session->getPage()->selectFieldOption('select', 'green');
Chris@0 54
Chris@0 55 // Wait for the DOM to update. The HtmlCommand will update
Chris@0 56 // #ajax_selected_color to reflect the color change.
Chris@17 57 $green_span = $this->assertSession()->waitForElement('css', "#ajax_selected_color:contains('green')");
Chris@17 58 $this->assertNotNull($green_span, 'DOM update: The selected color SPAN is green.');
Chris@0 59
Chris@0 60 // Confirm the operation of the UpdateBuildIdCommand.
Chris@0 61 $build_id_first_ajax = $this->getFormBuildId();
Chris@0 62 $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 63
Chris@0 64 // Changing the value of a select input element, triggers a AJAX
Chris@0 65 // request/response.
Chris@0 66 $session->getPage()->selectFieldOption('select', 'red');
Chris@0 67
Chris@0 68 // Wait for the DOM to update.
Chris@17 69 $red_span = $this->assertSession()->waitForElement('css', "#ajax_selected_color:contains('red')");
Chris@17 70 $this->assertNotNull($red_span, 'DOM update: The selected color SPAN is red.');
Chris@0 71
Chris@0 72 // Confirm the operation of the UpdateBuildIdCommand.
Chris@0 73 $build_id_second_ajax = $this->getFormBuildId();
Chris@0 74 $this->assertNotEquals($build_id_first_ajax, $build_id_second_ajax, 'Build id changes on subsequent AJAX submissions');
Chris@0 75
Chris@0 76 // Emulate a push of the reload button and then repeat the test sequence
Chris@0 77 // this time with a page loaded from the cache.
Chris@0 78 $session->reload();
Chris@0 79 $build_id_from_cache_initial = $this->getFormBuildId();
Chris@0 80 $this->assertEquals($build_id_initial, $build_id_from_cache_initial, 'Build id is the same as on the first request');
Chris@0 81
Chris@0 82 // Changing the value of a select input element, triggers a AJAX
Chris@0 83 // request/response.
Chris@0 84 $session->getPage()->selectFieldOption('select', 'green');
Chris@0 85
Chris@0 86 // Wait for the DOM to update.
Chris@17 87 $green_span2 = $this->assertSession()->waitForElement('css', "#ajax_selected_color:contains('green')");
Chris@17 88 $this->assertNotNull($green_span2, 'DOM update: After reload - the selected color SPAN is green.');
Chris@0 89
Chris@0 90 $build_id_from_cache_first_ajax = $this->getFormBuildId();
Chris@0 91 $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 92 $this->assertNotEquals($build_id_first_ajax, $build_id_from_cache_first_ajax, 'Build id from first user is not reused');
Chris@0 93
Chris@0 94 // Changing the value of a select input element, triggers a AJAX
Chris@0 95 // request/response.
Chris@0 96 $session->getPage()->selectFieldOption('select', 'red');
Chris@0 97
Chris@0 98 // Wait for the DOM to update.
Chris@17 99 $red_span2 = $this->assertSession()->waitForElement('css', "#ajax_selected_color:contains('red')");
Chris@17 100 $this->assertNotNull($red_span2, 'DOM update: After reload - the selected color SPAN is red.');
Chris@0 101
Chris@0 102 $build_id_from_cache_second_ajax = $this->getFormBuildId();
Chris@0 103 $this->assertNotEquals($build_id_from_cache_first_ajax, $build_id_from_cache_second_ajax, 'Build id changes on subsequent AJAX submissions');
Chris@0 104
Chris@0 105 }
Chris@0 106
Chris@0 107 /**
Chris@0 108 * Tests that updating the text field trigger an AJAX request/response.
Chris@0 109 *
Chris@0 110 * @see \Drupal\system\Tests\Ajax\ElementValidationTest::testAjaxElementValidation()
Chris@0 111 */
Chris@0 112 public function testAjaxElementValidation() {
Chris@0 113 $this->drupalGet('ajax_validation_test');
Chris@0 114 // Changing the value of the textfield will trigger an AJAX
Chris@0 115 // request/response.
Chris@17 116 $field = $this->getSession()->getPage()->findField('drivertext');
Chris@17 117 $field->setValue('some dumb text');
Chris@17 118 $field->blur();
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 }