AjaxFormPageCacheTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace Drupal\FunctionalJavascriptTests\Ajax;
  3. use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
  4. /**
  5. * Performs tests on AJAX forms in cached pages.
  6. *
  7. * @group Ajax
  8. */
  9. class AjaxFormPageCacheTest extends WebDriverTestBase {
  10. /**
  11. * {@inheritdoc}
  12. */
  13. public static $modules = ['ajax_test', 'ajax_forms_test'];
  14. /**
  15. * {@inheritdoc}
  16. */
  17. protected function setUp() {
  18. parent::setUp();
  19. $config = $this->config('system.performance');
  20. $config->set('cache.page.max_age', 300);
  21. $config->save();
  22. }
  23. /**
  24. * Return the build id of the current form.
  25. */
  26. protected function getFormBuildId() {
  27. $build_id_fields = $this->xpath('//input[@name="form_build_id"]');
  28. $this->assertEquals(count($build_id_fields), 1, 'One form build id field on the page');
  29. return $build_id_fields[0]->getValue();
  30. }
  31. /**
  32. * Create a simple form, then submit the form via AJAX to change to it.
  33. */
  34. public function testSimpleAJAXFormValue() {
  35. $this->drupalGet('ajax_forms_test_get_form');
  36. $build_id_initial = $this->getFormBuildId();
  37. // Changing the value of a select input element, triggers a AJAX
  38. // request/response. The callback on the form responds with three AJAX
  39. // commands:
  40. // - UpdateBuildIdCommand
  41. // - HtmlCommand
  42. // - DataCommand
  43. $session = $this->getSession();
  44. $session->getPage()->selectFieldOption('select', 'green');
  45. // Wait for the DOM to update. The HtmlCommand will update
  46. // #ajax_selected_color to reflect the color change.
  47. $green_span = $this->assertSession()->waitForElement('css', "#ajax_selected_color:contains('green')");
  48. $this->assertNotNull($green_span, 'DOM update: The selected color SPAN is green.');
  49. // Confirm the operation of the UpdateBuildIdCommand.
  50. $build_id_first_ajax = $this->getFormBuildId();
  51. $this->assertNotEquals($build_id_initial, $build_id_first_ajax, 'Build id is changed in the form_build_id element on first AJAX submission');
  52. // Changing the value of a select input element, triggers a AJAX
  53. // request/response.
  54. $session->getPage()->selectFieldOption('select', 'red');
  55. // Wait for the DOM to update.
  56. $red_span = $this->assertSession()->waitForElement('css', "#ajax_selected_color:contains('red')");
  57. $this->assertNotNull($red_span, 'DOM update: The selected color SPAN is red.');
  58. // Confirm the operation of the UpdateBuildIdCommand.
  59. $build_id_second_ajax = $this->getFormBuildId();
  60. $this->assertNotEquals($build_id_first_ajax, $build_id_second_ajax, 'Build id changes on subsequent AJAX submissions');
  61. // Emulate a push of the reload button and then repeat the test sequence
  62. // this time with a page loaded from the cache.
  63. $session->reload();
  64. $build_id_from_cache_initial = $this->getFormBuildId();
  65. $this->assertEquals($build_id_initial, $build_id_from_cache_initial, 'Build id is the same as on the first request');
  66. // Changing the value of a select input element, triggers a AJAX
  67. // request/response.
  68. $session->getPage()->selectFieldOption('select', 'green');
  69. // Wait for the DOM to update.
  70. $green_span2 = $this->assertSession()->waitForElement('css', "#ajax_selected_color:contains('green')");
  71. $this->assertNotNull($green_span2, 'DOM update: After reload - the selected color SPAN is green.');
  72. $build_id_from_cache_first_ajax = $this->getFormBuildId();
  73. $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');
  74. $this->assertNotEquals($build_id_first_ajax, $build_id_from_cache_first_ajax, 'Build id from first user is not reused');
  75. // Changing the value of a select input element, triggers a AJAX
  76. // request/response.
  77. $session->getPage()->selectFieldOption('select', 'red');
  78. // Wait for the DOM to update.
  79. $red_span2 = $this->assertSession()->waitForElement('css', "#ajax_selected_color:contains('red')");
  80. $this->assertNotNull($red_span2, 'DOM update: After reload - the selected color SPAN is red.');
  81. $build_id_from_cache_second_ajax = $this->getFormBuildId();
  82. $this->assertNotEquals($build_id_from_cache_first_ajax, $build_id_from_cache_second_ajax, 'Build id changes on subsequent AJAX submissions');
  83. }
  84. /**
  85. * Tests that updating the text field trigger an AJAX request/response.
  86. *
  87. * @see \Drupal\system\Tests\Ajax\ElementValidationTest::testAjaxElementValidation()
  88. */
  89. public function testAjaxElementValidation() {
  90. $this->drupalGet('ajax_validation_test');
  91. // Changing the value of the textfield will trigger an AJAX
  92. // request/response.
  93. $field = $this->getSession()->getPage()->findField('drivertext');
  94. $field->setValue('some dumb text');
  95. $field->blur();
  96. // When the AJAX command updates the DOM a <ul> unsorted list
  97. // "message__list" structure will appear on the page echoing back the
  98. // "some dumb text" message.
  99. $placeholder = $this->assertSession()->waitForElement('css', "ul.messages__list li.messages__item em:contains('some dumb text')");
  100. $this->assertNotNull($placeholder, 'Message structure containing input data located.');
  101. }
  102. }