3 namespace Drupal\Tests\libraries\Functional\ExternalLibrary\Definition;
5 use Drupal\Tests\BrowserTestBase;
8 * Tests that remote library definitions are found and downloaded.
10 * This is a browser test because Guzzle is not usable from a kernel test.
14 * @todo Make this a kernel test when https://www.drupal.org/node/2571475 is in.
16 class DefinitionDiscoveryFactoryTest extends BrowserTestBase {
19 * The 'libraries.settings' configuration object.
21 * @var \Drupal\Core\Config\Config
26 * The path to the test library definitions.
30 protected $definitionPath;
35 public static $modules = ['libraries'];
40 protected function setUp() {
43 /** @var \Drupal\Core\Config\ConfigFactoryInterface $config_factory */
44 $config_factory = $this->container->get('config.factory');
45 $this->config = $config_factory->getEditable('libraries.settings');
47 // Set up the remote library definition URL to point to the local website.
48 /** @var \Drupal\Core\Extension\ModuleHandlerInterface $module_handler */
49 $module_handler = $this->container->get('module_handler');
50 $module_path = $module_handler->getModule('libraries')->getPath();
51 $this->definitionPath = "$module_path/tests/library_definitions";
55 * Tests that the discovery works according to the configuration.
57 public function testDiscovery() {
58 $library_id = 'test_asset_library';
59 $expected_definition = [
61 'version_detector' => [
67 'remote_url' => 'http://example.com',
77 $discovery_service_id = 'libraries.definition.discovery';
79 // Test the local discovery with an incorrect path.
81 ->set('definition.local.path', 'path/that/does/not/exist')
82 ->set('definition.remote.enable', FALSE)
84 $discovery = $this->container->get($discovery_service_id);
85 $this->assertFalse($discovery->hasDefinition($library_id));
87 // Test the local discovery with a proper path.
89 ->set('definition.local.path', $this->definitionPath)
91 $discovery = $this->container->get($discovery_service_id);
92 $this->assertTrue($discovery->hasDefinition($library_id));
94 // Test a remote discovery with an incorrect path.
95 $definitions_directory = 'public://library-definitions';
97 ->set('definition.local.path', $definitions_directory)
98 ->set('definition.remote.enable', TRUE)
99 ->set('definition.remote.urls', ["$this->baseUrl/path/that/does/not/exist"])
101 $discovery = $this->container->get($discovery_service_id);
102 $this->assertFalse($discovery->hasDefinition($library_id));
104 // Test a remote discovery with a proper path.
106 ->set('definition.remote.urls', ["$this->baseUrl/$this->definitionPath"])
108 /** @var \Drupal\libraries\ExternalLibrary\Definition\DefinitionDiscoveryInterface $discovery */
109 $discovery = $this->container->get($discovery_service_id);
110 $definition_file = "$definitions_directory/$library_id.json";
111 $this->assertFalse(file_exists($definition_file));
112 $this->assertTrue($discovery->hasDefinition($library_id));
113 $this->assertFalse(file_exists($definition_file));
114 $this->assertEquals($discovery->getDefinition($library_id), $expected_definition);
115 $this->assertTrue(file_exists($definition_file));