3 namespace Drupal\Tests\system\FunctionalJavascript;
5 use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
8 * Tests the off-canvas dialog functionality.
12 class FrameworkTest extends WebDriverTestBase {
17 protected static $modules = ['node', 'ajax_test', 'ajax_forms_test'];
20 * Tests that new JavaScript and CSS files are lazy-loaded on an AJAX request.
22 public function testLazyLoad() {
24 'setting_name' => 'ajax_forms_test_lazy_load_form_submit',
25 'setting_value' => 'executed',
26 'library_1' => 'system/admin',
27 'library_2' => 'system/drupal.system',
31 $this->drupalGet('ajax_forms_test_lazy_load_form');
32 $page = $this->getSession()->getPage();
33 $assert = $this->assertSession();
35 $original_settings = $this->getDrupalSettings();
36 $original_libraries = explode(',', $original_settings['ajaxPageState']['libraries']);
38 // Verify that the base page doesn't have the settings and files that are to
39 // be lazy loaded as part of the next requests.
40 $this->assertTrue(!isset($original_settings[$expected['setting_name']]), format_string('Page originally lacks the %setting, as expected.', ['%setting' => $expected['setting_name']]));
41 $this->assertTrue(!in_array($expected['library_1'], $original_libraries), format_string('Page originally lacks the %library library, as expected.', ['%library' => $expected['library_1']]));
42 $this->assertTrue(!in_array($expected['library_2'], $original_libraries), format_string('Page originally lacks the %library library, as expected.', ['%library' => $expected['library_2']]));
44 // Submit the AJAX request without triggering files getting added.
45 $page->pressButton('Submit');
46 $assert->assertWaitOnAjaxRequest();
47 $new_settings = $this->getDrupalSettings();
48 $new_libraries = explode(',', $new_settings['ajaxPageState']['libraries']);
50 // Verify the setting was not added when not expected.
51 $this->assertTrue(!isset($new_settings[$expected['setting_name']]), format_string('Page still lacks the %setting, as expected.', ['%setting' => $expected['setting_name']]));
52 $this->assertTrue(!in_array($expected['library_1'], $new_libraries), format_string('Page still lacks the %library library, as expected.', ['%library' => $expected['library_1']]));
53 $this->assertTrue(!in_array($expected['library_2'], $new_libraries), format_string('Page still lacks the %library library, as expected.', ['%library' => $expected['library_2']]));
55 // Submit the AJAX request and trigger adding files.
56 $page->checkField('add_files');
57 $page->pressButton('Submit');
58 $assert->assertWaitOnAjaxRequest();
59 $new_settings = $this->getDrupalSettings();
60 $new_libraries = explode(',', $new_settings['ajaxPageState']['libraries']);
62 // Verify the expected setting was added, both to drupalSettings, and as
63 // the first AJAX command.
64 $this->assertIdentical($new_settings[$expected['setting_name']], $expected['setting_value'], format_string('Page now has the %setting.', ['%setting' => $expected['setting_name']]));
66 // Verify the expected CSS file was added, both to drupalSettings, and as
67 // the second AJAX command for inclusion into the HTML.
68 $this->assertTrue(in_array($expected['library_1'], $new_libraries), format_string('Page state now has the %library library.', ['%library' => $expected['library_1']]));
70 // Verify the expected JS file was added, both to drupalSettings, and as
71 // the third AJAX command for inclusion into the HTML. By testing for an
72 // exact HTML string containing the SCRIPT tag, we also ensure that
73 // unexpected JavaScript code, such as a jQuery.extend() that would
74 // potentially clobber rather than properly merge settings, didn't
75 // accidentally get added.
76 $this->assertTrue(in_array($expected['library_2'], $new_libraries), format_string('Page state now has the %library library.', ['%library' => $expected['library_2']]));
80 * Tests that drupalSettings.currentPath is not updated on AJAX requests.
82 public function testCurrentPathChange() {
83 $this->drupalGet('ajax_forms_test_lazy_load_form');
84 $page = $this->getSession()->getPage();
85 $assert = $this->assertSession();
87 $old_settings = $this->getDrupalSettings();
88 $page->pressButton('Submit');
89 $assert->assertWaitOnAjaxRequest();
90 $new_settings = $this->getDrupalSettings();
91 $this->assertEquals($old_settings['path']['currentPath'], $new_settings['path']['currentPath']);
95 * Tests that overridden CSS files are not added during lazy load.
97 public function testLazyLoadOverriddenCSS() {
98 // The test theme overrides js.module.css without an implementation,
99 // thereby removing it.
100 \Drupal::service('theme_handler')->install(['test_theme']);
101 $this->config('system.theme')
102 ->set('default', 'test_theme')
105 // This gets the form, and does an Ajax submission on it.
106 $this->drupalGet('ajax_forms_test_lazy_load_form');
107 $page = $this->getSession()->getPage();
108 $assert = $this->assertSession();
110 $page->checkField('add_files');
111 $page->pressButton('Submit');
112 $assert->assertWaitOnAjaxRequest();
114 // Verify that the resulting HTML does not load the overridden CSS file.
115 // We add a "?" to the assertion, because drupalSettings may include
116 // information about the file; we only really care about whether it appears
117 // in a LINK or STYLE tag, for which Drupal always adds a query string for
119 $assert->responseNotContains('js.module.css?', 'Ajax lazy loading does not add overridden CSS files.');