Further modules included.
[yaffs-website] / web / modules / contrib / libraries / tests / src / Functional / ExternalLibrary / Definition / DefinitionDiscoveryFactoryTest.php
1 <?php
2
3 namespace Drupal\Tests\libraries\Functional\ExternalLibrary\Definition;
4
5 use Drupal\Tests\BrowserTestBase;
6
7 /**
8  * Tests that remote library definitions are found and downloaded.
9  *
10  * This is a browser test because Guzzle is not usable from a kernel test.
11  *
12  * @group libraries
13  *
14  * @todo Make this a kernel test when https://www.drupal.org/node/2571475 is in.
15  */
16 class DefinitionDiscoveryFactoryTest extends BrowserTestBase {
17
18   /**
19    * The 'libraries.settings' configuration object.
20    *
21    * @var \Drupal\Core\Config\Config
22    */
23   protected $config;
24
25   /**
26    * The path to the test library definitions.
27    *
28    * @var string
29    */
30   protected $definitionPath;
31
32   /**
33    * {@inheritdoc}
34    */
35   public static $modules = ['libraries'];
36
37   /**
38    * {@inheritdoc}
39    */
40   protected function setUp() {
41     parent::setUp();
42
43     /** @var \Drupal\Core\Config\ConfigFactoryInterface $config_factory */
44     $config_factory = $this->container->get('config.factory');
45     $this->config = $config_factory->getEditable('libraries.settings');
46
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";
52   }
53
54   /**
55    * Tests that the discovery works according to the configuration.
56    */
57   public function testDiscovery() {
58     $library_id = 'test_asset_library';
59     $expected_definition = [
60       'type' => 'asset',
61       'version_detector' => [
62         'id' => 'static',
63         'configuration' => [
64           'version' => '1.0.0'
65         ],
66       ],
67       'remote_url' => 'http://example.com',
68       'css' => [
69         'base' => [
70           'example.css' => [],
71         ],
72       ],
73       'js' => [
74         'example.js' => [],
75       ],
76     ];
77     $discovery_service_id = 'libraries.definition.discovery';
78
79     // Test the local discovery with an incorrect path.
80     $this->config
81       ->set('definition.local.path', 'path/that/does/not/exist')
82       ->set('definition.remote.enable', FALSE)
83       ->save();
84     $discovery = $this->container->get($discovery_service_id);
85     $this->assertFalse($discovery->hasDefinition($library_id));
86
87     // Test the local discovery with a proper path.
88     $this->config
89       ->set('definition.local.path', $this->definitionPath)
90       ->save();
91     $discovery = $this->container->get($discovery_service_id);
92     $this->assertTrue($discovery->hasDefinition($library_id));
93
94     // Test a remote discovery with an incorrect path.
95     $definitions_directory = 'public://library-definitions';
96     $this->config
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"])
100       ->save();
101     $discovery = $this->container->get($discovery_service_id);
102     $this->assertFalse($discovery->hasDefinition($library_id));
103
104     // Test a remote discovery with a proper path.
105     $this->config
106       ->set('definition.remote.urls', ["$this->baseUrl/$this->definitionPath"])
107       ->save();
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));
116   }
117
118 }