comparison core/tests/Drupal/FunctionalJavascriptTests/Ajax/AjaxFormPageCacheTest.php @ 0:c75dbcec494b

Initial commit from drush-created site
author Chris Cannam
date Thu, 05 Jul 2018 14:24:15 +0000
parents
children a9cd425dd02b
comparison
equal deleted inserted replaced
-1:000000000000 0:c75dbcec494b
1 <?php
2
3 namespace Drupal\FunctionalJavascriptTests\Ajax;
4
5 use Drupal\FunctionalJavascriptTests\JavascriptTestBase;
6
7 /**
8 * Performs tests on AJAX forms in cached pages.
9 *
10 * @group Ajax
11 */
12 class AjaxFormPageCacheTest extends JavascriptTestBase {
13
14 /**
15 * {@inheritdoc}
16 */
17 public static $modules = ['ajax_test', 'ajax_forms_test'];
18
19 /**
20 * {@inheritdoc}
21 */
22 protected function setUp() {
23 parent::setUp();
24
25 $config = $this->config('system.performance');
26 $config->set('cache.page.max_age', 300);
27 $config->save();
28 }
29
30 /**
31 * Return the build id of the current form.
32 */
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();
37 }
38
39 /**
40 * Create a simple form, then submit the form via AJAX to change to it.
41 */
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();
46
47 // Changing the value of a select input element, triggers a AJAX
48 // request/response. The callback on the form responds with three AJAX
49 // commands:
50 // - UpdateBuildIdCommand
51 // - HtmlCommand
52 // - DataCommand
53 $session = $this->getSession();
54 $session->getPage()->selectFieldOption('select', 'green');
55
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.');
60
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');
64
65 // Changing the value of a select input element, triggers a AJAX
66 // request/response.
67 $session->getPage()->selectFieldOption('select', 'red');
68
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.');
72
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');
76
77 // Emulate a push of the reload button and then repeat the test sequence
78 // this time with a page loaded from the cache.
79 $session->reload();
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');
83
84 // Changing the value of a select input element, triggers a AJAX
85 // request/response.
86 $session->getPage()->selectFieldOption('select', 'green');
87
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.');
91
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');
95
96 // Changing the value of a select input element, triggers a AJAX
97 // request/response.
98 $session->getPage()->selectFieldOption('select', 'red');
99
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.');
103
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');
106
107 }
108
109 /**
110 * Tests that updating the text field trigger an AJAX request/response.
111 *
112 * @see \Drupal\system\Tests\Ajax\ElementValidationTest::testAjaxElementValidation()
113 */
114 public function testAjaxElementValidation() {
115 $this->drupalGet('ajax_validation_test');
116 // Changing the value of the textfield will trigger an AJAX
117 // request/response.
118 $this->getSession()->getPage()->fillField('drivertext', 'some dumb text');
119
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.');
125 }
126
127 }