11a4044e07aff15897ee83c259ba16cd827d6cac
[yaffs-website] / web / core / modules / system / src / Tests / Ajax / AjaxFormCacheTest.php
1 <?php
2
3 namespace Drupal\system\Tests\Ajax;
4
5 use Drupal\Core\EventSubscriber\MainContentViewSubscriber;
6 use Drupal\Core\Form\FormBuilderInterface;
7 use Drupal\Core\Url;
8
9 /**
10  * Tests the usage of form caching for AJAX forms.
11  *
12  * @group Ajax
13  */
14 class AjaxFormCacheTest extends AjaxTestBase {
15
16   /**
17    * Tests the usage of form cache for AJAX forms.
18    */
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);
23
24     // Ensure that the cache is empty.
25     $this->assertEqual(0, count($key_value_expirable->getAll()));
26
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);
32
33     // The number of cache entries should not have changed.
34     $this->assertEqual(0, count($key_value_expirable->getAll()));
35   }
36
37   /**
38    * Tests AJAX forms in blocks.
39    */
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);
45
46     $this->drupalPlaceBlock('search_form_block', ['weight' => -5]);
47     $this->drupalPlaceBlock('ajax_forms_test_block');
48
49     $this->drupalGet('');
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.');
55   }
56
57   /**
58    * Tests AJAX forms on pages with a query string.
59    */
60   public function testQueryString() {
61     $this->container->get('module_installer')->install(['block']);
62     $this->drupalLogin($this->rootUser);
63
64     $this->drupalPlaceBlock('ajax_forms_test_block');
65
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', [
70       'foo' => 'bar',
71       FormBuilderInterface::AJAX_FORM_REQUEST => 1,
72       MainContentViewSubscriber::WRAPPER_FORMAT => 'drupal_ajax',
73     ]);
74     $this->assertUrl($url);
75   }
76
77 }