diff --git a/core/includes/file.inc b/core/includes/file.inc index 545fca3..c487b81 100644 --- a/core/includes/file.inc +++ b/core/includes/file.inc @@ -191,12 +191,16 @@ function file_get_stream_wrappers($filter = STREAM_WRAPPERS_ALL) { $wrappers_storage = &drupal_static(__FUNCTION__); if (!isset($wrappers_storage)) { - $wrappers = \Drupal::moduleHandler()->invokeAll('stream_wrappers'); - foreach ($wrappers as $scheme => $info) { - // Add defaults. - $wrappers[$scheme] += array('type' => STREAM_WRAPPERS_NORMAL); + $wrappers = array(); + $container = \Drupal::getContainer(); + if (is_object($container) && $container->has('module_handler')) { + $wrappers = \Drupal::moduleHandler()->invokeAll('stream_wrappers'); + foreach ($wrappers as $scheme => $info) { + // Add defaults. + $wrappers[$scheme] += array('type' => STREAM_WRAPPERS_NORMAL); + } + drupal_alter('stream_wrappers', $wrappers); } - drupal_alter('stream_wrappers', $wrappers); $existing = stream_get_wrappers(); foreach ($wrappers as $scheme => $info) { // We only register classes that implement our interface. diff --git a/core/modules/file/lib/Drupal/file/Tests/CopyTest.php b/core/modules/file/lib/Drupal/file/Tests/CopyTest.php index 1c0dce6..6ee9c99 100644 --- a/core/modules/file/lib/Drupal/file/Tests/CopyTest.php +++ b/core/modules/file/lib/Drupal/file/Tests/CopyTest.php @@ -15,7 +15,7 @@ public static function getInfo() { return array( 'name' => 'File copying', 'description' => 'Tests the file copy function.', - 'group' => 'File API', + 'group' => 'File Managed API', ); } diff --git a/core/modules/file/lib/Drupal/file/Tests/DeleteTest.php b/core/modules/file/lib/Drupal/file/Tests/DeleteTest.php index 9d1911b..f65e905 100644 --- a/core/modules/file/lib/Drupal/file/Tests/DeleteTest.php +++ b/core/modules/file/lib/Drupal/file/Tests/DeleteTest.php @@ -15,7 +15,7 @@ public static function getInfo() { return array( 'name' => 'File delete', 'description' => 'Tests the file delete function.', - 'group' => 'File API', + 'group' => 'File Managed API', ); } diff --git a/core/modules/file/lib/Drupal/file/Tests/DownloadTest.php b/core/modules/file/lib/Drupal/file/Tests/DownloadTest.php index 1870fe7..e39b39c 100644 --- a/core/modules/file/lib/Drupal/file/Tests/DownloadTest.php +++ b/core/modules/file/lib/Drupal/file/Tests/DownloadTest.php @@ -17,7 +17,7 @@ public static function getInfo() { return array( 'name' => 'File download', 'description' => 'Tests for file download/transfer functions.', - 'group' => 'File API', + 'group' => 'File Managed API', ); } diff --git a/core/modules/file/lib/Drupal/file/Tests/FileManagedTestBase.php b/core/modules/file/lib/Drupal/file/Tests/FileManagedTestBase.php index 85ee553..b4c70c3 100644 --- a/core/modules/file/lib/Drupal/file/Tests/FileManagedTestBase.php +++ b/core/modules/file/lib/Drupal/file/Tests/FileManagedTestBase.php @@ -7,14 +7,14 @@ namespace Drupal\file\Tests; -use Drupal\system\Tests\File\FileTestBase; -use \stdClass; +use Drupal\file\FileInterface; +use Drupal\simpletest\WebTestBase; /** * Base class for file tests that use the file_test module to test uploads and * hooks. */ -abstract class FileManagedTestBase extends FileTestBase { +abstract class FileManagedTestBase extends WebTestBase { /** * Modules to enable. @@ -90,6 +90,50 @@ function assertFileHookCalled($hook, $expected_count = 1, $message = NULL) { } /** + * Asserts that two files have the same values (except timestamp). + * + * @param \Drupal\file\FileInterface $before + * File object to compare. + * @param \Drupal\file\FileInterface $after + * File object to compare. + */ + function assertFileUnchanged(FileInterface $before, FileInterface $after) { + $this->assertEqual($before->id(), $after->id(), t('File id is the same: %file1 == %file2.', array('%file1' => $before->id(), '%file2' => $after->id())), 'File unchanged'); + $this->assertEqual($before->getOwner()->id(), $after->getOwner()->id(), t('File owner is the same: %file1 == %file2.', array('%file1' => $before->getOwner()->id(), '%file2' => $after->getOwner()->id())), 'File unchanged'); + $this->assertEqual($before->getFilename(), $after->getFilename(), t('File name is the same: %file1 == %file2.', array('%file1' => $before->getFilename(), '%file2' => $after->getFilename())), 'File unchanged'); + $this->assertEqual($before->getFileUri(), $after->getFileUri(), t('File path is the same: %file1 == %file2.', array('%file1' => $before->getFileUri(), '%file2' => $after->getFileUri())), 'File unchanged'); + $this->assertEqual($before->getMimeType(), $after->getMimeType(), t('File MIME type is the same: %file1 == %file2.', array('%file1' => $before->getMimeType(), '%file2' => $after->getMimeType())), 'File unchanged'); + $this->assertEqual($before->getSize(), $after->getSize(), t('File size is the same: %file1 == %file2.', array('%file1' => $before->getSize(), '%file2' => $after->getSize())), 'File unchanged'); + $this->assertEqual($before->isPermanent(), $after->isPermanent(), t('File status is the same: %file1 == %file2.', array('%file1' => $before->isPermanent(), '%file2' => $after->isPermanent())), 'File unchanged'); + } + + /** + * Asserts that two files are not the same by comparing the fid and filepath. + * + * @param \Drupal\file\FileInterface $file1 + * File object to compare. + * @param \Drupal\file\FileInterface $file2 + * File object to compare. + */ + function assertDifferentFile(FileInterface $file1, FileInterface $file2) { + $this->assertNotEqual($file1->id(), $file2->id(), t('Files have different ids: %file1 != %file2.', array('%file1' => $file1->id(), '%file2' => $file2->id())), 'Different file'); + $this->assertNotEqual($file1->getFileUri(), $file2->getFileUri(), t('Files have different paths: %file1 != %file2.', array('%file1' => $file1->getFileUri(), '%file2' => $file2->getFileUri())), 'Different file'); + } + + /** + * Asserts that two files are the same by comparing the fid and filepath. + * + * @param \Drupal\file\FileInterface $file1 + * File object to compare. + * @param \Drupal\file\FileInterface $file2 + * File object to compare. + */ + function assertSameFile(FileInterface $file1, FileInterface $file2) { + $this->assertEqual($file1->id(), $file2->id(), t('Files have the same ids: %file1 == %file2.', array('%file1' => $file1->id(), '%file2-fid' => $file2->id())), 'Same file'); + $this->assertEqual($file1->getFileUri(), $file2->getFileUri(), t('Files have the same path: %file1 == %file2.', array('%file1' => $file1->getFileUri(), '%file2' => $file2->getFileUri())), 'Same file'); + } + + /** * Create a file and save it to the files table and assert that it occurs * correctly. * @@ -106,7 +150,7 @@ function assertFileHookCalled($hook, $expected_count = 1, $message = NULL) { * File entity. */ function createFile($filepath = NULL, $contents = NULL, $scheme = NULL) { - $file = new stdClass(); + $file = new \stdClass(); $file->uri = $this->createUri($filepath, $contents, $scheme); $file->filename = drupal_basename($file->uri); $file->filemime = 'text/plain'; @@ -121,4 +165,41 @@ function createFile($filepath = NULL, $contents = NULL, $scheme = NULL) { return entity_create('file', (array) $file); } + + /** + * Creates a file and returns its URI. + * + * @param string $filepath + * Optional string specifying the file path. If none is provided then a + * randomly named file will be created in the site's files directory. + * @param string $contents + * Optional contents to save into the file. If a NULL value is provided an + * arbitrary string will be used. + * @param string $scheme + * Optional string indicating the stream scheme to use. Drupal core includes + * public, private, and temporary. The public wrapper is the default. + * + * @return string + * File URI. + */ + function createUri($filepath = NULL, $contents = NULL, $scheme = NULL) { + if (!isset($filepath)) { + // Prefix with non-latin characters to ensure that all file-related + // tests work with international filenames. + $filepath = 'Файл для тестирования ' . $this->randomName(); + } + if (!isset($scheme)) { + $scheme = file_default_scheme(); + } + $filepath = $scheme . '://' . $filepath; + + if (!isset($contents)) { + $contents = "file_put_contents() doesn't seem to appreciate empty strings so let's put in some data."; + } + + file_put_contents($filepath, $contents); + $this->assertTrue(is_file($filepath), t('The test file exists on the disk.'), 'Create test file'); + return $filepath; + } + } diff --git a/core/modules/file/lib/Drupal/file/Tests/LoadTest.php b/core/modules/file/lib/Drupal/file/Tests/LoadTest.php index 0199ea2..6a85e83 100644 --- a/core/modules/file/lib/Drupal/file/Tests/LoadTest.php +++ b/core/modules/file/lib/Drupal/file/Tests/LoadTest.php @@ -15,7 +15,7 @@ public static function getInfo() { return array( 'name' => 'File loading', 'description' => 'Tests the file_load() function.', - 'group' => 'File API', + 'group' => 'File Managed API', ); } diff --git a/core/modules/file/lib/Drupal/file/Tests/MoveTest.php b/core/modules/file/lib/Drupal/file/Tests/MoveTest.php index 5037af0..8fb7b07 100644 --- a/core/modules/file/lib/Drupal/file/Tests/MoveTest.php +++ b/core/modules/file/lib/Drupal/file/Tests/MoveTest.php @@ -15,7 +15,7 @@ public static function getInfo() { return array( 'name' => 'File moving', 'description' => 'Tests the file move function.', - 'group' => 'File API', + 'group' => 'File Managed API', ); } diff --git a/core/modules/file/lib/Drupal/file/Tests/RemoteFileSaveUploadTest.php b/core/modules/file/lib/Drupal/file/Tests/RemoteFileSaveUploadTest.php index b9932b7..6f2000c 100644 --- a/core/modules/file/lib/Drupal/file/Tests/RemoteFileSaveUploadTest.php +++ b/core/modules/file/lib/Drupal/file/Tests/RemoteFileSaveUploadTest.php @@ -21,7 +21,7 @@ class RemoteFileSaveUploadTest extends SaveUploadTest { public static function getInfo() { $info = parent::getInfo(); - $info['group'] = 'File API (remote)'; + $info['group'] = 'File Managed API (remote)'; return $info; } diff --git a/core/modules/file/lib/Drupal/file/Tests/SaveDataTest.php b/core/modules/file/lib/Drupal/file/Tests/SaveDataTest.php index 5579baf..9b1bf2a 100644 --- a/core/modules/file/lib/Drupal/file/Tests/SaveDataTest.php +++ b/core/modules/file/lib/Drupal/file/Tests/SaveDataTest.php @@ -15,7 +15,7 @@ public static function getInfo() { return array( 'name' => 'File save data', 'description' => 'Tests the file save data function.', - 'group' => 'File API', + 'group' => 'File Managed API', ); } diff --git a/core/modules/file/lib/Drupal/file/Tests/SaveTest.php b/core/modules/file/lib/Drupal/file/Tests/SaveTest.php index 4c3b0c0..c2f0c38 100644 --- a/core/modules/file/lib/Drupal/file/Tests/SaveTest.php +++ b/core/modules/file/lib/Drupal/file/Tests/SaveTest.php @@ -17,7 +17,7 @@ public static function getInfo() { return array( 'name' => 'File saving', 'description' => 'File saving tests', - 'group' => 'File API', + 'group' => 'File Managed API', ); } diff --git a/core/modules/file/lib/Drupal/file/Tests/SaveUploadTest.php b/core/modules/file/lib/Drupal/file/Tests/SaveUploadTest.php index 15e007b..d9b570f 100644 --- a/core/modules/file/lib/Drupal/file/Tests/SaveUploadTest.php +++ b/core/modules/file/lib/Drupal/file/Tests/SaveUploadTest.php @@ -30,7 +30,7 @@ public static function getInfo() { return array( 'name' => 'File uploading', 'description' => 'Tests the file uploading functions.', - 'group' => 'File API', + 'group' => 'File Managed API', ); } diff --git a/core/modules/file/lib/Drupal/file/Tests/SpaceUsedTest.php b/core/modules/file/lib/Drupal/file/Tests/SpaceUsedTest.php index 4925cbf..b98134b 100644 --- a/core/modules/file/lib/Drupal/file/Tests/SpaceUsedTest.php +++ b/core/modules/file/lib/Drupal/file/Tests/SpaceUsedTest.php @@ -15,7 +15,7 @@ public static function getInfo() { return array( 'name' => 'File space used tests', 'description' => 'Tests the spaceUsed() function.', - 'group' => 'File API', + 'group' => 'File Managed API', ); } diff --git a/core/modules/file/lib/Drupal/file/Tests/UsageTest.php b/core/modules/file/lib/Drupal/file/Tests/UsageTest.php index 5ce7937..713cc33 100644 --- a/core/modules/file/lib/Drupal/file/Tests/UsageTest.php +++ b/core/modules/file/lib/Drupal/file/Tests/UsageTest.php @@ -15,7 +15,7 @@ public static function getInfo() { return array( 'name' => 'File usage', 'description' => 'Tests the file usage functions.', - 'group' => 'File API', + 'group' => 'File Managed API', ); } diff --git a/core/modules/file/lib/Drupal/file/Tests/ValidateTest.php b/core/modules/file/lib/Drupal/file/Tests/ValidateTest.php index faef73f..5656fdc 100644 --- a/core/modules/file/lib/Drupal/file/Tests/ValidateTest.php +++ b/core/modules/file/lib/Drupal/file/Tests/ValidateTest.php @@ -15,7 +15,7 @@ public static function getInfo() { return array( 'name' => 'File validate', 'description' => 'Tests the file_validate() function.', - 'group' => 'File API', + 'group' => 'File Managed API', ); } diff --git a/core/modules/file/lib/Drupal/file/Tests/ValidatorTest.php b/core/modules/file/lib/Drupal/file/Tests/ValidatorTest.php index cfde9a3..62b51fb 100644 --- a/core/modules/file/lib/Drupal/file/Tests/ValidatorTest.php +++ b/core/modules/file/lib/Drupal/file/Tests/ValidatorTest.php @@ -15,7 +15,7 @@ public static function getInfo() { return array( 'name' => 'File validator tests', 'description' => 'Tests the functions used to validate uploaded files.', - 'group' => 'File API', + 'group' => 'File Managed API', ); } diff --git a/core/modules/simpletest/lib/Drupal/simpletest/DrupalUnitTestBase.php b/core/modules/simpletest/lib/Drupal/simpletest/DrupalUnitTestBase.php index b7848de..f4738fa 100644 --- a/core/modules/simpletest/lib/Drupal/simpletest/DrupalUnitTestBase.php +++ b/core/modules/simpletest/lib/Drupal/simpletest/DrupalUnitTestBase.php @@ -64,6 +64,15 @@ protected $keyValueFactory; /** + * A list of stream wrappers that have been registered for this test. + * + * @see \Drupal\simpletest\DrupalUnitTestBase::registerStreamWrapper() + * + * @var array + */ + private $streamWrappers = array(); + + /** * Overrides \Drupal\simpletest\UnitTestBase::__construct(). */ function __construct($test_id = NULL) { @@ -88,6 +97,7 @@ protected function setUp() { $this->keyValueFactory = new KeyValueMemoryFactory(); parent::setUp(); + // Build a minimal, partially mocked environment for unit tests. $this->containerBuild(\Drupal::getContainer()); // Make sure it survives kernel rebuilds. @@ -130,10 +140,28 @@ protected function setUp() { $this->enableModules($modules, FALSE); // In order to use theme functions default theme config needs to exist. \Drupal::config('system.theme')->set('default', 'stark'); + + // Tests based on DrupalUnitTestBase are entitled to use Drupal's File API. + $this->registerStreamWrapper('public', 'Drupal\Core\StreamWrapper\PublicStream'); + + // The private and temporary stream wrappers depend on the system.file + // configuration. If System module was enabled, the test will most likely + // install System module's configuration, so we can register them here. + if (in_array('system', $modules)) { + $this->registerStreamWrapper('private', 'Drupal\Core\StreamWrapper\PrivateStream'); + $this->registerStreamWrapper('temporary', 'Drupal\Core\StreamWrapper\TemporaryStream'); + } } protected function tearDown() { $this->kernel->shutdown(); + // Before tearing down the test environment, ensure that no stream wrapper + // of this test leaks into the parent environment. Unlike all other global + // state variables in Drupal, stream wrappers are a global state construct + // of PHP core, which has to be maintained manually. + foreach ($this->streamWrappers as $scheme) { + $this->unregisterStreamWrapper($scheme); + } parent::tearDown(); } @@ -334,4 +362,53 @@ protected function disableModules(array $modules) { ))); } + /** + * Registers a stream wrapper for this test. + * + * @param string $scheme + * The scheme to register. + * @param string $class + * The fully qualified class name to register. + * @param int $type + * The Drupal Stream Wrapper API type. Defaults to + * STREAM_WRAPPERS_LOCAL_NORMAL. + */ + protected function registerStreamWrapper($scheme, $class, $type = STREAM_WRAPPERS_LOCAL_NORMAL) { + if (isset($this->streamWrappers[$scheme])) { + $this->unregisterStreamWrapper($scheme); + } + $this->streamWrappers[$scheme] = $scheme; + if (($type & STREAM_WRAPPERS_LOCAL) == STREAM_WRAPPERS_LOCAL) { + stream_wrapper_register($scheme, $class); + } + else { + stream_wrapper_register($scheme, $class, STREAM_IS_URL); + } + // @todo Revamp Drupal's stream wrapper API for D8. + $wrappers = &drupal_static('file_get_stream_wrappers'); + $wrappers[$scheme] = array( + 'type' => $type, + 'class' => $class, + ); + $wrappers[STREAM_WRAPPERS_ALL] = $wrappers; + } + + /** + * Unregisters a stream wrapper previously registered by this test. + * + * DrupalUnitTestBase::tearDown() automatically cleans up all registered + * stream wrappers, so this usually does not have to be called manually. + * + * @param string $scheme + * The scheme to unregister. + */ + protected function unregisterStreamWrapper($scheme) { + stream_wrapper_unregister($scheme); + unset($this->streamWrappers[$scheme]); + // @todo Revamp Drupal's stream wrapper API for D8. + $wrappers = &drupal_static('file_get_stream_wrappers'); + unset($wrappers[$scheme]); + unset($wrappers[STREAM_WRAPPERS_ALL][$scheme]); + } + } diff --git a/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php b/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php index 669c615..dbf8440 100644 --- a/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php +++ b/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php @@ -963,6 +963,19 @@ protected function prepareEnvironment() { // Create and set new configuration directories. $this->prepareConfigDirectories(); + // Unregister all custom stream wrappers of the parent site. + // Availability of Drupal stream wrappers varies by test base class: + // - UnitTestBase operates in a completely empty environment. + // - DrupalUnitTestBase supports and maintains stream wrappers in a custom + // way. + // - WebTestBase re-initializes Drupal stream wrappers after installation. + // The original stream wrappers are restored after the test run. + // @see TestBase::tearDown() + $wrappers = file_get_stream_wrappers(); + foreach ($wrappers as $scheme => $info) { + stream_wrapper_unregister($scheme); + } + // Reset statics before the old container is replaced so that objects with a // __destruct() method still have access to it. // @todo: Remove once they have been converted to services. @@ -1106,10 +1119,6 @@ protected function tearDown() { } } - // In case a fatal error occurred that was not in the test process read the - // log to pick up any fatal errors. - simpletest_log_read($this->testId, $this->databasePrefix, get_class($this), TRUE); - // Delete temporary files directory. file_unmanaged_delete_recursive($this->originalFileDirectory . '/simpletest/' . substr($this->databasePrefix, 10), array($this, 'filePreDeleteCallback')); @@ -1139,6 +1148,17 @@ protected function tearDown() { // Restore original statics and globals. \Drupal::setContainer($this->originalContainer); $GLOBALS['config_directories'] = $this->originalConfigDirectories; + + // Re-initialize original stream wrappers of the parent site. + // This must happen after static variables have been reset and the original + // container and $config_directories are restored, as simpletest_log_read() + // uses the public stream wrapper to locate the error.log. + file_get_stream_wrappers(); + + // In case a fatal error occurred that was not in the test process read the + // log to pick up any fatal errors. + simpletest_log_read($this->testId, $this->databasePrefix, get_class($this), TRUE); + if (isset($this->originalPrefix)) { drupal_valid_test_ua($this->originalPrefix); } diff --git a/core/modules/system/lib/Drupal/system/Tests/File/ConfigTest.php b/core/modules/system/lib/Drupal/system/Tests/File/ConfigTest.php index ce48dfd..a38322b 100644 --- a/core/modules/system/lib/Drupal/system/Tests/File/ConfigTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/File/ConfigTest.php @@ -7,10 +7,12 @@ namespace Drupal\system\Tests\File; +use Drupal\simpletest\WebTestBase; + /** * File system configuration related tests. */ -class ConfigTest extends FileTestBase { +class ConfigTest extends WebTestBase { public static function getInfo() { return array( diff --git a/core/modules/system/lib/Drupal/system/Tests/File/FileTestBase.php b/core/modules/system/lib/Drupal/system/Tests/File/FileTestBase.php index 540c19a..2071ced 100644 --- a/core/modules/system/lib/Drupal/system/Tests/File/FileTestBase.php +++ b/core/modules/system/lib/Drupal/system/Tests/File/FileTestBase.php @@ -7,68 +7,28 @@ namespace Drupal\system\Tests\File; -use Drupal\file\FileInterface; -use Drupal\simpletest\WebTestBase; +use Drupal\simpletest\DrupalUnitTestBase; /** * Base class for file tests that adds some additional file specific * assertions and helper functions. */ -abstract class FileTestBase extends WebTestBase { - - function setUp() { - parent::setUp(); - // Make sure that custom stream wrappers are registered. - // @todo This has the potential to be a major bug deeply buried in File API; - // file_unmanaged_*() API functions and test functions are invoking native - // PHP functions directly, whereas Drupal's custom stream wrappers are not - // registered yet. - file_get_stream_wrappers(); - } +abstract class FileTestBase extends DrupalUnitTestBase { /** - * Check that two files have the same values for all fields other than the - * timestamp. + * Modules to enable. * - * @param \Drupal\file\FileInterface $before - * File object to compare. - * @param \Drupal\file\FileInterface $after - * File object to compare. + * @var array */ - function assertFileUnchanged(FileInterface $before, FileInterface $after) { - $this->assertEqual($before->id(), $after->id(), t('File id is the same: %file1 == %file2.', array('%file1' => $before->id(), '%file2' => $after->id())), 'File unchanged'); - $this->assertEqual($before->getOwner()->id(), $after->getOwner()->id(), t('File owner is the same: %file1 == %file2.', array('%file1' => $before->getOwner()->id(), '%file2' => $after->getOwner()->id())), 'File unchanged'); - $this->assertEqual($before->getFilename(), $after->getFilename(), t('File name is the same: %file1 == %file2.', array('%file1' => $before->getFilename(), '%file2' => $after->getFilename())), 'File unchanged'); - $this->assertEqual($before->getFileUri(), $after->getFileUri(), t('File path is the same: %file1 == %file2.', array('%file1' => $before->getFileUri(), '%file2' => $after->getFileUri())), 'File unchanged'); - $this->assertEqual($before->getMimeType(), $after->getMimeType(), t('File MIME type is the same: %file1 == %file2.', array('%file1' => $before->getMimeType(), '%file2' => $after->getMimeType())), 'File unchanged'); - $this->assertEqual($before->getSize(), $after->getSize(), t('File size is the same: %file1 == %file2.', array('%file1' => $before->getSize(), '%file2' => $after->getSize())), 'File unchanged'); - $this->assertEqual($before->isPermanent(), $after->isPermanent(), t('File status is the same: %file1 == %file2.', array('%file1' => $before->isPermanent(), '%file2' => $after->isPermanent())), 'File unchanged'); - } + public static $modules = array('system'); - /** - * Check that two files are not the same by comparing the fid and filepath. - * - * @param \Drupal\file\FileInterface $file1 - * File object to compare. - * @param \Drupal\file\FileInterface $file2 - * File object to compare. - */ - function assertDifferentFile(FileInterface $file1, FileInterface $file2) { - $this->assertNotEqual($file1->id(), $file2->id(), t('Files have different ids: %file1 != %file2.', array('%file1' => $file1->id(), '%file2' => $file2->id())), 'Different file'); - $this->assertNotEqual($file1->getFileUri(), $file2->getFileUri(), t('Files have different paths: %file1 != %file2.', array('%file1' => $file1->getFileUri(), '%file2' => $file2->getFileUri())), 'Different file'); - } + function setUp() { + parent::setUp(); + $this->installConfig(array('system')); - /** - * Check that two files are the same by comparing the fid and filepath. - * - * @param \Drupal\file\FileInterface $file1 - * File object to compare. - * @param \Drupal\file\FileInterface $file2 - * File object to compare. - */ - function assertSameFile(FileInterface $file1, FileInterface $file2) { - $this->assertEqual($file1->id(), $file2->id(), t('Files have the same ids: %file1 == %file2.', array('%file1' => $file1->id(), '%file2-fid' => $file2->id())), 'Same file'); - $this->assertEqual($file1->getFileUri(), $file2->getFileUri(), t('Files have the same path: %file1 == %file2.', array('%file1' => $file1->getFileUri(), '%file2' => $file2->getFileUri())), 'Same file'); + if (isset($this->scheme)) { + $this->registerStreamWrapper($this->scheme, $this->classname); + } } /** @@ -170,7 +130,7 @@ function createDirectory($path = NULL) { return $path; } - /** + /** * Create a file and return the URI of it. * * @param $filepath diff --git a/core/modules/system/lib/Drupal/system/Tests/File/MimeTypeTest.php b/core/modules/system/lib/Drupal/system/Tests/File/MimeTypeTest.php index 5ee981d..e9c388d 100644 --- a/core/modules/system/lib/Drupal/system/Tests/File/MimeTypeTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/File/MimeTypeTest.php @@ -7,12 +7,10 @@ namespace Drupal\system\Tests\File; -use Drupal\simpletest\WebTestBase; - /** * Tests for file_get_mimetype(). */ -class MimeTypeTest extends WebTestBase { +class MimeTypeTest extends FileTestBase { /** * Modules to enable. diff --git a/core/modules/system/lib/Drupal/system/Tests/File/ReadOnlyStreamWrapperTest.php b/core/modules/system/lib/Drupal/system/Tests/File/ReadOnlyStreamWrapperTest.php index ef84796..779ba12 100644 --- a/core/modules/system/lib/Drupal/system/Tests/File/ReadOnlyStreamWrapperTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/File/ReadOnlyStreamWrapperTest.php @@ -12,13 +12,6 @@ */ class ReadOnlyStreamWrapperTest extends FileTestBase { - /** - * Modules to enable. - * - * @var array - */ - public static $modules = array('file_test'); - protected $scheme = 'dummy-readonly'; protected $classname = 'Drupal\file_test\DummyReadOnlyStreamWrapper'; @@ -30,16 +23,6 @@ public static function getInfo() { ); } - function setUp() { - parent::setUp(); - drupal_static_reset('file_get_stream_wrappers'); - } - - function tearDown() { - parent::tearDown(); - stream_wrapper_unregister($this->scheme); - } - /** * Test write functionality of the read-only stream wrapper. */ diff --git a/core/modules/system/lib/Drupal/system/Tests/File/RemoteFileDirectoryTest.php b/core/modules/system/lib/Drupal/system/Tests/File/RemoteFileDirectoryTest.php index 4c7abd2..af39312 100644 --- a/core/modules/system/lib/Drupal/system/Tests/File/RemoteFileDirectoryTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/File/RemoteFileDirectoryTest.php @@ -19,6 +19,9 @@ class RemoteFileDirectoryTest extends DirectoryTest { */ public static $modules = array('file_test'); + protected $scheme = 'dummy-remote'; + protected $classname = 'Drupal\file_test\DummyRemoteStreamWrapper'; + public static function getInfo() { $info = parent::getInfo(); $info['group'] = 'File API (remote)'; @@ -26,7 +29,7 @@ public static function getInfo() { } function setUp() { - parent::setUp('file_test'); + parent::setUp(); \Drupal::config('system.file')->set('default_scheme', 'dummy-remote')->save(); } } diff --git a/core/modules/system/lib/Drupal/system/Tests/File/RemoteFileScanDirectoryTest.php b/core/modules/system/lib/Drupal/system/Tests/File/RemoteFileScanDirectoryTest.php index fe554cd..93e2016 100644 --- a/core/modules/system/lib/Drupal/system/Tests/File/RemoteFileScanDirectoryTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/File/RemoteFileScanDirectoryTest.php @@ -19,6 +19,9 @@ class RemoteFileScanDirectoryTest extends ScanDirectoryTest { */ public static $modules = array('file_test'); + protected $scheme = 'dummy-remote'; + protected $classname = 'Drupal\file_test\DummyRemoteStreamWrapper'; + public static function getInfo() { $info = parent::getInfo(); $info['group'] = 'File API (remote)'; @@ -26,7 +29,7 @@ public static function getInfo() { } function setUp() { - parent::setUp('file_test'); + parent::setUp(); \Drupal::config('system.file')->set('default_scheme', 'dummy-remote')->save(); } } diff --git a/core/modules/system/lib/Drupal/system/Tests/File/RemoteFileUnmanagedCopyTest.php b/core/modules/system/lib/Drupal/system/Tests/File/RemoteFileUnmanagedCopyTest.php index 21973ba..d81213e 100644 --- a/core/modules/system/lib/Drupal/system/Tests/File/RemoteFileUnmanagedCopyTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/File/RemoteFileUnmanagedCopyTest.php @@ -19,6 +19,9 @@ class RemoteFileUnmanagedCopyTest extends UnmanagedCopyTest { */ public static $modules = array('file_test'); + protected $scheme = 'dummy-remote'; + protected $classname = 'Drupal\file_test\DummyRemoteStreamWrapper'; + public static function getInfo() { $info = parent::getInfo(); $info['group'] = 'File API (remote)'; @@ -26,7 +29,7 @@ public static function getInfo() { } function setUp() { - parent::setUp('file_test'); + parent::setUp(); \Drupal::config('system.file')->set('default_scheme', 'dummy-remote')->save(); } } diff --git a/core/modules/system/lib/Drupal/system/Tests/File/RemoteFileUnmanagedDeleteRecursiveTest.php b/core/modules/system/lib/Drupal/system/Tests/File/RemoteFileUnmanagedDeleteRecursiveTest.php index 9fbafac..a43c3a1 100644 --- a/core/modules/system/lib/Drupal/system/Tests/File/RemoteFileUnmanagedDeleteRecursiveTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/File/RemoteFileUnmanagedDeleteRecursiveTest.php @@ -19,6 +19,9 @@ class RemoteFileUnmanagedDeleteRecursiveTest extends UnmanagedDeleteRecursiveTes */ public static $modules = array('file_test'); + protected $scheme = 'dummy-remote'; + protected $classname = 'Drupal\file_test\DummyRemoteStreamWrapper'; + public static function getInfo() { $info = parent::getInfo(); $info['group'] = 'File API (remote)'; @@ -26,7 +29,7 @@ public static function getInfo() { } function setUp() { - parent::setUp('file_test'); + parent::setUp(); \Drupal::config('system.file')->set('default_scheme', 'dummy-remote')->save(); } } diff --git a/core/modules/system/lib/Drupal/system/Tests/File/RemoteFileUnmanagedDeleteTest.php b/core/modules/system/lib/Drupal/system/Tests/File/RemoteFileUnmanagedDeleteTest.php index b5dfcf7..d6a9b07 100644 --- a/core/modules/system/lib/Drupal/system/Tests/File/RemoteFileUnmanagedDeleteTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/File/RemoteFileUnmanagedDeleteTest.php @@ -19,6 +19,9 @@ class RemoteFileUnmanagedDeleteTest extends UnmanagedDeleteTest { */ public static $modules = array('file_test'); + protected $scheme = 'dummy-remote'; + protected $classname = 'Drupal\file_test\DummyRemoteStreamWrapper'; + public static function getInfo() { $info = parent::getInfo(); $info['group'] = 'File API (remote)'; @@ -26,7 +29,7 @@ public static function getInfo() { } function setUp() { - parent::setUp('file_test'); + parent::setUp(); \Drupal::config('system.file')->set('default_scheme', 'dummy-remote')->save(); } } diff --git a/core/modules/system/lib/Drupal/system/Tests/File/RemoteFileUnmanagedMoveTest.php b/core/modules/system/lib/Drupal/system/Tests/File/RemoteFileUnmanagedMoveTest.php index 243c22d..52a74fb 100644 --- a/core/modules/system/lib/Drupal/system/Tests/File/RemoteFileUnmanagedMoveTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/File/RemoteFileUnmanagedMoveTest.php @@ -19,6 +19,9 @@ class RemoteFileUnmanagedMoveTest extends UnmanagedMoveTest { */ public static $modules = array('file_test'); + protected $scheme = 'dummy-remote'; + protected $classname = 'Drupal\file_test\DummyRemoteStreamWrapper'; + public static function getInfo() { $info = parent::getInfo(); $info['group'] = 'File API (remote)'; diff --git a/core/modules/system/lib/Drupal/system/Tests/File/RemoteFileUnmanagedSaveDataTest.php b/core/modules/system/lib/Drupal/system/Tests/File/RemoteFileUnmanagedSaveDataTest.php index 1e5712a..8dfd0e9 100644 --- a/core/modules/system/lib/Drupal/system/Tests/File/RemoteFileUnmanagedSaveDataTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/File/RemoteFileUnmanagedSaveDataTest.php @@ -19,6 +19,9 @@ class RemoteFileUnmanagedSaveDataTest extends UnmanagedSaveDataTest { */ public static $modules = array('file_test'); + protected $scheme = 'dummy-remote'; + protected $classname = 'Drupal\file_test\DummyRemoteStreamWrapper'; + public static function getInfo() { $info = parent::getInfo(); $info['group'] = 'File API (remote)'; @@ -26,7 +29,7 @@ public static function getInfo() { } function setUp() { - parent::setUp('file_test'); + parent::setUp(); \Drupal::config('system.file')->set('default_scheme', 'dummy-remote')->save(); } } diff --git a/core/modules/system/lib/Drupal/system/Tests/File/StreamWrapperTest.php b/core/modules/system/lib/Drupal/system/Tests/File/StreamWrapperTest.php index 16bc592..e095ed4 100644 --- a/core/modules/system/lib/Drupal/system/Tests/File/StreamWrapperTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/File/StreamWrapperTest.php @@ -8,12 +8,11 @@ namespace Drupal\system\Tests\File; use Drupal\Core\StreamWrapper\PublicStream; -use Drupal\simpletest\WebTestBase; /** * Tests stream wrapper functions. */ -class StreamWrapperTest extends WebTestBase { +class StreamWrapperTest extends FileTestBase { /** * Modules to enable. @@ -33,16 +32,6 @@ public static function getInfo() { ); } - function setUp() { - parent::setUp(); - drupal_static_reset('file_get_stream_wrappers'); - } - - function tearDown() { - parent::tearDown(); - stream_wrapper_unregister($this->scheme); - } - /** * Test the getClassName() function. */ diff --git a/core/modules/system/lib/Drupal/system/Tests/File/UrlRewritingTest.php b/core/modules/system/lib/Drupal/system/Tests/File/UrlRewritingTest.php index 9022632..11f9d16 100644 --- a/core/modules/system/lib/Drupal/system/Tests/File/UrlRewritingTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/File/UrlRewritingTest.php @@ -7,6 +7,8 @@ namespace Drupal\system\Tests\File; +use Symfony\Component\HttpFoundation\Request; + /** * Tests for file URL rewriting. */ @@ -23,7 +25,7 @@ public static function getInfo() { return array( 'name' => 'File URL rewriting', 'description' => 'Tests for file URL rewriting.', - 'group' => 'File', + 'group' => 'File API', ); } @@ -95,6 +97,11 @@ function testRelativeFileURL() { // Disable file_test.module's hook_file_url_alter() implementation. \Drupal::state()->set('file_test.hook_file_url_alter', NULL); + // Create a mock Request for file_url_transform_relative(). + $request = Request::create($GLOBALS['base_url']); + $this->container->set('request', $request); + \Drupal::setContainer($this->container); + // Shipped file. $filepath = 'core/assets/vendor/jquery/jquery.js'; $url = file_create_url($filepath);