Newer
Older

Lucas Hedding
committed
<?php
namespace Drupal\Tests\automatic_updates\Build\QuickStart;
use Drupal\BuildTests\Framework\BuildTestBase;
use Drupal\Component\FileSystem\FileSystem as DrupalFilesystem;
use Drupal\Core\Archiver\Zip;
use GuzzleHttp\Client;
use Symfony\Component\Filesystem\Filesystem as SymfonyFilesystem;
use Symfony\Component\Finder\Finder;

Lucas Hedding
committed
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
use Symfony\Component\Process\PhpExecutableFinder;
/**
* Helper methods for using the quickstart feature of Drupal.
*/
abstract class QuickStartTestBase extends BuildTestBase {
/**
* User name of the admin account generated during install.
*
* @var string
*/
protected $adminUsername;
/**
* Password of the admin account generated during install.
*
* @var string
*/
protected $adminPassword;
/**
* Install a Drupal site using the quick start feature.
*
* @param string $profile
* Drupal profile to install.
* @param string $working_dir
* (optional) A working directory relative to the workspace, within which to
* execute the command. Defaults to the workspace directory.
*/
public function installQuickStart($profile, $working_dir = NULL) {
$finder = new PhpExecutableFinder();
$process = $this->executeCommand($finder->find() . ' ./core/scripts/drupal install ' . $profile, $working_dir);
$this->assertCommandSuccessful();
$this->assertCommandOutputContains('Username:');
preg_match('/Username: (.+)\vPassword: (.+)/', $process->getOutput(), $matches);
$this->assertNotEmpty($this->adminUsername = $matches[1]);
$this->assertNotEmpty($this->adminPassword = $matches[2]);
}
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
/**
* Prepare core for testing.
*
* @param string $starting_version
* The starting version.
*/
protected function installCore($starting_version) {
// Get tarball of drupal core.
$drupal_tarball = "drupal-$starting_version.zip";
$destination = DrupalFileSystem::getOsTemporaryDirectory() . DIRECTORY_SEPARATOR . 'drupal-' . random_int(10000, 99999) . microtime(TRUE);
$fs = new SymfonyFilesystem();
$fs->mkdir($destination);
$http_client = new Client();
$http_client->get("https://ftp.drupal.org/files/projects/$drupal_tarball", ['sink' => $destination . DIRECTORY_SEPARATOR . $drupal_tarball]);
$zip = new Zip($destination . DIRECTORY_SEPARATOR . $drupal_tarball);
$zip->extract($destination);
// Move the tarball codebase over to the test workspace.
$finder = new Finder();
$finder->files()
->ignoreUnreadableDirs()
->ignoreDotFiles(FALSE)
->in("$destination/drupal-$starting_version");
$options = ['override' => TRUE, 'delete' => FALSE];
$fs->mirror("$destination/drupal-$starting_version", $this->getWorkingPath(), $finder->getIterator(), $options);
$fs->remove("$destination/drupal-$starting_version");
// Copy in this module from the original code base.
$finder = new Finder();
$finder->files()
->ignoreUnreadableDirs()
->in($this->getDrupalRoot())
->path('automatic_updates');
$this->copyCodebase($finder->getIterator());
$fs->chmod($this->getWorkspaceDirectory() . '/sites/default', 0700);
$this->installQuickStart('minimal');
// Currently, this test has to use extension_discovery_scan_tests so we can
// install test modules.
$settings_php = $this->getWorkspaceDirectory() . '/sites/default/settings.php';
$fs->chmod($this->getWorkspaceDirectory() . '/sites/default', 0755);
$fs->chmod($settings_php, 0640);
$fs->appendToFile($settings_php, '$settings[\'extension_discovery_scan_tests\'] = TRUE;' . PHP_EOL);
// Log in so that we can install modules.
$this->formLogin($this->adminUsername, $this->adminPassword);
$this->moduleInstall('update');
$this->moduleInstall('automatic_updates');
$this->moduleInstall('test_automatic_updates');
// Confirm we are running correct Drupal version.
$finder = new Finder();
$finder->files()->in($this->getWorkspaceDirectory())->path('core/lib/Drupal.php');
$finder->contains("/const VERSION = '$starting_version'/");
$this->assertTrue($finder->hasResults(), "Expected version $starting_version does not exist in {$this->getWorkspaceDirectory()}/core/lib/Drupal.php");
// Assert that the site is functional after install.
$this->visit();
$this->assertDrupalVisit();
}

Lucas Hedding
committed
/**
* Helper that uses Drupal's user/login form to log in.
*
* @param string $username
* Username.
* @param string $password
* Password.
* @param string $working_dir
* (optional) A working directory within which to login. Defaults to the
* workspace directory.
*/
public function formLogin($username, $password, $working_dir = NULL) {
$mink = $this->visit('/user/login', $working_dir);
$this->assertEquals(200, $mink->getSession()->getStatusCode());
$assert = $mink->assertSession();
$assert->fieldExists('edit-name')->setValue($username);
$assert->fieldExists('edit-pass')->setValue($password);
$mink->getSession()->getPage()->findButton('Log in')->submit();
}
}