Same filename in this branch
- 10 core/modules/file/tests/src/Functional/DownloadTest.php
- 10 core/modules/migrate/tests/src/Kernel/process/DownloadTest.php
Same filename and directory in other branches
- 8.9.x core/modules/migrate/tests/src/Kernel/process/DownloadTest.php
- 9 core/modules/migrate/tests/src/Kernel/process/DownloadTest.php
Namespace
Drupal\Tests\migrate\Kernel\process
File
core/modules/migrate/tests/src/Kernel/process/DownloadTest.php
View source
<?php
declare (strict_types=1);
namespace Drupal\Tests\migrate\Kernel\process;
use Drupal\Core\StreamWrapper\StreamWrapperInterface;
use Drupal\KernelTests\Core\File\FileTestBase;
use Drupal\migrate\MigrateException;
use Drupal\migrate\Plugin\migrate\process\Download;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\Row;
use GuzzleHttp\Client;
class DownloadTest extends FileTestBase {
protected static $modules = [
'system',
];
protected function setUp() : void {
parent::setUp();
$this->container
->get('stream_wrapper_manager')
->registerWrapper('temporary', 'Drupal\\Core\\StreamWrapper\\TemporaryStream', StreamWrapperInterface::LOCAL_NORMAL);
}
public function testOverwritingDownload() {
$destination_uri = $this
->createUri('existing_file.txt');
$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');
}
public function testNonDestructiveDownload() {
$destination_uri = $this
->createUri('another_existing_file.txt');
$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);
}
public function testWriteProtectedDestination() {
$destination_uri = $this
->createUri('not-writable.txt');
$this->container
->get('file_system')
->chmod($destination_uri, 0444);
$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();
}
}
protected function doTransform($destination_uri, $configuration = []) {
$this->container
->set('http_client', $this
->createMock(Client::class));
$plugin = Download::create($this->container, $configuration, 'download', []);
$executable = $this
->createMock(MigrateExecutableInterface::class);
$row = new Row([], []);
$value = [
'http://drupal.org/favicon.ico',
$destination_uri,
];
$initial_count = count(get_resources('stream'));
$return = $plugin
->transform($value, $executable, $row, 'foo');
$this
->assertCount($initial_count, get_resources('stream'));
return $return;
}
}
Classes