function FrameworkTest::testLazyLoad

Same name in other branches
  1. 9 core/modules/system/tests/src/FunctionalJavascript/FrameworkTest.php \Drupal\Tests\system\FunctionalJavascript\FrameworkTest::testLazyLoad()
  2. 8.9.x core/modules/system/tests/src/FunctionalJavascript/FrameworkTest.php \Drupal\Tests\system\FunctionalJavascript\FrameworkTest::testLazyLoad()
  3. 10 core/modules/system/tests/src/FunctionalJavascript/FrameworkTest.php \Drupal\Tests\system\FunctionalJavascript\FrameworkTest::testLazyLoad()

Tests that new JavaScript and CSS files are lazy-loaded on an AJAX request.

File

core/modules/system/tests/src/FunctionalJavascript/FrameworkTest.php, line 29

Class

FrameworkTest
Tests the off-canvas dialog functionality.

Namespace

Drupal\Tests\system\FunctionalJavascript

Code

public function testLazyLoad() : void {
    $expected = [
        'setting_name' => 'ajax_forms_test_lazy_load_form_submit',
        'setting_value' => 'executed',
        'library_1' => 'system/admin',
        'library_2' => 'system/drupal.system',
    ];
    // Get the base page.
    $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']);
    // Verify that the base page doesn't have the settings and files that are to
    // be lazy loaded as part of the next requests.
    $this->assertTrue(!isset($original_settings[$expected['setting_name']]), "Page originally lacks the {$expected['setting_name']}, as expected.");
    $this->assertNotContains($expected['library_1'], $original_libraries, "Page originally lacks the {$expected['library_1']} library, as expected.");
    $this->assertNotContains($expected['library_2'], $original_libraries, "Page originally lacks the {$expected['library_2']} library, as expected.");
    // Submit the AJAX request without triggering files getting added.
    $page->pressButton('Submit');
    $assert->assertWaitOnAjaxRequest();
    $new_settings = $this->getDrupalSettings();
    $new_libraries = explode(',', $new_settings['ajaxPageState']['libraries']);
    // Verify the setting was not added when not expected.
    $this->assertTrue(!isset($new_settings[$expected['setting_name']]), "Page still lacks the {$expected['setting_name']}, as expected.");
    $this->assertNotContains($expected['library_1'], $new_libraries, "Page still lacks the {$expected['library_1']} library, as expected.");
    $this->assertNotContains($expected['library_2'], $new_libraries, "Page still lacks the {$expected['library_2']} library, as expected.");
    // Submit the AJAX request and trigger adding files.
    $page->checkField('add_files');
    $page->pressButton('Submit');
    $assert->assertWaitOnAjaxRequest();
    $new_settings = $this->getDrupalSettings();
    $new_libraries = explode(',', $new_settings['ajaxPageState']['libraries']);
    // Verify the expected setting was added, both to drupalSettings, and as
    // the first AJAX command.
    $this->assertSame($expected['setting_value'], $new_settings[$expected['setting_name']], "Page now has the {$expected['setting_name']}.");
    // Verify the expected CSS file was added, both to drupalSettings, and as
    // the second AJAX command for inclusion into the HTML.
    $this->assertContains($expected['library_1'], $new_libraries, "Page state now has the {$expected['library_1']} library.");
    // Verify the expected JS file was added, both to drupalSettings, and as
    // the third AJAX command for inclusion into the HTML. By testing for an
    // exact HTML string containing the SCRIPT tag, we also ensure that
    // unexpected JavaScript code, such as a jQuery.extend() that would
    // potentially clobber rather than properly merge settings, didn't
    // accidentally get added.
    $this->assertContains($expected['library_2'], $new_libraries, "Page state now has the {$expected['library_2']} library.");
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.