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