Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / migrate / tests / src / Unit / process / FileCopyTest.php
1 <?php
2
3 namespace Drupal\Tests\migrate\Unit\process;
4
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;
9
10 /**
11  * Flag for dealing with existing files: Appends number until name is unique.
12  */
13 define('FILE_EXISTS_RENAME', 0);
14
15 /**
16  * Flag for dealing with existing files: Replace the existing file.
17  */
18 define('FILE_EXISTS_REPLACE', 1);
19
20 /**
21  * Flag for dealing with existing files: Do nothing and return FALSE.
22  */
23 define('FILE_EXISTS_ERROR', 2);
24
25 /**
26  * Tests the file copy process plugin.
27  *
28  * @group migrate
29  * @group legacy
30  *
31  * @coversDefaultClass \Drupal\migrate\Plugin\migrate\process\FileCopy
32  */
33 class FileCopyTest extends MigrateProcessTestCase {
34
35   /**
36    * Tests that the rename configuration key will trigger a deprecation notice.
37    *
38    * @dataProvider providerDeprecationNoticeRename
39    *
40    * @param array $configuration
41    *   The plugin configuration.
42    * @param $expected
43    *   The expected value of the plugin configuration.
44    *
45    * @expectedDeprecation Using the key 'rename' is deprecated, use 'file_exists' => 'rename' instead. See https://www.drupal.org/node/2981389.
46    */
47   public function testDeprecationNoticeRename($configuration, $expected) {
48     $this->assertPlugin($configuration, $expected);
49   }
50
51   /**
52    * Data provider for testDeprecationNoticeRename.
53    */
54   public function providerDeprecationNoticeRename() {
55     return [
56       [['rename' => TRUE], FILE_EXISTS_RENAME],
57       [['rename' => FALSE], FILE_EXISTS_REPLACE],
58     ];
59   }
60
61   /**
62    * Tests that the reuse configuration key will trigger a deprecation notice.
63    *
64    * @dataProvider providerDeprecationNoticeReuse
65    *
66    * @param array $configuration
67    *   The plugin configuration.
68    * @param $expected
69    *   The expected value of the plugin configuration.
70    *
71    * @expectedDeprecation Using the key 'reuse' is deprecated, use 'file_exists' => 'use existing' instead. See https://www.drupal.org/node/2981389.
72    */
73   public function testDeprecationNoticeReuse($configuration, $expected) {
74     $this->assertPlugin($configuration, $expected);
75   }
76
77   /**
78    * Data provider for testDeprecationNoticeReuse.
79    */
80   public function providerDeprecationNoticeReuse() {
81     return [
82       [['reuse' => TRUE], FILE_EXISTS_ERROR],
83       [['reuse' => FALSE], FILE_EXISTS_REPLACE],
84     ];
85   }
86
87   /**
88    * Tests that the plugin constructor correctly sets the configuration.
89    *
90    * @dataProvider providerFileProcessBaseConstructor
91    *
92    * @param array $configuration
93    *   The plugin configuration.
94    * @param $expected
95    *   The expected value of the plugin configuration.
96    */
97   public function testFileProcessBaseConstructor($configuration, $expected) {
98     $this->assertPlugin($configuration, $expected);
99   }
100
101   /**
102    * Data provider for testFileProcessBaseConstructor.
103    */
104   public function providerFileProcessBaseConstructor() {
105     return [
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],
111     ];
112   }
113
114   /**
115    * Creates a TestFileCopy process plugin.
116    *
117    * @param array $configuration
118    *   The plugin configuration.
119    * @param $expected
120    *   The expected value of the plugin configuration.
121    */
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']);
130   }
131
132 }
133
134 /**
135  * Class for testing FileCopy.
136  */
137 class TestFileCopy extends FileCopy {
138
139   /**
140    * Gets this plugin's configuration.
141    *
142    * @return array
143    *   An array of this plugin's configuration.
144    */
145   public function getConfiguration() {
146     return $this->configuration;
147   }
148
149 }