-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathAppManagementContext.php
More file actions
548 lines (516 loc) · 14.9 KB
/
AppManagementContext.php
File metadata and controls
548 lines (516 loc) · 14.9 KB
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
<?php declare(strict_types=1);
/**
* @author Victor Dubiniuk <dubiniuk@owncloud.com>
*
* @copyright Copyright (c) 2018, ownCloud GmbH
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace Tests\Acceptance;
use Behat\Behat\Context\Context;
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use Behat\Gherkin\Node\TableNode;
use PHPUnit\Framework\Assert;
use TestHelpers\SetupHelper;
/**
* Context for steps that test apps_paths.
*/
class AppManagementContext implements Context {
private FeatureContext $featureContext;
private $oldAppsPaths;
/**
* stdout of last command
*/
private string $cmdOutput;
private array $createdApps = [];
/**
*
* @param array $appsPaths of apps_paths entries
*
* @return string[] associated array with "code", "stdOut", "stdErr"
* @throws Exception
*/
public function setAppsPaths(array $appsPaths):array {
return SetupHelper::setSystemConfig(
'apps_paths',
\json_encode($appsPaths),
$this->featureContext->getStepLineRef(),
'json'
);
}
/**
* @Given these apps' path has been configured additionally with following attributes:
*
* @param TableNode $table
*
* @return void
* @throws Exception
*/
public function setAppDirectories(TableNode $table):void {
$appsPathsConfigs = \json_decode(
SetupHelper::getSystemConfig(
"apps_paths",
$this->featureContext->getStepLineRef(),
"json"
)['stdOut'],
true
);
$this->featureContext->verifyTableNodeColumns($table, ['dir'], ['is_writable']);
foreach ($table as $appsPathToAdd) {
$dir = $appsPathToAdd['dir'];
$appsPathsConfigs[] = [
'url' => $dir,
'path' => $this->featureContext->getServerRoot() . "/$dir",
'writable' => $appsPathToAdd['is_writable'] === 'true',
];
$this->featureContext->mkDirOnServer($appsPathToAdd['dir']);
}
$resp = $this->setAppsPaths($appsPathsConfigs);
if (!empty($resp['stdErr'])) {
throw new \Exception(
__METHOD__
. " Expected to set app path but failed due to error: " . $resp['stdErr']
);
}
}
/**
* @When the administrator puts app :appId with version :version in dir :dir
*
* @param string $appId app id
* @param string $version app version
* @param string $dir app directory
*
* @return void
* @throws Exception
*/
public function putAppInDir(string $appId, string $version, string $dir):void {
$ocVersion = SetupHelper::getSystemConfigValue(
'version',
$this->featureContext->getStepLineRef()
);
$appInfo = \sprintf(
'<?xml version="1.0"?>
<info>
<id>%s</id>
<name>%s</name>
<description>description</description>
<licence>AGPL</licence>
<author>Author</author>
<version>%s</version>
<category>collaboration</category>
<website>https://github.com/owncloud/</website>
<bugs>https://github.com/owncloud/</bugs>
<repository type="git">https://github.com/owncloud/</repository>
<screenshot>https://raw.githubusercontent.com/owncloud/screenshots/</screenshot>
<dependencies>
<owncloud min-version="%s" max-version="%s" />
</dependencies>
</info>',
$appId,
$appId,
$version,
$ocVersion,
$ocVersion
);
$targetDir = "$dir/$appId/appinfo";
$this->featureContext->mkDirOnServer($targetDir);
$this->featureContext->createFileOnServerWithContent("$targetDir/info.xml", $appInfo);
if (!\array_key_exists($appId, $this->createdApps)) {
$this->createdApps[$appId][] = $targetDir;
} else {
if (!\in_array($targetDir, $this->createdApps[$appId])) {
$this->createdApps[$appId][] = $targetDir;
}
}
}
/**
* @Given app :appId with version :version has been put in dir :dir
*
* @param string $appId app id
* @param string $version app version
* @param string $dir app directory
*
* @return void
* @throws Exception
*/
public function appHasBeenPutInDir(string $appId, string $version, string $dir):void {
$this->putAppInDir($appId, $version, $dir);
$check = SetupHelper::runOcc(
['app:list', '--output json'],
$this->featureContext->getStepLineRef()
);
$appsDisabled = \json_decode($check['stdOut'], true)['disabled'];
if (!\array_key_exists($appId, $appsDisabled)) {
throw new \Exception(
__METHOD__
. ' Expected: ' . $appId . 'to be present in apps(disabled) list, but not found'
);
}
}
/**
* @When the administrator gets the path for app :appId using the occ command
* @Given the administrator has got the path for app :appId using the occ command
*
* @param string $appId app id
*
* @return void
* @throws Exception
*/
public function adminGetsPathForApp(string $appId):void {
$this->featureContext->setOccLastCode(
$this->featureContext->runOcc(
['app:getpath', $appId, '--no-ansi']
)
);
$this->cmdOutput = $this->featureContext->getStdOutOfOccCommand();
// check that the command seems to have executed OK, for both When and Given
// step forms. There is no point continuing the scenario if the command itself
// has reported an error.
$statusCode = $this->featureContext->getExitStatusCodeOfOccCommand();
Assert::assertEquals(
"0",
$statusCode,
"app:getpath returned error code " . $statusCode
);
}
/**
* @When the administrator lists the apps using the occ command
*
* @return void
* @throws Exception
*/
public function adminListsTheApps():void {
$this->featureContext->setOccLastCode(
$this->featureContext->runOcc(
['app:list', '--no-ansi']
)
);
}
/**
* @When the administrator lists the enabled apps using the occ command
*
* @return void
* @throws Exception
*/
public function adminListsTheEnabledApps():void {
$this->featureContext->setOccLastCode(
$this->featureContext->runOcc(
['app:list', '--enabled', '--no-ansi']
)
);
}
/**
* @When the administrator lists the disabled apps using the occ command
*
* @return void
* @throws Exception
*/
public function adminListsTheDisabledApps():void {
$this->featureContext->setOccLastCode(
$this->featureContext->runOcc(
['app:list', '--disabled', '--no-ansi']
)
);
}
/**
* @When the administrator lists the enabled and disabled apps using the occ command
*
* @return void
* @throws Exception
*/
public function adminListsTheEnabledAndDisabledApps():void {
$this->featureContext->setOccLastCode(
$this->featureContext->runOcc(
['app:list', '--enabled', '--disabled', '--no-ansi']
)
);
}
/**
* @When the administrator lists the apps in minimal format using the occ command
*
* @return void
* @throws Exception
*/
public function adminListsTheAppsInMinimalFormat():void {
$this->featureContext->setOccLastCode(
$this->featureContext->runOcc(
['app:list', '--minimal --no-ansi']
)
);
}
/**
* @Then app :appId with version :appVersion and path :appPath should have been listed in the enabled apps section
*
* @param string $appId
* @param string $appVersion
* @param string $appPath
*
* @return void
* @throws Exception
*/
public function appWithVersionAndPathShouldHaveBeenListedInTheEnabledAppsSection(
string $appId,
string $appVersion,
string $appPath
):void {
$commandOutput = $this->featureContext->getStdOutOfOccCommand();
$expectedStartOfOutput = "Enabled:";
Assert::assertEquals(
$expectedStartOfOutput,
\substr($commandOutput, 0, 8),
"app:list command output did not start with '$expectedStartOfOutput'"
);
$startOfDisabledSection = \strpos($commandOutput, "Disabled:");
if ($startOfDisabledSection) {
$commandOutput = \substr($commandOutput, 0, $startOfDisabledSection);
}
$expectedString = \sprintf("- %s:\n - Version: %s\n - Path: %s/%s", $appId, $appVersion, $this->featureContext->getServerRoot(), $appPath);
Assert::assertNotFalse(
\strpos($commandOutput, $expectedString),
"app:list output did not contain '$expectedString' in the enabled section"
);
}
/**
* @Then app :appId with version :appVersion and path :appPath should have been listed in the disabled apps section
*
* @param string $appId
* @param string $appVersion
* @param string $appPath
*
* @return void
* @throws Exception
*/
public function appWithVersionAndPathShouldHaveBeenListedInTheDisabledAppsSection(
string $appId,
string $appVersion,
string $appPath
):void {
$commandOutput = $this->featureContext->getStdOutOfOccCommand();
$startOfDisabledSection = \strpos($commandOutput, "Disabled:");
Assert::assertNotFalse(
$startOfDisabledSection,
"app:list output did not contain the disabled section"
);
$commandOutput = \substr($commandOutput, $startOfDisabledSection);
$expectedString = \sprintf("- %s:\n - Version: %s\n - Path: %s/%s", $appId, $appVersion, $this->featureContext->getServerRoot(), $appPath);
Assert::assertNotFalse(
\strpos($commandOutput, $expectedString),
"app:list output did not contain '$expectedString' in the disabled section"
);
}
/**
* @Then app :appId should have been listed in the enabled apps section
*
* @param string $appId
*
* @return void
*/
public function appShouldHaveBeenListedInTheEnabledAppsSection(string $appId):void {
$commandOutput = $this->featureContext->getStdOutOfOccCommand();
$expectedStartOfOutput = "Enabled:";
Assert::assertEquals(
$expectedStartOfOutput,
\substr($commandOutput, 0, 8),
"app:list command output did not start with '$expectedStartOfOutput'"
);
$startOfDisabledSection = \strpos($commandOutput, "Disabled:");
if ($startOfDisabledSection) {
$commandOutput = \substr($commandOutput, 0, $startOfDisabledSection);
}
$expectedString = "- $appId";
Assert::assertNotFalse(
\strpos($commandOutput, $expectedString),
"app:list output did not contain '$expectedString' in the enabled section"
);
}
/**
* @Then app :appId should have been listed in the disabled apps section
*
* @param string $appId
*
* @return void
*/
public function appShouldHaveBeenListedInTheDisabledAppsSection(string $appId):void {
$commandOutput = $this->featureContext->getStdOutOfOccCommand();
$startOfDisabledSection = \strpos($commandOutput, "Disabled:");
Assert::assertNotFalse(
$startOfDisabledSection,
"app:list output did not contain the disabled section"
);
$commandOutput = \substr($commandOutput, $startOfDisabledSection);
$expectedString = "- $appId";
Assert::assertNotFalse(
\strpos($commandOutput, $expectedString),
"app:list output did not contain '$expectedString' in the disabled section"
);
}
/**
* @Then app version and path lines should not exist in the list app output
*
* @return void
*/
public function appVersionAndPathLinesShouldNotExist():void {
$commandOutput = $this->featureContext->getStdOutOfOccCommand();
Assert::assertStringNotContainsString(
"Version:",
$commandOutput
);
Assert::assertStringNotContainsString(
"Path:",
$commandOutput
);
}
/**
* @Then the enabled apps section should not exist
*
* @return void
*/
public function theEnabledAppsSectionShouldNotExist():void {
$commandOutput = $this->featureContext->getStdOutOfOccCommand();
Assert::assertFalse(
\strpos($commandOutput, "Enabled:"),
"app:list output contains the enabled section but it should not"
);
}
/**
* @Then the disabled apps section should not exist
*
* @return void
*/
public function theDisabledAppsSectionShouldNotExist():void {
$commandOutput = $this->featureContext->getStdOutOfOccCommand();
Assert::assertFalse(
\strpos($commandOutput, "Disabled:"),
"app:list output contains the disabled section but it should not"
);
}
/**
* @Then the path to :appId should be :dir
*
* @param string $appId
* @param string $dir
*
* @return void
* @throws Exception
*/
public function appPathIs(string $appId, string $dir):void {
Assert::assertEquals(
$this->featureContext->getServerRoot() . "/$dir/$appId",
\trim($this->cmdOutput),
"Expected: the path to $appId should be $dir but got "
. \trim($this->cmdOutput)
);
}
/**
* @Then the installed version of :appId should be :version
*
* @param string $appId
* @param string $version
*
* @return void
* @throws Exception
*/
public function assertInstalledVersionOfAppIs(string $appId, string $version):void {
$cmdOutput = SetupHelper::runOcc(
['config:app:get', $appId, 'installed_version', '--no-ansi'],
$this->featureContext->getStepLineRef()
)['stdOut'];
Assert::assertEquals(
$version,
\trim($cmdOutput),
"Expected: the installed version of $appId should be $version but got " . \trim($cmdOutput)
);
}
/**
* This will run before EVERY scenario.
* It will set the properties for this object.
*
* @BeforeScenario
*
* @param BeforeScenarioScope $scope
*
* @return void
* @throws Exception
*/
public function prepareParameters(BeforeScenarioScope $scope):void {
// Get the environment
$environment = $scope->getEnvironment();
// Get all the contexts you need in this context
$this->featureContext = $environment->getContext('FeatureContext');
$value = SetupHelper::getSystemConfigValue(
'apps_paths',
$this->featureContext->getStepLineRef(),
'json'
);
if ($value === '') {
$this->oldAppsPaths = null;
} else {
$this->oldAppsPaths = \json_decode($value, true);
}
}
/**
* @AfterScenario
*
* Reset the config values after each scenario
*
* @return void
* @throws Exception
*/
public function undoChangingParameters():void {
if ($this->oldAppsPaths === null) {
SetupHelper::deleteSystemConfig(
'apps_paths',
$this->featureContext->getStepLineRef()
);
} else {
$this->setAppsPaths($this->oldAppsPaths);
}
}
/**
* @AfterScenario
*
* delete created apps including files and values in DB after each scenario
*
* @return void
* @throws Exception
*/
public function deleteCreatedApps():void {
$configJson = SetupHelper::runOcc(
['config:list'],
$this->featureContext->getStepLineRef()
)['stdOut'];
$configList = \json_decode($configJson, true);
foreach ($this->createdApps as $app => $paths) {
//disable the app
$this->featureContext->appHasBeenDisabled($app, 'disables');
//delete config values out of the database
if (\array_key_exists($app, $configList['apps'])) {
foreach ($configList['apps'][$app] as $key => $value) {
SetupHelper::runOcc(
['config:app:delete', $app, $key],
$this->featureContext->getStepLineRef()
);
}
}
//delete the app from the drive
foreach ($paths as $path) {
SetupHelper::rmDirOnServer(
\dirname($path),
$this->featureContext->getStepLineRef()
);
}
}
}
}