Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / tests / Drupal / FunctionalJavascriptTests / Ajax / AjaxFormPageCacheTest.php
1 <?php
2
3 namespace Drupal\FunctionalJavascriptTests\Ajax;
4
5 use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
6
7 /**
8  * Performs tests on AJAX forms in cached pages.
9  *
10  * @group Ajax
11  */
12 class AjaxFormPageCacheTest extends WebDriverTestBase {
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     $build_id_initial = $this->getFormBuildId();
45
46     // Changing the value of a select input element, triggers a AJAX
47     // request/response. The callback on the form responds with three AJAX
48     // commands:
49     // - UpdateBuildIdCommand
50     // - HtmlCommand
51     // - DataCommand
52     $session = $this->getSession();
53     $session->getPage()->selectFieldOption('select', 'green');
54
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.');
59
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');
63
64     // Changing the value of a select input element, triggers a AJAX
65     // request/response.
66     $session->getPage()->selectFieldOption('select', 'red');
67
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.');
71
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');
75
76     // Emulate a push of the reload button and then repeat the test sequence
77     // this time with a page loaded from the cache.
78     $session->reload();
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');
81
82     // Changing the value of a select input element, triggers a AJAX
83     // request/response.
84     $session->getPage()->selectFieldOption('select', 'green');
85
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.');
89
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');
93
94     // Changing the value of a select input element, triggers a AJAX
95     // request/response.
96     $session->getPage()->selectFieldOption('select', 'red');
97
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.');
101
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');
104
105   }
106
107   /**
108    * Tests that updating the text field trigger an AJAX request/response.
109    *
110    * @see \Drupal\system\Tests\Ajax\ElementValidationTest::testAjaxElementValidation()
111    */
112   public function testAjaxElementValidation() {
113     $this->drupalGet('ajax_validation_test');
114     // Changing the value of the textfield will trigger an AJAX
115     // request/response.
116     $field = $this->getSession()->getPage()->findField('drivertext');
117     $field->setValue('some dumb text');
118     $field->blur();
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 }