3 namespace Drupal\Tests\migrate\Unit\process;
5 use Drupal\Core\File\FileSystemInterface;
6 use Drupal\Core\StreamWrapper\StreamWrapperManagerInterface;
7 use Drupal\migrate\Plugin\migrate\process\FileCopy;
8 use Drupal\migrate\Plugin\MigrateProcessInterface;
11 * Flag for dealing with existing files: Appends number until name is unique.
13 define('FILE_EXISTS_RENAME', 0);
16 * Flag for dealing with existing files: Replace the existing file.
18 define('FILE_EXISTS_REPLACE', 1);
21 * Flag for dealing with existing files: Do nothing and return FALSE.
23 define('FILE_EXISTS_ERROR', 2);
26 * Tests the file copy process plugin.
31 * @coversDefaultClass \Drupal\migrate\Plugin\migrate\process\FileCopy
33 class FileCopyTest extends MigrateProcessTestCase {
36 * Tests that the rename configuration key will trigger a deprecation notice.
38 * @dataProvider providerDeprecationNoticeRename
40 * @param array $configuration
41 * The plugin configuration.
43 * The expected value of the plugin configuration.
45 * @expectedDeprecation Using the key 'rename' is deprecated, use 'file_exists' => 'rename' instead. See https://www.drupal.org/node/2981389.
47 public function testDeprecationNoticeRename($configuration, $expected) {
48 $this->assertPlugin($configuration, $expected);
52 * Data provider for testDeprecationNoticeRename.
54 public function providerDeprecationNoticeRename() {
56 [['rename' => TRUE], FILE_EXISTS_RENAME],
57 [['rename' => FALSE], FILE_EXISTS_REPLACE],
62 * Tests that the reuse configuration key will trigger a deprecation notice.
64 * @dataProvider providerDeprecationNoticeReuse
66 * @param array $configuration
67 * The plugin configuration.
69 * The expected value of the plugin configuration.
71 * @expectedDeprecation Using the key 'reuse' is deprecated, use 'file_exists' => 'use existing' instead. See https://www.drupal.org/node/2981389.
73 public function testDeprecationNoticeReuse($configuration, $expected) {
74 $this->assertPlugin($configuration, $expected);
78 * Data provider for testDeprecationNoticeReuse.
80 public function providerDeprecationNoticeReuse() {
82 [['reuse' => TRUE], FILE_EXISTS_ERROR],
83 [['reuse' => FALSE], FILE_EXISTS_REPLACE],
88 * Tests that the plugin constructor correctly sets the configuration.
90 * @dataProvider providerFileProcessBaseConstructor
92 * @param array $configuration
93 * The plugin configuration.
95 * The expected value of the plugin configuration.
97 public function testFileProcessBaseConstructor($configuration, $expected) {
98 $this->assertPlugin($configuration, $expected);
102 * Data provider for testFileProcessBaseConstructor.
104 public function providerFileProcessBaseConstructor() {
106 [['file_exists' => 'replace'], FILE_EXISTS_REPLACE],
107 [['file_exists' => 'rename'], FILE_EXISTS_RENAME],
108 [['file_exists' => 'use existing'], FILE_EXISTS_ERROR],
109 [['file_exists' => 'foobar'], FILE_EXISTS_REPLACE],
110 [[], FILE_EXISTS_REPLACE],
115 * Creates a TestFileCopy process plugin.
117 * @param array $configuration
118 * The plugin configuration.
120 * The expected value of the plugin configuration.
122 protected function assertPlugin($configuration, $expected) {
123 $stream_wrapper_manager = $this->prophesize(StreamWrapperManagerInterface::class)->reveal();
124 $file_system = $this->prophesize(FileSystemInterface::class)->reveal();
125 $download_plugin = $this->prophesize(MigrateProcessInterface::class)->reveal();
126 $this->plugin = new TestFileCopy($configuration, 'test', [], $stream_wrapper_manager, $file_system, $download_plugin);
127 $plugin_config = $this->plugin->getConfiguration();
128 $this->assertArrayHasKey('file_exists', $plugin_config);
129 $this->assertSame($expected, $plugin_config['file_exists']);
135 * Class for testing FileCopy.
137 class TestFileCopy extends FileCopy {
140 * Gets this plugin's configuration.
143 * An array of this plugin's configuration.
145 public function getConfiguration() {
146 return $this->configuration;