|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 8 | + */ |
| 9 | +use Behat\Behat\Context\Context; |
| 10 | + |
| 11 | +class GuestsContext implements Context { |
| 12 | + public const TEST_PASSWORD = '123456'; |
| 13 | + protected static $lastStdOut = null; |
| 14 | + protected static $lastCode = null; |
| 15 | + |
| 16 | + #[\Behat\Hook\BeforeScenario('@Guests')] |
| 17 | + #[\Behat\Hook\BeforeFeature('@Guests')] |
| 18 | + public static function skipTestsIfGuestsIsNotInstalled() { |
| 19 | + if (!self::isGuestsInstalled()) { |
| 20 | + throw new Exception('Guests needs to be installed to run features or scenarios tagged with @Guests'); |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + #[\Behat\Hook\AfterScenario('@Guests')] |
| 25 | + public static function disableGuests() { |
| 26 | + self::runOcc(['app:disable', 'guests']); |
| 27 | + } |
| 28 | + |
| 29 | + private static function isGuestsInstalled(): bool { |
| 30 | + self::runOcc(['app:list']); |
| 31 | + return strpos(self::$lastStdOut, 'guests') !== false; |
| 32 | + } |
| 33 | + |
| 34 | + private static function runOcc(array $args, array $env = []): int { |
| 35 | + // Based on "runOcc" from CommandLine trait (which can not be used due |
| 36 | + // to not being static and being already used in other sibling |
| 37 | + // contexts). |
| 38 | + $args = array_map(function ($arg) { |
| 39 | + return escapeshellarg($arg); |
| 40 | + }, $args); |
| 41 | + $args[] = '--no-ansi --no-warnings'; |
| 42 | + $args = implode(' ', $args); |
| 43 | + |
| 44 | + $descriptor = [ |
| 45 | + 0 => ['pipe', 'r'], |
| 46 | + 1 => ['pipe', 'w'], |
| 47 | + 2 => ['pipe', 'w'], |
| 48 | + ]; |
| 49 | + $process = proc_open('php console.php ' . $args, $descriptor, $pipes, $ocPath = '../..', $env); |
| 50 | + self::$lastStdOut = stream_get_contents($pipes[1]); |
| 51 | + self::$lastCode = proc_close($process); |
| 52 | + |
| 53 | + return self::$lastCode; |
| 54 | + } |
| 55 | + |
| 56 | + #[\Behat\Step\Given('/^user "([^"]*)" is a guest account user$/')] |
| 57 | + public function createGuestUser(string $email): void { |
| 58 | + self::runOcc([ |
| 59 | + 'user:delete', |
| 60 | + $email, |
| 61 | + ]); |
| 62 | + |
| 63 | + $lastCode = self::runOcc([ |
| 64 | + 'config:app:set', |
| 65 | + 'guests', |
| 66 | + 'hash_user_ids', |
| 67 | + '--value=false', |
| 68 | + '--type=boolean', |
| 69 | + ]); |
| 70 | + \PHPUnit\Framework\Assert::assertEquals(0, $lastCode); |
| 71 | + |
| 72 | + $lastCode = self::runOcc([ |
| 73 | + 'guests:add', |
| 74 | + // creator user |
| 75 | + 'admin', |
| 76 | + // email |
| 77 | + $email, |
| 78 | + '--display-name', |
| 79 | + $email . '-displayname', |
| 80 | + '--password-from-env', |
| 81 | + ], [ |
| 82 | + 'OC_PASS' => self::TEST_PASSWORD, |
| 83 | + ]); |
| 84 | + \PHPUnit\Framework\Assert::assertEquals(0, $lastCode, 'Guest creation succeeded for ' . $email); |
| 85 | + } |
| 86 | + |
| 87 | +} |
0 commit comments