3 namespace Drupal\system\Tests\Ajax;
5 use Drupal\Core\EventSubscriber\MainContentViewSubscriber;
6 use Drupal\Core\Form\FormBuilderInterface;
10 * Tests the usage of form caching for AJAX forms.
14 class AjaxFormCacheTest extends AjaxTestBase {
17 * Tests the usage of form cache for AJAX forms.
19 public function testFormCacheUsage() {
20 /** @var \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface $key_value_expirable */
21 $key_value_expirable = \Drupal::service('keyvalue.expirable')->get('form');
22 $this->drupalLogin($this->rootUser);
24 // Ensure that the cache is empty.
25 $this->assertEqual(0, count($key_value_expirable->getAll()));
27 // Visit an AJAX form that is not cached, 3 times.
28 $uncached_form_url = Url::fromRoute('ajax_forms_test.commands_form');
29 $this->drupalGet($uncached_form_url);
30 $this->drupalGet($uncached_form_url);
31 $this->drupalGet($uncached_form_url);
33 // The number of cache entries should not have changed.
34 $this->assertEqual(0, count($key_value_expirable->getAll()));
38 * Tests AJAX forms in blocks.
40 public function testBlockForms() {
41 $this->container->get('module_installer')->install(['block', 'search']);
42 $this->rebuildContainer();
43 $this->container->get('router.builder')->rebuild();
44 $this->drupalLogin($this->rootUser);
46 $this->drupalPlaceBlock('search_form_block', ['weight' => -5]);
47 $this->drupalPlaceBlock('ajax_forms_test_block');
50 $this->drupalPostAjaxForm(NULL, ['test1' => 'option1'], 'test1');
51 $this->assertOptionSelectedWithDrupalSelector('edit-test1', 'option1');
52 $this->assertOptionWithDrupalSelector('edit-test1', 'option3');
53 $this->drupalPostForm(NULL, ['test1' => 'option1'], 'Submit');
54 $this->assertText('Submission successful.');
58 * Tests AJAX forms on pages with a query string.
60 public function testQueryString() {
61 $this->container->get('module_installer')->install(['block']);
62 $this->drupalLogin($this->rootUser);
64 $this->drupalPlaceBlock('ajax_forms_test_block');
66 $url = Url::fromRoute('entity.user.canonical', ['user' => $this->rootUser->id()], ['query' => ['foo' => 'bar']]);
67 $this->drupalGet($url);
68 $this->drupalPostAjaxForm(NULL, ['test1' => 'option1'], 'test1');
69 $url->setOption('query', [
71 FormBuilderInterface::AJAX_FORM_REQUEST => 1,
72 MainContentViewSubscriber::WRAPPER_FORMAT => 'drupal_ajax',
74 $this->assertUrl($url);