DefinitionDiscoveryFactoryTest.php in Libraries API 8.3
File
tests/src/Functional/ExternalLibrary/Definition/DefinitionDiscoveryFactoryTest.php
View source
<?php
namespace Drupal\Tests\libraries\Functional\ExternalLibrary\Definition;
use Drupal\Tests\BrowserTestBase;
class DefinitionDiscoveryFactoryTest extends BrowserTestBase {
protected $config;
protected $definitionPath;
protected $defaultTheme = 'stable';
public static $modules = [
'libraries',
];
protected function setUp() {
parent::setUp();
$config_factory = $this->container
->get('config.factory');
$this->config = $config_factory
->getEditable('libraries.settings');
$module_handler = $this->container
->get('module_handler');
$module_path = $module_handler
->getModule('libraries')
->getPath();
$this->definitionPath = "{$module_path}/tests/library_definitions";
}
public function testDiscovery() {
$library_id = 'test_asset_library';
$expected_definition = [
'type' => 'asset',
'version_detector' => [
'id' => 'static',
'configuration' => [
'version' => '1.0.0',
],
],
'remote_url' => 'http://example.com',
'css' => [
'base' => [
'example.css' => [],
],
],
'js' => [
'example.js' => [],
],
];
$discovery_service_id = 'libraries.definition.discovery';
$this->config
->set('definition.local.path', 'path/that/does/not/exist')
->set('definition.remote.enable', FALSE)
->save();
$discovery = $this->container
->get($discovery_service_id);
$this
->assertFalse($discovery
->hasDefinition($library_id));
$this->config
->set('definition.local.path', $this->definitionPath)
->save();
$discovery = $this->container
->get($discovery_service_id);
$this
->assertTrue($discovery
->hasDefinition($library_id));
$definitions_directory = 'public://library-definitions';
$this->config
->set('definition.local.path', $definitions_directory)
->set('definition.remote.enable', TRUE)
->set('definition.remote.urls', [
"{$this->baseUrl}/path/that/does/not/exist",
])
->save();
$discovery = $this->container
->get($discovery_service_id);
$this
->assertFalse($discovery
->hasDefinition($library_id));
$this->config
->set('definition.remote.urls', [
"{$this->baseUrl}/{$this->definitionPath}",
])
->save();
$discovery = $this->container
->get($discovery_service_id);
$definition_file = "{$definitions_directory}/{$library_id}.json";
$this
->assertFileNotExists($definition_file);
$this
->assertTrue($discovery
->hasDefinition($library_id));
$this
->assertFileNotExists($definition_file);
$this
->assertEquals($discovery
->getDefinition($library_id), $expected_definition);
$this
->assertFileExists($definition_file);
}
}