AjaxFormPageCacheTest.php 5.1 KB

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