function AssetOptimizationTest::doTestAggregation

Same name and namespace in other branches
  1. 11.x core/tests/Drupal/FunctionalTests/Asset/AssetOptimizationTest.php \Drupal\FunctionalTests\Asset\AssetOptimizationTest::doTestAggregation()
  2. 10 core/tests/Drupal/FunctionalTests/Asset/AssetOptimizationTest.php \Drupal\FunctionalTests\Asset\AssetOptimizationTest::doTestAggregation()

Helper to test aggregate file URLs.

Parameters

array $settings: A settings array to pass to ::writeSettings()

1 call to AssetOptimizationTest::doTestAggregation()
AssetOptimizationTest::testAssetAggregation in core/tests/Drupal/FunctionalTests/Asset/AssetOptimizationTest.php
Tests that asset aggregates are rendered and created on disk.

File

core/tests/Drupal/FunctionalTests/Asset/AssetOptimizationTest.php, line 74

Class

AssetOptimizationTest
Tests asset aggregation.

Namespace

Drupal\FunctionalTests\Asset

Code

protected function doTestAggregation(array $settings) : void {
  $this->writeSettings($settings);
  $this->rebuildAll();
  $this->config('system.performance')
    ->set('css', [
    'preprocess' => TRUE,
    'compress' => TRUE,
  ])
    ->save();
  $this->config('system.performance')
    ->set('js', [
    'preprocess' => TRUE,
    'compress' => TRUE,
  ])
    ->save();
  $this->requestPage();
  $session = $this->getSession();
  $page = $session->getPage();
  // Collect all the URLs for all the script and styles prior to making any
  // more requests.
  $style_elements = $page->findAll('xpath', '//link[@href and @rel="stylesheet"]');
  $script_elements = $page->findAll('xpath', '//script[@src]');
  $style_urls = [];
  foreach ($style_elements as $element) {
    $style_urls[] = $element->getAttribute('href');
  }
  $script_urls = [];
  foreach ($script_elements as $element) {
    $script_urls[] = $element->getAttribute('src');
  }
  foreach ($style_urls as $url) {
    $this->assertAggregate($url, TRUE, 'text/css');
    // Once the file has been requested once, it's on disk. It is possible for
    // a second request to hit the controller, and then find that another
    // request has created the file already. Actually simulating this race
    // condition is not really possible since it relies on timing. However, by
    // changing the case of the part of the URL that is handled by Drupal
    // routing, we can force the request to be served by Drupal.
    $this->assertAggregate(str_replace($this->fileAssetsPath, strtoupper($this->fileAssetsPath), $url), TRUE, 'text/css', FALSE);
    $this->assertAggregate($url, FALSE, 'text/css');
    $this->assertInvalidAggregates($url);
  }
  foreach ($script_urls as $url) {
    $this->assertAggregate($url);
    $this->assertAggregate($url, FALSE);
    $this->assertInvalidAggregates($url);
  }
  // The aggregates have just been created, so ::deleteAll() should avoid
  // removing them.
  \Drupal::service('asset.css.collection_optimizer')->deleteAll();
  \Drupal::service('asset.js.collection_optimizer')->deleteAll();
  foreach ($style_urls as $url) {
    $this->assertAggregate($url, FALSE, 'text/css');
  }
  foreach ($script_urls as $url) {
    $this->assertAggregate($url, FALSE);
  }
  // Now set the mtime of all the files in the directory to a year in the
  // past, this is older than the threshold so they should be deleted.
  foreach ([
    'js',
    'css',
  ] as $type) {
    $iterator = new \DirectoryIterator('assets://' . $type);
    foreach ($iterator as $file) {
      if ($file->isFile()) {
        $mtime = $file->getMtime();
        $return = touch($file->getPathName(), $mtime - 86400 * 365);
        $this->assertTrue($return);
      }
    }
  }
  \Drupal::service('asset.css.collection_optimizer')->deleteAll();
  \Drupal::service('asset.js.collection_optimizer')->deleteAll();
  foreach ($style_urls as $url) {
    $this->assertAggregate($url, TRUE, 'text/css');
  }
  foreach ($script_urls as $url) {
    $this->assertAggregate($url, TRUE);
  }
}

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