AjaxFormCacheTest.php in Drupal 9        
                          
                  
                        
  
  
  
  
File
  core/tests/Drupal/FunctionalJavascriptTests/Ajax/AjaxFormCacheTest.php
  
    View source  
  <?php
namespace Drupal\FunctionalJavascriptTests\Ajax;
use Drupal\Core\Url;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
class AjaxFormCacheTest extends WebDriverTestBase {
  
  protected static $modules = [
    'ajax_test',
    'ajax_forms_test',
  ];
  
  protected $defaultTheme = 'stark';
  
  public function testFormCacheUsage() {
    
    $key_value_expirable = \Drupal::service('keyvalue.expirable')
      ->get('form');
    $this
      ->drupalLogin($this->rootUser);
    
    $this
      ->assertCount(0, $key_value_expirable
      ->getAll());
    
    $uncached_form_url = Url::fromRoute('ajax_forms_test.commands_form');
    $this
      ->drupalGet($uncached_form_url);
    $this
      ->drupalGet($uncached_form_url);
    $this
      ->drupalGet($uncached_form_url);
    
    $this
      ->assertCount(0, $key_value_expirable
      ->getAll());
  }
  
  public function testBlockForms() {
    $this->container
      ->get('module_installer')
      ->install([
      'block',
      'search',
    ]);
    $this
      ->rebuildContainer();
    $this
      ->drupalLogin($this->rootUser);
    $this
      ->drupalPlaceBlock('search_form_block', [
      'weight' => -5,
    ]);
    $this
      ->drupalPlaceBlock('ajax_forms_test_block');
    $this
      ->drupalGet('');
    $session = $this
      ->getSession();
    
    $session
      ->getPage()
      ->selectFieldOption('edit-test1', 'option1');
    
    $opt1_selector = $this
      ->assertSession()
      ->waitForElement('css', "select[data-drupal-selector='edit-test1'] option:contains('Option 1!!!')");
    $this
      ->assertNotEmpty($opt1_selector);
    $this
      ->assertTrue($opt1_selector
      ->isSelected());
    
    $page = $session
      ->getPage();
    $opt3_selector = $page
      ->find('xpath', '//select[@data-drupal-selector="edit-test1"]//option[@value="option3"]');
    $this
      ->assertNotEmpty($opt3_selector);
    
    $page
      ->findButton('edit-submit')
      ->click();
    $this
      ->assertSession()
      ->waitForButton('edit-submit');
    $updated_page = $session
      ->getPage();
    $updated_page
      ->hasContent('Submission successful.');
  }
  
  public function testQueryString() {
    $this->container
      ->get('module_installer')
      ->install([
      'block',
    ]);
    $this
      ->drupalLogin($this->rootUser);
    $this
      ->drupalPlaceBlock('ajax_forms_test_block');
    $url = Url::fromRoute('entity.user.canonical', [
      'user' => $this->rootUser
        ->id(),
    ], [
      'query' => [
        'foo' => 'bar',
      ],
    ]);
    $this
      ->drupalGet($url);
    $session = $this
      ->getSession();
    
    $session
      ->getPage()
      ->selectFieldOption('edit-test1', 'option1');
    
    $opt1_selector = $this
      ->assertSession()
      ->waitForElement('css', "option:contains('Option 1!!!')");
    $this
      ->assertNotEmpty($opt1_selector);
    $url
      ->setOption('query', [
      'foo' => 'bar',
    ]);
    $this
      ->assertSession()
      ->addressEquals($url);
  }
}