You are here

public function DefinitionDiscoveryFactoryTest::testDiscovery in Libraries API 8.3

Tests that the discovery works according to the configuration.

File

tests/src/Functional/ExternalLibrary/Definition/DefinitionDiscoveryFactoryTest.php, line 64

Class

DefinitionDiscoveryFactoryTest
Tests that remote library definitions are found and downloaded.

Namespace

Drupal\Tests\libraries\Functional\ExternalLibrary\Definition

Code

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
    ->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);
}