You are here

public function FileUrlGeneratorTest::testGenerateFarfuture in CDN 8.3

@covers ::generate

File

tests/src/Unit/File/FileUrlGeneratorTest.php, line 240

Class

FileUrlGeneratorTest
@coversDefaultClass \Drupal\cdn\File\FileUrlGenerator @group cdn

Namespace

Drupal\Tests\cdn\Unit\File

Code

public function testGenerateFarfuture() {
  $config = [
    'status' => TRUE,
    'mapping' => [
      'type' => 'simple',
      'domain' => 'cdn.example.com',
      'conditions' => [],
    ],
    'scheme' => '//',
    'farfuture' => [
      'status' => TRUE,
    ],
    // File is used here generically to test a stream wrapper that is not
    // shipped with Drupal, but is natively supported by PHP.
    // @see \Drupal\cdn\File\FileUrlGenerator::generate(), which uses
    // file_exists() and would require actually configuring the stream
    // wrapper in the context of the unit test.
    'stream_wrappers' => [
      'public',
      'file',
    ],
  ];

  // Generate file for testing managed file.
  $llama_jpg_filename = 'llama%20 (' . $this
    ->randomMachineName() . ').jpg';
  $llama_jpg_filepath = $this->root . '/sites/default/files/' . $llama_jpg_filename;
  file_put_contents($llama_jpg_filepath, $this
    ->randomMachineName());
  $llama_jpg_mtime = filemtime($llama_jpg_filepath);
  $this
    ->assertTrue(file_exists($llama_jpg_filepath));

  // In root: 1) non-existing file, 2) shipped file, 3) managed file.
  $gen = $this
    ->createFileUrlGenerator('', $config);
  $this
    ->assertSame('//cdn.example.com/core/misc/does-not-exist.js', $gen
    ->generate('core/misc/does-not-exist.js'));
  $drupal_js_mtime = filemtime($this->root . '/core/misc/drupal.js');
  $drupal_js_security_token = Crypt::hmacBase64($drupal_js_mtime . ':relative:' . UrlHelper::encodePath('/core/misc/drupal.js'), static::$privateKey . Settings::getHashSalt());
  $this
    ->assertSame('//cdn.example.com/cdn/ff/' . $drupal_js_security_token . '/' . $drupal_js_mtime . '/:relative:/core/misc/drupal.js', $gen
    ->generate('core/misc/drupal.js'));

  // Since the public stream wrapper is not available in the unit test,
  // and we use file_exists() in the target method, we are using the
  // file:// scheme that ships with PHP. This does require
  // injecting a leading into the path that we compare against, to match
  // the method.
  $llama_jpg_security_token = Crypt::hmacBase64($llama_jpg_mtime . 'file' . UrlHelper::encodePath('/' . $llama_jpg_filepath), static::$privateKey . Settings::getHashSalt());
  $this
    ->assertSame('//cdn.example.com/cdn/ff/' . $llama_jpg_security_token . '/' . $llama_jpg_mtime . '/file/' . UrlHelper::encodePath($llama_jpg_filepath), $gen
    ->generate('file://' . $llama_jpg_filepath));

  // In subdir: 1) non-existing file, 2) shipped file, 3) managed file.
  $gen = $this
    ->createFileUrlGenerator('/subdir', $config);
  $this
    ->assertSame('//cdn.example.com/subdir/core/misc/does-not-exist.js', $gen
    ->generate('core/misc/does-not-exist.js'));
  $this
    ->assertSame('//cdn.example.com/subdir/cdn/ff/' . $drupal_js_security_token . '/' . $drupal_js_mtime . '/:relative:/core/misc/drupal.js', $gen
    ->generate('core/misc/drupal.js'));
  $this
    ->assertSame('//cdn.example.com/subdir/cdn/ff/' . $llama_jpg_security_token . '/' . $llama_jpg_mtime . '/file/' . UrlHelper::encodePath($llama_jpg_filepath), $gen
    ->generate('file://' . $llama_jpg_filepath));
  unlink($llama_jpg_filepath);
}