Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / vendor / drush / drush / isolation / tests / LegacyAliasConverterTest.php
1 <?php
2 namespace Drush\SiteAlias;
3
4 use PHPUnit\Framework\TestCase;
5 use Consolidation\SiteAlias\SiteAliasFileDiscovery;
6
7 class LegacyAliasConverterTest extends TestCase
8 {
9     use \Drush\FixtureFactory;
10     use \Drush\FunctionUtils;
11     use \Drush\FSUtils;
12
13     protected $discovery;
14     protected $target;
15
16     protected function setUp()
17     {
18         $this->discovery = new SiteAliasFileDiscovery();
19         $this->discovery->addSearchLocation($this->fixturesDir() . '/sitealiases/legacy');
20
21         $this->sut = new LegacyAliasConverter($this->discovery);
22
23         $this->target = $this->tempdir();
24         $this->sut->setTargetDir($this->target);
25     }
26
27     protected function tearDown()
28     {
29         $this->removeDir($this->target);
30     }
31
32     protected function tempdir()
33     {
34         $tempfile = tempnam(sys_get_temp_dir(),'');
35         if (file_exists($tempfile)) {
36             unlink($tempfile);
37         }
38         mkdir($tempfile);
39         if (is_dir($tempfile)) {
40             return $tempfile;
41         }
42     }
43
44     public function testWriteOne()
45     {
46         $testPath = $this->target . '/testWriteOne.yml';
47         $checksumPath = $this->target . '/.checksums/testWriteOne.md5';
48         $testContents = 'test: This is the initial file contents';
49
50         // Write the data once, and confirm it was written.
51         $this->callProtected('writeOne', [$testPath, $testContents]);
52         $this->assertStringEqualsFile($testPath, $testContents);
53
54         // Check to see that the checksum file was written, and that
55         // it contains a useful comment.
56         $checksumContents = file_get_contents($checksumPath);
57         $this->assertContains("# Checksum for converted Drush alias file testWriteOne.yml.\n# Delete this checksum file or modify testWriteOne.yml to prevent further updates to it.", $checksumContents);
58
59         $overwriteContents = 'test: Overwrite the file contents';
60
61         // Write the data again, and confirm it was changed.
62         $this->callProtected('writeOne', [$testPath, $overwriteContents]);
63         $this->assertStringEqualsFile($testPath, $overwriteContents);
64
65         $simulatedEditedContents = 'test: My simulated edit';
66         file_put_contents($testPath, $simulatedEditedContents);
67
68         $ignoredContents = 'test: Data that is not written';
69
70         // Write the yet data again; this time, confirm that
71         // nothing changed, because the checksum does not match.
72         $this->callProtected('writeOne', [$testPath, $ignoredContents]);
73         $this->assertStringEqualsFile($testPath, $simulatedEditedContents);
74
75         // Write yet again, this time removing the target so that it will
76         // be writable again.
77         unlink($testPath);
78         $this->callProtected('writeOne', [$testPath, $overwriteContents]);
79         $this->assertStringEqualsFile($testPath, $overwriteContents);
80         $this->assertFileExists($checksumPath);
81
82         // Remove the checksum file, and confirm that the target cannot
83         // be overwritten
84         unlink($checksumPath);
85         $this->callProtected('writeOne', [$testPath, $ignoredContents]);
86         $this->assertStringEqualsFile($testPath, $overwriteContents);
87     }
88
89     public function testConvertAll()
90     {
91         $legacyFiles = $this->discovery->findAllLegacyAliasFiles();
92         $result = $this->callProtected('convertAll', [$legacyFiles]);
93         ksort($result);
94         $this->assertEquals('cc.site.yml,isp.site.yml,live.site.yml,nitrogen.site.yml,one.site.yml,outlandish-josh.site.yml,pantheon.site.yml,server.site.yml,update.site.yml', implode(',', array_keys($result)));
95         //$this->assertEquals('', var_export($result, true));
96         $this->assertEquals('dev-outlandish-josh.pantheonsite.io', $result['outlandish-josh.site.yml']['dev']['uri']);
97     }
98
99     public function testWriteAll()
100     {
101         $convertedFileFixtures = [
102             'a.yml' => [
103                 'foo' => 'bar',
104             ],
105             'b.yml' => [
106             ],
107         ];
108
109         $this->callProtected('cacheConvertedFilePath', ['b.aliases.drushrc.php', 'b.yml']);
110         $this->callProtected('writeAll', [$convertedFileFixtures]);
111         $this->assertFileExists($this->target . '/a.yml');
112         $this->assertFileExists($this->target . '/.checksums/a.md5');
113         $this->assertFileExists($this->target . '/b.yml');
114         $this->assertFileExists($this->target . '/.checksums/b.md5');
115
116         $this->assertStringEqualsFile($this->target . '/b.yml', "# This is a placeholder file used to track when b.aliases.drushrc.php was converted.\n# If you delete b.aliases.drushrc.php, then you may delete this file.");
117         $aContents = file_get_contents($this->target . '/a.yml');
118         $this->assertEquals('foo: bar', trim($aContents));
119     }
120
121     /**
122      * Test to see if the data converter produces the right data for the
123      * legacy alias file fixtures.
124      *
125      * @dataProvider convertLegacyFileTestData
126      */
127     public function testConvertLegacyFile($source, $expected)
128     {
129         $legacyFile = $this->fixturesDir() . '/sitealiases/legacy/' . $source;
130         $result = $this->callProtected('convertLegacyFile', [$legacyFile]);
131         $this->assertEquals($expected, $result);
132     }
133
134     public function convertLegacyFileTestData()
135     {
136         return [
137             [
138                 'one.alias.drushrc.php',
139                 [
140                     'one.site.yml' =>
141                     [
142                         'dev' =>
143                         [
144                             'uri' => 'http://example.com',
145                             'root' => '/path/to/drupal',
146                         ],
147                     ],
148                 ],
149             ],
150
151             [
152                 'server.aliases.drushrc.php',
153                 [
154                     'isp.site.yml' =>
155                     [
156                         'dev' =>
157                         [
158                             'host' => 'hydrogen.server.org',
159                             'user' => 'www-admin',
160                         ],
161                     ],
162                     'nitrogen.site.yml' =>
163                     [
164                         'dev' =>
165                         [
166                             'host' => 'nitrogen.server.org',
167                             'user' => 'admin',
168                         ],
169                     ],
170                 ],
171             ],
172
173             [
174                 'pantheon.aliases.drushrc.php',
175                 [
176                     'outlandish-josh.site.yml' =>
177                     [
178                         'dev' =>
179                         [
180                             'uri' => 'dev-outlandish-josh.pantheonsite.io',
181                             'host' => 'appserver.dev.site-id.drush.in',
182                             'user' => 'dev.site-id',
183                             'paths' => [
184                                 'files' => 'code/sites/default/files',
185                                 'drush-script' => 'drush',
186                             ],
187                             'options' => [
188                                 'db-url' => 'mysql://pantheon:pw@dbserver.dev.site-id.drush.in:21086/pantheon',
189                                 'db-allows-remote' => true,
190                             ],
191                             'ssh' => [
192                                 'options' => '-p 2222 -o "AddressFamily inet"',
193                             ],
194                         ],
195                         'live' =>
196                         [
197                             'uri' => 'www.outlandishjosh.com',
198                             'host' => 'appserver.live.site-id.drush.in',
199                             'user' => 'live.site-id',
200                             'paths' => [
201                                 'files' => 'code/sites/default/files',
202                                 'drush-script' => 'drush',
203                             ],
204                             'options' => [
205                                 'db-url' => 'mysql://pantheon:pw@dbserver.live.site-id.drush.in:10516/pantheon',
206                                 'db-allows-remote' => true,
207                             ],
208                             'ssh' => [
209                                 'options' => '-p 2222 -o "AddressFamily inet"',
210                             ],
211                         ],
212                         'test' =>
213                         [
214                             'uri' => 'test-outlandish-josh.pantheonsite.io',
215                             'host' => 'appserver.test.site-id.drush.in',
216                             'user' => 'test.site-id',
217                             'paths' => [
218                                 'files' => 'code/sites/default/files',
219                                 'drush-script' => 'drush',
220                             ],
221                             'options' => [
222                                 'db-url' => 'mysql://pantheon:pw@dbserver.test.site-id.drush.in:11621/pantheon',
223                                 'db-allows-remote' => true,
224                             ],
225                             'ssh' => [
226                                 'options' => '-p 2222 -o "AddressFamily inet"',
227                             ],
228                         ],
229                     ],
230                 ],
231             ],
232
233 /*
234             // Future: this test includes 'parent' and 'target-command-specific',
235             // which are not converted yet.
236
237             [
238                 'cc.aliases.drushrc.php',
239                 [
240                     'cc.site.yml' =>
241                     [
242                         'live' =>
243                         [
244                         ],
245
246                         'update' =>
247                         [
248                         ],
249                     ],
250                 ],
251             ],
252 */
253         ];
254     }
255 }