Unverified Commit 4a899e21 authored by Geoff Appleby's avatar Geoff Appleby
Browse files

Issue #3223558: Handle libraries with empty file definitions better

parent 87fae5f6
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -182,14 +182,14 @@ class LibraryPolicyBuilder {
    $sources = [];

    foreach ($libraryInfo['js'] as $jsInfo) {
      if ($jsInfo['type'] == 'external') {
      if ($jsInfo['type'] == 'external' && !empty($jsInfo['data'])) {
        $host = self::getHostFromUri($jsInfo['data']);
        $sources['script-src'][] = $host;
        $sources['script-src-elem'][] = $host;
      }
    }
    foreach ($libraryInfo['css'] as $cssInfo) {
      if ($cssInfo['type'] == 'external') {
      if ($cssInfo['type'] == 'external' && !empty($cssInfo['data'])) {
        $host = self::getHostFromUri($cssInfo['data']);
        $sources['style-src'][] = $host;
        $sources['style-src-elem'][] = $host;
+60 −0
Original line number Diff line number Diff line
@@ -174,4 +174,64 @@ class LibraryPolicyBuilderTest extends UnitTestCase {
    );
  }

  /**
   * Handle if a library has an empty URL.
   *
   * @covers ::getSources
   * @covers ::getExtensionSources
   * @covers ::getLibrarySources
   */
  public function testLibraryWithEmptyStringSource() {

    $this->moduleHandler->expects($this->any())
      ->method('getModuleList')
      ->willReturn([]);
    $this->themeHandler->expects($this->any())
      ->method('listInfo')
      ->willReturn([
        'stark' => (object) ['name' => 'stark'],
      ]);

    $extensionMap = [
      ['core', []],
      ['stark', ['test' => []]],
    ];
    $this->libraryDiscovery->expects($this->any())
      ->method('getLibrariesByExtension')
      ->willReturnMap($extensionMap);

    $libraryInfo = [
      'js' => [
        [
          'type' => 'external',
          'data' => '',
        ],
        [
          'type' => 'external',
          'data' => 'http://js.example.com/js/script.js',
        ],
      ],
      'css' => [
        [
          'type' => 'external',
          'data' => '',
        ],
      ],
    ];
    $this->libraryDiscovery->expects($this->atLeastOnce())
      ->method('getLibraryByName')
      ->with('stark', 'test')
      ->willReturn($libraryInfo);

    $libraryPolicy = new LibraryPolicyBuilder($this->cache, $this->moduleHandler, $this->themeHandler, $this->libraryDiscovery);

    $this->assertEquals(
      [
        'script-src' => ['js.example.com'],
        'script-src-elem' => ['js.example.com'],
      ],
      $libraryPolicy->getSources()
    );
  }

}