1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* admin |
5
|
|
|
* |
6
|
|
|
* @category Apparat |
7
|
|
|
* @package Apparat\Server |
8
|
|
|
* @subpackage Tollwerk\Admin\Tests |
9
|
|
|
* @author Joschi Kuphal <[email protected]> / @jkphl |
10
|
|
|
* @copyright Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl |
11
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License (MIT) |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
/*********************************************************************************** |
15
|
|
|
* The MIT License (MIT) |
16
|
|
|
* |
17
|
|
|
* Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl |
18
|
|
|
* |
19
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of |
20
|
|
|
* this software and associated documentation files (the "Software"), to deal in |
21
|
|
|
* the Software without restriction, including without limitation the rights to |
22
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
23
|
|
|
* the Software, and to permit persons to whom the Software is furnished to do so, |
24
|
|
|
* subject to the following conditions: |
25
|
|
|
* |
26
|
|
|
* The above copyright notice and this permission notice shall be included in all |
27
|
|
|
* copies or substantial portions of the Software. |
28
|
|
|
* |
29
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
30
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
31
|
|
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
32
|
|
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
33
|
|
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
34
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
35
|
|
|
***********************************************************************************/ |
36
|
|
|
|
37
|
|
|
namespace Tollwerk\Admin\Tests; |
38
|
|
|
|
39
|
|
|
use mikehaertl\shellcommand\Command; |
40
|
|
|
use Tollwerk\Admin\Infrastructure\App; |
41
|
|
|
use Tollwerk\Admin\Infrastructure\Shell\Binary; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Command line tool tests |
45
|
|
|
* |
46
|
|
|
* @package Tollwerk\Admin |
47
|
|
|
* @subpackage Tollwerk\Admin\Tests |
48
|
|
|
*/ |
49
|
|
|
class CliTest extends AbstractDatabaseTest |
50
|
|
|
{ |
51
|
|
|
/** |
52
|
|
|
* Admin CLI interface |
53
|
|
|
* |
54
|
|
|
* @var string |
55
|
|
|
*/ |
56
|
|
|
protected static $admincli; |
57
|
|
|
/** |
58
|
|
|
* Home directory base |
59
|
|
|
* |
60
|
|
|
* @var string |
61
|
|
|
*/ |
62
|
|
|
protected static $homebase; |
63
|
|
|
/** |
64
|
|
|
* Temporary directories |
65
|
|
|
* |
66
|
|
|
* @var array |
67
|
|
|
*/ |
68
|
|
|
protected static $tmpDirectories = []; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* This method is called before the first test of this test class is run. |
72
|
|
|
*/ |
73
|
|
|
public static function setUpBeforeClass() |
74
|
|
|
{ |
75
|
|
|
self::$admincli = |
76
|
|
|
dirname(dirname(dirname(__DIR__))).DIRECTORY_SEPARATOR.'app'.DIRECTORY_SEPARATOR.'tollwerk-admin.php'; |
77
|
|
|
|
78
|
|
|
// TODO: Make environment dependent |
79
|
|
|
self::$homebase = dirname(dirname(dirname(__DIR__))).DIRECTORY_SEPARATOR.'tmp'.DIRECTORY_SEPARATOR; |
80
|
|
|
|
81
|
|
|
// Ensure the account system group exists |
82
|
|
|
App::bootstrap(); |
83
|
|
|
$command = new Command(); |
84
|
|
|
$command->setCommand(Binary::get('groupadd')); |
85
|
|
|
$command->addArg('-f'); |
86
|
|
|
$command->addArg(App::getConfig('shell.group')); |
87
|
|
|
if (!$command->execute()) { |
88
|
|
|
throw new \RuntimeException($command->getOutput(), $command->getExitCode()); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Performs operation returned by getTearDownOperation(). |
94
|
|
|
*/ |
95
|
|
|
public function tearDown() |
96
|
|
|
{ |
97
|
|
|
parent::tearDown(); |
98
|
|
|
|
99
|
|
|
// Remove all temporary directories |
100
|
|
|
foreach (self::$tmpDirectories as $tmpDirectory) { |
101
|
|
|
$this->deleteTree($tmpDirectory); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Test creating an account |
107
|
|
|
*/ |
108
|
|
|
public function testAccount() |
109
|
|
|
{ |
110
|
|
|
$command = $this->getAdminCmd()->addArg('account:create')->addArg('test'); |
111
|
|
|
$success = $command->execute(); |
112
|
|
|
if (!$success) { |
113
|
|
|
echo $command->getOutput().' ('.$command->getExitCode().')'; |
114
|
|
|
} |
115
|
|
|
$this->assertTrue($success); |
116
|
|
|
|
117
|
|
|
$this->assertEquals(1, $this->getConnection()->getRowCount('account')); |
118
|
|
|
$queryTable = $this->getConnection()->createQueryTable('account', 'SELECT * FROM account'); |
119
|
|
|
$expectedTable = $this->createFlatXMLDataSet($this->getFixture('account_create.xml'))->getTable('account'); |
120
|
|
|
$this->assertTablesEqual($expectedTable, $queryTable); |
121
|
|
|
$this->assertDirectoryExists(self::$homebase.'test'); |
122
|
|
|
$this->assertUserExists('test'); |
123
|
|
|
|
124
|
|
|
// Enable the account |
125
|
|
|
$this->assertTrue($this->getAdminCmd()->addArg('account:enable')->addArg('test')->execute()); |
126
|
|
|
$queryTable = $this->getConnection()->createQueryTable('account', 'SELECT * FROM account'); |
127
|
|
|
$expectedTable = $this->createFlatXMLDataSet($this->getFixture('account_enable.xml'))->getTable('account'); |
128
|
|
|
$this->assertTablesEqual($expectedTable, $queryTable); |
129
|
|
|
|
130
|
|
|
// Disable the account |
131
|
|
|
$this->assertTrue($this->getAdminCmd()->addArg('account:disable')->addArg('test')->execute()); |
132
|
|
|
$queryTable = $this->getConnection()->createQueryTable('account', 'SELECT * FROM account'); |
133
|
|
|
$expectedTable = $this->createFlatXMLDataSet($this->getFixture('account_create.xml'))->getTable('account'); |
134
|
|
|
$this->assertTablesEqual($expectedTable, $queryTable); |
135
|
|
|
|
136
|
|
|
// Rename the account |
137
|
|
|
$this->assertTrue($this->getAdminCmd()->addArg('account:rename')->addArg('test')->addArg('renamed')->execute()); |
138
|
|
|
$queryTable = $this->getConnection()->createQueryTable('account', 'SELECT * FROM account'); |
139
|
|
|
$expectedTable = $this->createFlatXMLDataSet($this->getFixture('account_rename.xml'))->getTable('account'); |
140
|
|
|
$this->assertTablesEqual($expectedTable, $queryTable); |
141
|
|
|
$this->assertDirectoryExists(self::$homebase.'renamed'); |
142
|
|
|
$this->assertDirectoryNotExists(self::$homebase.'test'); |
143
|
|
|
$this->assertUserExists('renamed'); |
144
|
|
|
$this->assertUserNotExists('tests'); |
145
|
|
|
|
146
|
|
|
// Delete the account |
147
|
|
|
$this->assertTrue($this->getAdminCmd()->addArg('account:delete')->addArg('renamed')->execute()); |
148
|
|
|
$this->assertEquals(0, $this->getConnection()->getRowCount('account')); |
149
|
|
|
$this->assertDirectoryExists(self::$homebase.'renamed'); |
150
|
|
|
$this->assertUserNotExists('renamed'); |
151
|
|
|
|
152
|
|
|
// Register home directory for removal |
153
|
|
|
self::$tmpDirectories[] = self::$homebase.'renamed'; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Returns the test dataset |
158
|
|
|
* |
159
|
|
|
* @return \PHPUnit_Extensions_Database_DataSet_IDataSet |
160
|
|
|
*/ |
161
|
|
|
protected function getDataSet() |
162
|
|
|
{ |
163
|
|
|
return $this->createMySQLXMLDataSet( |
164
|
|
|
__DIR__.DIRECTORY_SEPARATOR.'Fixture'.DIRECTORY_SEPARATOR.'admin_test_cli.xml' |
165
|
|
|
); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Create and return an admin base command |
170
|
|
|
* |
171
|
|
|
* @return Command Admin base command |
172
|
|
|
*/ |
173
|
|
|
protected function getAdminCmd() |
174
|
|
|
{ |
175
|
|
|
$command = new Command(); |
176
|
|
|
$command->setCommand(Binary::get('php')); |
177
|
|
|
$command->addArg(self::$admincli); |
178
|
|
|
return $command; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Return the absolute path to a fixture file |
183
|
|
|
* |
184
|
|
|
* @param string $fixture Fixture file name |
185
|
|
|
* @return string Absolute fixture file |
186
|
|
|
*/ |
187
|
|
|
protected function getFixture($fixture) |
188
|
|
|
{ |
189
|
|
|
return __DIR__.DIRECTORY_SEPARATOR.'Fixture'.DIRECTORY_SEPARATOR.$fixture; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Recursively delete a directory |
194
|
|
|
* |
195
|
|
|
* @param string $dir Directory |
196
|
|
|
* @return bool Success |
197
|
|
|
*/ |
198
|
|
|
protected function deleteTree($dir) |
199
|
|
|
{ |
200
|
|
|
foreach (array_diff(scandir($dir), array('.', '..')) as $file) { |
201
|
|
|
if (is_dir("$dir/$file")) { |
202
|
|
|
$this->deleteTree("$dir/$file"); |
203
|
|
|
continue; |
204
|
|
|
} |
205
|
|
|
unlink("$dir/$file"); |
206
|
|
|
} |
207
|
|
|
return rmdir($dir); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Assert that a system user exists |
212
|
|
|
* |
213
|
|
|
* @param string $user User name |
214
|
|
|
*/ |
215
|
|
|
protected function assertUserExists($user) |
216
|
|
|
{ |
217
|
|
|
$command = new Command(); |
218
|
|
|
$command->setCommand(Binary::get('id')); |
219
|
|
|
$command->addArg($user); |
220
|
|
|
return $command->execute(); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Assert that a system user doesn't exist |
225
|
|
|
* |
226
|
|
|
* @param string $user User name |
227
|
|
|
*/ |
228
|
|
|
protected function assertUserNotExists($user) |
229
|
|
|
{ |
230
|
|
|
$command = new Command(); |
231
|
|
|
$command->setCommand(Binary::get('id')); |
232
|
|
|
$command->addArg($user); |
233
|
|
|
return !$command->execute(); |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
|