3 namespace Drupal\FunctionalJavascriptTests\Ajax;
5 use Drupal\FunctionalJavascriptTests\JavascriptTestBase;
8 * Performs tests on AJAX forms in cached pages.
12 class AjaxFormPageCacheTest extends JavascriptTestBase {
17 public static $modules = ['ajax_test', 'ajax_forms_test'];
22 protected function setUp() {
25 $config = $this->config('system.performance');
26 $config->set('cache.page.max_age', 300);
31 * Return the build id of the current form.
33 protected function getFormBuildId() {
34 $build_id_fields = $this->xpath('//input[@name="form_build_id"]');
35 $this->assertEquals(count($build_id_fields), 1, 'One form build id field on the page');
36 return $build_id_fields[0]->getValue();
40 * Create a simple form, then submit the form via AJAX to change to it.
42 public function testSimpleAJAXFormValue() {
43 $this->drupalGet('ajax_forms_test_get_form');
44 $this->assertEquals($this->drupalGetHeader('X-Drupal-Cache'), 'MISS', 'Page was not cached.');
45 $build_id_initial = $this->getFormBuildId();
47 // Changing the value of a select input element, triggers a AJAX
48 // request/response. The callback on the form responds with three AJAX
50 // - UpdateBuildIdCommand
53 $session = $this->getSession();
54 $session->getPage()->selectFieldOption('select', 'green');
56 // Wait for the DOM to update. The HtmlCommand will update
57 // #ajax_selected_color to reflect the color change.
58 $green_div = $this->assertSession()->waitForElement('css', "#ajax_selected_color div:contains('green')");
59 $this->assertNotNull($green_div, 'DOM update: The selected color DIV is green.');
61 // Confirm the operation of the UpdateBuildIdCommand.
62 $build_id_first_ajax = $this->getFormBuildId();
63 $this->assertNotEquals($build_id_initial, $build_id_first_ajax, 'Build id is changed in the form_build_id element on first AJAX submission');
65 // Changing the value of a select input element, triggers a AJAX
67 $session->getPage()->selectFieldOption('select', 'red');
69 // Wait for the DOM to update.
70 $red_div = $this->assertSession()->waitForElement('css', "#ajax_selected_color div:contains('red')");
71 $this->assertNotNull($red_div, 'DOM update: The selected color DIV is red.');
73 // Confirm the operation of the UpdateBuildIdCommand.
74 $build_id_second_ajax = $this->getFormBuildId();
75 $this->assertNotEquals($build_id_first_ajax, $build_id_second_ajax, 'Build id changes on subsequent AJAX submissions');
77 // Emulate a push of the reload button and then repeat the test sequence
78 // this time with a page loaded from the cache.
80 $this->assertEquals($this->drupalGetHeader('X-Drupal-Cache'), 'HIT', 'Page was cached.');
81 $build_id_from_cache_initial = $this->getFormBuildId();
82 $this->assertEquals($build_id_initial, $build_id_from_cache_initial, 'Build id is the same as on the first request');
84 // Changing the value of a select input element, triggers a AJAX
86 $session->getPage()->selectFieldOption('select', 'green');
88 // Wait for the DOM to update.
89 $green_div2 = $this->assertSession()->waitForElement('css', "#ajax_selected_color div:contains('green')");
90 $this->assertNotNull($green_div2, 'DOM update: After reload - the selected color DIV is green.');
92 $build_id_from_cache_first_ajax = $this->getFormBuildId();
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');
94 $this->assertNotEquals($build_id_first_ajax, $build_id_from_cache_first_ajax, 'Build id from first user is not reused');
96 // Changing the value of a select input element, triggers a AJAX
98 $session->getPage()->selectFieldOption('select', 'red');
100 // Wait for the DOM to update.
101 $red_div2 = $this->assertSession()->waitForElement('css', "#ajax_selected_color div:contains('red')");
102 $this->assertNotNull($red_div2, 'DOM update: After reload - the selected color DIV is red.');
104 $build_id_from_cache_second_ajax = $this->getFormBuildId();
105 $this->assertNotEquals($build_id_from_cache_first_ajax, $build_id_from_cache_second_ajax, 'Build id changes on subsequent AJAX submissions');
110 * Tests that updating the text field trigger an AJAX request/response.
112 * @see \Drupal\system\Tests\Ajax\ElementValidationTest::testAjaxElementValidation()
114 public function testAjaxElementValidation() {
115 $this->drupalGet('ajax_validation_test');
116 // Changing the value of the textfield will trigger an AJAX
118 $this->getSession()->getPage()->fillField('drivertext', 'some dumb text');
120 // When the AJAX command updates the DOM a <ul> unsorted list
121 // "message__list" structure will appear on the page echoing back the
122 // "some dumb text" message.
123 $placeholder = $this->assertSession()->waitForElement('css', "ul.messages__list li.messages__item em:contains('some dumb text')");
124 $this->assertNotNull($placeholder, 'Message structure containing input data located.');