Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / tests / Drupal / FunctionalJavascriptTests / Ajax / AjaxFormCacheTest.php
1 <?php
2
3 namespace Drupal\FunctionalJavascriptTests\Ajax;
4
5 use Drupal\Core\EventSubscriber\MainContentViewSubscriber;
6 use Drupal\Core\Form\FormBuilderInterface;
7 use Drupal\Core\Url;
8 use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
9
10 /**
11  * Tests the usage of form caching for AJAX forms.
12  *
13  * @group Ajax
14  */
15 class AjaxFormCacheTest extends WebDriverTestBase {
16
17   /**
18    * {@inheritdoc}
19    */
20   public static $modules = ['ajax_test', 'ajax_forms_test'];
21
22   /**
23    * Tests the usage of form cache for AJAX forms.
24    */
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);
29
30     // Ensure that the cache is empty.
31     $this->assertEqual(0, count($key_value_expirable->getAll()));
32
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);
38
39     // The number of cache entries should not have changed.
40     $this->assertEqual(0, count($key_value_expirable->getAll()));
41   }
42
43   /**
44    * Tests AJAX forms in blocks.
45    */
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);
51
52     $this->drupalPlaceBlock('search_form_block', ['weight' => -5]);
53     $this->drupalPlaceBlock('ajax_forms_test_block');
54
55     $this->drupalGet('');
56     $session = $this->getSession();
57
58     // Select first option and trigger ajax update.
59     $session->getPage()->selectFieldOption('edit-test1', 'option1');
60
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());
66
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);
71
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.');
77   }
78
79   /**
80    * Tests AJAX forms on pages with a query string.
81    */
82   public function testQueryString() {
83     $this->container->get('module_installer')->install(['block']);
84     $this->drupalLogin($this->rootUser);
85
86     $this->drupalPlaceBlock('ajax_forms_test_block');
87
88     $url = Url::fromRoute('entity.user.canonical', ['user' => $this->rootUser->id()], ['query' => ['foo' => 'bar']]);
89     $this->drupalGet($url);
90
91     $session = $this->getSession();
92     // Select first option and trigger ajax update.
93     $session->getPage()->selectFieldOption('edit-test1', 'option1');
94
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);
99
100     $url->setOption('query', [
101       'foo' => 'bar',
102       FormBuilderInterface::AJAX_FORM_REQUEST => 1,
103       MainContentViewSubscriber::WRAPPER_FORMAT => 'drupal_ajax',
104     ]);
105     $this->assertUrl($url);
106   }
107
108 }