View source
<?php
namespace Drupal\system\Tests\File;
use Symfony\Component\HttpFoundation\Request;
class UrlRewritingTest extends FileTestBase {
public static $modules = array(
'file_test',
);
function testShippedFileURL() {
\Drupal::state()
->set('file_test.hook_file_url_alter', 'cdn');
$filepath = 'core/assets/vendor/jquery/jquery.min.js';
$url = file_create_url($filepath);
$this
->assertEqual(FILE_URL_TEST_CDN_1 . '/' . $filepath, $url, 'Correctly generated a CDN URL for a shipped file.');
$filepath = 'core/misc/favicon.ico';
$url = file_create_url($filepath);
$this
->assertEqual(FILE_URL_TEST_CDN_2 . '/' . $filepath, $url, 'Correctly generated a CDN URL for a shipped file.');
\Drupal::state()
->set('file_test.hook_file_url_alter', 'root-relative');
$filepath = 'core/assets/vendor/jquery/jquery.min.js';
$url = file_create_url($filepath);
$this
->assertEqual(base_path() . '/' . $filepath, $url, 'Correctly generated a root-relative URL for a shipped file.');
$filepath = 'core/misc/favicon.ico';
$url = file_create_url($filepath);
$this
->assertEqual(base_path() . '/' . $filepath, $url, 'Correctly generated a root-relative URL for a shipped file.');
\Drupal::state()
->set('file_test.hook_file_url_alter', 'protocol-relative');
$filepath = 'core/assets/vendor/jquery/jquery.min.js';
$url = file_create_url($filepath);
$this
->assertEqual('/' . base_path() . '/' . $filepath, $url, 'Correctly generated a protocol-relative URL for a shipped file.');
$filepath = 'core/misc/favicon.ico';
$url = file_create_url($filepath);
$this
->assertEqual('/' . base_path() . '/' . $filepath, $url, 'Correctly generated a protocol-relative URL for a shipped file.');
\Drupal::state()
->delete('file_test.hook_file_url_alter');
$filepath = 'core/misc/favicon.ico';
$url = file_create_url($filepath . '?foo');
$this
->assertEqual($GLOBALS['base_url'] . '/' . $filepath . '?foo=', $url, 'Correctly generated url. The query string is present.');
$url = file_create_url($filepath . '?foo=bar');
$this
->assertEqual($GLOBALS['base_url'] . '/' . $filepath . '?foo=bar', $url, 'Correctly generated url. The query string is present.');
$url = file_create_url($filepath . '#v1.2');
$this
->assertEqual($GLOBALS['base_url'] . '/' . $filepath . '#v1.2', $url, 'Correctly generated url. The fragment is present.');
$url = file_create_url($filepath . '?foo=bar#v1.2');
$this
->assertEqual($GLOBALS['base_url'] . '/' . $filepath . '?foo=bar#v1.2', $url, 'Correctly generated url. The query string amd fragment is present.');
}
function testPublicManagedFileURL() {
\Drupal::state()
->set('file_test.hook_file_url_alter', 'cdn');
$uri = $this
->createUri();
$url = file_create_url($uri);
$public_directory_path = \Drupal::service('stream_wrapper_manager')
->getViaScheme('public')
->getDirectoryPath();
$this
->assertEqual(FILE_URL_TEST_CDN_2 . '/' . $public_directory_path . '/' . drupal_basename($uri), $url, 'Correctly generated a CDN URL for a created file.');
\Drupal::state()
->set('file_test.hook_file_url_alter', 'root-relative');
$uri = $this
->createUri();
$url = file_create_url($uri);
$this
->assertEqual(base_path() . '/' . $public_directory_path . '/' . drupal_basename($uri), $url, 'Correctly generated a root-relative URL for a created file.');
\Drupal::state()
->set('file_test.hook_file_url_alter', 'protocol-relative');
$uri = $this
->createUri();
$url = file_create_url($uri);
$this
->assertEqual('/' . base_path() . '/' . $public_directory_path . '/' . drupal_basename($uri), $url, 'Correctly generated a protocol-relative URL for a created file.');
}
function testRelativeFileURL() {
\Drupal::state()
->set('file_test.hook_file_url_alter', NULL);
$request = Request::create($GLOBALS['base_url']);
$this->container
->get('request_stack')
->push($request);
\Drupal::setContainer($this->container);
$filepath = 'core/assets/vendor/jquery/jquery.min.js';
$url = file_create_url($filepath);
$this
->assertIdentical(base_path() . $filepath, file_url_transform_relative($url));
$uri = $this
->createUri();
$url = file_create_url($uri);
$public_directory_path = \Drupal::service('stream_wrapper_manager')
->getViaScheme('public')
->getDirectoryPath();
$this
->assertIdentical(base_path() . $public_directory_path . '/' . rawurlencode(drupal_basename($uri)), file_url_transform_relative($url));
}
}