3 namespace Drupal\FunctionalJavascriptTests\Ajax;
5 use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
8 * Performs tests on AJAX forms in cached pages.
12 class AjaxFormPageCacheTest extends WebDriverTestBase {
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 $build_id_initial = $this->getFormBuildId();
46 // Changing the value of a select input element, triggers a AJAX
47 // request/response. The callback on the form responds with three AJAX
49 // - UpdateBuildIdCommand
52 $session = $this->getSession();
53 $session->getPage()->selectFieldOption('select', 'green');
55 // Wait for the DOM to update. The HtmlCommand will update
56 // #ajax_selected_color to reflect the color change.
57 $green_span = $this->assertSession()->waitForElement('css', "#ajax_selected_color:contains('green')");
58 $this->assertNotNull($green_span, 'DOM update: The selected color SPAN is green.');
60 // Confirm the operation of the UpdateBuildIdCommand.
61 $build_id_first_ajax = $this->getFormBuildId();
62 $this->assertNotEquals($build_id_initial, $build_id_first_ajax, 'Build id is changed in the form_build_id element on first AJAX submission');
64 // Changing the value of a select input element, triggers a AJAX
66 $session->getPage()->selectFieldOption('select', 'red');
68 // Wait for the DOM to update.
69 $red_span = $this->assertSession()->waitForElement('css', "#ajax_selected_color:contains('red')");
70 $this->assertNotNull($red_span, 'DOM update: The selected color SPAN is red.');
72 // Confirm the operation of the UpdateBuildIdCommand.
73 $build_id_second_ajax = $this->getFormBuildId();
74 $this->assertNotEquals($build_id_first_ajax, $build_id_second_ajax, 'Build id changes on subsequent AJAX submissions');
76 // Emulate a push of the reload button and then repeat the test sequence
77 // this time with a page loaded from the cache.
79 $build_id_from_cache_initial = $this->getFormBuildId();
80 $this->assertEquals($build_id_initial, $build_id_from_cache_initial, 'Build id is the same as on the first request');
82 // Changing the value of a select input element, triggers a AJAX
84 $session->getPage()->selectFieldOption('select', 'green');
86 // Wait for the DOM to update.
87 $green_span2 = $this->assertSession()->waitForElement('css', "#ajax_selected_color:contains('green')");
88 $this->assertNotNull($green_span2, 'DOM update: After reload - the selected color SPAN is green.');
90 $build_id_from_cache_first_ajax = $this->getFormBuildId();
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');
92 $this->assertNotEquals($build_id_first_ajax, $build_id_from_cache_first_ajax, 'Build id from first user is not reused');
94 // Changing the value of a select input element, triggers a AJAX
96 $session->getPage()->selectFieldOption('select', 'red');
98 // Wait for the DOM to update.
99 $red_span2 = $this->assertSession()->waitForElement('css', "#ajax_selected_color:contains('red')");
100 $this->assertNotNull($red_span2, 'DOM update: After reload - the selected color SPAN is red.');
102 $build_id_from_cache_second_ajax = $this->getFormBuildId();
103 $this->assertNotEquals($build_id_from_cache_first_ajax, $build_id_from_cache_second_ajax, 'Build id changes on subsequent AJAX submissions');
108 * Tests that updating the text field trigger an AJAX request/response.
110 * @see \Drupal\system\Tests\Ajax\ElementValidationTest::testAjaxElementValidation()
112 public function testAjaxElementValidation() {
113 $this->drupalGet('ajax_validation_test');
114 // Changing the value of the textfield will trigger an AJAX
116 $field = $this->getSession()->getPage()->findField('drivertext');
117 $field->setValue('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.');