AjaxFormCacheTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace Drupal\FunctionalJavascriptTests\Ajax;
  3. use Drupal\Core\EventSubscriber\MainContentViewSubscriber;
  4. use Drupal\Core\Form\FormBuilderInterface;
  5. use Drupal\Core\Url;
  6. use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
  7. /**
  8. * Tests the usage of form caching for AJAX forms.
  9. *
  10. * @group Ajax
  11. */
  12. class AjaxFormCacheTest extends WebDriverTestBase {
  13. /**
  14. * {@inheritdoc}
  15. */
  16. public static $modules = ['ajax_test', 'ajax_forms_test'];
  17. /**
  18. * {@inheritdoc}
  19. */
  20. protected $defaultTheme = 'stark';
  21. /**
  22. * Tests the usage of form cache for AJAX forms.
  23. */
  24. public function testFormCacheUsage() {
  25. /** @var \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface $key_value_expirable */
  26. $key_value_expirable = \Drupal::service('keyvalue.expirable')->get('form');
  27. $this->drupalLogin($this->rootUser);
  28. // Ensure that the cache is empty.
  29. $this->assertCount(0, $key_value_expirable->getAll());
  30. // Visit an AJAX form that is not cached, 3 times.
  31. $uncached_form_url = Url::fromRoute('ajax_forms_test.commands_form');
  32. $this->drupalGet($uncached_form_url);
  33. $this->drupalGet($uncached_form_url);
  34. $this->drupalGet($uncached_form_url);
  35. // The number of cache entries should not have changed.
  36. $this->assertCount(0, $key_value_expirable->getAll());
  37. }
  38. /**
  39. * Tests AJAX forms in blocks.
  40. */
  41. public function testBlockForms() {
  42. $this->container->get('module_installer')->install(['block', 'search']);
  43. $this->rebuildContainer();
  44. $this->container->get('router.builder')->rebuild();
  45. $this->drupalLogin($this->rootUser);
  46. $this->drupalPlaceBlock('search_form_block', ['weight' => -5]);
  47. $this->drupalPlaceBlock('ajax_forms_test_block');
  48. $this->drupalGet('');
  49. $session = $this->getSession();
  50. // Select first option and trigger ajax update.
  51. $session->getPage()->selectFieldOption('edit-test1', 'option1');
  52. // DOM update: The InsertCommand in the AJAX response changes the text
  53. // in the option element to 'Option1!!!'.
  54. $opt1_selector = $this->assertSession()->waitForElement('css', "select[data-drupal-selector='edit-test1'] option:contains('Option 1!!!')");
  55. $this->assertNotEmpty($opt1_selector);
  56. $this->assertTrue($opt1_selector->isSelected());
  57. // Confirm option 3 exists.
  58. $page = $session->getPage();
  59. $opt3_selector = $page->find('xpath', '//select[@data-drupal-selector="edit-test1"]//option[@value="option3"]');
  60. $this->assertNotEmpty($opt3_selector);
  61. // Confirm success message appears after a submit.
  62. $page->findButton('edit-submit')->click();
  63. $this->assertSession()->waitForButton('edit-submit');
  64. $updated_page = $session->getPage();
  65. $updated_page->hasContent('Submission successful.');
  66. }
  67. /**
  68. * Tests AJAX forms on pages with a query string.
  69. */
  70. public function testQueryString() {
  71. $this->container->get('module_installer')->install(['block']);
  72. $this->drupalLogin($this->rootUser);
  73. $this->drupalPlaceBlock('ajax_forms_test_block');
  74. $url = Url::fromRoute('entity.user.canonical', ['user' => $this->rootUser->id()], ['query' => ['foo' => 'bar']]);
  75. $this->drupalGet($url);
  76. $session = $this->getSession();
  77. // Select first option and trigger ajax update.
  78. $session->getPage()->selectFieldOption('edit-test1', 'option1');
  79. // DOM update: The InsertCommand in the AJAX response changes the text
  80. // in the option element to 'Option1!!!'.
  81. $opt1_selector = $this->assertSession()->waitForElement('css', "option:contains('Option 1!!!')");
  82. $this->assertNotEmpty($opt1_selector);
  83. $url->setOption('query', [
  84. 'foo' => 'bar',
  85. FormBuilderInterface::AJAX_FORM_REQUEST => 1,
  86. MainContentViewSubscriber::WRAPPER_FORMAT => 'drupal_ajax',
  87. ]);
  88. $this->assertUrl($url);
  89. }
  90. }