class JsCollectionGrouperUnitTest

Tests the CSS asset collection grouper.

Attributes

#[Group('Asset')]

Hierarchy

Expanded class hierarchy of JsCollectionGrouperUnitTest

File

core/tests/Drupal/Tests/Core/Asset/JsCollectionGrouperUnitTest.php, line 14

Namespace

Drupal\Tests\Core\Asset
View source
class JsCollectionGrouperUnitTest extends UnitTestCase {
  
  /**
   * A CSS asset grouper.
   *
   * @var \Drupal\Core\Asset\CssCollectionGrouper
   */
  protected $grouper;
  
  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    $this->grouper = new JsCollectionGrouper();
  }
  
  /**
   * Tests \Drupal\Core\Asset\CssCollectionGrouper.
   */
  public function testGrouperWithAggregateTargets() : void {
    $js_assets = [
      'a.js' => [
        'group' => -100,
        'type' => 'file',
        'weight' => 0,
        'preprocess' => TRUE,
        'aggregate_target' => [
          'js' => TRUE,
        ],
        'data' => 'a.js',
        'basename' => 'a.js',
        'library' => 'base',
      ],
      'b.js' => [
        'group' => -100,
        'type' => 'file',
        'weight' => 0.05,
        'preprocess' => TRUE,
        'aggregate_target' => [
          'js' => TRUE,
        ],
        'data' => 'b.js',
        'basename' => 'c.js',
        'library' => 'base',
      ],
      'c.js' => [
        'group' => -100,
        'type' => 'file',
        'weight' => 0.1,
        'preprocess' => TRUE,
        'aggregate_target' => [
          'js' => TRUE,
        ],
        'data' => 'c.js',
        'basename' => 'c.js',
        'library' => 'base',
      ],
    ];
    $groups = $this->grouper
      ->group($js_assets);
    // When all files are sequentially in the same library, the resultant group
    // should have the 'libraries' key set.
    $this->assertArrayHasKey('libraries', $groups[0]);
    $this->assertCount(1, $groups);
    // Now change b.js to be in a separate library. Because its position
    // is in-between a.js and c.js it will force a fallback in the
    // aggregate_target logic, resulting in three groups, and only b.js
    // having the 'libraries' key.
    $js_assets['b.js']['library'] = 'different';
    $groups = $this->grouper
      ->group($js_assets);
    $this->assertArrayNotHasKey('libraries', $groups[0]);
    $this->assertArrayHasKey('libraries', $groups[1]);
    $this->assertArrayNotHasKey('libraries', $groups[2]);
    $this->assertCount(3, $groups);
    // Now set aggregate_target FALSE for b.js.
    $js_assets['b.js']['aggregate_target']['js'] = FALSE;
    $groups = $this->grouper
      ->group($js_assets);
    $this->assertArrayNotHasKey('libraries', $groups[0]);
    // Even though the second asset doesn't specify an aggregate target,
    // the library is self contained in a single asset group so the libraries
    // key can be used.
    $this->assertArrayHasKey('libraries', $groups[1]);
    $this->assertArrayNotHasKey('libraries', $groups[2]);
    $this->assertCount(3, $groups);
    // With aggregate_target FALSE for all three files, there should be one
    // group. The libraries key is added even though there was no aggregate
    // target because all of the libraries were encapsulated within the asset
    // group.
    $js_assets['a.js']['aggregate_target']['js'] = FALSE;
    $js_assets['c.js']['aggregate_target']['js'] = FALSE;
    $groups = $this->grouper
      ->group($js_assets);
    $this->assertArrayHasKey('libraries', $groups[0]);
    $this->assertCount(1, $groups);
  }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overriden Title Overrides
DrupalTestCaseTrait::$root protected property The Drupal root directory.
DrupalTestCaseTrait::checkErrorHandlerOnTearDown public function Checks the test error handler after test execution. 1
DrupalTestCaseTrait::checkLegacySymfonyDeprecationHelperEnvVariable public function Checks legacy SYMFONY_DEPRECATIONS_HELPER env variable is not used.
DrupalTestCaseTrait::expectExceptionMessageIs protected function Expects an exactly matching exception message.
DrupalTestCaseTrait::expectExceptionMessageIsOrContains protected function Expects an exception message containing a specified string.
DrupalTestCaseTrait::getDrupalRoot Deprecated protected static function Returns the Drupal root directory. 1
DrupalTestCaseTrait::setDebugDumpHandler public static function Registers the dumper CLI handler when the DebugDump extension is enabled.
DrupalTestCaseTrait::setUpRoot final protected function Ensure that the $root property is set initially.
DrupalTestCaseTrait::skipTestWithAttribute final protected function Supports skipping tests with the Skip attribute.
ExpectDeprecationTrait::expectDeprecation Deprecated public function Adds an expected deprecation.
ExpectDeprecationTrait::regularExpressionForFormatDescription private function
JsCollectionGrouperUnitTest::$grouper protected property A CSS asset grouper.
JsCollectionGrouperUnitTest::setUp protected function Overrides UnitTestCase::setUp
JsCollectionGrouperUnitTest::testGrouperWithAggregateTargets public function Tests \Drupal\Core\Asset\CssCollectionGrouper.
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers.
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
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::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setupMockIterator protected function Set up a traversable class mock to return specific items when iterated.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.