forked from drush-ops/drush
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UnishTestCase.php
774 lines (692 loc) · 23.1 KB
/
UnishTestCase.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
<?php
namespace Unish;
use Composer\Semver\Comparator;
use Consolidation\SiteAlias\SiteAlias;
use Consolidation\SiteProcess\SiteProcess;
use Yoast\PHPUnitPolyfills\TestCases\TestCase;
use Symfony\Component\Yaml\Yaml;
use Webmozart\PathUtil\Path;
use Consolidation\SiteProcess\ProcessManager;
abstract class UnishTestCase extends TestCase
{
// Unix exit codes.
const EXIT_SUCCESS = 0;
const EXIT_ERROR = 1;
const EXIT_ERROR_WITH_CLARITY = 3;
const UNISH_EXITCODE_USER_ABORT = 75; // Same as DRUSH_EXITCODE_USER_ABORT
const INTEGRATION_TEST_ENV = 'default';
protected $processManager;
/**
* Process of last executed command.
*
* @var Process
*/
protected $process;
/**
* A list of Drupal sites that have been recently installed. They key is the
* site name and values are details about each site.
*
* @var array
*/
private static $sites = [];
private static $sandbox;
private static $drush;
private static $db_url;
private static $usergroup = null;
public function __construct($name = null, array $data = [], $dataName = '')
{
parent::__construct($name, $data, $dataName);
// We read from env then globals then default to mysql.
self::$db_url = getenv('UNISH_DB_URL') ?: (isset($GLOBALS['UNISH_DB_URL']) ? $GLOBALS['UNISH_DB_URL'] : 'mysql://root:@127.0.0.1');
// require_once __DIR__ . '/unish.inc';
// list($unish_tmp, $unish_sandbox, $unish_drush_dir) = \unishGetPaths();
$unish_sandbox = Path::join(dirname(dirname(__DIR__)), 'sandbox');
self::mkdir($unish_sandbox);
$unish_cache = Path::join($unish_sandbox, 'cache');
self::$drush = Path::join(self::getComposerRoot(), 'drush');
self::$sandbox = $unish_sandbox;
self::$usergroup = isset($GLOBALS['UNISH_USERGROUP']) ? $GLOBALS['UNISH_USERGROUP'] : null;
self::setEnv(['CACHE_PREFIX' => $unish_cache]);
$home = $unish_sandbox . '/home';
self::setEnv(['HOME' => $home]);
self::setEnv(['HOMEDRIVE' => $home]);
$composer_home = $unish_cache . '/.composer';
self::setEnv(['COMPOSER_HOME' => $composer_home]);
self::setEnv(['ETC_PREFIX' => $unish_sandbox]);
self::setEnv(['SHARE_PREFIX' => $unish_sandbox]);
self::setEnv(['TEMP' => Path::join($unish_sandbox, 'tmp')]);
self::setEnv(['FIXTURES_DIR' => Path::join(dirname(__DIR__), 'fixtures')]);
}
/**
* @return array
*/
public static function getSites()
{
return self::$sites;
}
/**
* @return array
*/
public static function getAliases()
{
// Prefix @sut. onto each site.
foreach (self::$sites as $key => $site) {
$aliases[$key] = '@sut.' . $key;
}
return $aliases;
}
public static function getUri($site = 'dev')
{
return self::$sites[$site]['uri'];
}
/**
* @return string
*/
public static function getDrush()
{
return self::$drush;
}
/**
* @return string
*/
public static function getSandbox()
{
return self::$sandbox;
}
/**
* @return string
*/
public static function getSut()
{
return self::getComposerRoot();
}
public static function getComposerRoot()
{
return Path::canonicalize(dirname(dirname(__DIR__)));
}
/**
* - Remove sandbox directory.
* - Empty /modules, /profiles, /themes in SUT.
*/
public static function cleanDirs()
{
$dirty = getenv('UNISH_DIRTY');
// First step: delete the entire sandbox unless 'UNISH_DIRTY' is set,
// in which case we will delete only the 'transient' directory.
$sandbox = self::getSandbox();
if (!empty($dirty)) {
$sandbox = Path::join($sandbox, 'transient');
}
// The transient files generally should not need to be inspected, but
// if you need to examine them, use the special value of 'UNISH_DIRTY=VERY'
// to keep them.
if (file_exists($sandbox) && ($dirty != 'VERY')) {
self::recursiveDelete($sandbox);
}
// Next step: If 'UNISH_DIRTY' is not set, then delete the portions
// of our fixtures that we set up dynamically during the tests.
if (empty($dirty)) {
$webrootSlashDrush = self::webrootSlashDrush();
if (file_exists($webrootSlashDrush)) {
self::recursiveDelete($webrootSlashDrush, true, false, ['Commands', 'sites']);
}
foreach (['modules', 'themes', 'profiles'] as $dir) {
$target = Path::join(self::webroot(), $dir, 'contrib');
if (file_exists($target)) {
self::recursiveDeleteDirContents($target);
}
}
foreach (['sites/dev', 'sites/stage', 'sites/prod'] as $dir) {
$target = Path::join(self::webroot(), $dir);
if (file_exists($target)) {
self::recursiveDelete($target);
}
}
}
}
/**
* @return string
*/
public static function getDbUrl()
{
return self::$db_url;
}
/**
* @return string
*/
public static function getUserGroup()
{
return self::$usergroup;
}
/**
* We used to assure that each class starts with an empty sandbox directory and
* a clean environment except for the SUT. History: http://drupal.org/node/1103568.
*/
public static function setUpBeforeClass(): void
{
self::cleanDirs();
// Create all the dirs.
$sandbox = self::getSandbox();
$dirs = [getenv('HOME') . '/.drush', $sandbox . '/etc/drush', $sandbox . '/share/drush/commands', "$sandbox/cache", getenv('TEMP')];
foreach ($dirs as $dir) {
self::mkdir($dir);
}
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
// Hack to make git use unix line endings on windows
exec("git config --file $sandbox\\home\\.gitconfig core.autocrlf false", $output, $return);
}
parent::setUpBeforeClass();
}
/**
* Runs after all tests in a class are run.
*/
public static function tearDownAfterClass(): void
{
self::cleanDirs();
self::$sites = [];
parent::tearDownAfterClass();
}
/**
* Print a log message to the console.
*
* @param string $message
* @param string $type
* Supported types are:
* - notice
* - verbose
* - debug
*/
public function log($message, $type = 'notice')
{
$line = "\nLog: $message\n";
switch ($this->logLevel()) {
case 'verbose':
if (in_array($type, ['notice', 'verbose'])) {
fwrite(STDERR, $line);
}
break;
case 'debug':
fwrite(STDERR, $line);
break;
default:
if ($type == 'notice') {
fwrite(STDERR, $line);
}
break;
}
}
public function logLevel()
{
$argv = $_SERVER['argv'];
// -d is reserved by `phpunit`
if (in_array('--debug', $argv) || in_array('-vvv', $argv)) {
return 'debug';
} elseif (in_array('--verbose', $argv) || in_array('-v', $argv)) {
return 'verbose';
}
}
public static function isWindows()
{
return strtoupper(substr(PHP_OS, 0, 3)) == "WIN";
}
/**
* Print out a tick mark.
*
* Useful for longer running tests to indicate they're working.
*/
public function tick()
{
static $chars = ['/', '-', '\\', '|'];
static $counter = 0;
// ANSI support is flaky on Win32, so don't try to do ticks there.
if (!$this->isWindows()) {
print $chars[($counter++ % 4)] . "\033[1D";
}
}
/**
* Borrowed from Drush.
* Checks operating system and returns
* supported bit bucket folder.
*/
public function bitBucket()
{
if (!$this->isWindows()) {
return '/dev/null';
} else {
return 'nul';
}
}
public static function escapeshellarg($arg)
{
// Short-circuit escaping for simple params (keep stuff readable)
if (preg_match('|^[a-zA-Z0-9.:/_-]*$|', $arg)) {
return $arg;
} elseif (self::isWindows()) {
return self::_escapeshellargWindows($arg);
} else {
return escapeshellarg($arg);
}
}
public static function _escapeshellargWindows($arg)
{
// Double up existing backslashes
$arg = preg_replace('/\\\/', '\\\\\\\\', $arg);
// Double up double quotes
$arg = preg_replace('/"/', '""', $arg);
// Double up percents.
// $arg = preg_replace('/%/', '%%', $arg);
// Add surrounding quotes.
$arg = '"' . $arg . '"';
return $arg;
}
/**
* Helper function to generate a random string of arbitrary length.
*
* Copied from drush_generate_password(), which is otherwise not available here.
*
* @param $length
* Number of characters the generated string should contain.
* @return
* The generated string.
*/
public function randomString($length = 10)
{
// This variable contains the list of allowable characters for the
// password. Note that the number 0 and the letter 'O' have been
// removed to avoid confusion between the two. The same is true
// of 'I', 1, and 'l'.
$allowable_characters = 'abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789';
// Zero-based count of characters in the allowable list:
$len = strlen($allowable_characters) - 1;
// Declare the password as a blank string.
$pass = '';
// Loop the number of times specified by $length.
for ($i = 0; $i < $length; $i++) {
// Each iteration, pick a random character from the
// allowable string and append it to the password:
$pass .= $allowable_characters[mt_rand(0, $len)];
}
return $pass;
}
public static function mkdir($path)
{
if (!is_dir($path)) {
if (self::mkdir(dirname($path))) {
if (@mkdir($path)) {
return true;
}
}
return false;
}
return true;
}
public static function recursiveCopy($src, $dst)
{
$dir = opendir($src);
self::mkdir($dst);
while (false !== ( $file = readdir($dir))) {
if (( $file != '.' ) && ( $file != '..' )) {
if (is_dir($src . '/' . $file)) {
self::recursiveCopy($src . '/' . $file, $dst . '/' . $file);
} else {
copy($src . '/' . $file, $dst . '/' . $file);
}
}
}
closedir($dir);
}
/**
* Deletes the specified file or directory and everything inside it.
*
* Usually respects read-only files and folders. To do a forced delete use
* drush_delete_tmp_dir() or set the parameter $forced.
*
* To avoid permission denied error on Windows, make sure your CWD is not
* inside the directory being deleted.
*
* This is essentially a copy of drush_delete_dir().
*
* @todo This sort of duplication isn't very DRY. This is bound to get out of
* sync with drush_delete_dir(), as in fact it already has before.
*
* @param string $dir
* The file or directory to delete.
* @param bool $force
* Whether or not to try everything possible to delete the directory, even if
* it's read-only. Defaults to FALSE.
* @param bool $follow_symlinks
* Whether or not to delete symlinked files. Defaults to FALSE--simply
* unlinking symbolic links.
* @param string[] $exclude
* Top-level items to retain
*
* @return bool
* FALSE on failure, TRUE if everything was deleted.
*
* @see drush_delete_dir()
*/
public static function recursiveDelete($dir, $force = true, $follow_symlinks = false, $exclude = [])
{
// Do not delete symlinked files, only unlink symbolic links
if (is_link($dir) && !$follow_symlinks) {
return unlink($dir);
}
// Allow to delete symlinks even if the target doesn't exist.
if (!is_link($dir) && !file_exists($dir)) {
return true;
}
if (!is_dir($dir)) {
if ($force) {
// Force deletion of items with readonly flag.
@chmod($dir, 0777);
}
return unlink($dir);
}
if (self::recursiveDeleteDirContents($dir, $force, $exclude) === false) {
return false;
}
// Don't delete the directory itself if we are retaining some of its contents
if (!empty($exclude)) {
return true;
}
if ($force) {
// Force deletion of items with readonly flag.
@chmod($dir, 0777);
}
return rmdir($dir);
}
/**
* Deletes the contents of a directory.
*
* This is essentially a copy of drush_delete_dir_contents().
*
* @param string $dir
* The directory to delete.
* @param bool $force
* Whether or not to try everything possible to delete the contents, even if
* they're read-only. Defaults to FALSE.
* @param string[] $exclude
* Top-level items to retain
*
* @return bool
* FALSE on failure, TRUE if everything was deleted.
*
* @see drush_delete_dir_contents()
*/
public static function recursiveDeleteDirContents($dir, $force = false, $exclude = [])
{
$scandir = @scandir($dir);
if (!is_array($scandir)) {
return false;
}
foreach ($scandir as $item) {
if ($item == '.' || $item == '..') {
continue;
}
if (in_array($item, $exclude)) {
continue;
}
if ($force) {
@chmod($dir, 0777);
}
if (!self::recursiveDelete($dir . '/' . $item, $force)) {
return false;
}
}
return true;
}
public static function webroot()
{
return Path::join(self::getSut(), 'sut');
}
public static function webrootSlashDrush()
{
return Path::join(self::webroot(), 'drush');
}
public static function directoryCache($subdir = '')
{
return getenv('CACHE_PREFIX') . '/' . $subdir;
}
/**
* @param $env
* @return string
*/
public function dbUrl($env)
{
return substr(self::getDbUrl(), 0, 6) == 'sqlite' ? "sqlite://sites/$env/files/unish.sqlite" : self::getDbUrl() . '/unish_' . $env;
}
public function dbDriver($db_url = null)
{
return parse_url($db_url ?: self::getDbUrl(), PHP_URL_SCHEME);
}
/**
* Create some fixture sites that only have a 'settings.php' file
* with a database record.
*
* @param array $sites key=site_subdir value=array of extra alias data
* @param string $aliasGroup Write aliases into a file named group.alias.yml
*/
public function setupSettings(array $sites, $aliasGroup = 'fixture')
{
foreach ($sites as $subdir => $extra) {
$this->createSettings($subdir);
}
// Create basic site alias data with root and uri
$siteAliasData = $this->aliasFileData(array_keys($sites), $aliasGroup);
// Add in caller-provided site alias data
$siteAliasData = array_merge_recursive($siteAliasData, $sites);
$this->writeSiteAliases($siteAliasData, $aliasGroup);
}
public function createSettings($subdir)
{
$settingsContents = <<<EOT
<?php
\$databases['default']['default'] = array (
'database' => 'unish_$subdir',
'username' => 'root',
'password' => '',
'prefix' => '',
'host' => '127.0.0.1',
'port' => '',
'namespace' => 'Drupal\\Core\\Database\\Driver\\mysql',
'driver' => 'mysql',
);
\$settings['install_profile'] = 'testing';
EOT;
$root = $this->webroot();
$settingsPath = "$root/sites/$subdir/settings.php";
self::mkdir(dirname($settingsPath));
file_put_contents($settingsPath, $settingsContents);
}
/**
* Prepare (and optionally install) one or more Drupal sites using a single codebase.
*
* It is no longer supported to pass alternative versions of Drupal or an alternative install_profile.
*/
public function setupDrupal($num_sites = 1, $install = false, $options = [])
{
$sites_subdirs_all = ['dev', 'stage', 'prod'];
$sites_subdirs = array_slice($sites_subdirs_all, 0, $num_sites);
$root = $this->webroot();
// Install (if needed).
foreach ($sites_subdirs as $subdir) {
$this->installDrupal($subdir, $install, $options);
}
// Write an empty sites.php. Needed for multi-site on D8+.
if (!file_exists($root . '/sites/sites.php')) {
copy($root . '/sites/example.sites.php', $root . '/sites/sites.php');
}
$siteData = $this->aliasFileData($sites_subdirs);
self::$sites = [];
foreach ($siteData as $key => $data) {
self::$sites[$key] = $data;
}
return self::$sites;
}
/**
* Test if current Drupal is >= a target version.
*
* @param string $version2
* @return bool
*/
public function isDrupalGreaterThanOrEqualTo($version2)
{
return Comparator::greaterThanOrEqualTo(\Drupal::VERSION, $version2);
}
public function aliasFileData($sites_subdirs)
{
$root = $this->webroot();
// Stash details about each site.
$sites = [];
foreach ($sites_subdirs as $subdir) {
$sites[$subdir] = [
'root' => $root,
'uri' => $subdir,
'dbUrl' => $this->dbUrl($subdir),
];
}
return $sites;
}
protected function sutAlias($uri = self::INTEGRATION_TEST_ENV)
{
return new SiteAlias(['root' => $this->webroot(), 'uri' => $uri], "@sut.$uri");
}
/**
* Write an alias group file and a config file which points to same dir.
*
* @param $sites
*/
public function writeSiteAliases($sites, $aliasGroup = 'sut')
{
$target = Path::join(self::webrootSlashDrush(), "sites/$aliasGroup.site.yml");
$this->mkdir(dirname($target));
file_put_contents($target, Yaml::dump($sites, PHP_INT_MAX, 2));
}
/**
* Install a Drupal site.
*
* It is no longer supported to pass alternative versions of Drupal or an alternative install_profile.
*/
public function installDrupal($env = 'dev', $install = false, $options = [], $refreshSettings = true)
{
$root = $this->webroot();
$uri = $env;
$site = "$root/sites/$uri";
// If specified, install Drupal as a multi-site.
if ($install) {
$this->installSut($uri, $options, $refreshSettings);
} else {
$this->mkdir($site);
touch("$site/settings.php");
}
}
protected function processManager()
{
if (!$this->processManager) {
$this->processManager = new ProcessManager();
}
return $this->processManager;
}
protected function checkInstallSut($uri = self::INTEGRATION_TEST_ENV)
{
$sutAlias = $this->sutAlias($uri);
$options = [
'root' => $this->webroot(),
'uri' => $uri
];
// TODO: Maybe there is a faster command to use for this check
$process = $this->processManager()->siteProcess($sutAlias, [self::getDrush(), 'pm:list'], $options);
$process->run();
if (!$process->isSuccessful()) {
$this->installSut($uri);
}
}
protected function installSut($uri = self::INTEGRATION_TEST_ENV, $optionsFromTest = [], $refreshSettings = true)
{
$root = $this->webroot();
$siteDir = "$root/sites/$uri";
@mkdir($siteDir);
chmod("$siteDir", 0777);
@chmod("$siteDir/settings.php", 0777);
if ($refreshSettings) {
copy("$root/sites/default/default.settings.php", "$siteDir/settings.php");
}
$sutAlias = $this->sutAlias($uri);
$options = $optionsFromTest + [
'root' => $this->webroot(),
'uri' => $uri,
'db-url' => $this->dbUrl($uri),
'sites-subdir' => $uri,
'yes' => true,
// quiet suppresses error reporting as well.
// 'quiet' => true,
];
if ($level = $this->logLevel()) {
$options[$level] = true;
}
$process = $this->processManager()->siteProcess($sutAlias, [self::getDrush(), 'site:install', 'testing', 'install_configure_form.enable_update_status_emails=NULL'], $options);
// Set long timeout because Xdebug slows everything.
$process->setTimeout(0);
$this->process = $process;
$process->run();
$this->assertTrue($process->isSuccessful(), $this->buildProcessMessage());
// Give us our write perms back.
chmod($this->webroot() . "/sites/$uri", 0777);
}
/**
* The sitewide directory for Drupal extensions.
*/
public function drupalSitewideDirectory()
{
return '/sites/all';
}
/**
* Write the provided string to a temporary file that will be
* automatically deleted one exit.
*/
protected function writeToTmpFile($contents)
{
$transient = Path::join($this->getSandbox(), 'transient');
self::mkdir($transient);
$path = tempnam($transient, "unishtmp");
file_put_contents($path, $contents);
return $path;
}
/**
* Set environment variables that should be passed to child processes.
*
* @param array $vars
* The variables to set.
*/
public static function setEnv(array $vars)
{
foreach ($vars as $k => $v) {
putenv($k . '=' . $v);
// Value must be a string. See \Symfony\Component\Process\Process::getDefaultEnv.
$_SERVER[$k] = (string) $v;
}
}
/**
* Borrowed from \Symfony\Component\Process\Exception\ProcessTimedOutException
*
* @return string
*/
public function buildProcessMessage()
{
$error = sprintf(
"%s\n\nExit Code: %s(%s)\n\nWorking directory: %s",
$this->process->getCommandLine(),
$this->process->getExitCode(),
$this->process->getExitCodeText(),
$this->process->getWorkingDirectory()
);
if (!$this->process->isOutputDisabled()) {
$error .= sprintf(
"\n\nOutput:\n================\n%s\n\nError Output:\n================\n%s",
$this->process->getOutput(),
$this->process->getErrorOutput()
);
}
return $error;
}
}