Skip to content
Snippets Groups Projects
Commit 56e95671 authored by Andrii Podanenko's avatar Andrii Podanenko
Browse files

fix(cleanup): Issue #3297522 by podarok: Remove LibrariesWebTest

parent e3e5dd7a
No related branches found
Tags 4.0.0
No related merge requests found
<?php
namespace Drupal\Tests\libraries\Functional;
use Drupal\Component\Utility\Html;
use Drupal\Tests\BrowserTestBase;
/**
* Tests basic detection and loading of libraries.
*
* @group libraries
*/
class LibrariesWebTest extends BrowserTestBase {
/**
* {@inheritdoc}
*/
protected $profile = 'testing';
/**
* Set default theme to stark.
*
* @var string
*/
protected $defaultTheme = 'stark';
/**
* Modules to install.
*
* @var array
*/
protected static $modules = ['libraries', 'libraries_test'];
/**
* The URL generator used in this test.
*
* @var \Drupal\Core\Utility\UnroutedUrlAssemblerInterface
*/
protected $urlAssembler;
/**
* The state service used in this test.
*
* @var \Drupal\Core\State\StateInterface
*/
protected $state;
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->urlAssembler = $this->container->get('unrouted_url_assembler');
$this->state = $this->container->get('state');
}
/**
* Tests libraries_load().
*
* @todo Remove or rewrite to accomodate integration with core Libraries.
*/
public function _testLibrariesLoad() {
// Test dependencies.
$library = \Drupal::service('libraries.manager')->load('example_dependency_missing');
dump('<pre>' . var_export($library, TRUE) . '</pre>');
$this->assertFalse($library['loaded'], 'Library with missing dependency cannot be loaded');
$library = \Drupal::service('libraries.manager')->load('example_dependency_incompatible');
dump('<pre>' . var_export($library, TRUE) . '</pre>');
$this->assertFalse($library['loaded'], 'Library with incompatible dependency cannot be loaded');
$library = \Drupal::service('libraries.manager')->load('example_dependency_compatible');
dump('<pre>' . var_export($library, TRUE) . '</pre>');
$this->assertEquals($library['loaded'], 1, 'Library with compatible dependency is loaded');
$loaded = &drupal_static('libraries_load');
dump('<pre>' . var_export($loaded, TRUE) . '</pre>');
$this->assertEquals($loaded['example_dependency']['loaded'], 1, 'Dependency library is also loaded');
}
/**
* Tests that library files are properly added to the page output.
*
* We check for JavaScript and CSS files directly in the DOM and add a list of
* included PHP files manually to the page output.
*
* @todo Remove or rewrite to accomodate integration with core Libraries.
* @see _libraries_test_load()
*/
public function _testLibrariesOutput() {
// Test loading of a simple library with a top-level files property.
$this->drupalGet('libraries_test/files');
$this->assertLibraryFiles('example_1', 'File loading');
// Test loading of integration files.
$this->drupalGet('libraries_test/integration_files');
$this->assertSession()->responseContains('libraries_test.js');
$this->assertSession()->responseContains('libraries_test.css');
$this->assertSession()->responseContains('libraries_test.inc');
// Test version overloading.
$this->drupalGet('libraries_test/versions');
$this->assertLibraryFiles('example_2', 'Version overloading');
// Test variant loading.
$this->drupalGet('libraries_test/variant');
$this->assertLibraryFiles('example_3', 'Variant loading');
// Test version overloading and variant loading.
$this->drupalGet('libraries_test/versions_and_variants');
$this->assertLibraryFiles('example_4', 'Concurrent version and variant overloading');
// Test caching.
\Drupal::state()->set('libraries_test.cache', TRUE);
\Drupal::cache('libraries')->delete('example_callback');
// When the library information is not cached, all callback groups should be
// invoked.
$this->drupalGet('libraries_test/cache');
$this->assertSession()->responseContains('The <em>info</em> callback group was invoked.');
$this->assertSession()->responseContains('The <em>pre-detect</em> callback group was invoked.');
$this->assertSession()->responseContains('The <em>post-detect</em> callback group was invoked.');
$this->assertSession()->responseContains('The <em>pre-load</em> callback group was invoked.');
$this->assertSession()->responseContains('The <em>post-load</em> callback group was invoked.');
// When the library information is cached only the 'pre-load' and
// 'post-load' callback groups should be invoked.
$this->drupalGet('libraries_test/cache');
$this->assertSession()->responseNotContains('The <em>info</em> callback group was not invoked.');
$this->assertSession()->responseNotContains('The <em>pre-detect</em> callback group was not invoked.');
$this->assertSession()->responseNotContains('The <em>post-detect</em> callback group was not invoked.');
$this->assertSession()->responseContains('The <em>pre-load</em> callback group was invoked.');
$this->assertSession()->responseContains('The <em>post-load</em> callback group was invoked.');
\Drupal::state()->set('libraries_test.cache', FALSE);
}
/**
* Helper function to assert that a library was correctly loaded.
*
* Asserts that all the correct files were loaded and all the incorrect ones
* were not.
*
* @param $name
* The name of the files that should be loaded. The current testing system
* knows of 'example_1', 'example_2', 'example_3' and 'example_4'. Each name
* has an associated JavaScript, CSS and PHP file that will be asserted. All
* other files will be asserted to not be loaded. See
* tests/example/README.txt for more information on how the loading of the
* files is tested.
* @param $label
* (optional) A label to prepend to the assertion messages, to make them
* less ambiguous.
* @param $extensions
* (optional) The expected file extensions of $name. Defaults to
* array('js', 'css', 'php').
*/
public function assertLibraryFiles($name, $label = '', $extensions = ['js', 'css', 'php']) {
$label = ($label !== '' ? "$label: " : '');
// Test that the wrong files are not loaded...
$names = [
'example_1' => FALSE,
'example_2' => FALSE,
'example_3' => FALSE,
'example_4' => FALSE,
];
// ...and the correct ones are.
$names[$name] = TRUE;
// Test for the specific HTML that the different file types appear as in the
// DOM.
$html = [
'js' => ['<script src="', '"></script>'],
'css' => ['<link rel="stylesheet" href="', '" media="all" />'],
// PHP files do not get added to the DOM directly.
// @see _libraries_test_load()
'php' => ['<li>', '</li>'],
];
$html_expected = [];
$html_not_expected = [];
foreach ($names as $name => $expected) {
foreach ($extensions as $extension) {
$filepath = \Drupal::service('extension.list.module')->getPath('libraries') . "/tests/example/$name.$extension";
// JavaScript and CSS files appear as full URLs and with an appended
// query string.
if (in_array($extension, ['js', 'css'])) {
$filepath = $this->urlAssembler->assemble("base://$filepath", [
'query' => [
$this->state->get('system.css_js_query_string') ?: '0' => NULL,
],
'absolute' => TRUE,
]);
// If index.php is part of the generated URLs, we need to strip it.
// $filepath = str_replace('index.php/', '', $filepath);
}
list($prefix, $suffix) = $html[$extension];
$raw = $prefix . $filepath . $suffix;
if ($expected) {
$html_expected[] = Html::escape($raw);
$this->assertSession()->responseContains($raw);
}
else {
$html_not_expected[] = Html::escape($raw);
$this->assertSession()->responseNotContains($raw);
}
}
}
$html_expected = '<ul><li><pre>' . implode('</pre></li><li><pre>', $html_expected) . '</pre></li></ul>';
$html_not_expected = '<ul><li><pre>' . implode('</pre></li><li><pre>', $html_not_expected) . '</pre></li></ul>';
dump("Strings of HTML that are expected to be present:{$html_expected}Strings of HTML that are expected to not be present:{$html_not_expected}");
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment