a392e060d9556a8500275877ce878c5d9c095c00
[yaffs-website] / vendor / drupal-composer / drupal-scaffold / tests / PluginTest.php
1 <?php
2
3 namespace DrupalComposer\DrupalScaffold\Tests;
4
5 use Composer\Util\Filesystem;
6 use PHPUnit\Framework\TestCase;
7
8 /**
9  * Tests composer plugin functionality.
10  */
11 class PluginTest extends TestCase {
12
13   /**
14    * @var \Composer\Util\Filesystem
15    */
16   protected $fs;
17
18   /**
19    * @var string
20    */
21   protected $tmpDir;
22
23   /**
24    * @var string
25    */
26   protected $rootDir;
27
28   /**
29    * @var string
30    */
31   protected $tmpReleaseTag;
32
33   /**
34    * SetUp test.
35    */
36   public function setUp() {
37     $this->rootDir = realpath(realpath(__DIR__ . '/..'));
38
39     // Prepare temp directory.
40     $this->fs = new Filesystem();
41     $this->tmpDir = realpath(sys_get_temp_dir()) . DIRECTORY_SEPARATOR . 'drupal-scaffold';
42     $this->ensureDirectoryExistsAndClear($this->tmpDir);
43
44     $this->writeTestReleaseTag();
45     $this->writeComposerJSON();
46
47     chdir($this->tmpDir);
48   }
49
50   /**
51    * TearDown.
52    *
53    * @return void
54    */
55   public function tearDown() {
56     $this->fs->removeDirectory($this->tmpDir);
57     $this->git(sprintf('tag -d "%s"', $this->tmpReleaseTag));
58   }
59
60   /**
61    * Tests a simple composer install without core, but adding core later.
62    */
63   public function testComposerInstallAndUpdate() {
64     $exampleScaffoldFile = $this->tmpDir . DIRECTORY_SEPARATOR . 'index.php';
65     $this->assertFileNotExists($exampleScaffoldFile, 'Scaffold file should not be exist.');
66     $this->composer('install --no-dev --prefer-dist');
67     $this->assertFileExists($this->tmpDir . DIRECTORY_SEPARATOR . 'core', 'Drupal core is installed.');
68     $this->assertFileExists($exampleScaffoldFile, 'Scaffold file should be automatically installed.');
69     $this->fs->remove($exampleScaffoldFile);
70     $this->assertFileNotExists($exampleScaffoldFile, 'Scaffold file should not be exist.');
71     $this->composer('drupal:scaffold');
72     $this->assertFileExists($exampleScaffoldFile, 'Scaffold file should be installed by "drupal:scaffold" command.');
73
74     foreach (['8.0.1', '8.1.x-dev', '8.3.0', '8.5.x-dev'] as $version) {
75       // We touch a scaffold file, so we can check the file was modified after
76       // the scaffold update.
77       touch($exampleScaffoldFile);
78       $mtime_touched = filemtime($exampleScaffoldFile);
79       // Requiring a newer version triggers "composer update".
80       $this->composer('require --update-with-dependencies --prefer-dist --update-no-dev drupal/core:"' . $version . '"');
81       clearstatcache();
82       $mtime_after = filemtime($exampleScaffoldFile);
83       $this->assertNotEquals($mtime_after, $mtime_touched, 'Scaffold file was modified by composer update. (' . $version . ')');
84       switch ($version) {
85         case '8.0.1':
86         case '8.1.x-dev':
87           $this->assertFileExists($this->tmpDir . DIRECTORY_SEPARATOR . '.eslintrc');
88           $this->assertFileNotExists($this->tmpDir . DIRECTORY_SEPARATOR . '.eslintrc.json');
89           $this->assertFileNotExists($this->tmpDir . DIRECTORY_SEPARATOR . '.ht.router.php');
90           break;
91
92         case '8.3.0':
93           // Note we don't clean up .eslintrc file.
94           $this->assertFileExists($this->tmpDir . DIRECTORY_SEPARATOR . '.eslintrc');
95           $this->assertFileExists($this->tmpDir . DIRECTORY_SEPARATOR . '.eslintrc.json');
96           $this->assertFileNotExists($this->tmpDir . DIRECTORY_SEPARATOR . '.ht.router.php');
97           break;
98
99         case '8.5.x-dev':
100           $this->assertFileExists($this->tmpDir . DIRECTORY_SEPARATOR . '.eslintrc');
101           $this->assertFileExists($this->tmpDir . DIRECTORY_SEPARATOR . '.eslintrc.json');
102           $this->assertFileExists($this->tmpDir . DIRECTORY_SEPARATOR . '.ht.router.php');
103           break;
104       }
105     }
106
107     // We touch a scaffold file, so we can check the file was modified by the
108     // custom command.
109     file_put_contents($exampleScaffoldFile, 1);
110     $this->composer('drupal:scaffold');
111     $this->assertNotEquals(file_get_contents($exampleScaffoldFile), 1, 'Scaffold file was modified by custom command.');
112   }
113
114   /**
115    * Writes the default composer json to the temp direcoty.
116    */
117   protected function writeComposerJSON() {
118     $json = json_encode($this->composerJSONDefaults(), JSON_PRETTY_PRINT);
119     // Write composer.json.
120     file_put_contents($this->tmpDir . '/composer.json', $json);
121   }
122
123   /**
124    * Writes a tag for the current commit, so we can reference it directly in the
125    * composer.json.
126    */
127   protected function writeTestReleaseTag() {
128     // Tag the current state.
129     $this->tmpReleaseTag = '999.0.' . time();
130     $this->git(sprintf('tag -a "%s" -m "%s"', $this->tmpReleaseTag, 'Tag for testing this exact commit'));
131   }
132
133   /**
134    * Provides the default composer.json data.
135    *
136    * @return array
137    */
138   protected function composerJSONDefaults() {
139     return array(
140       'repositories' => array(
141         array(
142           'type' => 'vcs',
143           'url' => $this->rootDir,
144         ),
145       ),
146       'require' => array(
147         'drupal-composer/drupal-scaffold' => $this->tmpReleaseTag,
148         'composer/installers' => '^1.0.20',
149         'drupal/core' => '8.0.0',
150       ),
151       'minimum-stability' => 'dev',
152     );
153   }
154
155   /**
156    * Wrapper for the composer command.
157    *
158    * @param string $command
159    *   Composer command name, arguments and/or options.
160    */
161   protected function composer($command) {
162     chdir($this->tmpDir);
163     passthru(escapeshellcmd($this->rootDir . '/vendor/bin/composer ' . $command), $exit_code);
164     if ($exit_code !== 0) {
165       throw new \Exception('Composer returned a non-zero exit code');
166     }
167   }
168
169   /**
170    * Wrapper for git command in the root directory.
171    *
172    * @param $command
173    *   Git command name, arguments and/or options.
174    */
175   protected function git($command) {
176     chdir($this->rootDir);
177     passthru(escapeshellcmd('git ' . $command), $exit_code);
178     if ($exit_code !== 0) {
179       throw new \Exception('Git returned a non-zero exit code');
180     }
181   }
182
183   /**
184    * Makes sure the given directory exists and has no content.
185    *
186    * @param string $directory
187    */
188   protected function ensureDirectoryExistsAndClear($directory) {
189     if (is_dir($directory)) {
190       $this->fs->removeDirectory($directory);
191     }
192     mkdir($directory, 0777, TRUE);
193   }
194
195 }