2 namespace Drush\SiteAlias;
4 use PHPUnit\Framework\TestCase;
5 use Consolidation\SiteAlias\SiteAliasFileDiscovery;
7 class LegacyAliasConverterTest extends TestCase
9 use \Drush\FixtureFactory;
10 use \Drush\FunctionUtils;
16 protected function setUp()
18 $this->discovery = new SiteAliasFileDiscovery();
19 $this->discovery->addSearchLocation($this->fixturesDir() . '/sitealiases/legacy');
21 $this->sut = new LegacyAliasConverter($this->discovery);
23 $this->target = $this->tempdir();
24 $this->sut->setTargetDir($this->target);
27 protected function tearDown()
29 $this->removeDir($this->target);
32 protected function tempdir()
34 $tempfile = tempnam(sys_get_temp_dir(),'');
35 if (file_exists($tempfile)) {
39 if (is_dir($tempfile)) {
44 public function testWriteOne()
46 $testPath = $this->target . '/testWriteOne.yml';
47 $checksumPath = $this->target . '/.checksums/testWriteOne.md5';
48 $testContents = 'test: This is the initial file contents';
50 // Write the data once, and confirm it was written.
51 $this->callProtected('writeOne', [$testPath, $testContents]);
52 $this->assertStringEqualsFile($testPath, $testContents);
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);
59 $overwriteContents = 'test: Overwrite the file contents';
61 // Write the data again, and confirm it was changed.
62 $this->callProtected('writeOne', [$testPath, $overwriteContents]);
63 $this->assertStringEqualsFile($testPath, $overwriteContents);
65 $simulatedEditedContents = 'test: My simulated edit';
66 file_put_contents($testPath, $simulatedEditedContents);
68 $ignoredContents = 'test: Data that is not written';
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);
75 // Write yet again, this time removing the target so that it will
78 $this->callProtected('writeOne', [$testPath, $overwriteContents]);
79 $this->assertStringEqualsFile($testPath, $overwriteContents);
80 $this->assertFileExists($checksumPath);
82 // Remove the checksum file, and confirm that the target cannot
84 unlink($checksumPath);
85 $this->callProtected('writeOne', [$testPath, $ignoredContents]);
86 $this->assertStringEqualsFile($testPath, $overwriteContents);
89 public function testConvertAll()
91 $legacyFiles = $this->discovery->findAllLegacyAliasFiles();
92 $result = $this->callProtected('convertAll', [$legacyFiles]);
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']);
99 public function testWriteAll()
101 $convertedFileFixtures = [
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');
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));
122 * Test to see if the data converter produces the right data for the
123 * legacy alias file fixtures.
125 * @dataProvider convertLegacyFileTestData
127 public function testConvertLegacyFile($source, $expected)
129 $legacyFile = $this->fixturesDir() . '/sitealiases/legacy/' . $source;
130 $result = $this->callProtected('convertLegacyFile', [$legacyFile]);
131 $this->assertEquals($expected, $result);
134 public function convertLegacyFileTestData()
138 'one.alias.drushrc.php',
144 'uri' => 'http://example.com',
145 'root' => '/path/to/drupal',
152 'server.aliases.drushrc.php',
158 'host' => 'hydrogen.server.org',
159 'user' => 'www-admin',
162 'nitrogen.site.yml' =>
166 'host' => 'nitrogen.server.org',
174 'pantheon.aliases.drushrc.php',
176 'outlandish-josh.site.yml' =>
180 'uri' => 'dev-outlandish-josh.pantheonsite.io',
181 'host' => 'appserver.dev.site-id.drush.in',
182 'user' => 'dev.site-id',
184 'files' => 'code/sites/default/files',
185 'drush-script' => 'drush',
188 'db-url' => 'mysql://pantheon:pw@dbserver.dev.site-id.drush.in:21086/pantheon',
189 'db-allows-remote' => true,
192 'options' => '-p 2222 -o "AddressFamily inet"',
197 'uri' => 'www.outlandishjosh.com',
198 'host' => 'appserver.live.site-id.drush.in',
199 'user' => 'live.site-id',
201 'files' => 'code/sites/default/files',
202 'drush-script' => 'drush',
205 'db-url' => 'mysql://pantheon:pw@dbserver.live.site-id.drush.in:10516/pantheon',
206 'db-allows-remote' => true,
209 'options' => '-p 2222 -o "AddressFamily inet"',
214 'uri' => 'test-outlandish-josh.pantheonsite.io',
215 'host' => 'appserver.test.site-id.drush.in',
216 'user' => 'test.site-id',
218 'files' => 'code/sites/default/files',
219 'drush-script' => 'drush',
222 'db-url' => 'mysql://pantheon:pw@dbserver.test.site-id.drush.in:11621/pantheon',
223 'db-allows-remote' => true,
226 'options' => '-p 2222 -o "AddressFamily inet"',
234 // Future: this test includes 'parent' and 'target-command-specific',
235 // which are not converted yet.
238 'cc.aliases.drushrc.php',