X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=web%2Fmodules%2Fcontrib%2Flibraries%2Ftests%2Fsrc%2FFunctional%2FExternalLibrary%2FDefinition%2FDefinitionDiscoveryFactoryTest.php;fp=web%2Fmodules%2Fcontrib%2Flibraries%2Ftests%2Fsrc%2FFunctional%2FExternalLibrary%2FDefinition%2FDefinitionDiscoveryFactoryTest.php;h=14bf74e4f6ecfbbd5e1a7a693c4a88349af084be;hp=0000000000000000000000000000000000000000;hb=8acec36f19c470dfcda1ae2336826a782f41874c;hpb=e0411c4e83ba0d079034db83c3f7f55be24a0e35 diff --git a/web/modules/contrib/libraries/tests/src/Functional/ExternalLibrary/Definition/DefinitionDiscoveryFactoryTest.php b/web/modules/contrib/libraries/tests/src/Functional/ExternalLibrary/Definition/DefinitionDiscoveryFactoryTest.php new file mode 100644 index 000000000..14bf74e4f --- /dev/null +++ b/web/modules/contrib/libraries/tests/src/Functional/ExternalLibrary/Definition/DefinitionDiscoveryFactoryTest.php @@ -0,0 +1,118 @@ +container->get('config.factory'); + $this->config = $config_factory->getEditable('libraries.settings'); + + // Set up the remote library definition URL to point to the local website. + /** @var \Drupal\Core\Extension\ModuleHandlerInterface $module_handler */ + $module_handler = $this->container->get('module_handler'); + $module_path = $module_handler->getModule('libraries')->getPath(); + $this->definitionPath = "$module_path/tests/library_definitions"; + } + + /** + * Tests that the discovery works according to the configuration. + */ + 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'; + + // Test the local discovery with an incorrect path. + $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)); + + // Test the local discovery with a proper path. + $this->config + ->set('definition.local.path', $this->definitionPath) + ->save(); + $discovery = $this->container->get($discovery_service_id); + $this->assertTrue($discovery->hasDefinition($library_id)); + + // Test a remote discovery with an incorrect path. + $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)); + + // Test a remote discovery with a proper path. + $this->config + ->set('definition.remote.urls', ["$this->baseUrl/$this->definitionPath"]) + ->save(); + /** @var \Drupal\libraries\ExternalLibrary\Definition\DefinitionDiscoveryInterface $discovery */ + $discovery = $this->container->get($discovery_service_id); + $definition_file = "$definitions_directory/$library_id.json"; + $this->assertFalse(file_exists($definition_file)); + $this->assertTrue($discovery->hasDefinition($library_id)); + $this->assertFalse(file_exists($definition_file)); + $this->assertEquals($discovery->getDefinition($library_id), $expected_definition); + $this->assertTrue(file_exists($definition_file)); + } + +}