3 namespace Drupal\FunctionalJavascriptTests\Ajax;
5 use Drupal\Core\EventSubscriber\MainContentViewSubscriber;
6 use Drupal\Core\Form\FormBuilderInterface;
8 use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
11 * Tests the usage of form caching for AJAX forms.
15 class AjaxFormCacheTest extends WebDriverTestBase {
20 public static $modules = ['ajax_test', 'ajax_forms_test'];
23 * Tests the usage of form cache for AJAX forms.
25 public function testFormCacheUsage() {
26 /** @var \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface $key_value_expirable */
27 $key_value_expirable = \Drupal::service('keyvalue.expirable')->get('form');
28 $this->drupalLogin($this->rootUser);
30 // Ensure that the cache is empty.
31 $this->assertEqual(0, count($key_value_expirable->getAll()));
33 // Visit an AJAX form that is not cached, 3 times.
34 $uncached_form_url = Url::fromRoute('ajax_forms_test.commands_form');
35 $this->drupalGet($uncached_form_url);
36 $this->drupalGet($uncached_form_url);
37 $this->drupalGet($uncached_form_url);
39 // The number of cache entries should not have changed.
40 $this->assertEqual(0, count($key_value_expirable->getAll()));
44 * Tests AJAX forms in blocks.
46 public function testBlockForms() {
47 $this->container->get('module_installer')->install(['block', 'search']);
48 $this->rebuildContainer();
49 $this->container->get('router.builder')->rebuild();
50 $this->drupalLogin($this->rootUser);
52 $this->drupalPlaceBlock('search_form_block', ['weight' => -5]);
53 $this->drupalPlaceBlock('ajax_forms_test_block');
56 $session = $this->getSession();
58 // Select first option and trigger ajax update.
59 $session->getPage()->selectFieldOption('edit-test1', 'option1');
61 // DOM update: The InsertCommand in the AJAX response changes the text
62 // in the option element to 'Option1!!!'.
63 $opt1_selector = $this->assertSession()->waitForElement('css', "select[data-drupal-selector='edit-test1'] option:contains('Option 1!!!')");
64 $this->assertNotEmpty($opt1_selector);
65 $this->assertTrue($opt1_selector->isSelected());
67 // Confirm option 3 exists.
68 $page = $session->getPage();
69 $opt3_selector = $page->find('xpath', '//select[@data-drupal-selector="edit-test1"]//option[@value="option3"]');
70 $this->assertNotEmpty($opt3_selector);
72 // Confirm success message appears after a submit.
73 $page->findButton('edit-submit')->click();
74 $this->assertSession()->waitForButton('edit-submit');
75 $updated_page = $session->getPage();
76 $updated_page->hasContent('Submission successful.');
80 * Tests AJAX forms on pages with a query string.
82 public function testQueryString() {
83 $this->container->get('module_installer')->install(['block']);
84 $this->drupalLogin($this->rootUser);
86 $this->drupalPlaceBlock('ajax_forms_test_block');
88 $url = Url::fromRoute('entity.user.canonical', ['user' => $this->rootUser->id()], ['query' => ['foo' => 'bar']]);
89 $this->drupalGet($url);
91 $session = $this->getSession();
92 // Select first option and trigger ajax update.
93 $session->getPage()->selectFieldOption('edit-test1', 'option1');
95 // DOM update: The InsertCommand in the AJAX response changes the text
96 // in the option element to 'Option1!!!'.
97 $opt1_selector = $this->assertSession()->waitForElement('css', "option:contains('Option 1!!!')");
98 $this->assertNotEmpty($opt1_selector);
100 $url->setOption('query', [
102 FormBuilderInterface::AJAX_FORM_REQUEST => 1,
103 MainContentViewSubscriber::WRAPPER_FORMAT => 'drupal_ajax',
105 $this->assertUrl($url);