Completed
Push — master ( 7fe5c8...b69109 )
by Robin
20:12 queued 14s
created
lib/private/Files/ObjectStore/PrimaryObjectStoreConfig.php 1 patch
Indentation   +202 added lines, -202 removed lines patch added patch discarded remove patch
@@ -17,209 +17,209 @@
 block discarded – undo
17 17
  * @psalm-type ObjectStoreConfig array{class: class-string<IObjectStore>, arguments: array{multibucket: bool, ...}}
18 18
  */
19 19
 class PrimaryObjectStoreConfig {
20
-	public function __construct(
21
-		private readonly IConfig $config,
22
-		private readonly IAppManager $appManager,
23
-	) {
24
-	}
25
-
26
-	/**
27
-	 * @param ObjectStoreConfig $config
28
-	 */
29
-	public function buildObjectStore(array $config): IObjectStore {
30
-		return new $config['class']($config['arguments']);
31
-	}
32
-
33
-	/**
34
-	 * @return ?ObjectStoreConfig
35
-	 */
36
-	public function getObjectStoreConfigForRoot(): ?array {
37
-		if (!$this->hasObjectStore()) {
38
-			return null;
39
-		}
40
-
41
-		$config = $this->getObjectStoreConfiguration('root');
42
-
43
-		if ($config['arguments']['multibucket']) {
44
-			if (!isset($config['arguments']['bucket'])) {
45
-				$config['arguments']['bucket'] = '';
46
-			}
47
-
48
-			// put the root FS always in first bucket for multibucket configuration
49
-			$config['arguments']['bucket'] .= '0';
50
-		}
51
-		return $config;
52
-	}
53
-
54
-	/**
55
-	 * @return ?ObjectStoreConfig
56
-	 */
57
-	public function getObjectStoreConfigForUser(IUser $user): ?array {
58
-		if (!$this->hasObjectStore()) {
59
-			return null;
60
-		}
61
-
62
-		$store = $this->getObjectStoreForUser($user);
63
-		$config = $this->getObjectStoreConfiguration($store);
64
-
65
-		if ($config['arguments']['multibucket']) {
66
-			$config['arguments']['bucket'] = $this->getBucketForUser($user, $config);
67
-		}
68
-		return $config;
69
-	}
70
-
71
-	/**
72
-	 * @param string $name
73
-	 * @return ObjectStoreConfig
74
-	 */
75
-	public function getObjectStoreConfiguration(string $name): array {
76
-		$configs = $this->getObjectStoreConfigs();
77
-		$name = $this->resolveAlias($name);
78
-		if (!isset($configs[$name])) {
79
-			throw new \Exception("Object store configuration for '$name' not found");
80
-		}
81
-		if (is_string($configs[$name])) {
82
-			throw new \Exception("Object store configuration for '{$configs[$name]}' not found");
83
-		}
84
-		return $configs[$name];
85
-	}
86
-
87
-	public function resolveAlias(string $name): string {
88
-		$configs = $this->getObjectStoreConfigs();
89
-
90
-		while (isset($configs[$name]) && is_string($configs[$name])) {
91
-			$name = $configs[$name];
92
-		}
93
-		return $name;
94
-	}
95
-
96
-	public function hasObjectStore(): bool {
97
-		$objectStore = $this->config->getSystemValue('objectstore', null);
98
-		$objectStoreMultiBucket = $this->config->getSystemValue('objectstore_multibucket', null);
99
-		return $objectStore || $objectStoreMultiBucket;
100
-	}
101
-
102
-	public function hasMultipleObjectStorages(): bool {
103
-		$objectStore = $this->config->getSystemValue('objectstore', []);
104
-		return isset($objectStore['default']);
105
-	}
106
-
107
-	/**
108
-	 * @return ?array<string, ObjectStoreConfig|string>
109
-	 * @throws InvalidObjectStoreConfigurationException
110
-	 */
111
-	public function getObjectStoreConfigs(): ?array {
112
-		$objectStore = $this->config->getSystemValue('objectstore', null);
113
-		$objectStoreMultiBucket = $this->config->getSystemValue('objectstore_multibucket', null);
114
-
115
-		// new-style multibucket config uses the same 'objectstore' key but sets `'multibucket' => true`, transparently upgrade older style config
116
-		if ($objectStoreMultiBucket) {
117
-			$objectStoreMultiBucket['arguments']['multibucket'] = true;
118
-			return [
119
-				'default' => 'server1',
120
-				'server1' => $this->validateObjectStoreConfig($objectStoreMultiBucket),
121
-				'root' => 'server1',
122
-			];
123
-		} elseif ($objectStore) {
124
-			if (!isset($objectStore['default'])) {
125
-				$objectStore = [
126
-					'default' => 'server1',
127
-					'root' => 'server1',
128
-					'server1' => $objectStore,
129
-				];
130
-			}
131
-			if (!isset($objectStore['root'])) {
132
-				$objectStore['root'] = 'default';
133
-			}
134
-
135
-			if (!is_string($objectStore['default'])) {
136
-				throw new InvalidObjectStoreConfigurationException('The \'default\' object storage configuration is required to be a reference to another configuration.');
137
-			}
138
-			return array_map($this->validateObjectStoreConfig(...), $objectStore);
139
-		} else {
140
-			return null;
141
-		}
142
-	}
143
-
144
-	/**
145
-	 * @param array|string $config
146
-	 * @return string|ObjectStoreConfig
147
-	 */
148
-	private function validateObjectStoreConfig(array|string $config): array|string {
149
-		if (is_string($config)) {
150
-			return $config;
151
-		}
152
-		if (!isset($config['class'])) {
153
-			throw new InvalidObjectStoreConfigurationException('No class configured for object store');
154
-		}
155
-		if (!isset($config['arguments'])) {
156
-			$config['arguments'] = [];
157
-		}
158
-		$class = $config['class'];
159
-		$arguments = $config['arguments'];
160
-		if (!is_array($arguments)) {
161
-			throw new InvalidObjectStoreConfigurationException('Configured object store arguments are not an array');
162
-		}
163
-		if (!isset($arguments['multibucket'])) {
164
-			$arguments['multibucket'] = false;
165
-		}
166
-		if (!is_bool($arguments['multibucket'])) {
167
-			throw new InvalidObjectStoreConfigurationException('arguments.multibucket must be a boolean in object store configuration');
168
-		}
169
-
170
-		if (!is_string($class)) {
171
-			throw new InvalidObjectStoreConfigurationException('Configured class for object store is not a string');
172
-		}
173
-
174
-		if (str_starts_with($class, 'OCA\\') && substr_count($class, '\\') >= 2) {
175
-			[$appId] = explode('\\', $class);
176
-			$this->appManager->loadApp(strtolower($appId));
177
-		}
178
-
179
-		if (!is_a($class, IObjectStore::class, true)) {
180
-			throw new InvalidObjectStoreConfigurationException('Configured class for object store is not an object store');
181
-		}
182
-		return [
183
-			'class' => $class,
184
-			'arguments' => $arguments,
185
-		];
186
-	}
187
-
188
-	public function getBucketForUser(IUser $user, array $config): string {
189
-		$bucket = $this->getSetBucketForUser($user);
190
-
191
-		if ($bucket === null) {
192
-			/*
20
+    public function __construct(
21
+        private readonly IConfig $config,
22
+        private readonly IAppManager $appManager,
23
+    ) {
24
+    }
25
+
26
+    /**
27
+     * @param ObjectStoreConfig $config
28
+     */
29
+    public function buildObjectStore(array $config): IObjectStore {
30
+        return new $config['class']($config['arguments']);
31
+    }
32
+
33
+    /**
34
+     * @return ?ObjectStoreConfig
35
+     */
36
+    public function getObjectStoreConfigForRoot(): ?array {
37
+        if (!$this->hasObjectStore()) {
38
+            return null;
39
+        }
40
+
41
+        $config = $this->getObjectStoreConfiguration('root');
42
+
43
+        if ($config['arguments']['multibucket']) {
44
+            if (!isset($config['arguments']['bucket'])) {
45
+                $config['arguments']['bucket'] = '';
46
+            }
47
+
48
+            // put the root FS always in first bucket for multibucket configuration
49
+            $config['arguments']['bucket'] .= '0';
50
+        }
51
+        return $config;
52
+    }
53
+
54
+    /**
55
+     * @return ?ObjectStoreConfig
56
+     */
57
+    public function getObjectStoreConfigForUser(IUser $user): ?array {
58
+        if (!$this->hasObjectStore()) {
59
+            return null;
60
+        }
61
+
62
+        $store = $this->getObjectStoreForUser($user);
63
+        $config = $this->getObjectStoreConfiguration($store);
64
+
65
+        if ($config['arguments']['multibucket']) {
66
+            $config['arguments']['bucket'] = $this->getBucketForUser($user, $config);
67
+        }
68
+        return $config;
69
+    }
70
+
71
+    /**
72
+     * @param string $name
73
+     * @return ObjectStoreConfig
74
+     */
75
+    public function getObjectStoreConfiguration(string $name): array {
76
+        $configs = $this->getObjectStoreConfigs();
77
+        $name = $this->resolveAlias($name);
78
+        if (!isset($configs[$name])) {
79
+            throw new \Exception("Object store configuration for '$name' not found");
80
+        }
81
+        if (is_string($configs[$name])) {
82
+            throw new \Exception("Object store configuration for '{$configs[$name]}' not found");
83
+        }
84
+        return $configs[$name];
85
+    }
86
+
87
+    public function resolveAlias(string $name): string {
88
+        $configs = $this->getObjectStoreConfigs();
89
+
90
+        while (isset($configs[$name]) && is_string($configs[$name])) {
91
+            $name = $configs[$name];
92
+        }
93
+        return $name;
94
+    }
95
+
96
+    public function hasObjectStore(): bool {
97
+        $objectStore = $this->config->getSystemValue('objectstore', null);
98
+        $objectStoreMultiBucket = $this->config->getSystemValue('objectstore_multibucket', null);
99
+        return $objectStore || $objectStoreMultiBucket;
100
+    }
101
+
102
+    public function hasMultipleObjectStorages(): bool {
103
+        $objectStore = $this->config->getSystemValue('objectstore', []);
104
+        return isset($objectStore['default']);
105
+    }
106
+
107
+    /**
108
+     * @return ?array<string, ObjectStoreConfig|string>
109
+     * @throws InvalidObjectStoreConfigurationException
110
+     */
111
+    public function getObjectStoreConfigs(): ?array {
112
+        $objectStore = $this->config->getSystemValue('objectstore', null);
113
+        $objectStoreMultiBucket = $this->config->getSystemValue('objectstore_multibucket', null);
114
+
115
+        // new-style multibucket config uses the same 'objectstore' key but sets `'multibucket' => true`, transparently upgrade older style config
116
+        if ($objectStoreMultiBucket) {
117
+            $objectStoreMultiBucket['arguments']['multibucket'] = true;
118
+            return [
119
+                'default' => 'server1',
120
+                'server1' => $this->validateObjectStoreConfig($objectStoreMultiBucket),
121
+                'root' => 'server1',
122
+            ];
123
+        } elseif ($objectStore) {
124
+            if (!isset($objectStore['default'])) {
125
+                $objectStore = [
126
+                    'default' => 'server1',
127
+                    'root' => 'server1',
128
+                    'server1' => $objectStore,
129
+                ];
130
+            }
131
+            if (!isset($objectStore['root'])) {
132
+                $objectStore['root'] = 'default';
133
+            }
134
+
135
+            if (!is_string($objectStore['default'])) {
136
+                throw new InvalidObjectStoreConfigurationException('The \'default\' object storage configuration is required to be a reference to another configuration.');
137
+            }
138
+            return array_map($this->validateObjectStoreConfig(...), $objectStore);
139
+        } else {
140
+            return null;
141
+        }
142
+    }
143
+
144
+    /**
145
+     * @param array|string $config
146
+     * @return string|ObjectStoreConfig
147
+     */
148
+    private function validateObjectStoreConfig(array|string $config): array|string {
149
+        if (is_string($config)) {
150
+            return $config;
151
+        }
152
+        if (!isset($config['class'])) {
153
+            throw new InvalidObjectStoreConfigurationException('No class configured for object store');
154
+        }
155
+        if (!isset($config['arguments'])) {
156
+            $config['arguments'] = [];
157
+        }
158
+        $class = $config['class'];
159
+        $arguments = $config['arguments'];
160
+        if (!is_array($arguments)) {
161
+            throw new InvalidObjectStoreConfigurationException('Configured object store arguments are not an array');
162
+        }
163
+        if (!isset($arguments['multibucket'])) {
164
+            $arguments['multibucket'] = false;
165
+        }
166
+        if (!is_bool($arguments['multibucket'])) {
167
+            throw new InvalidObjectStoreConfigurationException('arguments.multibucket must be a boolean in object store configuration');
168
+        }
169
+
170
+        if (!is_string($class)) {
171
+            throw new InvalidObjectStoreConfigurationException('Configured class for object store is not a string');
172
+        }
173
+
174
+        if (str_starts_with($class, 'OCA\\') && substr_count($class, '\\') >= 2) {
175
+            [$appId] = explode('\\', $class);
176
+            $this->appManager->loadApp(strtolower($appId));
177
+        }
178
+
179
+        if (!is_a($class, IObjectStore::class, true)) {
180
+            throw new InvalidObjectStoreConfigurationException('Configured class for object store is not an object store');
181
+        }
182
+        return [
183
+            'class' => $class,
184
+            'arguments' => $arguments,
185
+        ];
186
+    }
187
+
188
+    public function getBucketForUser(IUser $user, array $config): string {
189
+        $bucket = $this->getSetBucketForUser($user);
190
+
191
+        if ($bucket === null) {
192
+            /*
193 193
 			 * Use any provided bucket argument as prefix
194 194
 			 * and add the mapping from username => bucket
195 195
 			 */
196
-			if (!isset($config['arguments']['bucket'])) {
197
-				$config['arguments']['bucket'] = '';
198
-			}
199
-			$mapper = new Mapper($user, $this->config);
200
-			$numBuckets = $config['arguments']['num_buckets'] ?? 64;
201
-			$bucket = $config['arguments']['bucket'] . $mapper->getBucket($numBuckets);
202
-
203
-			$this->config->setUserValue($user->getUID(), 'homeobjectstore', 'bucket', $bucket);
204
-		}
205
-
206
-		return $bucket;
207
-	}
208
-
209
-	public function getSetBucketForUser(IUser $user): ?string {
210
-		return $this->config->getUserValue($user->getUID(), 'homeobjectstore', 'bucket', null);
211
-	}
212
-
213
-	public function getObjectStoreForUser(IUser $user): string {
214
-		if ($this->hasMultipleObjectStorages()) {
215
-			$value = $this->config->getUserValue($user->getUID(), 'homeobjectstore', 'objectstore', null);
216
-			if ($value === null) {
217
-				$value = $this->resolveAlias('default');
218
-				$this->config->setUserValue($user->getUID(), 'homeobjectstore', 'objectstore', $value);
219
-			}
220
-			return $value;
221
-		} else {
222
-			return 'default';
223
-		}
224
-	}
196
+            if (!isset($config['arguments']['bucket'])) {
197
+                $config['arguments']['bucket'] = '';
198
+            }
199
+            $mapper = new Mapper($user, $this->config);
200
+            $numBuckets = $config['arguments']['num_buckets'] ?? 64;
201
+            $bucket = $config['arguments']['bucket'] . $mapper->getBucket($numBuckets);
202
+
203
+            $this->config->setUserValue($user->getUID(), 'homeobjectstore', 'bucket', $bucket);
204
+        }
205
+
206
+        return $bucket;
207
+    }
208
+
209
+    public function getSetBucketForUser(IUser $user): ?string {
210
+        return $this->config->getUserValue($user->getUID(), 'homeobjectstore', 'bucket', null);
211
+    }
212
+
213
+    public function getObjectStoreForUser(IUser $user): string {
214
+        if ($this->hasMultipleObjectStorages()) {
215
+            $value = $this->config->getUserValue($user->getUID(), 'homeobjectstore', 'objectstore', null);
216
+            if ($value === null) {
217
+                $value = $this->resolveAlias('default');
218
+                $this->config->setUserValue($user->getUID(), 'homeobjectstore', 'objectstore', $value);
219
+            }
220
+            return $value;
221
+        } else {
222
+            return 'default';
223
+        }
224
+    }
225 225
 }
Please login to merge, or discard this patch.
apps/files/lib/Command/Object/Multi/Users.php 1 patch
Indentation   +71 added lines, -71 removed lines patch added patch discarded remove patch
@@ -18,81 +18,81 @@
 block discarded – undo
18 18
 use Symfony\Component\Console\Output\OutputInterface;
19 19
 
20 20
 class Users extends Base {
21
-	public function __construct(
22
-		private readonly IUserManager $userManager,
23
-		private readonly PrimaryObjectStoreConfig $objectStoreConfig,
24
-		private readonly IConfig $config,
25
-	) {
26
-		parent::__construct();
27
-	}
21
+    public function __construct(
22
+        private readonly IUserManager $userManager,
23
+        private readonly PrimaryObjectStoreConfig $objectStoreConfig,
24
+        private readonly IConfig $config,
25
+    ) {
26
+        parent::__construct();
27
+    }
28 28
 
29
-	protected function configure(): void {
30
-		parent::configure();
31
-		$this
32
-			->setName('files:object:multi:users')
33
-			->setDescription('Get the mapping between users and object store buckets')
34
-			->addOption('bucket', 'b', InputOption::VALUE_REQUIRED, 'Only list users using the specified bucket')
35
-			->addOption('object-store', 'o', InputOption::VALUE_REQUIRED, 'Only list users using the specified object store configuration')
36
-			->addOption('user', 'u', InputOption::VALUE_REQUIRED, 'Only show the mapping for the specified user, ignores all other options');
37
-	}
29
+    protected function configure(): void {
30
+        parent::configure();
31
+        $this
32
+            ->setName('files:object:multi:users')
33
+            ->setDescription('Get the mapping between users and object store buckets')
34
+            ->addOption('bucket', 'b', InputOption::VALUE_REQUIRED, 'Only list users using the specified bucket')
35
+            ->addOption('object-store', 'o', InputOption::VALUE_REQUIRED, 'Only list users using the specified object store configuration')
36
+            ->addOption('user', 'u', InputOption::VALUE_REQUIRED, 'Only show the mapping for the specified user, ignores all other options');
37
+    }
38 38
 
39
-	public function execute(InputInterface $input, OutputInterface $output): int {
40
-		if ($userId = $input->getOption('user')) {
41
-			$user = $this->userManager->get($userId);
42
-			if (!$user) {
43
-				$output->writeln("<error>User $userId not found</error>");
44
-				return 1;
45
-			}
46
-			$users = new \ArrayIterator([$user]);
47
-		} else {
48
-			$bucket = (string)$input->getOption('bucket');
49
-			$objectStore = (string)$input->getOption('object-store');
50
-			if ($bucket !== '' && $objectStore === '') {
51
-				$users = $this->getUsers($this->config->getUsersForUserValue('homeobjectstore', 'bucket', $bucket));
52
-			} elseif ($bucket === '' && $objectStore !== '') {
53
-				$users = $this->getUsers($this->config->getUsersForUserValue('homeobjectstore', 'objectstore', $objectStore));
54
-			} elseif ($bucket) {
55
-				$users = $this->getUsers(array_intersect(
56
-					$this->config->getUsersForUserValue('homeobjectstore', 'bucket', $bucket),
57
-					$this->config->getUsersForUserValue('homeobjectstore', 'objectstore', $objectStore)
58
-				));
59
-			} else {
60
-				$users = $this->userManager->getSeenUsers();
61
-			}
62
-		}
39
+    public function execute(InputInterface $input, OutputInterface $output): int {
40
+        if ($userId = $input->getOption('user')) {
41
+            $user = $this->userManager->get($userId);
42
+            if (!$user) {
43
+                $output->writeln("<error>User $userId not found</error>");
44
+                return 1;
45
+            }
46
+            $users = new \ArrayIterator([$user]);
47
+        } else {
48
+            $bucket = (string)$input->getOption('bucket');
49
+            $objectStore = (string)$input->getOption('object-store');
50
+            if ($bucket !== '' && $objectStore === '') {
51
+                $users = $this->getUsers($this->config->getUsersForUserValue('homeobjectstore', 'bucket', $bucket));
52
+            } elseif ($bucket === '' && $objectStore !== '') {
53
+                $users = $this->getUsers($this->config->getUsersForUserValue('homeobjectstore', 'objectstore', $objectStore));
54
+            } elseif ($bucket) {
55
+                $users = $this->getUsers(array_intersect(
56
+                    $this->config->getUsersForUserValue('homeobjectstore', 'bucket', $bucket),
57
+                    $this->config->getUsersForUserValue('homeobjectstore', 'objectstore', $objectStore)
58
+                ));
59
+            } else {
60
+                $users = $this->userManager->getSeenUsers();
61
+            }
62
+        }
63 63
 
64
-		$this->writeStreamingTableInOutputFormat($input, $output, $this->infoForUsers($users), 100);
65
-		return 0;
66
-	}
64
+        $this->writeStreamingTableInOutputFormat($input, $output, $this->infoForUsers($users), 100);
65
+        return 0;
66
+    }
67 67
 
68
-	/**
69
-	 * @param string[] $userIds
70
-	 * @return \Iterator<IUser>
71
-	 */
72
-	private function getUsers(array $userIds): \Iterator {
73
-		foreach ($userIds as $userId) {
74
-			$user = $this->userManager->get($userId);
75
-			if ($user) {
76
-				yield $user;
77
-			}
78
-		}
79
-	}
68
+    /**
69
+     * @param string[] $userIds
70
+     * @return \Iterator<IUser>
71
+     */
72
+    private function getUsers(array $userIds): \Iterator {
73
+        foreach ($userIds as $userId) {
74
+            $user = $this->userManager->get($userId);
75
+            if ($user) {
76
+                yield $user;
77
+            }
78
+        }
79
+    }
80 80
 
81
-	/**
82
-	 * @param \Iterator<IUser> $users
83
-	 * @return \Iterator<array>
84
-	 */
85
-	private function infoForUsers(\Iterator $users): \Iterator {
86
-		foreach ($users as $user) {
87
-			yield $this->infoForUser($user);
88
-		}
89
-	}
81
+    /**
82
+     * @param \Iterator<IUser> $users
83
+     * @return \Iterator<array>
84
+     */
85
+    private function infoForUsers(\Iterator $users): \Iterator {
86
+        foreach ($users as $user) {
87
+            yield $this->infoForUser($user);
88
+        }
89
+    }
90 90
 
91
-	private function infoForUser(IUser $user): array {
92
-		return [
93
-			'user' => $user->getUID(),
94
-			'object-store' => $this->objectStoreConfig->getObjectStoreForUser($user),
95
-			'bucket' => $this->objectStoreConfig->getSetBucketForUser($user) ?? 'unset',
96
-		];
97
-	}
91
+    private function infoForUser(IUser $user): array {
92
+        return [
93
+            'user' => $user->getUID(),
94
+            'object-store' => $this->objectStoreConfig->getObjectStoreForUser($user),
95
+            'bucket' => $this->objectStoreConfig->getSetBucketForUser($user) ?? 'unset',
96
+        ];
97
+    }
98 98
 }
Please login to merge, or discard this patch.
apps/files/lib/Command/Object/Multi/Rename.php 1 patch
Indentation   +75 added lines, -75 removed lines patch added patch discarded remove patch
@@ -19,90 +19,90 @@
 block discarded – undo
19 19
 use Symfony\Component\Console\Question\ConfirmationQuestion;
20 20
 
21 21
 class Rename extends Base {
22
-	public function __construct(
23
-		private readonly IDBConnection $connection,
24
-		private readonly PrimaryObjectStoreConfig $objectStoreConfig,
25
-		private readonly IConfig $config,
26
-	) {
27
-		parent::__construct();
28
-	}
22
+    public function __construct(
23
+        private readonly IDBConnection $connection,
24
+        private readonly PrimaryObjectStoreConfig $objectStoreConfig,
25
+        private readonly IConfig $config,
26
+    ) {
27
+        parent::__construct();
28
+    }
29 29
 
30
-	protected function configure(): void {
31
-		parent::configure();
32
-		$this
33
-			->setName('files:object:multi:rename-config')
34
-			->setDescription('Rename an object store configuration and move all users over to the new configuration,')
35
-			->addArgument('source', InputArgument::REQUIRED, 'Object store configuration to rename')
36
-			->addArgument('target', InputArgument::REQUIRED, 'New name for the object store configuration');
37
-	}
30
+    protected function configure(): void {
31
+        parent::configure();
32
+        $this
33
+            ->setName('files:object:multi:rename-config')
34
+            ->setDescription('Rename an object store configuration and move all users over to the new configuration,')
35
+            ->addArgument('source', InputArgument::REQUIRED, 'Object store configuration to rename')
36
+            ->addArgument('target', InputArgument::REQUIRED, 'New name for the object store configuration');
37
+    }
38 38
 
39
-	public function execute(InputInterface $input, OutputInterface $output): int {
40
-		$source = $input->getArgument('source');
41
-		$target = $input->getArgument('target');
39
+    public function execute(InputInterface $input, OutputInterface $output): int {
40
+        $source = $input->getArgument('source');
41
+        $target = $input->getArgument('target');
42 42
 
43
-		$configs = $this->objectStoreConfig->getObjectStoreConfigs();
44
-		if (!isset($configs[$source])) {
45
-			$output->writeln('<error>Unknown object store configuration: ' . $source . '</error>');
46
-			return 1;
47
-		}
43
+        $configs = $this->objectStoreConfig->getObjectStoreConfigs();
44
+        if (!isset($configs[$source])) {
45
+            $output->writeln('<error>Unknown object store configuration: ' . $source . '</error>');
46
+            return 1;
47
+        }
48 48
 
49
-		if ($source === 'root') {
50
-			$output->writeln('<error>Renaming the root configuration is not supported.</error>');
51
-			return 1;
52
-		}
49
+        if ($source === 'root') {
50
+            $output->writeln('<error>Renaming the root configuration is not supported.</error>');
51
+            return 1;
52
+        }
53 53
 
54
-		if ($source === 'default') {
55
-			$output->writeln('<error>Renaming the default configuration is not supported.</error>');
56
-			return 1;
57
-		}
54
+        if ($source === 'default') {
55
+            $output->writeln('<error>Renaming the default configuration is not supported.</error>');
56
+            return 1;
57
+        }
58 58
 
59
-		if (!isset($configs[$target])) {
60
-			$output->writeln('<comment>Target object store configuration ' . $target . ' doesn\'t exist yet.</comment>');
61
-			$output->writeln('The target configuration can be created automatically.');
62
-			$output->writeln('However, as this depends on modifying the config.php, this only works as long as the instance runs on a single node or all nodes in a clustered setup have a shared config file (such as from a shared network mount).');
63
-			$output->writeln('If the different nodes have a separate copy of the config.php file, the automatic object store configuration creation will lead to the configuration going out of sync.');
64
-			$output->writeln('If these requirements are not met, you can manually create the target object store configuration in each node\'s configuration before running the command.');
65
-			$output->writeln('');
66
-			$output->writeln('<error>Failure to check these requirements will lead to data loss for users.</error>');
59
+        if (!isset($configs[$target])) {
60
+            $output->writeln('<comment>Target object store configuration ' . $target . ' doesn\'t exist yet.</comment>');
61
+            $output->writeln('The target configuration can be created automatically.');
62
+            $output->writeln('However, as this depends on modifying the config.php, this only works as long as the instance runs on a single node or all nodes in a clustered setup have a shared config file (such as from a shared network mount).');
63
+            $output->writeln('If the different nodes have a separate copy of the config.php file, the automatic object store configuration creation will lead to the configuration going out of sync.');
64
+            $output->writeln('If these requirements are not met, you can manually create the target object store configuration in each node\'s configuration before running the command.');
65
+            $output->writeln('');
66
+            $output->writeln('<error>Failure to check these requirements will lead to data loss for users.</error>');
67 67
 
68
-			/** @var QuestionHelper $helper */
69
-			$helper = $this->getHelper('question');
70
-			$question = new ConfirmationQuestion('Automatically create target object store configuration? [y/N] ', false);
71
-			if ($helper->ask($input, $output, $question)) {
72
-				$configs[$target] = $configs[$source];
68
+            /** @var QuestionHelper $helper */
69
+            $helper = $this->getHelper('question');
70
+            $question = new ConfirmationQuestion('Automatically create target object store configuration? [y/N] ', false);
71
+            if ($helper->ask($input, $output, $question)) {
72
+                $configs[$target] = $configs[$source];
73 73
 
74
-				// update all aliases
75
-				foreach ($configs as &$config) {
76
-					if ($config === $source) {
77
-						$config = $target;
78
-					}
79
-				}
80
-				$this->config->setSystemValue('objectstore', $configs);
81
-			} else {
82
-				return 0;
83
-			}
84
-		} elseif (($configs[$source] !== $configs[$target]) || $configs[$source] !== $target) {
85
-			$output->writeln('<error>Source and target configuration differ.</error>');
86
-			$output->writeln('');
87
-			$output->writeln('To ensure proper migration of users, the source and target configuration must be the same to ensure that the objects for the moved users exist on the target configuration.');
88
-			$output->writeln('The usual migration process consists of creating a clone of the old configuration, moving the users from the old configuration to the new one, and then adjust the old configuration that is longer used.');
89
-			return 1;
90
-		}
74
+                // update all aliases
75
+                foreach ($configs as &$config) {
76
+                    if ($config === $source) {
77
+                        $config = $target;
78
+                    }
79
+                }
80
+                $this->config->setSystemValue('objectstore', $configs);
81
+            } else {
82
+                return 0;
83
+            }
84
+        } elseif (($configs[$source] !== $configs[$target]) || $configs[$source] !== $target) {
85
+            $output->writeln('<error>Source and target configuration differ.</error>');
86
+            $output->writeln('');
87
+            $output->writeln('To ensure proper migration of users, the source and target configuration must be the same to ensure that the objects for the moved users exist on the target configuration.');
88
+            $output->writeln('The usual migration process consists of creating a clone of the old configuration, moving the users from the old configuration to the new one, and then adjust the old configuration that is longer used.');
89
+            return 1;
90
+        }
91 91
 
92
-		$query = $this->connection->getQueryBuilder();
93
-		$query->update('preferences')
94
-			->set('configvalue', $query->createNamedParameter($target))
95
-			->where($query->expr()->eq('appid', $query->createNamedParameter('homeobjectstore')))
96
-			->andWhere($query->expr()->eq('configkey', $query->createNamedParameter('objectstore')))
97
-			->andWhere($query->expr()->eq('configvalue', $query->createNamedParameter($source)));
98
-		$count = $query->executeStatement();
92
+        $query = $this->connection->getQueryBuilder();
93
+        $query->update('preferences')
94
+            ->set('configvalue', $query->createNamedParameter($target))
95
+            ->where($query->expr()->eq('appid', $query->createNamedParameter('homeobjectstore')))
96
+            ->andWhere($query->expr()->eq('configkey', $query->createNamedParameter('objectstore')))
97
+            ->andWhere($query->expr()->eq('configvalue', $query->createNamedParameter($source)));
98
+        $count = $query->executeStatement();
99 99
 
100
-		if ($count > 0) {
101
-			$output->writeln('Moved <info>' . $count . '</info> users');
102
-		} else {
103
-			$output->writeln('No users moved');
104
-		}
100
+        if ($count > 0) {
101
+            $output->writeln('Moved <info>' . $count . '</info> users');
102
+        } else {
103
+            $output->writeln('No users moved');
104
+        }
105 105
 
106
-		return 0;
107
-	}
106
+        return 0;
107
+    }
108 108
 }
Please login to merge, or discard this patch.
tests/lib/Files/ObjectStore/PrimaryObjectStoreConfigTest.php 1 patch
Indentation   +265 added lines, -265 removed lines patch added patch discarded remove patch
@@ -17,269 +17,269 @@
 block discarded – undo
17 17
 use Test\TestCase;
18 18
 
19 19
 class PrimaryObjectStoreConfigTest extends TestCase {
20
-	private array $systemConfig = [];
21
-	private array $userConfig = [];
22
-	private IConfig&MockObject $config;
23
-	private IAppManager&MockObject $appManager;
24
-	private PrimaryObjectStoreConfig $objectStoreConfig;
25
-
26
-	protected function setUp(): void {
27
-		parent::setUp();
28
-
29
-		$this->systemConfig = [];
30
-		$this->config = $this->createMock(IConfig::class);
31
-		$this->appManager = $this->createMock(IAppManager::class);
32
-		$this->config->method('getSystemValue')
33
-			->willReturnCallback(function ($key, $default = '') {
34
-				if (isset($this->systemConfig[$key])) {
35
-					return $this->systemConfig[$key];
36
-				} else {
37
-					return $default;
38
-				}
39
-			});
40
-		$this->config->method('getUserValue')
41
-			->willReturnCallback(function ($userId, $appName, $key, $default = '') {
42
-				if (isset($this->userConfig[$userId][$appName][$key])) {
43
-					return $this->userConfig[$userId][$appName][$key];
44
-				} else {
45
-					return $default;
46
-				}
47
-			});
48
-		$this->config->method('setUserValue')
49
-			->willReturnCallback(function ($userId, $appName, $key, $value) {
50
-				$this->userConfig[$userId][$appName][$key] = $value;
51
-			});
52
-
53
-		$this->objectStoreConfig = new PrimaryObjectStoreConfig($this->config, $this->appManager);
54
-	}
55
-
56
-	private function getUser(string $uid): IUser {
57
-		$user = $this->createMock(IUser::class);
58
-		$user->method('getUID')
59
-			->willReturn($uid);
60
-		return $user;
61
-	}
62
-
63
-	private function setConfig(string $key, $value) {
64
-		$this->systemConfig[$key] = $value;
65
-	}
66
-
67
-	public function testNewUserGetsDefault() {
68
-		$this->setConfig('objectstore', [
69
-			'default' => 'server1',
70
-			'server1' => [
71
-				'class' => StorageObjectStore::class,
72
-				'arguments' => [
73
-					'host' => 'server1',
74
-				],
75
-			],
76
-		]);
77
-
78
-		$result = $this->objectStoreConfig->getObjectStoreConfigForUser($this->getUser('test'));
79
-		$this->assertEquals('server1', $result['arguments']['host']);
80
-
81
-		$this->assertEquals('server1', $this->config->getUserValue('test', 'homeobjectstore', 'objectstore', null));
82
-	}
83
-
84
-	public function testExistingUserKeepsStorage() {
85
-		// setup user with `server1` as storage
86
-		$this->testNewUserGetsDefault();
87
-
88
-		$this->setConfig('objectstore', [
89
-			'default' => 'server2',
90
-			'server1' => [
91
-				'class' => StorageObjectStore::class,
92
-				'arguments' => [
93
-					'host' => 'server1',
94
-				],
95
-			],
96
-			'server2' => [
97
-				'class' => StorageObjectStore::class,
98
-				'arguments' => [
99
-					'host' => 'server2',
100
-				],
101
-			],
102
-		]);
103
-
104
-		$result = $this->objectStoreConfig->getObjectStoreConfigForUser($this->getUser('test'));
105
-		$this->assertEquals('server1', $result['arguments']['host']);
106
-
107
-		$this->assertEquals('server1', $this->config->getUserValue('test', 'homeobjectstore', 'objectstore', null));
108
-
109
-		$result = $this->objectStoreConfig->getObjectStoreConfigForUser($this->getUser('other-user'));
110
-		$this->assertEquals('server2', $result['arguments']['host']);
111
-	}
112
-
113
-	public function testNestedAliases() {
114
-		$this->setConfig('objectstore', [
115
-			'default' => 'a1',
116
-			'a1' => 'a2',
117
-			'a2' => 'server1',
118
-			'server1' => [
119
-				'class' => StorageObjectStore::class,
120
-				'arguments' => [
121
-					'host' => 'server1',
122
-				],
123
-			],
124
-		]);
125
-		$this->assertEquals('server1', $this->objectStoreConfig->resolveAlias('default'));
126
-	}
127
-
128
-	public function testMultibucketChangedConfig() {
129
-		$this->setConfig('objectstore', [
130
-			'default' => 'server1',
131
-			'server1' => [
132
-				'class' => StorageObjectStore::class,
133
-				'arguments' => [
134
-					'host' => 'server1',
135
-					'multibucket' => true,
136
-					'num_buckets' => 8,
137
-					'bucket' => 'bucket-'
138
-				],
139
-			],
140
-		]);
141
-
142
-		$result = $this->objectStoreConfig->getObjectStoreConfigForUser($this->getUser('test'));
143
-		$this->assertEquals('server1', $result['arguments']['host']);
144
-		$this->assertEquals('bucket-7', $result['arguments']['bucket']);
145
-
146
-		$this->setConfig('objectstore', [
147
-			'default' => 'server1',
148
-			'server1' => [
149
-				'class' => StorageObjectStore::class,
150
-				'arguments' => [
151
-					'host' => 'server1',
152
-					'multibucket' => true,
153
-					'num_buckets' => 64,
154
-					'bucket' => 'bucket-'
155
-				],
156
-			],
157
-		]);
158
-
159
-		$result = $this->objectStoreConfig->getObjectStoreConfigForUser($this->getUser('test'));
160
-		$this->assertEquals('server1', $result['arguments']['host']);
161
-		$this->assertEquals('bucket-7', $result['arguments']['bucket']);
162
-
163
-		$result = $this->objectStoreConfig->getObjectStoreConfigForUser($this->getUser('test-foo'));
164
-		$this->assertEquals('server1', $result['arguments']['host']);
165
-		$this->assertEquals('bucket-40', $result['arguments']['bucket']);
166
-
167
-		$this->setConfig('objectstore', [
168
-			'default' => 'server2',
169
-			'server1' => [
170
-				'class' => StorageObjectStore::class,
171
-				'arguments' => [
172
-					'host' => 'server1',
173
-					'multibucket' => true,
174
-					'num_buckets' => 64,
175
-					'bucket' => 'bucket-'
176
-				],
177
-			],
178
-			'server2' => [
179
-				'class' => StorageObjectStore::class,
180
-				'arguments' => [
181
-					'host' => 'server2',
182
-					'multibucket' => true,
183
-					'num_buckets' => 16,
184
-					'bucket' => 'bucket-'
185
-				],
186
-			],
187
-		]);
188
-
189
-		$result = $this->objectStoreConfig->getObjectStoreConfigForUser($this->getUser('test'));
190
-		$this->assertEquals('server1', $result['arguments']['host']);
191
-		$this->assertEquals('bucket-7', $result['arguments']['bucket']);
192
-
193
-		$result = $this->objectStoreConfig->getObjectStoreConfigForUser($this->getUser('test-bar'));
194
-		$this->assertEquals('server2', $result['arguments']['host']);
195
-		$this->assertEquals('bucket-4', $result['arguments']['bucket']);
196
-	}
197
-
198
-	public function testMultibucketOldConfig() {
199
-		$this->setConfig('objectstore_multibucket', [
200
-			'class' => StorageObjectStore::class,
201
-			'arguments' => [
202
-				'host' => 'server1',
203
-				'multibucket' => true,
204
-				'num_buckets' => 8,
205
-				'bucket' => 'bucket-'
206
-			],
207
-		]);
208
-		$configs = $this->objectStoreConfig->getObjectStoreConfigs();
209
-		$this->assertEquals([
210
-			'default' => 'server1',
211
-			'root' => 'server1',
212
-			'server1' => [
213
-				'class' => StorageObjectStore::class,
214
-				'arguments' => [
215
-					'host' => 'server1',
216
-					'multibucket' => true,
217
-					'num_buckets' => 8,
218
-					'bucket' => 'bucket-'
219
-				],
220
-			],
221
-		], $configs);
222
-	}
223
-
224
-	public function testSingleObjectStore() {
225
-		$this->setConfig('objectstore', [
226
-			'class' => StorageObjectStore::class,
227
-			'arguments' => [
228
-				'host' => 'server1',
229
-			],
230
-		]);
231
-		$configs = $this->objectStoreConfig->getObjectStoreConfigs();
232
-		$this->assertEquals([
233
-			'default' => 'server1',
234
-			'root' => 'server1',
235
-			'server1' => [
236
-				'class' => StorageObjectStore::class,
237
-				'arguments' => [
238
-					'host' => 'server1',
239
-					'multibucket' => false,
240
-				],
241
-			],
242
-		], $configs);
243
-	}
244
-
245
-	public function testRoot() {
246
-		$this->setConfig('objectstore', [
247
-			'default' => 'server1',
248
-			'server1' => [
249
-				'class' => StorageObjectStore::class,
250
-				'arguments' => [
251
-					'host' => 'server1',
252
-				],
253
-			],
254
-			'server2' => [
255
-				'class' => StorageObjectStore::class,
256
-				'arguments' => [
257
-					'host' => 'server2',
258
-				],
259
-			],
260
-		]);
261
-
262
-		$result = $this->objectStoreConfig->getObjectStoreConfigForRoot();
263
-		$this->assertEquals('server1', $result['arguments']['host']);
264
-
265
-		$this->setConfig('objectstore', [
266
-			'default' => 'server1',
267
-			'root' => 'server2',
268
-			'server1' => [
269
-				'class' => StorageObjectStore::class,
270
-				'arguments' => [
271
-					'host' => 'server1',
272
-				],
273
-			],
274
-			'server2' => [
275
-				'class' => StorageObjectStore::class,
276
-				'arguments' => [
277
-					'host' => 'server2',
278
-				],
279
-			],
280
-		]);
281
-
282
-		$result = $this->objectStoreConfig->getObjectStoreConfigForRoot();
283
-		$this->assertEquals('server2', $result['arguments']['host']);
284
-	}
20
+    private array $systemConfig = [];
21
+    private array $userConfig = [];
22
+    private IConfig&MockObject $config;
23
+    private IAppManager&MockObject $appManager;
24
+    private PrimaryObjectStoreConfig $objectStoreConfig;
25
+
26
+    protected function setUp(): void {
27
+        parent::setUp();
28
+
29
+        $this->systemConfig = [];
30
+        $this->config = $this->createMock(IConfig::class);
31
+        $this->appManager = $this->createMock(IAppManager::class);
32
+        $this->config->method('getSystemValue')
33
+            ->willReturnCallback(function ($key, $default = '') {
34
+                if (isset($this->systemConfig[$key])) {
35
+                    return $this->systemConfig[$key];
36
+                } else {
37
+                    return $default;
38
+                }
39
+            });
40
+        $this->config->method('getUserValue')
41
+            ->willReturnCallback(function ($userId, $appName, $key, $default = '') {
42
+                if (isset($this->userConfig[$userId][$appName][$key])) {
43
+                    return $this->userConfig[$userId][$appName][$key];
44
+                } else {
45
+                    return $default;
46
+                }
47
+            });
48
+        $this->config->method('setUserValue')
49
+            ->willReturnCallback(function ($userId, $appName, $key, $value) {
50
+                $this->userConfig[$userId][$appName][$key] = $value;
51
+            });
52
+
53
+        $this->objectStoreConfig = new PrimaryObjectStoreConfig($this->config, $this->appManager);
54
+    }
55
+
56
+    private function getUser(string $uid): IUser {
57
+        $user = $this->createMock(IUser::class);
58
+        $user->method('getUID')
59
+            ->willReturn($uid);
60
+        return $user;
61
+    }
62
+
63
+    private function setConfig(string $key, $value) {
64
+        $this->systemConfig[$key] = $value;
65
+    }
66
+
67
+    public function testNewUserGetsDefault() {
68
+        $this->setConfig('objectstore', [
69
+            'default' => 'server1',
70
+            'server1' => [
71
+                'class' => StorageObjectStore::class,
72
+                'arguments' => [
73
+                    'host' => 'server1',
74
+                ],
75
+            ],
76
+        ]);
77
+
78
+        $result = $this->objectStoreConfig->getObjectStoreConfigForUser($this->getUser('test'));
79
+        $this->assertEquals('server1', $result['arguments']['host']);
80
+
81
+        $this->assertEquals('server1', $this->config->getUserValue('test', 'homeobjectstore', 'objectstore', null));
82
+    }
83
+
84
+    public function testExistingUserKeepsStorage() {
85
+        // setup user with `server1` as storage
86
+        $this->testNewUserGetsDefault();
87
+
88
+        $this->setConfig('objectstore', [
89
+            'default' => 'server2',
90
+            'server1' => [
91
+                'class' => StorageObjectStore::class,
92
+                'arguments' => [
93
+                    'host' => 'server1',
94
+                ],
95
+            ],
96
+            'server2' => [
97
+                'class' => StorageObjectStore::class,
98
+                'arguments' => [
99
+                    'host' => 'server2',
100
+                ],
101
+            ],
102
+        ]);
103
+
104
+        $result = $this->objectStoreConfig->getObjectStoreConfigForUser($this->getUser('test'));
105
+        $this->assertEquals('server1', $result['arguments']['host']);
106
+
107
+        $this->assertEquals('server1', $this->config->getUserValue('test', 'homeobjectstore', 'objectstore', null));
108
+
109
+        $result = $this->objectStoreConfig->getObjectStoreConfigForUser($this->getUser('other-user'));
110
+        $this->assertEquals('server2', $result['arguments']['host']);
111
+    }
112
+
113
+    public function testNestedAliases() {
114
+        $this->setConfig('objectstore', [
115
+            'default' => 'a1',
116
+            'a1' => 'a2',
117
+            'a2' => 'server1',
118
+            'server1' => [
119
+                'class' => StorageObjectStore::class,
120
+                'arguments' => [
121
+                    'host' => 'server1',
122
+                ],
123
+            ],
124
+        ]);
125
+        $this->assertEquals('server1', $this->objectStoreConfig->resolveAlias('default'));
126
+    }
127
+
128
+    public function testMultibucketChangedConfig() {
129
+        $this->setConfig('objectstore', [
130
+            'default' => 'server1',
131
+            'server1' => [
132
+                'class' => StorageObjectStore::class,
133
+                'arguments' => [
134
+                    'host' => 'server1',
135
+                    'multibucket' => true,
136
+                    'num_buckets' => 8,
137
+                    'bucket' => 'bucket-'
138
+                ],
139
+            ],
140
+        ]);
141
+
142
+        $result = $this->objectStoreConfig->getObjectStoreConfigForUser($this->getUser('test'));
143
+        $this->assertEquals('server1', $result['arguments']['host']);
144
+        $this->assertEquals('bucket-7', $result['arguments']['bucket']);
145
+
146
+        $this->setConfig('objectstore', [
147
+            'default' => 'server1',
148
+            'server1' => [
149
+                'class' => StorageObjectStore::class,
150
+                'arguments' => [
151
+                    'host' => 'server1',
152
+                    'multibucket' => true,
153
+                    'num_buckets' => 64,
154
+                    'bucket' => 'bucket-'
155
+                ],
156
+            ],
157
+        ]);
158
+
159
+        $result = $this->objectStoreConfig->getObjectStoreConfigForUser($this->getUser('test'));
160
+        $this->assertEquals('server1', $result['arguments']['host']);
161
+        $this->assertEquals('bucket-7', $result['arguments']['bucket']);
162
+
163
+        $result = $this->objectStoreConfig->getObjectStoreConfigForUser($this->getUser('test-foo'));
164
+        $this->assertEquals('server1', $result['arguments']['host']);
165
+        $this->assertEquals('bucket-40', $result['arguments']['bucket']);
166
+
167
+        $this->setConfig('objectstore', [
168
+            'default' => 'server2',
169
+            'server1' => [
170
+                'class' => StorageObjectStore::class,
171
+                'arguments' => [
172
+                    'host' => 'server1',
173
+                    'multibucket' => true,
174
+                    'num_buckets' => 64,
175
+                    'bucket' => 'bucket-'
176
+                ],
177
+            ],
178
+            'server2' => [
179
+                'class' => StorageObjectStore::class,
180
+                'arguments' => [
181
+                    'host' => 'server2',
182
+                    'multibucket' => true,
183
+                    'num_buckets' => 16,
184
+                    'bucket' => 'bucket-'
185
+                ],
186
+            ],
187
+        ]);
188
+
189
+        $result = $this->objectStoreConfig->getObjectStoreConfigForUser($this->getUser('test'));
190
+        $this->assertEquals('server1', $result['arguments']['host']);
191
+        $this->assertEquals('bucket-7', $result['arguments']['bucket']);
192
+
193
+        $result = $this->objectStoreConfig->getObjectStoreConfigForUser($this->getUser('test-bar'));
194
+        $this->assertEquals('server2', $result['arguments']['host']);
195
+        $this->assertEquals('bucket-4', $result['arguments']['bucket']);
196
+    }
197
+
198
+    public function testMultibucketOldConfig() {
199
+        $this->setConfig('objectstore_multibucket', [
200
+            'class' => StorageObjectStore::class,
201
+            'arguments' => [
202
+                'host' => 'server1',
203
+                'multibucket' => true,
204
+                'num_buckets' => 8,
205
+                'bucket' => 'bucket-'
206
+            ],
207
+        ]);
208
+        $configs = $this->objectStoreConfig->getObjectStoreConfigs();
209
+        $this->assertEquals([
210
+            'default' => 'server1',
211
+            'root' => 'server1',
212
+            'server1' => [
213
+                'class' => StorageObjectStore::class,
214
+                'arguments' => [
215
+                    'host' => 'server1',
216
+                    'multibucket' => true,
217
+                    'num_buckets' => 8,
218
+                    'bucket' => 'bucket-'
219
+                ],
220
+            ],
221
+        ], $configs);
222
+    }
223
+
224
+    public function testSingleObjectStore() {
225
+        $this->setConfig('objectstore', [
226
+            'class' => StorageObjectStore::class,
227
+            'arguments' => [
228
+                'host' => 'server1',
229
+            ],
230
+        ]);
231
+        $configs = $this->objectStoreConfig->getObjectStoreConfigs();
232
+        $this->assertEquals([
233
+            'default' => 'server1',
234
+            'root' => 'server1',
235
+            'server1' => [
236
+                'class' => StorageObjectStore::class,
237
+                'arguments' => [
238
+                    'host' => 'server1',
239
+                    'multibucket' => false,
240
+                ],
241
+            ],
242
+        ], $configs);
243
+    }
244
+
245
+    public function testRoot() {
246
+        $this->setConfig('objectstore', [
247
+            'default' => 'server1',
248
+            'server1' => [
249
+                'class' => StorageObjectStore::class,
250
+                'arguments' => [
251
+                    'host' => 'server1',
252
+                ],
253
+            ],
254
+            'server2' => [
255
+                'class' => StorageObjectStore::class,
256
+                'arguments' => [
257
+                    'host' => 'server2',
258
+                ],
259
+            ],
260
+        ]);
261
+
262
+        $result = $this->objectStoreConfig->getObjectStoreConfigForRoot();
263
+        $this->assertEquals('server1', $result['arguments']['host']);
264
+
265
+        $this->setConfig('objectstore', [
266
+            'default' => 'server1',
267
+            'root' => 'server2',
268
+            'server1' => [
269
+                'class' => StorageObjectStore::class,
270
+                'arguments' => [
271
+                    'host' => 'server1',
272
+                ],
273
+            ],
274
+            'server2' => [
275
+                'class' => StorageObjectStore::class,
276
+                'arguments' => [
277
+                    'host' => 'server2',
278
+                ],
279
+            ],
280
+        ]);
281
+
282
+        $result = $this->objectStoreConfig->getObjectStoreConfigForRoot();
283
+        $this->assertEquals('server2', $result['arguments']['host']);
284
+    }
285 285
 }
Please login to merge, or discard this patch.
tests/lib/Files/Mount/ObjectHomeMountProviderTest.php 1 patch
Indentation   +242 added lines, -242 removed lines patch added patch discarded remove patch
@@ -16,256 +16,256 @@
 block discarded – undo
16 16
 use OCP\IUser;
17 17
 
18 18
 class ObjectHomeMountProviderTest extends \Test\TestCase {
19
-	/** @var ObjectHomeMountProvider */
20
-	protected $provider;
21
-
22
-	/** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
23
-	protected $config;
24
-
25
-	/** @var IUser|\PHPUnit\Framework\MockObject\MockObject */
26
-	protected $user;
27
-
28
-	/** @var IStorageFactory|\PHPUnit\Framework\MockObject\MockObject */
29
-	protected $loader;
30
-
31
-	protected function setUp(): void {
32
-		parent::setUp();
33
-
34
-		$this->config = $this->createMock(IConfig::class);
35
-		$this->user = $this->createMock(IUser::class);
36
-		$this->loader = $this->createMock(IStorageFactory::class);
37
-
38
-		$objectStoreConfig = new PrimaryObjectStoreConfig($this->config, $this->createMock(IAppManager::class));
39
-		$this->provider = new ObjectHomeMountProvider($objectStoreConfig);
40
-	}
41
-
42
-	public function testSingleBucket(): void {
43
-		$this->config->method('getSystemValue')
44
-			->willReturnCallback(function ($key, $default) {
45
-				if ($key === 'objectstore') {
46
-					return [
47
-						'class' => 'Test\Files\Mount\FakeObjectStore',
48
-						'arguments' => [
49
-							'foo' => 'bar'
50
-						],
51
-					];
52
-				} else {
53
-					return $default;
54
-				}
55
-			});
56
-
57
-		$mount = $this->provider->getHomeMountForUser($this->user, $this->loader);
58
-		$arguments = $this->invokePrivate($mount, 'arguments');
59
-
60
-		$objectStore = $arguments['objectstore'];
61
-		$this->assertInstanceOf(FakeObjectStore::class, $objectStore);
62
-		$this->assertEquals(['foo' => 'bar', 'multibucket' => false], $objectStore->getArguments());
63
-	}
64
-
65
-	public function testMultiBucket(): void {
66
-		$this->config->method('getSystemValue')
67
-			->willReturnCallback(function ($key, $default) {
68
-				if ($key === 'objectstore_multibucket') {
69
-					return [
70
-						'class' => 'Test\Files\Mount\FakeObjectStore',
71
-						'arguments' => [
72
-							'foo' => 'bar'
73
-						],
74
-					];
75
-				} else {
76
-					return $default;
77
-				}
78
-			});
79
-
80
-		$this->user->method('getUID')
81
-			->willReturn('uid');
82
-		$this->loader->expects($this->never())->method($this->anything());
83
-
84
-		$this->config->method('getUserValue')
85
-			->willReturn(null);
86
-
87
-		$this->config
88
-			->method('setUserValue')
89
-			->with(
90
-				$this->equalTo('uid'),
91
-				$this->equalTo('homeobjectstore'),
92
-				$this->equalTo('bucket'),
93
-				$this->equalTo('49'),
94
-				$this->equalTo(null)
95
-			);
96
-
97
-		$mount = $this->provider->getHomeMountForUser($this->user, $this->loader);
98
-		$arguments = $this->invokePrivate($mount, 'arguments');
99
-
100
-		$objectStore = $arguments['objectstore'];
101
-		$this->assertInstanceOf(FakeObjectStore::class, $objectStore);
102
-		$this->assertEquals(['foo' => 'bar', 'bucket' => 49, 'multibucket' => true], $objectStore->getArguments());
103
-	}
104
-
105
-	public function testMultiBucketWithPrefix(): void {
106
-		$this->config->method('getSystemValue')
107
-			->willReturnCallback(function ($key, $default) {
108
-				if ($key === 'objectstore_multibucket') {
109
-					return [
110
-						'class' => 'Test\Files\Mount\FakeObjectStore',
111
-						'arguments' => [
112
-							'foo' => 'bar',
113
-							'bucket' => 'myBucketPrefix',
114
-						],
115
-					];
116
-				} else {
117
-					return $default;
118
-				}
119
-			});
120
-
121
-		$this->user->method('getUID')
122
-			->willReturn('uid');
123
-		$this->loader->expects($this->never())->method($this->anything());
124
-
125
-		$this->config
126
-			->method('getUserValue')
127
-			->willReturn(null);
128
-
129
-		$this->config->expects($this->once())
130
-			->method('setUserValue')
131
-			->with(
132
-				$this->equalTo('uid'),
133
-				$this->equalTo('homeobjectstore'),
134
-				$this->equalTo('bucket'),
135
-				$this->equalTo('myBucketPrefix49'),
136
-				$this->equalTo(null)
137
-			);
138
-
139
-		$mount = $this->provider->getHomeMountForUser($this->user, $this->loader);
140
-		$arguments = $this->invokePrivate($mount, 'arguments');
141
-
142
-		$objectStore = $arguments['objectstore'];
143
-		$this->assertInstanceOf(FakeObjectStore::class, $objectStore);
144
-		$this->assertEquals(['foo' => 'bar', 'bucket' => 'myBucketPrefix49', 'multibucket' => true], $objectStore->getArguments());
145
-	}
146
-
147
-	public function testMultiBucketBucketAlreadySet(): void {
148
-		$this->config->method('getSystemValue')
149
-			->willReturnCallback(function ($key, $default) {
150
-				if ($key === 'objectstore_multibucket') {
151
-					return [
152
-						'class' => 'Test\Files\Mount\FakeObjectStore',
153
-						'arguments' => [
154
-							'foo' => 'bar',
155
-							'bucket' => 'myBucketPrefix',
156
-						],
157
-					];
158
-				} else {
159
-					return $default;
160
-				}
161
-			});
162
-
163
-		$this->user->method('getUID')
164
-			->willReturn('uid');
165
-		$this->loader->expects($this->never())->method($this->anything());
166
-
167
-		$this->config
168
-			->method('getUserValue')
169
-			->willReturnCallback(function ($uid, $app, $key, $default) {
170
-				if ($uid === 'uid' && $app === 'homeobjectstore' && $key === 'bucket') {
171
-					return 'awesomeBucket1';
172
-				} else {
173
-					return $default;
174
-				}
175
-			});
176
-
177
-		$this->config->expects($this->never())
178
-			->method('setUserValue');
179
-
180
-		$mount = $this->provider->getHomeMountForUser($this->user, $this->loader);
181
-		$arguments = $this->invokePrivate($mount, 'arguments');
182
-
183
-		$objectStore = $arguments['objectstore'];
184
-		$this->assertInstanceOf(FakeObjectStore::class, $objectStore);
185
-		$this->assertEquals(['foo' => 'bar', 'bucket' => 'awesomeBucket1', 'multibucket' => true], $objectStore->getArguments());
186
-	}
187
-
188
-	public function testMultiBucketConfigFirst(): void {
189
-		$this->config->method('getSystemValue')
190
-			->willReturnCallback(function ($key, $default) {
191
-				if ($key === 'objectstore_multibucket') {
192
-					return [
193
-						'class' => 'Test\Files\Mount\FakeObjectStore',
194
-						'arguments' => [
195
-							'foo' => 'bar',
196
-							'bucket' => 'myBucketPrefix',
197
-						],
198
-					];
199
-				} else {
200
-					return $default;
201
-				}
202
-			});
203
-
204
-		$this->user->method('getUID')
205
-			->willReturn('uid');
206
-		$this->loader->expects($this->never())->method($this->anything());
207
-
208
-		$mount = $this->provider->getHomeMountForUser($this->user, $this->loader);
209
-		$this->assertInstanceOf('OC\Files\Mount\MountPoint', $mount);
210
-	}
211
-
212
-	public function testMultiBucketConfigFirstFallBackSingle(): void {
213
-		$this->config
214
-			->method('getSystemValue')->willReturnMap([
215
-				['objectstore_multibucket', null, null],
216
-				['objectstore', null, [
217
-					'class' => 'Test\Files\Mount\FakeObjectStore',
218
-					'arguments' => [
219
-						'foo' => 'bar',
220
-						'bucket' => 'myBucketPrefix',
221
-					],
222
-				]],
223
-			]);
224
-
225
-		$this->user->method('getUID')
226
-			->willReturn('uid');
227
-		$this->loader->expects($this->never())->method($this->anything());
228
-
229
-		$mount = $this->provider->getHomeMountForUser($this->user, $this->loader);
230
-		$this->assertInstanceOf('OC\Files\Mount\MountPoint', $mount);
231
-	}
232
-
233
-	public function testNoObjectStore(): void {
234
-		$this->config->method('getSystemValue')
235
-			->willReturnCallback(function ($key, $default) {
236
-				return $default;
237
-			});
238
-
239
-		$mount = $this->provider->getHomeMountForUser($this->user, $this->loader);
240
-		$this->assertNull($mount);
241
-	}
19
+    /** @var ObjectHomeMountProvider */
20
+    protected $provider;
21
+
22
+    /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
23
+    protected $config;
24
+
25
+    /** @var IUser|\PHPUnit\Framework\MockObject\MockObject */
26
+    protected $user;
27
+
28
+    /** @var IStorageFactory|\PHPUnit\Framework\MockObject\MockObject */
29
+    protected $loader;
30
+
31
+    protected function setUp(): void {
32
+        parent::setUp();
33
+
34
+        $this->config = $this->createMock(IConfig::class);
35
+        $this->user = $this->createMock(IUser::class);
36
+        $this->loader = $this->createMock(IStorageFactory::class);
37
+
38
+        $objectStoreConfig = new PrimaryObjectStoreConfig($this->config, $this->createMock(IAppManager::class));
39
+        $this->provider = new ObjectHomeMountProvider($objectStoreConfig);
40
+    }
41
+
42
+    public function testSingleBucket(): void {
43
+        $this->config->method('getSystemValue')
44
+            ->willReturnCallback(function ($key, $default) {
45
+                if ($key === 'objectstore') {
46
+                    return [
47
+                        'class' => 'Test\Files\Mount\FakeObjectStore',
48
+                        'arguments' => [
49
+                            'foo' => 'bar'
50
+                        ],
51
+                    ];
52
+                } else {
53
+                    return $default;
54
+                }
55
+            });
56
+
57
+        $mount = $this->provider->getHomeMountForUser($this->user, $this->loader);
58
+        $arguments = $this->invokePrivate($mount, 'arguments');
59
+
60
+        $objectStore = $arguments['objectstore'];
61
+        $this->assertInstanceOf(FakeObjectStore::class, $objectStore);
62
+        $this->assertEquals(['foo' => 'bar', 'multibucket' => false], $objectStore->getArguments());
63
+    }
64
+
65
+    public function testMultiBucket(): void {
66
+        $this->config->method('getSystemValue')
67
+            ->willReturnCallback(function ($key, $default) {
68
+                if ($key === 'objectstore_multibucket') {
69
+                    return [
70
+                        'class' => 'Test\Files\Mount\FakeObjectStore',
71
+                        'arguments' => [
72
+                            'foo' => 'bar'
73
+                        ],
74
+                    ];
75
+                } else {
76
+                    return $default;
77
+                }
78
+            });
79
+
80
+        $this->user->method('getUID')
81
+            ->willReturn('uid');
82
+        $this->loader->expects($this->never())->method($this->anything());
83
+
84
+        $this->config->method('getUserValue')
85
+            ->willReturn(null);
86
+
87
+        $this->config
88
+            ->method('setUserValue')
89
+            ->with(
90
+                $this->equalTo('uid'),
91
+                $this->equalTo('homeobjectstore'),
92
+                $this->equalTo('bucket'),
93
+                $this->equalTo('49'),
94
+                $this->equalTo(null)
95
+            );
96
+
97
+        $mount = $this->provider->getHomeMountForUser($this->user, $this->loader);
98
+        $arguments = $this->invokePrivate($mount, 'arguments');
99
+
100
+        $objectStore = $arguments['objectstore'];
101
+        $this->assertInstanceOf(FakeObjectStore::class, $objectStore);
102
+        $this->assertEquals(['foo' => 'bar', 'bucket' => 49, 'multibucket' => true], $objectStore->getArguments());
103
+    }
104
+
105
+    public function testMultiBucketWithPrefix(): void {
106
+        $this->config->method('getSystemValue')
107
+            ->willReturnCallback(function ($key, $default) {
108
+                if ($key === 'objectstore_multibucket') {
109
+                    return [
110
+                        'class' => 'Test\Files\Mount\FakeObjectStore',
111
+                        'arguments' => [
112
+                            'foo' => 'bar',
113
+                            'bucket' => 'myBucketPrefix',
114
+                        ],
115
+                    ];
116
+                } else {
117
+                    return $default;
118
+                }
119
+            });
120
+
121
+        $this->user->method('getUID')
122
+            ->willReturn('uid');
123
+        $this->loader->expects($this->never())->method($this->anything());
124
+
125
+        $this->config
126
+            ->method('getUserValue')
127
+            ->willReturn(null);
128
+
129
+        $this->config->expects($this->once())
130
+            ->method('setUserValue')
131
+            ->with(
132
+                $this->equalTo('uid'),
133
+                $this->equalTo('homeobjectstore'),
134
+                $this->equalTo('bucket'),
135
+                $this->equalTo('myBucketPrefix49'),
136
+                $this->equalTo(null)
137
+            );
138
+
139
+        $mount = $this->provider->getHomeMountForUser($this->user, $this->loader);
140
+        $arguments = $this->invokePrivate($mount, 'arguments');
141
+
142
+        $objectStore = $arguments['objectstore'];
143
+        $this->assertInstanceOf(FakeObjectStore::class, $objectStore);
144
+        $this->assertEquals(['foo' => 'bar', 'bucket' => 'myBucketPrefix49', 'multibucket' => true], $objectStore->getArguments());
145
+    }
146
+
147
+    public function testMultiBucketBucketAlreadySet(): void {
148
+        $this->config->method('getSystemValue')
149
+            ->willReturnCallback(function ($key, $default) {
150
+                if ($key === 'objectstore_multibucket') {
151
+                    return [
152
+                        'class' => 'Test\Files\Mount\FakeObjectStore',
153
+                        'arguments' => [
154
+                            'foo' => 'bar',
155
+                            'bucket' => 'myBucketPrefix',
156
+                        ],
157
+                    ];
158
+                } else {
159
+                    return $default;
160
+                }
161
+            });
162
+
163
+        $this->user->method('getUID')
164
+            ->willReturn('uid');
165
+        $this->loader->expects($this->never())->method($this->anything());
166
+
167
+        $this->config
168
+            ->method('getUserValue')
169
+            ->willReturnCallback(function ($uid, $app, $key, $default) {
170
+                if ($uid === 'uid' && $app === 'homeobjectstore' && $key === 'bucket') {
171
+                    return 'awesomeBucket1';
172
+                } else {
173
+                    return $default;
174
+                }
175
+            });
176
+
177
+        $this->config->expects($this->never())
178
+            ->method('setUserValue');
179
+
180
+        $mount = $this->provider->getHomeMountForUser($this->user, $this->loader);
181
+        $arguments = $this->invokePrivate($mount, 'arguments');
182
+
183
+        $objectStore = $arguments['objectstore'];
184
+        $this->assertInstanceOf(FakeObjectStore::class, $objectStore);
185
+        $this->assertEquals(['foo' => 'bar', 'bucket' => 'awesomeBucket1', 'multibucket' => true], $objectStore->getArguments());
186
+    }
187
+
188
+    public function testMultiBucketConfigFirst(): void {
189
+        $this->config->method('getSystemValue')
190
+            ->willReturnCallback(function ($key, $default) {
191
+                if ($key === 'objectstore_multibucket') {
192
+                    return [
193
+                        'class' => 'Test\Files\Mount\FakeObjectStore',
194
+                        'arguments' => [
195
+                            'foo' => 'bar',
196
+                            'bucket' => 'myBucketPrefix',
197
+                        ],
198
+                    ];
199
+                } else {
200
+                    return $default;
201
+                }
202
+            });
203
+
204
+        $this->user->method('getUID')
205
+            ->willReturn('uid');
206
+        $this->loader->expects($this->never())->method($this->anything());
207
+
208
+        $mount = $this->provider->getHomeMountForUser($this->user, $this->loader);
209
+        $this->assertInstanceOf('OC\Files\Mount\MountPoint', $mount);
210
+    }
211
+
212
+    public function testMultiBucketConfigFirstFallBackSingle(): void {
213
+        $this->config
214
+            ->method('getSystemValue')->willReturnMap([
215
+                ['objectstore_multibucket', null, null],
216
+                ['objectstore', null, [
217
+                    'class' => 'Test\Files\Mount\FakeObjectStore',
218
+                    'arguments' => [
219
+                        'foo' => 'bar',
220
+                        'bucket' => 'myBucketPrefix',
221
+                    ],
222
+                ]],
223
+            ]);
224
+
225
+        $this->user->method('getUID')
226
+            ->willReturn('uid');
227
+        $this->loader->expects($this->never())->method($this->anything());
228
+
229
+        $mount = $this->provider->getHomeMountForUser($this->user, $this->loader);
230
+        $this->assertInstanceOf('OC\Files\Mount\MountPoint', $mount);
231
+    }
232
+
233
+    public function testNoObjectStore(): void {
234
+        $this->config->method('getSystemValue')
235
+            ->willReturnCallback(function ($key, $default) {
236
+                return $default;
237
+            });
238
+
239
+        $mount = $this->provider->getHomeMountForUser($this->user, $this->loader);
240
+        $this->assertNull($mount);
241
+    }
242 242
 }
243 243
 
244 244
 class FakeObjectStore implements IObjectStore {
245
-	public function __construct(
246
-		private array $arguments,
247
-	) {
248
-	}
245
+    public function __construct(
246
+        private array $arguments,
247
+    ) {
248
+    }
249 249
 
250
-	public function getArguments() {
251
-		return $this->arguments;
252
-	}
250
+    public function getArguments() {
251
+        return $this->arguments;
252
+    }
253 253
 
254
-	public function getStorageId() {
255
-	}
254
+    public function getStorageId() {
255
+    }
256 256
 
257
-	public function readObject($urn) {
258
-	}
257
+    public function readObject($urn) {
258
+    }
259 259
 
260
-	public function writeObject($urn, $stream, ?string $mimetype = null) {
261
-	}
260
+    public function writeObject($urn, $stream, ?string $mimetype = null) {
261
+    }
262 262
 
263
-	public function deleteObject($urn) {
264
-	}
263
+    public function deleteObject($urn) {
264
+    }
265 265
 
266
-	public function objectExists($urn) {
267
-	}
266
+    public function objectExists($urn) {
267
+    }
268 268
 
269
-	public function copyObject($from, $to) {
270
-	}
269
+    public function copyObject($from, $to) {
270
+    }
271 271
 }
Please login to merge, or discard this patch.