Loading src/LibraryPolicyBuilder.php +2 −2 Original line number Diff line number Diff line Loading @@ -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; Loading tests/src/Unit/LibraryPolicyBuilderTest.php +60 −0 Original line number Diff line number Diff line Loading @@ -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() ); } } Loading
src/LibraryPolicyBuilder.php +2 −2 Original line number Diff line number Diff line Loading @@ -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; Loading
tests/src/Unit/LibraryPolicyBuilderTest.php +60 −0 Original line number Diff line number Diff line Loading @@ -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() ); } }