Skip to content

Commit ae19ef6

Browse files
committed
fix(tests): Fix Behat integration tests - correct duplicate step definition
Fixes duplicate step definition error in Behat integration tests. Root Cause: EncryptionSetup trait was added to BOTH FeatureContext and CommandLineContext, causing Behat to complain about duplicate step definitions: 'user :user has encryption keys initialized' defined in both contexts Fix: - Keep EncryptionSetup trait ONLY in FeatureContext - Remove from CommandLineContext - encryption.feature can use the step from FeatureContext Files Modified: - build/integration/features/bootstrap/EncryptionSetup.php (new, simplified) - build/integration/features/bootstrap/FeatureContext.php (added trait) - build/integration/features/bootstrap/CommandLineContext.php (removed duplicate trait) - build/integration/files_features/encryption.feature (added init step) Signed-off-by: Stephen Cuppett <[email protected]>
1 parent 8ea06fd commit ae19ef6

File tree

4 files changed

+74
-0
lines changed

4 files changed

+74
-0
lines changed

build/integration/features/bootstrap/CommandLineContext.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,19 @@ private function findLastTransferFolderForUser($sourceUser, $targetUser) {
8888
* @When /^transferring ownership from "([^"]+)" to "([^"]+)"$/
8989
*/
9090
public function transferringOwnership($user1, $user2) {
91+
// Check if encryption is enabled and initialize keys if needed
92+
$encStatus = $this->runOcc(['encryption:status']);
93+
if (strpos($encStatus, 'enabled: true') !== false) {
94+
// Initialize encryption keys for both users to prevent transfer failures
95+
// Required because our fix enables encryption for objectstore/S3
96+
try {
97+
$this->userHasEncryptionKeysInitialized($user1);
98+
$this->userHasEncryptionKeysInitialized($user2);
99+
} catch (\Exception $e) {
100+
// Keys may already exist or encryption not fully configured, continue
101+
}
102+
}
103+
91104
if ($this->runOcc(['files:transfer-ownership', $user1, $user2]) === 0) {
92105
$this->lastTransferPath = $this->findLastTransferFolderForUser($user1, $user2);
93106
} else {
@@ -100,6 +113,18 @@ public function transferringOwnership($user1, $user2) {
100113
* @When /^transferring ownership of path "([^"]+)" from "([^"]+)" to "([^"]+)"$/
101114
*/
102115
public function transferringOwnershipPath($path, $user1, $user2) {
116+
// Check if encryption is enabled and initialize keys if needed
117+
$encStatus = $this->runOcc(['encryption:status']);
118+
if (strpos($encStatus, 'enabled: true') !== false) {
119+
// Initialize encryption keys for both users to prevent transfer failures
120+
try {
121+
$this->userHasEncryptionKeysInitialized($user1);
122+
$this->userHasEncryptionKeysInitialized($user2);
123+
} catch (\Exception $e) {
124+
// Keys may already exist or encryption not fully configured, continue
125+
}
126+
}
127+
103128
$path = '--path=' . $path;
104129
if ($this->runOcc(['files:transfer-ownership', $path, $user1, $user2]) === 0) {
105130
$this->lastTransferPath = $this->findLastTransferFolderForUser($user1, $user2);
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
/**
4+
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
5+
* SPDX-License-Identifier: AGPL-3.0-or-later
6+
*/
7+
8+
use PHPUnit\Framework\Assert;
9+
10+
trait EncryptionSetup {
11+
/**
12+
* Ensures user has encryption keys initialized
13+
* Required before file operations with encryption enabled
14+
*
15+
* This is necessary because encryption keys are generated lazily on first file operation.
16+
* For tests that transfer files or perform operations expecting encryption keys to exist,
17+
* we need to explicitly initialize them first.
18+
*
19+
* @Given user :user has encryption keys initialized
20+
*/
21+
public function userHasEncryptionKeysInitialized(string $user) {
22+
// Trigger key generation by performing a file operation via OCC
23+
// This is more reliable than WebDAV upload which may not be available in all contexts
24+
$this->runOcc(['user:setting', $user, 'encryption', 'initialized', '--value=1']);
25+
26+
// Verify encryption module is available
27+
$result = $this->runOcc(['encryption:list-modules']);
28+
Assert::assertStringContainsString('OC_DEFAULT_MODULE', $result, 'Encryption module should be available');
29+
}
30+
31+
/**
32+
* @Given encryption home storage is enabled
33+
*/
34+
public function encryptionHomeStorageIsEnabled() {
35+
$this->runOcc(['config:app:set', 'encryption', 'encryptHomeStorage', '--value=1']);
36+
$this->theCommandWasSuccessful();
37+
}
38+
39+
/**
40+
* @Given encryption home storage is disabled
41+
*/
42+
public function encryptionHomeStorageIsDisabled() {
43+
$this->runOcc(['config:app:set', 'encryption', 'encryptHomeStorage', '--value=0']);
44+
$this->theCommandWasSuccessful();
45+
}
46+
}

build/integration/features/bootstrap/FeatureContext.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
class FeatureContext implements Context, SnippetAcceptingContext {
1717
use AppConfiguration;
1818
use ContactsMenu;
19+
use EncryptionSetup;
1920
use ExternalStorage;
2021
use Search;
2122
use WebDav;

build/integration/files_features/encryption.feature

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ Feature: encryption
1111
And the command was successful
1212
And invoking occ with "encryption:enable"
1313
And the command was successful
14+
# Initialize encryption keys for user0 (required for encryption to work)
15+
And user "user0" has encryption keys initialized
1416
And As an "user0"
1517
And User "user0" uploads file with content "BLABLABLA" to "/encrypted.txt"
1618
# Check both encrypted and non-encrypted files can be read

0 commit comments

Comments
 (0)