Same name in this branch
- 10 core/modules/file/tests/src/Functional/DownloadTest.php \Drupal\Tests\file\Functional\DownloadTest
- 10 core/modules/migrate/tests/src/Kernel/process/DownloadTest.php \Drupal\Tests\migrate\Kernel\process\DownloadTest
Same name and namespace in other branches
- 8.9.x core/modules/migrate/tests/src/Kernel/process/DownloadTest.php \Drupal\Tests\migrate\Kernel\process\DownloadTest
- 9 core/modules/migrate/tests/src/Kernel/process/DownloadTest.php \Drupal\Tests\migrate\Kernel\process\DownloadTest
Tests the download process plugin.
@group migrate
Hierarchy
- class \Drupal\KernelTests\Core\File\FileTestBase extends \Drupal\KernelTests\KernelTestBase
- class \Drupal\Tests\migrate\Kernel\process\DownloadTest
Expanded class hierarchy of DownloadTest
File
- core/
modules/ migrate/ tests/ src/ Kernel/ process/ DownloadTest.php, line 20
Namespace
Drupal\Tests\migrate\Kernel\processView source
class DownloadTest extends FileTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = [
'system',
];
/**
* {@inheritdoc}
*/
protected function setUp() : void {
parent::setUp();
$this->container
->get('stream_wrapper_manager')
->registerWrapper('temporary', 'Drupal\\Core\\StreamWrapper\\TemporaryStream', StreamWrapperInterface::LOCAL_NORMAL);
}
/**
* Tests a download that overwrites an existing local file.
*/
public function testOverwritingDownload() {
// Create a pre-existing file at the destination.
$destination_uri = $this
->createUri('existing_file.txt');
// Test destructive download.
$actual_destination = $this
->doTransform($destination_uri);
$this
->assertSame($destination_uri, $actual_destination, 'Import returned a destination that was not renamed');
$this
->assertFileDoesNotExist('public://existing_file_0.txt');
}
/**
* Tests a download that renames the downloaded file if there's a collision.
*/
public function testNonDestructiveDownload() {
// Create a pre-existing file at the destination.
$destination_uri = $this
->createUri('another_existing_file.txt');
// Test non-destructive download.
$actual_destination = $this
->doTransform($destination_uri, [
'file_exists' => 'rename',
]);
$this
->assertSame('public://another_existing_file_0.txt', $actual_destination, 'Import returned a renamed destination');
$this
->assertFileExists($actual_destination);
}
/**
* Tests that an exception is thrown if the destination URI is not writable.
*/
public function testWriteProtectedDestination() {
// Create a pre-existing file at the destination.
$destination_uri = $this
->createUri('not-writable.txt');
// Make the destination non-writable.
$this->container
->get('file_system')
->chmod($destination_uri, 0444);
// Pass or fail, we'll need to make the file writable again so the test
// can clean up after itself.
$fix_permissions = function () use ($destination_uri) {
$this->container
->get('file_system')
->chmod($destination_uri, 0755);
};
try {
$this
->doTransform($destination_uri);
$fix_permissions();
$this
->fail('MigrateException was not thrown for non-writable destination URI.');
} catch (MigrateException $e) {
$this
->assertTrue(TRUE, 'MigrateException was thrown for non-writable destination URI.');
$fix_permissions();
}
}
/**
* Runs an input value through the download plugin.
*
* @param string $destination_uri
* The destination URI to download to.
* @param array $configuration
* (optional) Configuration for the download plugin.
*
* @return string
* The local URI of the downloaded file.
*/
protected function doTransform($destination_uri, $configuration = []) {
// Prepare a mock HTTP client.
$this->container
->set('http_client', $this
->createMock(Client::class));
// Instantiate the plugin statically so it can pull dependencies out of
// the container.
$plugin = Download::create($this->container, $configuration, 'download', []);
// Execute the transformation.
$executable = $this
->createMock(MigrateExecutableInterface::class);
$row = new Row([], []);
// Return the downloaded file's local URI.
$value = [
'http://drupal.org/favicon.ico',
$destination_uri,
];
// Assert that number of stream resources in use is the same before and
// after the download.
$initial_count = count(get_resources('stream'));
$return = $plugin
->transform($value, $executable, $row, 'foo');
$this
->assertCount($initial_count, get_resources('stream'));
return $return;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DownloadTest:: |
protected static | property |
Modules to enable. Overrides FileTestBase:: |
|
DownloadTest:: |
protected | function | Runs an input value through the download plugin. | |
DownloadTest:: |
protected | function |
Overrides FileTestBase:: |
|
DownloadTest:: |
public | function | Tests a download that renames the downloaded file if there's a collision. | |
DownloadTest:: |
public | function | Tests a download that overwrites an existing local file. | |
DownloadTest:: |
public | function | Tests that an exception is thrown if the destination URI is not writable. | |
FileTestBase:: |
protected | property | A fully-qualified stream wrapper class name to register for the test. | 9 |
FileTestBase:: |
protected | property | A stream wrapper scheme to register for the test. | 9 |
FileTestBase:: |
public | function | Helper function to test the permissions of a directory. | |
FileTestBase:: |
public | function | Helper function to test the permissions of a file. | |
FileTestBase:: |
public | function | Create a directory and assert it exists. | |
FileTestBase:: |
public | function | Create a file and return the URI of it. | |
FileTestBase:: |
public | function | ||
FileTestBase:: |
protected | function |