View source
<?php
namespace Drupal\Tests\system\FunctionalJavascript;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
class FrameworkTest extends WebDriverTestBase {
protected static $modules = [
'node',
'ajax_test',
'ajax_forms_test',
];
protected $defaultTheme = 'classy';
public function testLazyLoad() {
$expected = [
'setting_name' => 'ajax_forms_test_lazy_load_form_submit',
'setting_value' => 'executed',
'library_1' => 'system/admin',
'library_2' => 'system/drupal.system',
];
$this
->drupalGet('ajax_forms_test_lazy_load_form');
$page = $this
->getSession()
->getPage();
$assert = $this
->assertSession();
$original_settings = $this
->getDrupalSettings();
$original_libraries = explode(',', $original_settings['ajaxPageState']['libraries']);
$this
->assertTrue(!isset($original_settings[$expected['setting_name']]), new FormattableMarkup('Page originally lacks the %setting, as expected.', [
'%setting' => $expected['setting_name'],
]));
$this
->assertNotContains($expected['library_1'], $original_libraries, new FormattableMarkup('Page originally lacks the %library library, as expected.', [
'%library' => $expected['library_1'],
]));
$this
->assertNotContains($expected['library_2'], $original_libraries, new FormattableMarkup('Page originally lacks the %library library, as expected.', [
'%library' => $expected['library_2'],
]));
$page
->pressButton('Submit');
$assert
->assertWaitOnAjaxRequest();
$new_settings = $this
->getDrupalSettings();
$new_libraries = explode(',', $new_settings['ajaxPageState']['libraries']);
$this
->assertTrue(!isset($new_settings[$expected['setting_name']]), new FormattableMarkup('Page still lacks the %setting, as expected.', [
'%setting' => $expected['setting_name'],
]));
$this
->assertNotContains($expected['library_1'], $new_libraries, new FormattableMarkup('Page still lacks the %library library, as expected.', [
'%library' => $expected['library_1'],
]));
$this
->assertNotContains($expected['library_2'], $new_libraries, new FormattableMarkup('Page still lacks the %library library, as expected.', [
'%library' => $expected['library_2'],
]));
$page
->checkField('add_files');
$page
->pressButton('Submit');
$assert
->assertWaitOnAjaxRequest();
$new_settings = $this
->getDrupalSettings();
$new_libraries = explode(',', $new_settings['ajaxPageState']['libraries']);
$this
->assertSame($expected['setting_value'], $new_settings[$expected['setting_name']], new FormattableMarkup('Page now has the %setting.', [
'%setting' => $expected['setting_name'],
]));
$this
->assertContains($expected['library_1'], $new_libraries, new FormattableMarkup('Page state now has the %library library.', [
'%library' => $expected['library_1'],
]));
$this
->assertContains($expected['library_2'], $new_libraries, new FormattableMarkup('Page state now has the %library library.', [
'%library' => $expected['library_2'],
]));
}
public function testCurrentPathChange() {
$this
->drupalGet('ajax_forms_test_lazy_load_form');
$page = $this
->getSession()
->getPage();
$assert = $this
->assertSession();
$old_settings = $this
->getDrupalSettings();
$page
->pressButton('Submit');
$assert
->assertWaitOnAjaxRequest();
$new_settings = $this
->getDrupalSettings();
$this
->assertEquals($old_settings['path']['currentPath'], $new_settings['path']['currentPath']);
}
public function testLazyLoadOverriddenCSS() {
\Drupal::service('theme_installer')
->install([
'test_theme',
]);
$this
->config('system.theme')
->set('default', 'test_theme')
->save();
$this
->drupalGet('ajax_forms_test_lazy_load_form');
$page = $this
->getSession()
->getPage();
$assert = $this
->assertSession();
$page
->checkField('add_files');
$page
->pressButton('Submit');
$assert
->assertWaitOnAjaxRequest();
$assert
->responseNotContains('js.module.css?');
}
}