annotate core/tests/Drupal/FunctionalJavascriptTests/Ajax/AjaxFormCacheTest.php @ 19:fa3358dc1485 tip

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