function CssCollectionGrouperUnitTest::testGrouperWithAggregateTargets

Tests \Drupal\Core\Asset\CssCollectionGrouper.

File

core/tests/Drupal/Tests/Core/Asset/CssCollectionGrouperUnitTest.php, line 153

Class

CssCollectionGrouperUnitTest
Tests the CSS asset collection grouper.

Namespace

Drupal\Tests\Core\Asset

Code

public function testGrouperWithAggregateTargets() : void {
  $css_assets = [
    'a.css' => [
      'group' => -100,
      'category' => 'base',
      'type' => 'file',
      'weight' => 0,
      'media' => 'all',
      'preprocess' => TRUE,
      'aggregate_target' => [
        'css' => TRUE,
      ],
      'data' => 'a.css',
      'basename' => 'a.css',
      'library' => 'base',
    ],
    'b.css' => [
      'group' => -100,
      'category' => 'base',
      'type' => 'file',
      'weight' => 0.05,
      'media' => 'all',
      'preprocess' => TRUE,
      'aggregate_target' => [
        'css' => TRUE,
      ],
      'data' => 'b.css',
      'basename' => 'c.css',
      'library' => 'base',
    ],
    'c.css' => [
      'group' => -100,
      'category' => 'base',
      'type' => 'file',
      'weight' => 0.1,
      'media' => 'all',
      'preprocess' => TRUE,
      'aggregate_target' => [
        'css' => TRUE,
      ],
      'data' => 'c.css',
      'basename' => 'c.css',
      'library' => 'base',
    ],
  ];
  $groups = $this->grouper
    ->group($css_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.css to be in a separate library. Because its position
  // is in-between a.css and c.css it will force a fallback in the
  // aggregate_target logic, resulting in three groups, and only b.css
  // having the 'libraries' key.
  $css_assets['b.css']['library'] = 'different';
  $groups = $this->grouper
    ->group($css_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.css.
  $css_assets['b.css']['aggregate_target']['css'] = FALSE;
  $groups = $this->grouper
    ->group($css_assets);
  $this->assertArrayNotHasKey('libraries', $groups[0]);
  $this->assertArrayNotHasKey('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 with no libraries key.
  $css_assets['a.css']['aggregate_target']['css'] = FALSE;
  $css_assets['c.css']['aggregate_target']['css'] = FALSE;
  $groups = $this->grouper
    ->group($css_assets);
  $this->assertArrayNotHasKey('libraries', $groups[0]);
  $this->assertCount(1, $groups);
  // When the libraries match but category is different,
  // then aggregates should be split by category.
  $css_assets['a.css']['aggregate_target']['css'] = TRUE;
  $css_assets['b.css']['aggregate_target']['css'] = TRUE;
  $css_assets['b.css']['library'] = 'base';
  $css_assets['c.css']['aggregate_target']['css'] = TRUE;
  $css_assets['c.css']['category'] = 'different';
  $groups = $this->grouper
    ->group($css_assets);
  $this->assertArrayHasKey('libraries', $groups[0]);
  $this->assertArrayHasKey('libraries', $groups[1]);
  $this->assertCount(2, $groups);
}

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