You are here

class LibraryPolicyBuilderTest in Content-Security-Policy 8

@coversDefaultClass \Drupal\csp\LibraryPolicyBuilder @group csp

Hierarchy

Expanded class hierarchy of LibraryPolicyBuilderTest

File

tests/src/Unit/LibraryPolicyBuilderTest.php, line 16

Namespace

Drupal\Tests\csp\Unit
View source
class LibraryPolicyBuilderTest extends UnitTestCase {

  /**
   * Memory Cache backend.
   *
   * @var \Drupal\Core\Cache\CacheBackendInterface
   */
  protected $cache;

  /**
   * Mock Module Handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $moduleHandler;

  /**
   * Mock Active Theme.
   *
   * @var \Drupal\Core\Theme\ActiveTheme|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $activeTheme;

  /**
   * Mock Theme Handler.
   *
   * @var \Drupal\Core\Extension\ThemeHandlerInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $themeHandler;

  /**
   * Mock Library Discovery.
   *
   * @var \Drupal\Core\Asset\LibraryDiscoveryInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $libraryDiscovery;

  /**
   * {@inheritdoc}
   */
  public function setUp() : void {
    parent::setUp();
    $this->cache = new MemoryBackend();
    $this->moduleHandler = $this
      ->getMockBuilder(ModuleHandler::class)
      ->disableOriginalConstructor()
      ->getMock();
    $this->themeHandler = $this
      ->getMockBuilder(ThemeHandler::class)
      ->disableOriginalConstructor()
      ->getMock();
    $this->libraryDiscovery = $this
      ->getMockBuilder(LibraryDiscovery::class)
      ->disableOriginalConstructor()
      ->getMock();
  }

  /**
   * Test an empty extension set.
   *
   * @covers ::getSources
   * @covers ::getExtensionSources
   * @covers ::getLibrarySources
   */
  public function testEmptyPolicy() {
    $this->themeHandler
      ->expects($this
      ->atLeastOnce())
      ->method('listInfo')
      ->willReturn([]);
    $this->moduleHandler
      ->expects($this
      ->atLeastOnce())
      ->method('getModuleList')
      ->willReturn([]);
    $this->libraryDiscovery
      ->expects($this
      ->atLeastOnce())
      ->method('getLibrariesByExtension')
      ->with('core')
      ->willReturn([]);
    $libraryPolicy = new LibraryPolicyBuilder($this->cache, $this->moduleHandler, $this->themeHandler, $this->libraryDiscovery);
    $this
      ->assertEquals([], $libraryPolicy
      ->getSources());
  }

  /**
   * Test that a library's external sources are discovered.
   *
   * @covers ::getSources
   * @covers ::getExtensionSources
   * @covers ::getLibrarySources
   * @covers ::getHostFromUri
   */
  public function testLibraryWithSources() {
    $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);

    // Test a few behaviours:
    // - local files are ignored.
    // - script domains are sorted.
    // - duplicate style domains are filtered.
    $libraryInfo = [
      'js' => [
        [
          'type' => 'file',
          'data' => 'js/script.js',
        ],
        [
          'type' => 'external',
          'data' => 'http://js.example.org/js/script.js',
        ],
        [
          'type' => 'external',
          'data' => 'http://js.example.com/js/script.js',
        ],
      ],
      'css' => [
        [
          'type' => 'external',
          'data' => 'http://css.example.com/css/style1.css',
        ],
        [
          'type' => 'external',
          'data' => 'http://css.example.com/css/style2.css',
        ],
      ],
    ];
    $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',
        'js.example.org',
      ],
      'script-src-elem' => [
        'js.example.com',
        'js.example.org',
      ],
      'style-src' => [
        'css.example.com',
      ],
      'style-src-elem' => [
        'css.example.com',
      ],
    ], $libraryPolicy
      ->getSources());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LibraryPolicyBuilderTest::$activeTheme protected property Mock Active Theme.
LibraryPolicyBuilderTest::$cache protected property Memory Cache backend.
LibraryPolicyBuilderTest::$libraryDiscovery protected property Mock Library Discovery.
LibraryPolicyBuilderTest::$moduleHandler protected property Mock Module Handler.
LibraryPolicyBuilderTest::$themeHandler protected property Mock Theme Handler.
LibraryPolicyBuilderTest::setUp public function Overrides UnitTestCase::setUp
LibraryPolicyBuilderTest::testEmptyPolicy public function Test an empty extension set.
LibraryPolicyBuilderTest::testLibraryWithSources public function Test that a library's external sources are discovered.
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.