2 namespace Drush\SiteAlias;
4 use PHPUnit\Framework\TestCase;
6 class LegacyAliasConverterTest extends TestCase
8 use \Drush\FixtureFactory;
9 use \Drush\FunctionUtils;
15 protected function setUp()
17 $this->discovery = new SiteAliasFileDiscovery();
18 $this->discovery->addSearchLocation($this->fixturesDir() . '/sitealiases/legacy');
20 $this->sut = new LegacyAliasConverter($this->discovery);
22 $this->target = $this->tempdir();
23 $this->sut->setTargetDir($this->target);
26 protected function tearDown()
28 $this->removeDir($this->target);
31 protected function tempdir()
33 $tempfile = tempnam(sys_get_temp_dir(),'');
34 if (file_exists($tempfile)) {
38 if (is_dir($tempfile)) {
43 public function testWriteOne()
45 $testPath = $this->target . '/testWriteOne.yml';
46 $checksumPath = $this->target . '/.checksums/testWriteOne.md5';
47 $testContents = 'test: This is the initial file contents';
49 // Write the data once, and confirm it was written.
50 $this->callProtected('writeOne', [$testPath, $testContents]);
51 $this->assertStringEqualsFile($testPath, $testContents);
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);
58 $overwriteContents = 'test: Overwrite the file contents';
60 // Write the data again, and confirm it was changed.
61 $this->callProtected('writeOne', [$testPath, $overwriteContents]);
62 $this->assertStringEqualsFile($testPath, $overwriteContents);
64 $simulatedEditedContents = 'test: My simulated edit';
65 file_put_contents($testPath, $simulatedEditedContents);
67 $ignoredContents = 'test: Data that is not written';
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);
74 // Write yet again, this time removing the target so that it will
77 $this->callProtected('writeOne', [$testPath, $overwriteContents]);
78 $this->assertStringEqualsFile($testPath, $overwriteContents);
79 $this->assertFileExists($checksumPath);
81 // Remove the checksum file, and confirm that the target cannot
83 unlink($checksumPath);
84 $this->callProtected('writeOne', [$testPath, $ignoredContents]);
85 $this->assertStringEqualsFile($testPath, $overwriteContents);
88 public function testConvertAll()
90 $legacyFiles = $this->discovery->findAllLegacyAliasFiles();
91 $result = $this->callProtected('convertAll', [$legacyFiles]);
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']);
98 public function testWriteAll()
100 $convertedFileFixtures = [
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');
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));
121 * Test to see if the data converter produces the right data for the
122 * legacy alias file fixtures.
124 * @dataProvider convertLegacyFileTestData
126 public function testConvertLegacyFile($source, $expected)
128 $legacyFile = $this->fixturesDir() . '/sitealiases/legacy/' . $source;
129 $result = $this->callProtected('convertLegacyFile', [$legacyFile]);
130 $this->assertEquals($expected, $result);
133 public function convertLegacyFileTestData()
137 'one.alias.drushrc.php',
143 'uri' => 'http://example.com',
144 'root' => '/path/to/drupal',
151 'server.aliases.drushrc.php',
157 'host' => 'hydrogen.server.org',
158 'user' => 'www-admin',
161 'nitrogen.site.yml' =>
165 'host' => 'nitrogen.server.org',
173 'pantheon.aliases.drushrc.php',
175 'outlandish-josh.site.yml' =>
179 'uri' => 'dev-outlandish-josh.pantheonsite.io',
180 'host' => 'appserver.dev.site-id.drush.in',
181 'user' => 'dev.site-id',
183 'files' => 'code/sites/default/files',
184 'drush-script' => 'drush',
187 'db-url' => 'mysql://pantheon:pw@dbserver.dev.site-id.drush.in:21086/pantheon',
188 'db-allows-remote' => true,
191 'options' => '-p 2222 -o "AddressFamily inet"',
196 'uri' => 'www.outlandishjosh.com',
197 'host' => 'appserver.live.site-id.drush.in',
198 'user' => 'live.site-id',
200 'files' => 'code/sites/default/files',
201 'drush-script' => 'drush',
204 'db-url' => 'mysql://pantheon:pw@dbserver.live.site-id.drush.in:10516/pantheon',
205 'db-allows-remote' => true,
208 'options' => '-p 2222 -o "AddressFamily inet"',
213 'uri' => 'test-outlandish-josh.pantheonsite.io',
214 'host' => 'appserver.test.site-id.drush.in',
215 'user' => 'test.site-id',
217 'files' => 'code/sites/default/files',
218 'drush-script' => 'drush',
221 'db-url' => 'mysql://pantheon:pw@dbserver.test.site-id.drush.in:11621/pantheon',
222 'db-allows-remote' => true,
225 'options' => '-p 2222 -o "AddressFamily inet"',
233 // Future: this test includes 'parent' and 'target-command-specific',
234 // which are not converted yet.
237 'cc.aliases.drushrc.php',