/
InstallationControllerTest.php
206 lines (178 loc) · 7.42 KB
/
InstallationControllerTest.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
<?php
namespace ls\tests;
use Facebook\WebDriver\WebDriverBy;
use Facebook\WebDriver\Exception\NoSuchElementException;
/**
* @since 2017-11-24
* @group inst
*/
class InstallationControllerTest extends TestBaseClassWeb
{
/**
*
*/
public static function setupBeforeClass()
{
// NB: Does not call parent, because there might not
// be a database (happens if this test is run multiple
// times with failures).
self::$testHelper = new TestHelper();
self::$webDriver = self::$testHelper->getWebDriver();
self::$domain = getenv('DOMAIN');
}
/**
*
*/
public static function teardownAfterClass()
{
$configFile = \Yii::app()->getBasePath() . '/config/config.php';
if (file_exists($configFile)) {
self::$testHelper->connectToOriginalDatabase();
}
}
/**
*
* @throws \CException
*/
public function testBasic()
{
//$this->checkFolders();
$configFile = \Yii::app()->getBasePath() . '/config/config.php';
$databaseName = 'limesurvey';
$username = getenv('ADMINUSERNAME');
if (!$username) {
$username = 'admin';
}
$password = getenv('PASSWORD');
if (!$password) {
$password = 'password';
}
$dbuser = getenv('DBUSER');
if (!$dbuser) {
$dbuser = 'root';
echo 'Default to database user "root". Use DBUSER=... from command-line to override this.' . PHP_EOL;
}
$dbpwd = getenv('DBPASSWORD');
if (!$dbpwd) {
$dbpwd = '';
echo 'Default to empty database password. Use DBPASSWORD=... from command-line to override this.' . PHP_EOL;
}
if (file_exists($configFile)) {
// Delete possible previous database.
try {
$dbo = \Yii::app()->getDb();
$dbo->createCommand('DROP DATABASE ' . $databaseName)->execute();
} catch (\CDbException $ex) {
$msg = $ex->getMessage();
// Only this error is OK.
self::assertTrue(
strpos($msg, "database doesn't exist") !== false,
'Could drop database. Error message: ' . $msg
);
}
// Remove config.php if present.
$result = unlink($configFile);
$this->assertTrue($result, 'Could unlink config.php');
}
// Run installer.
$urlMan = \Yii::app()->urlManager;
$urlMan->setBaseUrl('http://' . self::$domain . '/index.php');
$url = $urlMan->createUrl('');
try {
// Installer start page.
self::$webDriver->get($url);
// Click "Start installation".
$start = self::$webDriver->findElement(WebDriverBy::id('ls-start-installation'));
$start->click();
// Accept license.
$accept = self::$webDriver->findElement(WebDriverBy::id('ls-accept-license'));
$accept->click();
// Click next at pre-check.
$next = self::$webDriver->findElement(WebDriverBy::id('ls-next'));
$next->click();
// Fill in database form.
$dbuserDbType = self::$webDriver->findElement(WebDriverBy::cssSelector('select[name="InstallerConfigForm[dbtype]"] option[value="mysql"]'));
$dbuserInput = self::$webDriver->findElement(WebDriverBy::cssSelector('input[name="InstallerConfigForm[dbuser]"]'));
$dbpwdInput = self::$webDriver->findElement(WebDriverBy::cssSelector('input[name="InstallerConfigForm[dbpwd]"]'));
$dbnameInput = self::$webDriver->findElement(WebDriverBy::cssSelector('input[name="InstallerConfigForm[dbname]"]'));
$dbuserDbType->click();
$dbuserInput->clear()->sendKeys($dbuser);
$dbpwdInput->clear()->sendKeys($dbpwd);
$dbnameInput->sendKeys($databaseName);
// Click next.
$next = self::$webDriver->findElement(WebDriverBy::id('ls-next'));
$next->click();
// Click "Create database".
$button = self::$webDriver->findElement(WebDriverBy::cssSelector('input[type="submit"]'));
$button->click();
// Click "Populate".
$button = self::$webDriver->findElement(WebDriverBy::cssSelector('input[type="submit"]'));
$button->click();
// Fill in admin username/password.
$adminLoginName = self::$webDriver->findElement(WebDriverBy::cssSelector('input[name="InstallerConfigForm[adminLoginName]"]'));
$adminLoginPwd = self::$webDriver->findElement(WebDriverBy::cssSelector('input[name="InstallerConfigForm[adminLoginPwd]"]'));
$confirmPwd = self::$webDriver->findElement(WebDriverBy::cssSelector('input[name="InstallerConfigForm[confirmPwd]"]'));
$adminLoginName->clear()->sendKeys($username);
$adminLoginPwd->clear()->sendKeys($password);
$confirmPwd->clear()->sendKeys($password);
// Confirm optional settings (admin password etc).
$button = self::$webDriver->findElement(WebDriverBy::cssSelector('input[type="submit"]'));
$button->click();
// Go to administration.
$button = self::$webDriver->findElement(WebDriverBy::id('ls-administration'));
$button->click();
// Set debug=2
/* TODO: Can't write to config.php after installation.
$configFile = \Yii::app()->getBasePath() . '/config/config.php';
$data = file($configFile);
$data = array_map(function($data) {
return stristr($data, "'debug'=>0") ? "'debug'=>2," : $data;
}, $data);
$output = [];
exec('chmod 777 ' . $configFile, $output);
var_dump($output);
$result = file_put_contents($configFile, implode('', $data));
$this->assertTrue($result > 0, 'Wrote config');
*/
// Reset urlManager to adapt to latest config.
$config = require($configFile);
$urlMan = \Yii::app()->urlManager;
$urlMan->setUrlFormat($config['components']['urlManager']['urlFormat']);
// Login.
self::adminLogin($username, $password);
} catch (NoSuchElementException $ex) {
self::$testHelper->takeScreenshot(self::$webDriver, (new \ReflectionClass($this))->getShortName() . '_' . __FUNCTION__);
$this->assertFalse(
true,
self::$testHelper->javaTrace($ex)
);
}
}
/**
* Check that upload/tmp folders are writable.
* @todo Does not work.
*/
public function checkFolders()
{
$instContr = new \InstallerController('dummyvalue');
$data = [];
$folder = \Yii::app()->getConfig('tempdir') . '/';
$tempdirIsWritable = $instContr->checkDirectoryWriteable(
$folder,
$data,
'tmpdir',
'tperror',
true
);
$this->assertTrue($tempdirIsWritable, 'Can write to tmp/');
$folder = \Yii::app()->getConfig('uploaddir') . '/';
$uploadIsWritable = $instContr->checkDirectoryWriteable(
$folder,
$data,
'uploaddir',
'uerror',
true
);
$this->assertTrue($uploadIsWritable, 'Can write to upload/');
}
}