--- /dev/null
+<?php
+
+declare(strict_types=1);
+/**
+ * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+namespace OC;
+
+use InvalidArgumentException;
+use JsonException;
+use OCP\DB\Exception as DBException;
+use OCP\DB\IResult;
+use OCP\DB\QueryBuilder\IQueryBuilder;
+use OCP\IConfig;
+use OCP\IDBConnection;
+use OCP\Security\ICrypto;
+use OCP\UserPreferences\Exceptions\IncorrectTypeException;
+use OCP\UserPreferences\Exceptions\TypeConflictException;
+use OCP\UserPreferences\Exceptions\UnknownKeyException;
+use OCP\UserPreferences\IUserPreferences;
+use OCP\UserPreferences\ValueType;
+use Psr\Log\LoggerInterface;
+use ValueError;
+
+/**
+ * This class provides an easy way for apps to store user preferences in the
+ * database.
+ * Supports **lazy loading**
+ *
+ * ### What is lazy loading ?
+ * In order to avoid loading useless user preferences into memory for each request,
+ * only non-lazy values are now loaded.
+ *
+ * Once a value that is lazy is requested, all lazy values will be loaded.
+ *
+ * Similarly, some methods from this class are marked with a warning about ignoring
+ * lazy loading. Use them wisely and only on parts of the code that are called
+ * during specific requests or actions to avoid loading the lazy values all the time.
+ *
+ * @since 31.0.0
+ */
+class UserPreferences implements IUserPreferences {
+ private const USER_MAX_LENGTH = 64;
+ private const APP_MAX_LENGTH = 32;
+ private const KEY_MAX_LENGTH = 64;
+ private const ENCRYPTION_PREFIX = '$UserPreferencesEncryption$';
+ private const ENCRYPTION_PREFIX_LENGTH = 27; // strlen(self::ENCRYPTION_PREFIX)
+
+ /** @var array<string, array<string, array<string, mixed>>> ['user_id' => ['app_id' => ['key' => 'value']]] */
+ private array $fastCache = []; // cache for normal preference keys
+ /** @var array<string, array<string, array<string, mixed>>> ['user_id' => ['app_id' => ['key' => 'value']]] */
+ private array $lazyCache = []; // cache for lazy preference keys
+ /** @var array<string, array<string, array<string, int>>> ['user_id' => ['app_id' => ['key' => bitflag]]] */
+ private array $valueTypes = []; // type for all preference values
+ /** @var array<string, boolean> ['user_id' => bool] */
+ private array $fastLoaded = [];
+ /** @var array<string, boolean> ['user_id' => bool] */
+ private array $lazyLoaded = [];
+
+ public function __construct(
+ protected IDBConnection $connection,
+ protected LoggerInterface $logger,
+ protected ICrypto $crypto,
+ ) {
+ }
+
+ /**
+ * @inheritDoc
+ *
+ * @param string $appId optional id of app
+ *
+ * @return list<string> list of userIds
+ * @since 31.0.0
+ */
+ public function getUserIds(string $appId = ''): array {
+ $this->assertParams(app: $appId, allowEmptyUser: true, allowEmptyApp: true);
+
+ $qb = $this->connection->getQueryBuilder();
+ $qb->from('preferences');
+ $qb->select('userid');
+ $qb->groupBy('userid');
+ if ($appId !== '') {
+ $qb->where($qb->expr()->eq('appid', $qb->createNamedParameter($appId)));
+ }
+
+ $result = $qb->executeQuery();
+ $rows = $result->fetchAll();
+ $userIds = [];
+ foreach ($rows as $row) {
+ $userIds[] = $row['userid'];
+ }
+
+ return $userIds;
+ }
+
+ /**
+ * @inheritDoc
+ *
+ * @return list<string> list of app ids
+ * @since 31.0.0
+ */
+ public function getApps(string $userId): array {
+ $this->assertParams($userId, allowEmptyApp: true);
+ $this->loadPreferencesAll($userId);
+ $apps = array_merge(array_keys($this->fastCache[$userId] ?? []), array_keys($this->lazyCache[$userId] ?? []));
+ sort($apps);
+
+ return array_values(array_unique($apps));
+ }
+
+ /**
+ * @inheritDoc
+ *
+ * @param string $userId id of the user
+ * @param string $app id of the app
+ *
+ * @return list<string> list of stored preference keys
+ * @since 31.0.0
+ */
+ public function getKeys(string $userId, string $app): array {
+ $this->assertParams($userId, $app);
+ $this->loadPreferencesAll($userId);
+ // array_merge() will remove numeric keys (here preference keys), so addition arrays instead
+ $keys = array_map('strval', array_keys(($this->fastCache[$userId][$app] ?? []) + ($this->lazyCache[$userId][$app] ?? [])));
+ sort($keys);
+
+ return array_values(array_unique($keys));
+ }
+
+ /**
+ * @inheritDoc
+ *
+ * @param string $userId id of the user
+ * @param string $app id of the app
+ * @param string $key preference key
+ * @param bool|null $lazy TRUE to search within lazy loaded preferences, NULL to search within all preferences
+ *
+ * @return bool TRUE if key exists
+ * @since 31.0.0
+ */
+ public function hasKey(string $userId, string $app, string $key, ?bool $lazy = false): bool {
+ $this->assertParams($userId, $app, $key);
+ $this->loadPreferences($userId, $lazy);
+
+ if ($lazy === null) {
+ $appCache = $this->getValues($userId, $app);
+ return isset($appCache[$key]);
+ }
+
+ if ($lazy) {
+ return isset($this->lazyCache[$userId][$app][$key]);
+ }
+
+ return isset($this->fastCache[$userId][$app][$key]);
+ }
+
+ /**
+ * @param string $userId id of the user
+ * @param string $app id of the app
+ * @param string $key preference key
+ * @param bool|null $lazy TRUE to search within lazy loaded preferences, NULL to search within all preferences
+ *
+ * @return bool
+ * @throws UnknownKeyException if preference key is not known
+ * @since 29.0.0
+ */
+ public function isSensitive(string $userId, string $app, string $key, ?bool $lazy = false): bool {
+ $this->assertParams($userId, $app, $key);
+ $this->loadPreferences($userId, $lazy);
+
+ if (!isset($this->valueTypes[$userId][$app][$key])) {
+ throw new UnknownKeyException('unknown preference key');
+ }
+
+ return $this->isTyped(ValueType::SENSITIVE, $this->valueTypes[$userId][$app][$key]);
+ }
+
+ /**
+ * @inheritDoc
+ *
+ * @param string $userId id of the user
+ * @param string $app if of the app
+ * @param string $key preference key
+ *
+ * @return bool TRUE if preference is lazy loaded
+ * @throws UnknownKeyException if preference key is not known
+ * @see IUserPreferences for details about lazy loading
+ * @since 29.0.0
+ */
+ public function isLazy(string $userId, string $app, string $key): bool {
+ // there is a huge probability the non-lazy preferences are already loaded
+ if ($this->hasKey($userId, $app, $key, false)) {
+ return false;
+ }
+
+ // key not found, we search in the lazy preferences
+ if ($this->hasKey($userId, $app, $key, true)) {
+ return true;
+ }
+
+ throw new UnknownKeyException('unknown preference key');
+ }
+
+ /**
+ * @inheritDoc
+ *
+ * @param string $userId id of the user
+ * @param string $app id of the app
+ * @param string $prefix preference keys prefix to search
+ * @param bool $filtered TRUE to hide sensitive preference values. Value are replaced by {@see IConfig::SENSITIVE_VALUE}
+ *
+ * @return array<string, string|int|float|bool|array> [key => value]
+ * @since 31.0.0
+ */
+ public function getValues(
+ string $userId,
+ string $app,
+ string $prefix = '',
+ bool $filtered = false,
+ ): array {
+ $this->assertParams($userId, $app, $prefix);
+ // if we want to filter values, we need to get sensitivity
+ $this->loadPreferencesAll($userId);
+ // array_merge() will remove numeric keys (here preference keys), so addition arrays instead
+ $values = array_filter(
+ $this->formatAppValues($userId, $app, ($this->fastCache[$userId][$app] ?? []) + ($this->lazyCache[$userId][$app] ?? []), $filtered),
+ function (string $key) use ($prefix): bool {
+ return str_starts_with($key, $prefix); // filter values based on $prefix
+ }, ARRAY_FILTER_USE_KEY
+ );
+
+ return $values;
+ }
+
+ /**
+ * @inheritDoc
+ *
+ * @param string $userId id of the user
+ * @param bool $filtered TRUE to hide sensitive preference values. Value are replaced by {@see IConfig::SENSITIVE_VALUE}
+ *
+ * @return array<string, array<string, string|int|float|bool|array>> [appId => [key => value]]
+ * @since 31.0.0
+ */
+ public function getAllValues(string $userId, bool $filtered = false): array {
+ $this->assertParams($userId, allowEmptyApp: true);
+ $this->loadPreferencesAll($userId);
+
+ $result = [];
+ foreach ($this->getApps($userId) as $app) {
+ // array_merge() will remove numeric keys (here preference keys), so addition arrays instead
+ $cached = ($this->fastCache[$userId][$app] ?? []) + ($this->lazyCache[$userId][$app] ?? []);
+ $result[$app] = $this->formatAppValues($userId, $app, $cached, $filtered);
+ }
+
+ return $result;
+ }
+
+ /**
+ * @inheritDoc
+ *
+ * @param string $userId id of the user
+ * @param string $key preference key
+ * @param bool $lazy search within lazy loaded preferences
+ * @param ValueType|null $typedAs enforce type for the returned values
+ *
+ * @return array<string, string|int|float|bool|array> [appId => value]
+ * @since 31.0.0
+ */
+ public function searchValuesByApps(string $userId, string $key, bool $lazy = false, ?ValueType $typedAs = null): array {
+ $this->assertParams($userId, '', $key, allowEmptyApp: true);
+ $this->loadPreferences($userId, $lazy);
+
+ /** @var array<array-key, array<array-key, mixed>> $cache */
+ if ($lazy) {
+ $cache = $this->lazyCache[$userId];
+ } else {
+ $cache = $this->fastCache[$userId];
+ }
+
+ $values = [];
+ foreach (array_keys($cache) as $app) {
+ if (isset($cache[$app][$key])) {
+ $value = $cache[$app][$key];
+ try {
+ $this->decryptSensitiveValue($userId, $app, $key, $value);
+ $value = $this->convertTypedValue($value, $typedAs ?? $this->getValueType($userId, $app, $key, $lazy));
+ } catch (IncorrectTypeException|UnknownKeyException) {
+ }
+ $values[$app] = $value;
+ }
+ }
+
+ return $values;
+ }
+
+
+ /**
+ * @inheritDoc
+ *
+ * @param string $app id of the app
+ * @param string $key preference key
+ * @param ValueType|null $typedAs enforce type for the returned values
+ * @param array|null $userIds limit to a list of user ids
+ *
+ * @return array<string, string|int|float|bool|array> [userId => value]
+ * @since 31.0.0
+ */
+ public function searchValuesByUsers(
+ string $app,
+ string $key,
+ ?ValueType $typedAs = null,
+ ?array $userIds = null,
+ ): array {
+ $this->assertParams('', $app, $key, allowEmptyUser: true);
+
+ $qb = $this->connection->getQueryBuilder();
+ $qb->select('userid', 'configvalue', 'type')
+ ->from('preferences')
+ ->where($qb->expr()->eq('appid', $qb->createNamedParameter($app)))
+ ->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter($key)));
+
+ $values = [];
+ // this nested function will execute current Query and store result within $values.
+ $executeAndStoreValue = function (IQueryBuilder $qb) use (&$values, $typedAs): IResult {
+ $result = $qb->executeQuery();
+ while ($row = $result->fetch()) {
+ $value = $row['configvalue'];
+ try {
+ $value = $this->convertTypedValue($value, $typedAs ?? $this->extractValueType($row['type']));
+ } catch (IncorrectTypeException) {
+ }
+ $values[$row['userid']] = $value;
+ }
+ return $result;
+ };
+
+ // if no userIds to filter, we execute query as it is and returns all values ...
+ if ($userIds === null) {
+ $result = $executeAndStoreValue($qb);
+ $result->closeCursor();
+ return $values;
+ }
+
+ // if userIds to filter, we chunk the list and execute the same query multiple times until we get all values
+ $result = null;
+ $qb->andWhere($qb->expr()->in('userid', $qb->createParameter('userIds')));
+ foreach (array_chunk($userIds, 50, true) as $chunk) {
+ $qb->setParameter('userIds', $chunk, IQueryBuilder::PARAM_STR_ARRAY);
+ $result = $executeAndStoreValue($qb);
+ }
+ $result?->closeCursor();
+
+ return $values;
+ }
+
+ /**
+ * @inheritDoc
+ *
+ * @param string $app id of the app
+ * @param string $key preference key
+ * @param string $value preference value
+ * @param bool $caseInsensitive non-case-sensitive search, only works if $value is a string
+ *
+ * @return list<string>
+ * @since 31.0.0
+ */
+ public function searchUsersByValueString(string $app, string $key, string $value, bool $caseInsensitive = false): array {
+ return $this->searchUsersByTypedValue($app, $key, $value, $caseInsensitive);
+ }
+
+ /**
+ * @inheritDoc
+ *
+ * @param string $app id of the app
+ * @param string $key preference key
+ * @param int $value preference value
+ *
+ * @return list<string>
+ * @since 31.0.0
+ */
+ public function searchUsersByValueInt(string $app, string $key, int $value): array {
+ return $this->searchUsersByValueString($app, $key, (string)$value);
+ }
+
+ /**
+ * @inheritDoc
+ *
+ * @param string $app id of the app
+ * @param string $key preference key
+ * @param array $values list of preference values
+ *
+ * @return list<string>
+ * @since 31.0.0
+ */
+ public function searchUsersByValues(string $app, string $key, array $values): array {
+ return $this->searchUsersByTypedValue($app, $key, $values);
+ }
+
+ /**
+ * @inheritDoc
+ *
+ * @param string $app id of the app
+ * @param string $key preference key
+ * @param bool $value preference value
+ *
+ * @return list<string>
+ * @since 31.0.0
+ */
+ public function searchUsersByValueBool(string $app, string $key, bool $value): array {
+ $values = ['0', 'off', 'false', 'no'];
+ if ($value) {
+ $values = ['1', 'on', 'true', 'yes'];
+ }
+ return $this->searchUsersByValues($app, $key, $values);
+ }
+
+ /**
+ * returns a list of users with preference key set to a specific value, or within the list of
+ * possible values
+ *
+ * @param string $app
+ * @param string $key
+ * @param string|array $value
+ * @param bool $caseInsensitive
+ *
+ * @return list<string>
+ */
+ private function searchUsersByTypedValue(string $app, string $key, string|array $value, bool $caseInsensitive = false): array {
+ $this->assertParams('', $app, $key, allowEmptyUser: true);
+
+ $qb = $this->connection->getQueryBuilder();
+ $qb->from('preferences');
+ $qb->select('userid');
+ $qb->where($qb->expr()->eq('appid', $qb->createNamedParameter($app)));
+ $qb->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter($key)));
+
+ $configValueColumn = ($this->connection->getDatabaseProvider() === IDBConnection::PLATFORM_ORACLE) ? $qb->expr()->castColumn('configvalue', IQueryBuilder::PARAM_STR) : 'configvalue';
+ if (is_array($value)) {
+ $qb->andWhere($qb->expr()->in($configValueColumn, $qb->createNamedParameter($value, IQueryBuilder::PARAM_STR_ARRAY)));
+ } else {
+ if ($caseInsensitive) {
+ $qb->andWhere($qb->expr()->eq(
+ $qb->func()->lower($configValueColumn),
+ $qb->createNamedParameter(strtolower($value)))
+ );
+ } else {
+ $qb->andWhere($qb->expr()->eq($configValueColumn, $qb->createNamedParameter($value)));
+ }
+ }
+
+ $userIds = [];
+ $result = $qb->executeQuery();
+ $rows = $result->fetchAll();
+ foreach ($rows as $row) {
+ $userIds[] = $row['userid'];
+ }
+
+ return $userIds;
+ }
+
+ /**
+ * Get the preference value as string.
+ * If the value does not exist the given default will be returned.
+ *
+ * Set lazy to `null` to ignore it and get the value from either source.
+ *
+ * **WARNING:** Method is internal and **SHOULD** not be used, as it is better to get the value with a type.
+ *
+ * @param string $userId id of the user
+ * @param string $app id of the app
+ * @param string $key preference key
+ * @param string $default preference value
+ * @param null|bool $lazy get preference as lazy loaded or not. can be NULL
+ *
+ * @return string the value or $default
+ * @throws TypeConflictException
+ * @internal
+ * @since 31.0.0
+ * @see IUserPreferences for explanation about lazy loading
+ * @see getValueString()
+ * @see getValueInt()
+ * @see getValueFloat()
+ * @see getValueBool()
+ * @see getValueArray()
+ */
+ public function getValueMixed(
+ string $userId,
+ string $app,
+ string $key,
+ string $default = '',
+ ?bool $lazy = false,
+ ): string {
+ try {
+ $lazy = ($lazy === null) ? $this->isLazy($userId, $app, $key) : $lazy;
+ } catch (UnknownKeyException) {
+ return $default;
+ }
+
+ return $this->getTypedValue(
+ $userId,
+ $app,
+ $key,
+ $default,
+ $lazy,
+ ValueType::MIXED
+ );
+ }
+
+ /**
+ * @inheritDoc
+ *
+ * @param string $userId id of the user
+ * @param string $app id of the app
+ * @param string $key preference key
+ * @param string $default default value
+ * @param bool $lazy search within lazy loaded preferences
+ *
+ * @return string stored preference value or $default if not set in database
+ * @throws InvalidArgumentException if one of the argument format is invalid
+ * @throws TypeConflictException in case of conflict with the value type set in database
+ * @since 31.0.0
+ * @see IUserPreferences for explanation about lazy loading
+ */
+ public function getValueString(
+ string $userId,
+ string $app,
+ string $key,
+ string $default = '',
+ bool $lazy = false,
+ ): string {
+ return $this->getTypedValue($userId, $app, $key, $default, $lazy, ValueType::STRING);
+ }
+
+ /**
+ * @inheritDoc
+ *
+ * @param string $userId id of the user
+ * @param string $app id of the app
+ * @param string $key preference key
+ * @param int $default default value
+ * @param bool $lazy search within lazy loaded preferences
+ *
+ * @return int stored preference value or $default if not set in database
+ * @throws InvalidArgumentException if one of the argument format is invalid
+ * @throws TypeConflictException in case of conflict with the value type set in database
+ * @since 31.0.0
+ * @see IUserPreferences for explanation about lazy loading
+ */
+ public function getValueInt(
+ string $userId,
+ string $app,
+ string $key,
+ int $default = 0,
+ bool $lazy = false,
+ ): int {
+ return (int)$this->getTypedValue($userId, $app, $key, (string)$default, $lazy, ValueType::INT);
+ }
+
+ /**
+ * @inheritDoc
+ *
+ * @param string $userId id of the user
+ * @param string $app id of the app
+ * @param string $key preference key
+ * @param float $default default value
+ * @param bool $lazy search within lazy loaded preferences
+ *
+ * @return float stored preference value or $default if not set in database
+ * @throws InvalidArgumentException if one of the argument format is invalid
+ * @throws TypeConflictException in case of conflict with the value type set in database
+ * @since 29.0.0
+ * @see IUserPreferences for explanation about lazy loading
+ */
+ public function getValueFloat(
+ string $userId,
+ string $app,
+ string $key,
+ float $default = 0,
+ bool $lazy = false,
+ ): float {
+ return (float)$this->getTypedValue($userId, $app, $key, (string)$default, $lazy, ValueType::FLOAT);
+ }
+
+ /**
+ * @inheritDoc
+ *
+ * @param string $userId id of the user
+ * @param string $app id of the app
+ * @param string $key preference key
+ * @param bool $default default value
+ * @param bool $lazy search within lazy loaded preferences
+ *
+ * @return bool stored preference value or $default if not set in database
+ * @throws InvalidArgumentException if one of the argument format is invalid
+ * @throws TypeConflictException in case of conflict with the value type set in database
+ * @since 29.0.0
+ * @see IUserPreferences for explanation about lazy loading
+ */
+ public function getValueBool(
+ string $userId,
+ string $app,
+ string $key,
+ bool $default = false,
+ bool $lazy = false,
+ ): bool {
+ $b = strtolower($this->getTypedValue($userId, $app, $key, $default ? 'true' : 'false', $lazy, ValueType::BOOL));
+ return in_array($b, ['1', 'true', 'yes', 'on']);
+ }
+
+ /**
+ * @inheritDoc
+ *
+ * @param string $userId id of the user
+ * @param string $app id of the app
+ * @param string $key preference key
+ * @param array $default default value
+ * @param bool $lazy search within lazy loaded preferences
+ *
+ * @return array stored preference value or $default if not set in database
+ * @throws InvalidArgumentException if one of the argument format is invalid
+ * @throws TypeConflictException in case of conflict with the value type set in database
+ * @since 29.0.0
+ * @see IUserPreferences for explanation about lazy loading
+ */
+ public function getValueArray(
+ string $userId,
+ string $app,
+ string $key,
+ array $default = [],
+ bool $lazy = false,
+ ): array {
+ try {
+ $defaultJson = json_encode($default, JSON_THROW_ON_ERROR);
+ $value = json_decode($this->getTypedValue($userId, $app, $key, $defaultJson, $lazy, ValueType::ARRAY), true, flags: JSON_THROW_ON_ERROR);
+
+ return is_array($value) ? $value : [];
+ } catch (JsonException) {
+ return [];
+ }
+ }
+
+ /**
+ * @param string $userId
+ * @param string $app id of the app
+ * @param string $key preference key
+ * @param string $default default value
+ * @param bool $lazy search within lazy loaded preferences
+ * @param ValueType $type value type
+ *
+ * @return string
+ * @throws TypeConflictException if type from database is not VALUE_MIXED and different from the requested one
+ */
+ private function getTypedValue(
+ string $userId,
+ string $app,
+ string $key,
+ string $default,
+ bool $lazy,
+ ValueType $type,
+ ): string {
+ $this->assertParams($userId, $app, $key, valueType: $type);
+ $this->loadPreferences($userId, $lazy);
+
+ /**
+ * We ignore check if mixed type is requested.
+ * If type of stored value is set as mixed, we don't filter.
+ * If type of stored value is defined, we compare with the one requested.
+ */
+ $knownType = $this->valueTypes[$userId][$app][$key] ?? 0;
+ if (!$this->isTyped(ValueType::MIXED, $type->value)
+ && $knownType > 0
+ && !$this->isTyped(ValueType::MIXED, $knownType)
+ && !$this->isTyped($type, $knownType)) {
+ $this->logger->warning('conflict with value type from database', ['app' => $app, 'key' => $key, 'type' => $type, 'knownType' => $knownType]);
+ throw new TypeConflictException('conflict with value type from database');
+ }
+
+ /**
+ * - the pair $app/$key cannot exist in both array,
+ * - we should still return an existing non-lazy value even if current method
+ * is called with $lazy is true
+ *
+ * This way, lazyCache will be empty until the load for lazy preferences value is requested.
+ */
+ if (isset($this->lazyCache[$userId][$app][$key])) {
+ $value = $this->lazyCache[$userId][$app][$key];
+ } elseif (isset($this->fastCache[$userId][$app][$key])) {
+ $value = $this->fastCache[$userId][$app][$key];
+ } else {
+ return $default;
+ }
+
+ $this->decryptSensitiveValue($userId, $app, $key, $value);
+ return $value;
+ }
+
+ /**
+ * @inheritDoc
+ *
+ * @param string $userId id of the user
+ * @param string $app id of the app
+ * @param string $key preference key
+ *
+ * @return ValueType type of the value
+ * @throws UnknownKeyException if preference key is not known
+ * @throws IncorrectTypeException if preferences value type is not known
+ * @since 31.0.0
+ */
+ public function getValueType(string $userId, string $app, string $key, ?bool $lazy = null): ValueType {
+ $this->assertParams($userId, $app, $key);
+ $this->loadPreferences($userId, $lazy);
+
+ if (!isset($this->valueTypes[$userId][$app][$key])) {
+ throw new UnknownKeyException('unknown preference key');
+ }
+
+ return $this->extractValueType($this->valueTypes[$userId][$app][$key]);
+ }
+
+ /**
+ * convert bitflag from value type to ValueType
+ *
+ * @param int $type
+ *
+ * @return ValueType
+ * @throws IncorrectTypeException
+ */
+ private function extractValueType(int $type): ValueType {
+ $type &= ~ValueType::SENSITIVE->value;
+
+ try {
+ return ValueType::from($type);
+ } catch (ValueError) {
+ throw new IncorrectTypeException('invalid value type');
+ }
+ }
+
+ /**
+ * Store a preference key and its value in database as VALUE_MIXED
+ *
+ * **WARNING:** Method is internal and **MUST** not be used as it is best to set a real value type
+ *
+ * @param string $userId id of the user
+ * @param string $app id of the app
+ * @param string $key preference key
+ * @param string $value preference value
+ * @param bool $lazy set preference as lazy loaded
+ * @param bool $sensitive if TRUE value will be hidden when listing preference values.
+ *
+ * @return bool TRUE if value was different, therefor updated in database
+ * @throws TypeConflictException if type from database is not VALUE_MIXED
+ * @internal
+ * @since 29.0.0
+ * @see IUserPreferences for explanation about lazy loading
+ * @see setValueString()
+ * @see setValueInt()
+ * @see setValueFloat()
+ * @see setValueBool()
+ * @see setValueArray()
+ */
+ public function setValueMixed(
+ string $userId,
+ string $app,
+ string $key,
+ string $value,
+ bool $lazy = false,
+ bool $sensitive = false,
+ ): bool {
+ return $this->setTypedValue(
+ $userId,
+ $app,
+ $key,
+ $value,
+ $lazy,
+ $sensitive,
+ ValueType::MIXED
+ );
+ }
+
+
+ /**
+ * @inheritDoc
+ *
+ * @param string $userId id of the user
+ * @param string $app id of the app
+ * @param string $key preference key
+ * @param string $value preference value
+ * @param bool $lazy set preference as lazy loaded
+ * @param bool $sensitive if TRUE value will be hidden when listing preference values.
+ *
+ * @return bool TRUE if value was different, therefor updated in database
+ * @throws TypeConflictException if type from database is not VALUE_MIXED and different from the requested one
+ * @since 29.0.0
+ * @see IUserPreferences for explanation about lazy loading
+ */
+ public function setValueString(
+ string $userId,
+ string $app,
+ string $key,
+ string $value,
+ bool $lazy = false,
+ bool $sensitive = false,
+ ): bool {
+ return $this->setTypedValue(
+ $userId,
+ $app,
+ $key,
+ $value,
+ $lazy,
+ $sensitive,
+ ValueType::STRING
+ );
+ }
+
+ /**
+ * @inheritDoc
+ *
+ * @param string $userId id of the user
+ * @param string $app id of the app
+ * @param string $key preference key
+ * @param int $value preference value
+ * @param bool $lazy set preference as lazy loaded
+ * @param bool $sensitive if TRUE value will be hidden when listing preference values.
+ *
+ * @return bool TRUE if value was different, therefor updated in database
+ * @throws TypeConflictException if type from database is not VALUE_MIXED and different from the requested one
+ * @since 29.0.0
+ * @see IUserPreferences for explanation about lazy loading
+ */
+ public function setValueInt(
+ string $userId,
+ string $app,
+ string $key,
+ int $value,
+ bool $lazy = false,
+ bool $sensitive = false,
+ ): bool {
+ if ($value > 2000000000) {
+ $this->logger->debug('You are trying to store an integer value around/above 2,147,483,647. This is a reminder that reaching this theoretical limit on 32 bits system will throw an exception.');
+ }
+
+ return $this->setTypedValue(
+ $userId,
+ $app,
+ $key,
+ (string)$value,
+ $lazy,
+ $sensitive,
+ ValueType::INT
+ );
+ }
+
+ /**
+ * @inheritDoc
+ *
+ * @param string $userId id of the user
+ * @param string $app id of the app
+ * @param string $key preference key
+ * @param float $value preference value
+ * @param bool $lazy set preference as lazy loaded
+ * @param bool $sensitive if TRUE value will be hidden when listing preference values.
+ *
+ * @return bool TRUE if value was different, therefor updated in database
+ * @throws TypeConflictException if type from database is not VALUE_MIXED and different from the requested one
+ * @since 29.0.0
+ * @see IUserPreferences for explanation about lazy loading
+ */
+ public function setValueFloat(
+ string $userId,
+ string $app,
+ string $key,
+ float $value,
+ bool $lazy = false,
+ bool $sensitive = false,
+ ): bool {
+ return $this->setTypedValue(
+ $userId,
+ $app,
+ $key,
+ (string)$value,
+ $lazy,
+ $sensitive,
+ ValueType::FLOAT
+ );
+ }
+
+ /**
+ * @inheritDoc
+ *
+ * @param string $userId id of the user
+ * @param string $app id of the app
+ * @param string $key preference key
+ * @param bool $value preference value
+ * @param bool $lazy set preference as lazy loaded
+ *
+ * @return bool TRUE if value was different, therefor updated in database
+ * @throws TypeConflictException if type from database is not VALUE_MIXED and different from the requested one
+ * @since 29.0.0
+ * @see IUserPreferences for explanation about lazy loading
+ */
+ public function setValueBool(
+ string $userId,
+ string $app,
+ string $key,
+ bool $value,
+ bool $lazy = false,
+ ): bool {
+ return $this->setTypedValue(
+ $userId,
+ $app,
+ $key,
+ ($value) ? '1' : '0',
+ $lazy,
+ false,
+ ValueType::BOOL
+ );
+ }
+
+ /**
+ * @inheritDoc
+ *
+ * @param string $userId id of the user
+ * @param string $app id of the app
+ * @param string $key preference key
+ * @param array $value preference value
+ * @param bool $lazy set preference as lazy loaded
+ * @param bool $sensitive if TRUE value will be hidden when listing preference values.
+ *
+ * @return bool TRUE if value was different, therefor updated in database
+ * @throws TypeConflictException if type from database is not VALUE_MIXED and different from the requested one
+ * @throws JsonException
+ * @since 29.0.0
+ * @see IUserPreferences for explanation about lazy loading
+ */
+ public function setValueArray(
+ string $userId,
+ string $app,
+ string $key,
+ array $value,
+ bool $lazy = false,
+ bool $sensitive = false,
+ ): bool {
+ try {
+ return $this->setTypedValue(
+ $userId,
+ $app,
+ $key,
+ json_encode($value, JSON_THROW_ON_ERROR),
+ $lazy,
+ $sensitive,
+ ValueType::ARRAY
+ );
+ } catch (JsonException $e) {
+ $this->logger->warning('could not setValueArray', ['app' => $app, 'key' => $key, 'exception' => $e]);
+ throw $e;
+ }
+ }
+
+ /**
+ * Store a preference key and its value in database
+ *
+ * If preference key is already known with the exact same preference value and same sensitive/lazy status, the
+ * database is not updated. If preference value was previously stored as sensitive, status will not be
+ * altered.
+ *
+ * @param string $userId id of the user
+ * @param string $app id of the app
+ * @param string $key preference key
+ * @param string $value preference value
+ * @param bool $lazy preferences set as lazy loaded
+ * @param ValueType $type value type
+ *
+ * @return bool TRUE if value was updated in database
+ * @throws TypeConflictException if type from database is not VALUE_MIXED and different from the requested one
+ * @see IUserPreferences for explanation about lazy loading
+ */
+ private function setTypedValue(
+ string $userId,
+ string $app,
+ string $key,
+ string $value,
+ bool $lazy,
+ bool $sensitive,
+ ValueType $type,
+ ): bool {
+ $this->assertParams($userId, $app, $key, valueType: $type);
+ $this->loadPreferences($userId, $lazy);
+
+ $inserted = $refreshCache = false;
+ $origValue = $value;
+ $typeValue = $type->value;
+ if ($sensitive || ($this->hasKey($userId, $app, $key, $lazy) && $this->isSensitive($userId, $app, $key, $lazy))) {
+ $value = self::ENCRYPTION_PREFIX . $this->crypto->encrypt($value);
+ $typeValue = $typeValue | ValueType::SENSITIVE->value;
+ }
+
+ if ($this->hasKey($userId, $app, $key, $lazy)) {
+ /**
+ * no update if key is already known with set lazy status and value is
+ * not different, unless sensitivity is switched from false to true.
+ */
+ if ($origValue === $this->getTypedValue($userId, $app, $key, $value, $lazy, $type)
+ && (!$sensitive || $this->isSensitive($userId, $app, $key, $lazy))) {
+ return false;
+ }
+ } else {
+ /**
+ * if key is not known yet, we try to insert.
+ * It might fail if the key exists with a different lazy flag.
+ */
+ try {
+ $insert = $this->connection->getQueryBuilder();
+ $insert->insert('preferences')
+ ->setValue('userid', $insert->createNamedParameter($userId))
+ ->setValue('appid', $insert->createNamedParameter($app))
+ ->setValue('lazy', $insert->createNamedParameter(($lazy) ? 1 : 0, IQueryBuilder::PARAM_INT))
+ ->setValue('type', $insert->createNamedParameter($typeValue, IQueryBuilder::PARAM_INT))
+ ->setValue('configkey', $insert->createNamedParameter($key))
+ ->setValue('configvalue', $insert->createNamedParameter($value));
+ $insert->executeStatement();
+ $inserted = true;
+ } catch (DBException $e) {
+ if ($e->getReason() !== DBException::REASON_UNIQUE_CONSTRAINT_VIOLATION) {
+ throw $e; // TODO: throw exception or just log and returns false !?
+ }
+ }
+ }
+
+ /**
+ * We cannot insert a new row, meaning we need to update an already existing one
+ */
+ if (!$inserted) {
+ $currType = $this->valueTypes[$userId][$app][$key] ?? 0;
+ if ($currType === 0) { // this might happen when switching lazy loading status
+ $this->loadPreferencesAll($userId);
+ $currType = $this->valueTypes[$userId][$app][$key] ?? 0;
+ }
+
+ /**
+ * This should only happen during the upgrade process from 28 to 29.
+ * We only log a warning and set it to VALUE_MIXED.
+ */
+ if ($currType === 0) {
+ $this->logger->warning('Value type is set to zero (0) in database. This is fine only during the upgrade process from 28 to 29.', ['app' => $app, 'key' => $key]);
+ $currType = ValueType::MIXED->value;
+ }
+
+ // if ($type->isSensitive()) {}
+
+ /**
+ * we only accept a different type from the one stored in database
+ * if the one stored in database is not-defined (VALUE_MIXED)
+ */
+ if (!$this->isTyped(ValueType::MIXED, $currType) &&
+ ($type->value | ValueType::SENSITIVE->value) !== ($currType | ValueType::SENSITIVE->value)) {
+ try {
+ $currType = $this->extractValueType($currType)->getDefinition();
+ $type = $type->getDefinition();
+ } catch (IncorrectTypeException) {
+ $type = $type->value;
+ }
+ throw new TypeConflictException('conflict between new type (' . $type . ') and old type (' . $currType . ')');
+ }
+
+ if ($lazy !== $this->isLazy($userId, $app, $key)) {
+ $refreshCache = true;
+ }
+
+ $update = $this->connection->getQueryBuilder();
+ $update->update('preferences')
+ ->set('configvalue', $update->createNamedParameter($value))
+ ->set('lazy', $update->createNamedParameter(($lazy) ? 1 : 0, IQueryBuilder::PARAM_INT))
+ ->set('type', $update->createNamedParameter($typeValue, IQueryBuilder::PARAM_INT))
+ ->where($update->expr()->eq('userid', $update->createNamedParameter($userId)))
+ ->andWhere($update->expr()->eq('appid', $update->createNamedParameter($app)))
+ ->andWhere($update->expr()->eq('configkey', $update->createNamedParameter($key)));
+
+ $update->executeStatement();
+ }
+
+ if ($refreshCache) {
+ $this->clearCache($userId);
+ return true;
+ }
+
+ // update local cache
+ if ($lazy) {
+ $this->lazyCache[$userId][$app][$key] = $value;
+ } else {
+ $this->fastCache[$userId][$app][$key] = $value;
+ }
+ $this->valueTypes[$userId][$app][$key] = $typeValue;
+
+ return true;
+ }
+
+ /**
+ * Change the type of preference value.
+ *
+ * **WARNING:** Method is internal and **MUST** not be used as it may break things.
+ *
+ * @param string $userId id of the user
+ * @param string $app id of the app
+ * @param string $key preference key
+ * @param ValueType $type value type
+ *
+ * @return bool TRUE if database update were necessary
+ * @throws UnknownKeyException if $key is now known in database
+ * @throws IncorrectTypeException if $type is not valid
+ * @internal
+ * @since 31.0.0
+ */
+ public function updateType(string $userId, string $app, string $key, ValueType $type = ValueType::MIXED): bool {
+ $this->assertParams($userId, $app, $key, valueType: $type);
+ $this->loadPreferencesAll($userId);
+ $this->isLazy($userId, $app, $key); // confirm key exists
+ $typeValue = $type->value;
+
+ $currType = $this->valueTypes[$userId][$app][$key];
+ if (($typeValue | ValueType::SENSITIVE->value) === ($currType | ValueType::SENSITIVE->value)) {
+ return false;
+ }
+
+ // we complete with sensitive flag if the stored value is set as sensitive
+ if ($this->isTyped(ValueType::SENSITIVE, $currType)) {
+ $typeValue = $typeValue | ValueType::SENSITIVE->value;
+ }
+
+ $update = $this->connection->getQueryBuilder();
+ $update->update('preferences')
+ ->set('type', $update->createNamedParameter($typeValue, IQueryBuilder::PARAM_INT))
+ ->where($update->expr()->eq('userid', $update->createNamedParameter($userId)))
+ ->andWhere($update->expr()->eq('appid', $update->createNamedParameter($app)))
+ ->andWhere($update->expr()->eq('configkey', $update->createNamedParameter($key)));
+ $update->executeStatement();
+ $this->valueTypes[$userId][$app][$key] = $typeValue;
+
+ return true;
+ }
+
+ /**
+ * @inheritDoc
+ *
+ * @param string $userId id of the user
+ * @param string $app id of the app
+ * @param string $key preference key
+ * @param bool $sensitive TRUE to set as sensitive, FALSE to unset
+ *
+ * @return bool TRUE if entry was found in database and an update was necessary
+ * @since 31.0.0
+ */
+ public function updateSensitive(string $userId, string $app, string $key, bool $sensitive): bool {
+ $this->assertParams($userId, $app, $key);
+ $this->loadPreferencesAll($userId);
+
+ try {
+ if ($sensitive === $this->isSensitive($userId, $app, $key, null)) {
+ return false;
+ }
+ } catch (UnknownKeyException) {
+ return false;
+ }
+
+ $lazy = $this->isLazy($userId, $app, $key);
+ if ($lazy) {
+ $cache = $this->lazyCache;
+ } else {
+ $cache = $this->fastCache;
+ }
+
+ if (!isset($cache[$userId][$app][$key])) {
+ throw new UnknownKeyException('unknown preference key');
+ }
+
+ /**
+ * type returned by getValueType() is already cleaned from sensitive flag
+ * we just need to update it based on $sensitive and store it in database
+ */
+ $typeValue = $this->getValueType($userId, $app, $key)->value;
+ $value = $cache[$userId][$app][$key];
+ if ($sensitive) {
+ $typeValue |= ValueType::SENSITIVE->value;
+ $value = self::ENCRYPTION_PREFIX . $this->crypto->encrypt($value);
+ } else {
+ $this->decryptSensitiveValue($userId, $app, $key, $value);
+ }
+
+ $update = $this->connection->getQueryBuilder();
+ $update->update('preferences')
+ ->set('type', $update->createNamedParameter($typeValue, IQueryBuilder::PARAM_INT))
+ ->set('configvalue', $update->createNamedParameter($value))
+ ->where($update->expr()->eq('userid', $update->createNamedParameter($userId)))
+ ->andWhere($update->expr()->eq('appid', $update->createNamedParameter($app)))
+ ->andWhere($update->expr()->eq('configkey', $update->createNamedParameter($key)));
+ $update->executeStatement();
+
+ $this->valueTypes[$userId][$app][$key] = $typeValue;
+
+ return true;
+ }
+
+ /**
+ * @inheritDoc
+ *
+ * @param string $app
+ * @param string $key
+ * @param bool $sensitive
+ *
+ * @since 31.0.0
+ */
+ public function updateGlobalSensitive(string $app, string $key, bool $sensitive): void {
+ $this->assertParams('', $app, $key, allowEmptyUser: true);
+ foreach (array_keys($this->searchValuesByUsers($app, $key)) as $userId) {
+ try {
+ $this->updateSensitive($userId, $app, $key, $sensitive);
+ } catch (UnknownKeyException) {
+ // should not happen and can be ignored
+ }
+ }
+
+ $this->clearCacheAll(); // we clear all cache
+ }
+
+ /**
+ * @inheritDoc
+ *
+ * @param string $userId id of the user
+ * @param string $app id of the app
+ * @param string $key preference key
+ * @param bool $lazy TRUE to set as lazy loaded, FALSE to unset
+ *
+ * @return bool TRUE if entry was found in database and an update was necessary
+ * @since 31.0.0
+ */
+ public function updateLazy(string $userId, string $app, string $key, bool $lazy): bool {
+ $this->assertParams($userId, $app, $key);
+ $this->loadPreferencesAll($userId);
+
+ try {
+ if ($lazy === $this->isLazy($userId, $app, $key)) {
+ return false;
+ }
+ } catch (UnknownKeyException) {
+ return false;
+ }
+
+ $update = $this->connection->getQueryBuilder();
+ $update->update('preferences')
+ ->set('lazy', $update->createNamedParameter($lazy ? 1 : 0, IQueryBuilder::PARAM_INT))
+ ->where($update->expr()->eq('userid', $update->createNamedParameter($userId)))
+ ->andWhere($update->expr()->eq('appid', $update->createNamedParameter($app)))
+ ->andWhere($update->expr()->eq('configkey', $update->createNamedParameter($key)));
+ $update->executeStatement();
+
+ // At this point, it is a lot safer to clean cache
+ $this->clearCache($userId);
+
+ return true;
+ }
+
+ /**
+ * @inheritDoc
+ *
+ * @param string $app id of the app
+ * @param string $key preference key
+ * @param bool $lazy TRUE to set as lazy loaded, FALSE to unset
+ *
+ * @since 31.0.0
+ */
+ public function updateGlobalLazy(string $app, string $key, bool $lazy): void {
+ $this->assertParams('', $app, $key, allowEmptyUser: true);
+
+ $update = $this->connection->getQueryBuilder();
+ $update->update('preferences')
+ ->set('lazy', $update->createNamedParameter($lazy ? 1 : 0, IQueryBuilder::PARAM_INT))
+ ->where($update->expr()->eq('appid', $update->createNamedParameter($app)))
+ ->andWhere($update->expr()->eq('configkey', $update->createNamedParameter($key)));
+ $update->executeStatement();
+
+ $this->clearCacheAll();
+ }
+
+ /**
+ * @inheritDoc
+ *
+ * @param string $userId id of the user
+ * @param string $app id of the app
+ * @param string $key preference key
+ *
+ * @return array
+ * @throws UnknownKeyException if preference key is not known in database
+ * @since 31.0.0
+ */
+ public function getDetails(string $userId, string $app, string $key): array {
+ $this->assertParams($userId, $app, $key);
+ $this->loadPreferencesAll($userId);
+ $lazy = $this->isLazy($userId, $app, $key);
+
+ if ($lazy) {
+ $cache = $this->lazyCache[$userId];
+ } else {
+ $cache = $this->fastCache[$userId];
+ }
+
+ $type = $this->getValueType($userId, $app, $key);
+ try {
+ $typeString = $type->getDefinition();
+ } catch (IncorrectTypeException $e) {
+ $this->logger->warning('type stored in database is not correct', ['exception' => $e, 'type' => $type]);
+ $typeString = (string)$type->value;
+ }
+
+ if (!isset($cache[$app][$key])) {
+ throw new UnknownKeyException('unknown preference key');
+ }
+
+ $value = $cache[$app][$key];
+ $sensitive = $this->isSensitive($userId, $app, $key, null);
+ $this->decryptSensitiveValue($userId, $app, $key, $value);
+
+ return [
+ 'userId' => $userId,
+ 'app' => $app,
+ 'key' => $key,
+ 'value' => $value,
+ 'type' => $type->value,
+ 'lazy' => $lazy,
+ 'typeString' => $typeString,
+ 'sensitive' => $sensitive
+ ];
+ }
+
+ /**
+ * @inheritDoc
+ *
+ * @param string $userId id of the user
+ * @param string $app id of the app
+ * @param string $key preference key
+ *
+ * @since 31.0.0
+ */
+ public function deletePreference(string $userId, string $app, string $key): void {
+ $this->assertParams($userId, $app, $key);
+ $qb = $this->connection->getQueryBuilder();
+ $qb->delete('preferences')
+ ->where($qb->expr()->eq('userid', $qb->createNamedParameter($userId)))
+ ->andWhere($qb->expr()->eq('appid', $qb->createNamedParameter($app)))
+ ->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter($key)));
+ $qb->executeStatement();
+
+ unset($this->lazyCache[$userId][$app][$key]);
+ unset($this->fastCache[$userId][$app][$key]);
+ }
+
+ /**
+ * @inheritDoc
+ *
+ * @param string $app id of the app
+ * @param string $key preference key
+ *
+ * @since 31.0.0
+ */
+ public function deleteKey(string $app, string $key): void {
+ $this->assertParams('', $app, $key, allowEmptyUser: true);
+ $qb = $this->connection->getQueryBuilder();
+ $qb->delete('preferences')
+ ->where($qb->expr()->eq('appid', $qb->createNamedParameter($app)))
+ ->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter($key)));
+ $qb->executeStatement();
+
+ $this->clearCacheAll();
+ }
+
+ /**
+ * @inheritDoc
+ *
+ * @param string $app id of the app
+ *
+ * @since 31.0.0
+ */
+ public function deleteApp(string $app): void {
+ $this->assertParams('', $app, allowEmptyUser: true);
+ $qb = $this->connection->getQueryBuilder();
+ $qb->delete('preferences')
+ ->where($qb->expr()->eq('appid', $qb->createNamedParameter($app)));
+ $qb->executeStatement();
+
+ $this->clearCacheAll();
+ }
+
+ public function deleteAllPreferences(string $userId): void {
+ $this->assertParams($userId, '', allowEmptyApp: true);
+ $qb = $this->connection->getQueryBuilder();
+ $qb->delete('preferences')
+ ->where($qb->expr()->eq('userid', $qb->createNamedParameter($userId)));
+ $qb->executeStatement();
+
+ $this->clearCache($userId);
+ }
+
+ /**
+ * @inheritDoc
+ *
+ * @param string $userId id of the user
+ * @param bool $reload set to TRUE to refill cache instantly after clearing it.
+ *
+ * @since 31.0.0
+ */
+ public function clearCache(string $userId, bool $reload = false): void {
+ $this->assertParams($userId, allowEmptyApp: true);
+ $this->lazyLoaded[$userId] = $this->fastLoaded[$userId] = false;
+ $this->lazyCache[$userId] = $this->fastCache[$userId] = $this->valueTypes[$userId] = [];
+
+ if (!$reload) {
+ return;
+ }
+
+ $this->loadPreferencesAll($userId);
+ }
+
+ /**
+ * @inheritDoc
+ *
+ * @since 31.0.0
+ */
+ public function clearCacheAll(): void {
+ $this->lazyLoaded = $this->fastLoaded = [];
+ $this->lazyCache = $this->fastCache = $this->valueTypes = [];
+ }
+
+ /**
+ * For debug purpose.
+ * Returns the cached data.
+ *
+ * @return array
+ * @since 31.0.0
+ * @internal
+ */
+ public function statusCache(): array {
+ return [
+ 'fastLoaded' => $this->fastLoaded,
+ 'fastCache' => $this->fastCache,
+ 'lazyLoaded' => $this->lazyLoaded,
+ 'lazyCache' => $this->lazyCache,
+ 'valueTypes' => $this->valueTypes,
+ ];
+ }
+
+ /**
+ * @param ValueType $needle bitflag to search
+ * @param int $type known value
+ *
+ * @return bool TRUE if bitflag $needle is set in $type
+ */
+ private function isTyped(ValueType $needle, int $type): bool {
+ return (($needle->value & $type) !== 0);
+ }
+
+ /**
+ * Confirm the string set for app and key fit the database description
+ *
+ * @param string $userId
+ * @param string $app assert $app fit in database
+ * @param string $prefKey assert preference key fit in database
+ * @param bool $allowEmptyUser
+ * @param bool $allowEmptyApp $app can be empty string
+ * @param ValueType|null $valueType assert value type is only one type
+ */
+ private function assertParams(
+ string $userId = '',
+ string $app = '',
+ string $prefKey = '',
+ bool $allowEmptyUser = false,
+ bool $allowEmptyApp = false,
+ ?ValueType $valueType = null,
+ ): void {
+ if (!$allowEmptyUser && $userId === '') {
+ throw new InvalidArgumentException('userId cannot be an empty string');
+ }
+ if (!$allowEmptyApp && $app === '') {
+ throw new InvalidArgumentException('app cannot be an empty string');
+ }
+ if (strlen($userId) > self::USER_MAX_LENGTH) {
+ throw new InvalidArgumentException('Value (' . $userId . ') for userId is too long (' . self::USER_MAX_LENGTH . ')');
+ }
+ if (strlen($app) > self::APP_MAX_LENGTH) {
+ throw new InvalidArgumentException('Value (' . $app . ') for app is too long (' . self::APP_MAX_LENGTH . ')');
+ }
+ if (strlen($prefKey) > self::KEY_MAX_LENGTH) {
+ throw new InvalidArgumentException('Value (' . $prefKey . ') for key is too long (' . self::KEY_MAX_LENGTH . ')');
+ }
+ if ($valueType !== null) {
+ $valueFlag = $valueType->value;
+ $valueFlag &= ~ValueType::SENSITIVE->value;
+ if (ValueType::tryFrom($valueFlag) === null) {
+ throw new InvalidArgumentException('Unknown value type');
+ }
+ }
+ }
+
+ private function loadPreferencesAll(string $userId): void {
+ $this->loadPreferences($userId, null);
+ }
+
+ /**
+ * Load normal preferences or preferences set as lazy loaded
+ *
+ * @param bool|null $lazy set to TRUE to load preferences set as lazy loaded, set to NULL to load all preferences
+ */
+ private function loadPreferences(string $userId, ?bool $lazy = false): void {
+ if ($this->isLoaded($userId, $lazy)) {
+ return;
+ }
+
+ if (($lazy ?? true) !== false) { // if lazy is null or true, we debug log
+ $this->logger->debug('The loading of lazy UserPreferences values have been requested', ['exception' => new \RuntimeException('ignorable exception')]);
+ }
+
+ $qb = $this->connection->getQueryBuilder();
+ $qb->from('preferences');
+ $qb->select('userid', 'appid', 'configkey', 'configvalue', 'type');
+ $qb->where($qb->expr()->eq('userid', $qb->createNamedParameter($userId)));
+
+ // we only need value from lazy when loadPreferences does not specify it
+ if ($lazy !== null) {
+ $qb->andWhere($qb->expr()->eq('lazy', $qb->createNamedParameter($lazy ? 1 : 0, IQueryBuilder::PARAM_INT)));
+ } else {
+ $qb->addSelect('lazy');
+ }
+
+ $result = $qb->executeQuery();
+ $rows = $result->fetchAll();
+ foreach ($rows as $row) {
+ if (($row['lazy'] ?? ($lazy ?? 0) ? 1 : 0) === 1) {
+ $this->lazyCache[$row['userid']][$row['appid']][$row['configkey']] = $row['configvalue'] ?? '';
+ } else {
+ $this->fastCache[$row['userid']][$row['appid']][$row['configkey']] = $row['configvalue'] ?? '';
+ }
+ $this->valueTypes[$row['userid']][$row['appid']][$row['configkey']] = (int)($row['type'] ?? 0);
+ }
+ $result->closeCursor();
+ $this->setAsLoaded($userId, $lazy);
+ }
+
+ /**
+ * if $lazy is:
+ * - false: will returns true if fast preferences are loaded
+ * - true : will returns true if lazy preferences are loaded
+ * - null : will returns true if both preferences are loaded
+ *
+ * @param string $userId
+ * @param bool $lazy
+ *
+ * @return bool
+ */
+ private function isLoaded(string $userId, ?bool $lazy): bool {
+ if ($lazy === null) {
+ return ($this->lazyLoaded[$userId] ?? false) && ($this->fastLoaded[$userId] ?? false);
+ }
+
+ return $lazy ? $this->lazyLoaded[$userId] ?? false : $this->fastLoaded[$userId] ?? false;
+ }
+
+ /**
+ * if $lazy is:
+ * - false: set fast preferences as loaded
+ * - true : set lazy preferences as loaded
+ * - null : set both preferences as loaded
+ *
+ * @param string $userId
+ * @param bool $lazy
+ */
+ private function setAsLoaded(string $userId, ?bool $lazy): void {
+ if ($lazy === null) {
+ $this->fastLoaded[$userId] = $this->lazyLoaded[$userId] = true;
+ return;
+ }
+
+ // We also create empty entry to keep both fastLoaded/lazyLoaded synced
+ if ($lazy) {
+ $this->lazyLoaded[$userId] = true;
+ $this->fastLoaded[$userId] = $this->fastLoaded[$userId] ?? false;
+ $this->fastCache[$userId] = $this->fastCache[$userId] ?? [];
+ } else {
+ $this->fastLoaded[$userId] = true;
+ $this->lazyLoaded[$userId] = $this->lazyLoaded[$userId] ?? false;
+ $this->lazyCache[$userId] = $this->lazyCache[$userId] ?? [];
+ }
+ }
+
+ /**
+ * **Warning:** this will load all lazy values from the database
+ *
+ * @param string $userId id of the user
+ * @param string $app id of the app
+ * @param bool $filtered TRUE to hide sensitive preference values. Value are replaced by {@see IConfig::SENSITIVE_VALUE}
+ *
+ * @return array<string, string|int|float|bool|array>
+ */
+ private function formatAppValues(string $userId, string $app, array $values, bool $filtered = false): array {
+ foreach ($values as $key => $value) {
+ //$key = (string)$key;
+ try {
+ $type = $this->getValueType($userId, $app, (string)$key);
+ } catch (UnknownKeyException) {
+ continue;
+ }
+
+ if ($this->isTyped(ValueType::SENSITIVE, $this->valueTypes[$userId][$app][$key] ?? 0)) {
+ if ($filtered) {
+ $value = IConfig::SENSITIVE_VALUE;
+ $type = ValueType::STRING;
+ } else {
+ $this->decryptSensitiveValue($userId, $app, (string)$key, $value);
+ }
+ }
+
+ $values[$key] = $this->convertTypedValue($value, $type);
+ }
+
+ return $values;
+ }
+
+ /**
+ * convert string value to the expected type
+ *
+ * @param string $value
+ * @param ValueType $type
+ *
+ * @return string|int|float|bool|array
+ */
+ private function convertTypedValue(string $value, ValueType $type): string|int|float|bool|array {
+ switch ($type) {
+ case ValueType::INT:
+ return (int)$value;
+ case ValueType::FLOAT:
+ return (float)$value;
+ case ValueType::BOOL:
+ return in_array(strtolower($value), ['1', 'true', 'yes', 'on']);
+ case ValueType::ARRAY:
+ try {
+ return json_decode($value, true, flags: JSON_THROW_ON_ERROR);
+ } catch (JsonException) {
+ // ignoreable
+ }
+ break;
+ }
+ return $value;
+ }
+
+
+ private function decryptSensitiveValue(string $userId, string $app, string $key, string &$value): void {
+ if (!$this->isTyped(ValueType::SENSITIVE, $this->valueTypes[$userId][$app][$key] ?? 0)) {
+ return;
+ }
+
+ if (!str_starts_with($value, self::ENCRYPTION_PREFIX)) {
+ return;
+ }
+
+ try {
+ $value = $this->crypto->decrypt(substr($value, self::ENCRYPTION_PREFIX_LENGTH));
+ } catch (\Exception $e) {
+ $this->logger->warning('could not decrypt sensitive value', [
+ 'userId' => $userId,
+ 'app' => $app,
+ 'key' => $key,
+ 'value' => $value,
+ 'exception' => $e
+ ]);
+ }
+ }
+}
--- /dev/null
+<?php
+
+declare(strict_types=1);
+/**
+ * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-only
+ */
+namespace lib;
+
+use OC\UserPreferences;
+use OCP\IDBConnection;
+use OCP\Security\ICrypto;
+use OCP\UserPreferences\Exceptions\TypeConflictException;
+use OCP\UserPreferences\Exceptions\UnknownKeyException;
+use OCP\UserPreferences\IUserPreferences;
+use OCP\UserPreferences\ValueType;
+use Psr\Log\LoggerInterface;
+use Test\TestCase;
+
+/**
+ * Class UserPreferencesTest
+ *
+ * @group DB
+ *
+ * @package Test
+ */
+class UserPreferencesTest extends TestCase {
+ protected IDBConnection $connection;
+ private LoggerInterface $logger;
+ private ICrypto $crypto;
+ private array $originalPreferences;
+
+ /**
+ * @var array<string, array<string, array<array<string, string, int, bool, bool>>> [userId => [appId => prefKey, prefValue, valueType, lazy, sensitive]]]
+ */
+ private array $basePreferences =
+ [
+ 'user1' =>
+ [
+ 'app1' => [
+ 'key1' => ['key1', 'value1'],
+ 'key22' => ['key22', '31'],
+ 'fast_string' => ['fast_string', 'f_value', ValueType::STRING],
+ 'lazy_string' => ['lazy_string', 'l_value', ValueType::STRING, true],
+ 'fast_string_sensitive' => [
+ 'fast_string_sensitive', 'fs_value', ValueType::STRING, false, true
+ ],
+ 'lazy_string_sensitive' => [
+ 'lazy_string_sensitive', 'ls_value', ValueType::STRING, true, true
+ ],
+ 'fast_int' => ['fast_int', 11, ValueType::INT],
+ 'lazy_int' => ['lazy_int', 12, ValueType::INT, true],
+ 'fast_int_sensitive' => ['fast_int_sensitive', 2024, ValueType::INT, false, true],
+ 'lazy_int_sensitive' => ['lazy_int_sensitive', 2048, ValueType::INT, true, true],
+ 'fast_float' => ['fast_float', 3.14, ValueType::FLOAT],
+ 'lazy_float' => ['lazy_float', 3.14159, ValueType::FLOAT, true],
+ 'fast_float_sensitive' => [
+ 'fast_float_sensitive', 1.41, ValueType::FLOAT, false, true
+ ],
+ 'lazy_float_sensitive' => [
+ 'lazy_float_sensitive', 1.4142, ValueType::FLOAT, true, true
+ ],
+ 'fast_array' => ['fast_array', ['year' => 2024], ValueType::ARRAY],
+ 'lazy_array' => ['lazy_array', ['month' => 'October'], ValueType::ARRAY, true],
+ 'fast_array_sensitive' => [
+ 'fast_array_sensitive', ['password' => 'pwd'], ValueType::ARRAY, false, true
+ ],
+ 'lazy_array_sensitive' => [
+ 'lazy_array_sensitive', ['password' => 'qwerty'], ValueType::ARRAY, true, true
+ ],
+ 'fast_boolean' => ['fast_boolean', true, ValueType::BOOL],
+ 'fast_boolean_0' => ['fast_boolean_0', false, ValueType::BOOL],
+ 'lazy_boolean' => ['lazy_boolean', true, ValueType::BOOL, true],
+ 'lazy_boolean_0' => ['lazy_boolean_0', false, ValueType::BOOL, true],
+ ],
+ 'app2' => [
+ 'key2' => ['key2', 'value2a', ValueType::STRING],
+ 'key3' => ['key3', 'value3', ValueType::STRING, true, false],
+ 'key4' => ['key4', 'value4', ValueType::STRING, false, true],
+ 'key8' => ['key8', 11, ValueType::INT],
+ 'key9' => ['key9', 'value9a', ValueType::STRING],
+ ],
+ 'app3' => [
+ 'key1' => ['key1', 'value123'],
+ 'key3' => ['key3', 'value3'],
+ 'key8' => ['key8', 12, ValueType::INT, false, true],
+ 'key9' => ['key9', 'value9b', ValueType::STRING, false, true],
+ 'key10' => ['key10', true, ValueType::BOOL],
+ ],
+ 'only-lazy' => [
+ 'key1' => ['key1', 'value456', ValueType::STRING, true],
+ 'key2' => ['key2', 'value2c', ValueType::STRING, true, true],
+ 'key3' => ['key3', 42, ValueType::INT, true],
+ 'key4' => ['key4', 17.42, ValueType::FLOAT, true],
+ 'key5' => ['key5', true, ValueType::BOOL, true],
+ ]
+ ],
+ 'user2' =>
+ [
+ 'app1' => [
+ '1' => ['1', 'value1'],
+ '2' => ['2', 'value2', ValueType::STRING, true, true],
+ '3' => ['3', 17, ValueType::INT, true],
+ '4' => ['4', 42, ValueType::INT, false, true],
+ '5' => ['5', 17.42, ValueType::FLOAT, false],
+ '6' => ['6', true, ValueType::BOOL, false],
+ ],
+ 'app2' => [
+ 'key2' => ['key2', 'value2b', ValueType::STRING],
+ 'key3' => ['key3', 'value3', ValueType::STRING, true, false],
+ 'key4' => ['key4', 'value4', ValueType::STRING, false, true],
+ 'key8' => ['key8', 12, ValueType::INT],
+ ],
+ 'app3' => [
+ 'key10' => ['key10', false, ValueType::BOOL],
+ ],
+ 'only-lazy' => [
+ 'key1' => ['key1', 'value1', ValueType::STRING, true]
+ ]
+ ],
+ 'user3' =>
+ [
+ 'app2' => [
+ 'key2' => ['key2', 'value2c'],
+ 'key3' => ['key3', 'value3', ValueType::STRING, true, false],
+ 'key4' => ['key4', 'value4', ValueType::STRING, false, true],
+ 'fast_string_sensitive' => [
+ 'fast_string_sensitive', 'fs_value', ValueType::STRING, false, true
+ ],
+ 'lazy_string_sensitive' => [
+ 'lazy_string_sensitive', 'ls_value', ValueType::STRING, true, true
+ ],
+ ],
+ 'only-lazy' => [
+ 'key3' => ['key3', 'value3', ValueType::STRING, true]
+ ]
+ ],
+ 'user4' =>
+ [
+ 'app2' => [
+ 'key1' => ['key1', 'value1'],
+ 'key2' => ['key2', 'value2A'],
+ 'key3' => ['key3', 'value3', ValueType::STRING, true, false],
+ 'key4' => ['key4', 'value4', ValueType::STRING, false, true],
+ ],
+ 'app3' => [
+ 'key10' => ['key10', true, ValueType::BOOL],
+ ],
+ 'only-lazy' => [
+ 'key1' => ['key1', 123, ValueType::INT, true]
+ ]
+ ],
+ 'user5' =>
+ [
+ 'app1' => [
+ 'key1' => ['key1', 'value1']
+ ],
+ 'app2' => [
+ 'key8' => ['key8', 12, ValueType::INT]
+ ],
+ 'only-lazy' => [
+ 'key1' => ['key1', 'value1', ValueType::STRING, true]
+ ]
+ ],
+
+ ];
+
+ protected function setUp(): void {
+ parent::setUp();
+
+ $this->connection = \OCP\Server::get(IDBConnection::class);
+ $this->logger = \OCP\Server::get(LoggerInterface::class);
+ $this->crypto = \OCP\Server::get(ICrypto::class);
+
+ // storing current preferences and emptying the data table
+ $sql = $this->connection->getQueryBuilder();
+ $sql->select('*')
+ ->from('preferences');
+ $result = $sql->executeQuery();
+ $this->originalPreferences = $result->fetchAll();
+ $result->closeCursor();
+
+ $sql = $this->connection->getQueryBuilder();
+ $sql->delete('preferences');
+ $sql->executeStatement();
+
+ $sql = $this->connection->getQueryBuilder();
+ $sql->insert('preferences')
+ ->values(
+ [
+ 'userid' => $sql->createParameter('userid'),
+ 'appid' => $sql->createParameter('appid'),
+ 'configkey' => $sql->createParameter('configkey'),
+ 'configvalue' => $sql->createParameter('configvalue'),
+ 'type' => $sql->createParameter('type'),
+ 'lazy' => $sql->createParameter('lazy')
+ ]
+ );
+
+ foreach ($this->basePreferences as $userId => $userData) {
+ foreach ($userData as $appId => $appData) {
+ foreach ($appData as $key => $row) {
+ $value = $row[1];
+ $type = ($row[2] ?? ValueType::MIXED)->value;
+
+ if ($type === ValueType::ARRAY->value) {
+ $value = json_encode($value);
+ }
+
+ if (($row[4] ?? false) === true) {
+ $type |= ValueType::SENSITIVE->value;
+ $value = self::invokePrivate(UserPreferences::class, 'ENCRYPTION_PREFIX')
+ . $this->crypto->encrypt((string)$value);
+ $this->basePreferences[$userId][$appId][$key]['encrypted'] = $value;
+ }
+
+ if ($type === ValueType::BOOL->value && $value === false) {
+ $value = '0';
+ }
+
+ $sql->setParameters(
+ [
+ 'userid' => $userId,
+ 'appid' => $appId,
+ 'configkey' => $row[0],
+ 'configvalue' => $value,
+ 'type' => $type,
+ 'lazy' => (($row[3] ?? false) === true) ? 1 : 0
+ ]
+ )->executeStatement();
+ }
+ }
+ }
+ }
+
+ protected function tearDown(): void {
+ $sql = $this->connection->getQueryBuilder();
+ $sql->delete('preferences');
+ $sql->executeStatement();
+
+ $sql = $this->connection->getQueryBuilder();
+ $sql->insert('preferences')
+ ->values(
+ [
+ 'userid' => $sql->createParameter('userid'),
+ 'appid' => $sql->createParameter('appid'),
+ 'configkey' => $sql->createParameter('configkey'),
+ 'configvalue' => $sql->createParameter('configvalue'),
+ 'lazy' => $sql->createParameter('lazy'),
+ 'type' => $sql->createParameter('type'),
+ ]
+ );
+
+ foreach ($this->originalPreferences as $key => $configs) {
+ $sql->setParameter('userid', $configs['userid'])
+ ->setParameter('appid', $configs['appid'])
+ ->setParameter('configkey', $configs['configkey'])
+ ->setParameter('configvalue', $configs['configvalue'])
+ ->setParameter('lazy', ($configs['lazy'] === '1') ? '1' : '0')
+ ->setParameter('type', $configs['type']);
+ $sql->executeStatement();
+ }
+
+ parent::tearDown();
+ }
+
+ /**
+ * @param array $preLoading preload the 'fast' cache for a list of users)
+ *
+ * @return IUserPreferences
+ */
+ private function generateUserPreferences(array $preLoading = []): IUserPreferences {
+ $preferences = new \OC\UserPreferences(
+ $this->connection,
+ $this->logger,
+ $this->crypto,
+ );
+ $msg = ' generateUserPreferences() failed to confirm cache status';
+
+ // confirm cache status
+ $status = $preferences->statusCache();
+ $this->assertSame([], $status['fastLoaded'], $msg);
+ $this->assertSame([], $status['lazyLoaded'], $msg);
+ $this->assertSame([], $status['fastCache'], $msg);
+ $this->assertSame([], $status['lazyCache'], $msg);
+ foreach ($preLoading as $preLoadUser) {
+ // simple way to initiate the load of non-lazy preferences values in cache
+ $preferences->getValueString($preLoadUser, 'core', 'preload');
+
+ // confirm cache status
+ $status = $preferences->statusCache();
+ $this->assertSame(true, $status['fastLoaded'][$preLoadUser], $msg);
+ $this->assertSame(false, $status['lazyLoaded'][$preLoadUser], $msg);
+
+ $apps = array_values(array_diff(array_keys($this->basePreferences[$preLoadUser]), ['only-lazy']));
+ $this->assertEqualsCanonicalizing($apps, array_keys($status['fastCache'][$preLoadUser]), $msg);
+ $this->assertSame([], array_keys($status['lazyCache'][$preLoadUser]), $msg);
+ }
+
+ return $preferences;
+ }
+
+ public function testGetUserIdsEmpty(): void {
+ $preferences = $this->generateUserPreferences();
+ $this->assertEqualsCanonicalizing(array_keys($this->basePreferences), $preferences->getUserIds());
+ }
+
+ public function testGetUserIds(): void {
+ $preferences = $this->generateUserPreferences();
+ $this->assertEqualsCanonicalizing(['user1', 'user2', 'user5'], $preferences->getUserIds('app1'));
+ }
+
+ public function testGetApps(): void {
+ $preferences = $this->generateUserPreferences();
+ $this->assertEqualsCanonicalizing(
+ array_keys($this->basePreferences['user1']), $preferences->getApps('user1')
+ );
+ }
+
+ public function testGetKeys(): void {
+ $preferences = $this->generateUserPreferences(['user1']);
+ $this->assertEqualsCanonicalizing(
+ array_keys($this->basePreferences['user1']['app1']), $preferences->getKeys('user1', 'app1')
+ );
+ }
+
+ /**
+ * @return array[]
+ */
+ public function providerHasKey(): array {
+ return [
+ ['user1', 'app1', 'key1', false, true],
+ ['user0', 'app1', 'key1', false, false],
+ ['user1', 'app1', 'key1', true, false],
+ ['user1', 'app1', 'key0', false, false],
+ ['user1', 'app1', 'key0', true, false],
+ ['user1', 'app1', 'fast_string_sensitive', false, true],
+ ['user1', 'app1', 'lazy_string_sensitive', true, true],
+ ['user2', 'only-lazy', 'key1', false, false],
+ ['user2', 'only-lazy', 'key1', true, true],
+ ];
+ }
+
+ /**
+ * @dataProvider providerHasKey
+ */
+ public function testHasKey(string $userId, string $appId, string $key, ?bool $lazy, bool $result): void {
+ $preferences = $this->generateUserPreferences();
+ $this->assertEquals($result, $preferences->hasKey($userId, $appId, $key, $lazy));
+ }
+
+ /**
+ * @return array[]
+ */
+ public function providerIsSensitive(): array {
+ return [
+ ['user1', 'app1', 'key1', false, false, false],
+ ['user0', 'app1', 'key1', false, false, true],
+ ['user1', 'app1', 'key1', true, false, true],
+ ['user1', 'app1', 'key1', null, false, false],
+ ['user1', 'app1', 'key0', false, false, true],
+ ['user1', 'app1', 'key0', true, false, true],
+ ['user1', 'app1', 'fast_string_sensitive', false, true, false],
+ ['user1', 'app1', 'lazy_string_sensitive', true, true, false],
+ ['user1', 'app1', 'fast_string_sensitive', true, true, true],
+ ['user1', 'app1', 'lazy_string_sensitive', false, true, true],
+ ['user1', 'app1', 'lazy_string_sensitive', null, true, false],
+ ['user2', 'only-lazy', 'key1', false, false, true],
+ ['user2', 'only-lazy', 'key1', true, false, false],
+ ['user2', 'only-lazy', 'key1', null, false, false],
+ ];
+ }
+
+ /**
+ * @dataProvider providerIsSensitive
+ */
+ public function testIsSensitive(
+ string $userId,
+ string $appId,
+ string $key,
+ ?bool $lazy,
+ bool $result,
+ bool $exception,
+ ): void {
+ $preferences = $this->generateUserPreferences();
+ if ($exception) {
+ $this->expectException(UnknownKeyException::class);
+ }
+
+ $this->assertEquals($result, $preferences->isSensitive($userId, $appId, $key, $lazy));
+ }
+
+ /**
+ * @return array[]
+ */
+ public function providerIsLazy(): array {
+ return [
+ ['user1', 'app1', 'key1', false, false],
+ ['user0', 'app1', 'key1', false, true],
+ ['user1', 'app1', 'key0', false, true],
+ ['user1', 'app1', 'key0', false, true],
+ ['user1', 'app1', 'fast_string_sensitive', false, false],
+ ['user1', 'app1', 'lazy_string_sensitive', true, false],
+ ['user2', 'only-lazy', 'key1', true, false],
+ ];
+ }
+
+ /**
+ * @dataProvider providerIsLazy
+ */
+ public function testIsLazy(
+ string $userId,
+ string $appId,
+ string $key,
+ bool $result,
+ bool $exception,
+ ): void {
+ $preferences = $this->generateUserPreferences();
+ if ($exception) {
+ $this->expectException(UnknownKeyException::class);
+ }
+
+ $this->assertEquals($result, $preferences->isLazy($userId, $appId, $key));
+ }
+
+ public function providerGetValues(): array {
+ return [
+ [
+ 'user1', 'app1', '', true,
+ [
+ 'fast_array' => ['year' => 2024],
+ 'fast_array_sensitive' => '***REMOVED SENSITIVE VALUE***',
+ 'fast_boolean' => true,
+ 'fast_boolean_0' => false,
+ 'fast_float' => 3.14,
+ 'fast_float_sensitive' => '***REMOVED SENSITIVE VALUE***',
+ 'fast_int' => 11,
+ 'fast_int_sensitive' => '***REMOVED SENSITIVE VALUE***',
+ 'fast_string' => 'f_value',
+ 'fast_string_sensitive' => '***REMOVED SENSITIVE VALUE***',
+ 'key1' => 'value1',
+ 'key22' => '31',
+ 'lazy_array' => ['month' => 'October'],
+ 'lazy_array_sensitive' => '***REMOVED SENSITIVE VALUE***',
+ 'lazy_boolean' => true,
+ 'lazy_boolean_0' => false,
+ 'lazy_float' => 3.14159,
+ 'lazy_float_sensitive' => '***REMOVED SENSITIVE VALUE***',
+ 'lazy_int' => 12,
+ 'lazy_int_sensitive' => '***REMOVED SENSITIVE VALUE***',
+ 'lazy_string' => 'l_value',
+ 'lazy_string_sensitive' => '***REMOVED SENSITIVE VALUE***',
+ ]
+ ],
+ [
+ 'user1', 'app1', '', false,
+ [
+ 'fast_array' => ['year' => 2024],
+ 'fast_array_sensitive' => ['password' => 'pwd'],
+ 'fast_boolean' => true,
+ 'fast_boolean_0' => false,
+ 'fast_float' => 3.14,
+ 'fast_float_sensitive' => 1.41,
+ 'fast_int' => 11,
+ 'fast_int_sensitive' => 2024,
+ 'fast_string' => 'f_value',
+ 'fast_string_sensitive' => 'fs_value',
+ 'key1' => 'value1',
+ 'key22' => '31',
+ 'lazy_array' => ['month' => 'October'],
+ 'lazy_array_sensitive' => ['password' => 'qwerty'],
+ 'lazy_boolean' => true,
+ 'lazy_boolean_0' => false,
+ 'lazy_float' => 3.14159,
+ 'lazy_float_sensitive' => 1.4142,
+ 'lazy_int' => 12,
+ 'lazy_int_sensitive' => 2048,
+ 'lazy_string' => 'l_value',
+ 'lazy_string_sensitive' => 'ls_value'
+ ]
+ ],
+ [
+ 'user1', 'app1', 'fast_', true,
+ [
+ 'fast_array' => ['year' => 2024],
+ 'fast_array_sensitive' => '***REMOVED SENSITIVE VALUE***',
+ 'fast_boolean' => true,
+ 'fast_boolean_0' => false,
+ 'fast_float' => 3.14,
+ 'fast_float_sensitive' => '***REMOVED SENSITIVE VALUE***',
+ 'fast_int' => 11,
+ 'fast_int_sensitive' => '***REMOVED SENSITIVE VALUE***',
+ 'fast_string' => 'f_value',
+ 'fast_string_sensitive' => '***REMOVED SENSITIVE VALUE***',
+ ]
+ ],
+ [
+ 'user1', 'app1', 'fast_', false,
+ [
+ 'fast_array' => ['year' => 2024],
+ 'fast_array_sensitive' => ['password' => 'pwd'],
+ 'fast_boolean' => true,
+ 'fast_boolean_0' => false,
+ 'fast_float' => 3.14,
+ 'fast_float_sensitive' => 1.41,
+ 'fast_int' => 11,
+ 'fast_int_sensitive' => 2024,
+ 'fast_string' => 'f_value',
+ 'fast_string_sensitive' => 'fs_value',
+ ]
+ ],
+ [
+ 'user1', 'app1', 'key1', true,
+ [
+ 'key1' => 'value1',
+ ]
+ ],
+ [
+ 'user2', 'app1', '', false,
+ [
+ '1' => 'value1',
+ '4' => 42,
+ '5' => 17.42,
+ '6' => true,
+ '2' => 'value2',
+ '3' => 17,
+ ]
+ ],
+ [
+ 'user2', 'app1', '', true,
+ [
+ '1' => 'value1',
+ '4' => '***REMOVED SENSITIVE VALUE***',
+ '5' => 17.42,
+ '6' => true,
+ '2' => '***REMOVED SENSITIVE VALUE***',
+ '3' => 17,
+ ]
+ ],
+ ];
+ }
+
+ /**
+ * @dataProvider providerGetValues
+ */
+ public function testGetValues(
+ string $userId,
+ string $appId,
+ string $prefix,
+ bool $filtered,
+ array $result,
+ ): void {
+ $preferences = $this->generateUserPreferences();
+ $this->assertJsonStringEqualsJsonString(
+ json_encode($result), json_encode($preferences->getValues($userId, $appId, $prefix, $filtered))
+ );
+ }
+
+ public function providerGetAllValues(): array {
+ return [
+ [
+ 'user2', false,
+ [
+ 'app1' => [
+ '1' => 'value1',
+ '4' => 42,
+ '5' => 17.42,
+ '6' => true,
+ '2' => 'value2',
+ '3' => 17,
+ ],
+ 'app2' => [
+ 'key2' => 'value2b',
+ 'key3' => 'value3',
+ 'key4' => 'value4',
+ 'key8' => 12,
+ ],
+ 'app3' => [
+ 'key10' => false,
+ ],
+ 'only-lazy' => [
+ 'key1' => 'value1',
+ ]
+ ],
+ ],
+ [
+ 'user2', true,
+ [
+ 'app1' => [
+ '1' => 'value1',
+ '4' => '***REMOVED SENSITIVE VALUE***',
+ '5' => 17.42,
+ '6' => true,
+ '2' => '***REMOVED SENSITIVE VALUE***',
+ '3' => 17,
+ ],
+ 'app2' => [
+ 'key2' => 'value2b',
+ 'key3' => 'value3',
+ 'key4' => '***REMOVED SENSITIVE VALUE***',
+ 'key8' => 12,
+ ],
+ 'app3' => [
+ 'key10' => false,
+ ],
+ 'only-lazy' => [
+ 'key1' => 'value1',
+ ]
+ ],
+ ],
+ [
+ 'user3', true,
+ [
+ 'app2' => [
+ 'key2' => 'value2c',
+ 'key3' => 'value3',
+ 'key4' => '***REMOVED SENSITIVE VALUE***',
+ 'fast_string_sensitive' => '***REMOVED SENSITIVE VALUE***',
+ 'lazy_string_sensitive' => '***REMOVED SENSITIVE VALUE***',
+ ],
+ 'only-lazy' => [
+ 'key3' => 'value3',
+ ]
+ ],
+ ],
+ [
+ 'user3', false,
+ [
+ 'app2' => [
+ 'key2' => 'value2c',
+ 'key3' => 'value3',
+ 'key4' => 'value4',
+ 'fast_string_sensitive' => 'fs_value',
+ 'lazy_string_sensitive' => 'ls_value',
+ ],
+ 'only-lazy' => [
+ 'key3' => 'value3',
+ ]
+ ],
+ ],
+ ];
+ }
+
+ /**
+ * @dataProvider providerGetAllValues
+ */
+ public function testGetAllValues(
+ string $userId,
+ bool $filtered,
+ array $result,
+ ): void {
+ $preferences = $this->generateUserPreferences();
+ $this->assertEqualsCanonicalizing($result, $preferences->getAllValues($userId, $filtered));
+ }
+
+ public function providerSearchValuesByApps(): array {
+ return [
+ [
+ 'user1', 'key1', false, null,
+ [
+ 'app1' => 'value1',
+ 'app3' => 'value123'
+ ]
+ ],
+ [
+ 'user1', 'key1', true, null,
+ [
+ 'only-lazy' => 'value456'
+ ]
+ ],
+ [
+ 'user1', 'key8', false, null,
+ [
+ 'app2' => 11,
+ 'app3' => 12,
+ ]
+ ],
+ [
+ 'user1', 'key9', false, ValueType::INT,
+ [
+ 'app2' => 0,
+ 'app3' => 0,
+ ]
+ ]
+ ];
+ }
+
+ /**
+ * @dataProvider providerSearchValuesByApps
+ */
+ public function testSearchValuesByApps(
+ string $userId,
+ string $key,
+ bool $lazy,
+ ?ValueType $typedAs,
+ array $result,
+ ): void {
+ $preferences = $this->generateUserPreferences();
+ $this->assertEquals($result, $preferences->searchValuesByApps($userId, $key, $lazy, $typedAs));
+ }
+
+ public function providerSearchValuesByUsers(): array {
+ return [
+ [
+ 'app2', 'key2', null, null,
+ [
+ 'user1' => 'value2a',
+ 'user2' => 'value2b',
+ 'user3' => 'value2c',
+ 'user4' => 'value2A'
+ ]
+ ],
+ [
+ 'app2', 'key2', null, ['user1', 'user3'],
+ [
+ 'user1' => 'value2a',
+ 'user3' => 'value2c',
+ ]
+ ],
+ [
+ 'app2', 'key2', ValueType::INT, ['user1', 'user3'],
+ [
+ 'user1' => 0,
+ 'user3' => 0,
+ ]
+ ],
+ [
+ 'app2', 'key8', ValueType::INT, null,
+ [
+ 'user1' => 11,
+ 'user2' => 12,
+ 'user5' => 12,
+ ]
+ ],
+ ];
+ }
+
+ /**
+ * @dataProvider providerSearchValuesByUsers
+ */
+ public function testSearchValuesByUsers(
+ string $app,
+ string $key,
+ ?ValueType $typedAs = null,
+ ?array $userIds = null,
+ array $result,
+ ): void {
+ $preferences = $this->generateUserPreferences();
+ $this->assertEqualsCanonicalizing(
+ $result, $preferences->searchValuesByUsers($app, $key, $typedAs, $userIds)
+ );
+ }
+
+ public function providerSearchValuesByValueString(): array {
+ return [
+ ['app2', 'key2', 'value2a', false, ['user1']],
+ ['app2', 'key2', 'value2A', false, ['user4']],
+ ['app2', 'key2', 'value2A', true, ['user1', 'user4']]
+ ];
+ }
+
+ /**
+ * @dataProvider providerSearchValuesByValueString
+ */
+ public function testSearchUsersByValueString(
+ string $app,
+ string $key,
+ string|array $value,
+ bool $ci,
+ array $result,
+ ): void {
+ $preferences = $this->generateUserPreferences();
+ $this->assertEqualsCanonicalizing(
+ $result, $preferences->searchUsersByValueString($app, $key, $value, $ci)
+ );
+ }
+
+ public function providerSearchValuesByValueInt(): array {
+ return [
+ ['app3', 'key8', 12, []], // sensitive value, cannot search
+ ['app2', 'key8', 12, ['user2', 'user5']], // sensitive value, cannot search
+ ['only-lazy', 'key1', 123, ['user4']],
+ ];
+ }
+
+ /**
+ * @dataProvider providerSearchValuesByValueInt
+ */
+ public function testSearchUsersByValueInt(
+ string $app,
+ string $key,
+ int $value,
+ array $result,
+ ): void {
+ $preferences = $this->generateUserPreferences();
+ $this->assertEqualsCanonicalizing($result, $preferences->searchUsersByValueInt($app, $key, $value));
+ }
+
+ public function providerSearchValuesByValues(): array {
+ return [
+ ['app2', 'key2', ['value2a', 'value2b'], ['user1', 'user2']],
+ ['app2', 'key2', ['value2a', 'value2c'], ['user1', 'user3']],
+ ];
+ }
+
+ /**
+ * @dataProvider providerSearchValuesByValues
+ */
+ public function testSearchUsersByValues(
+ string $app,
+ string $key,
+ array $values,
+ array $result,
+ ): void {
+ $preferences = $this->generateUserPreferences();
+ $this->assertEqualsCanonicalizing($result, $preferences->searchUsersByValues($app, $key, $values));
+ }
+
+ public function providerSearchValuesByValueBool(): array {
+ return [
+ ['app3', 'key10', true, ['user1', 'user4']],
+ ['app3', 'key10', false, ['user2']],
+ ];
+ }
+
+ /**
+ * @dataProvider providerSearchValuesByValueBool
+ */
+ public function testSearchUsersByValueBool(
+ string $app,
+ string $key,
+ bool $value,
+ array $result,
+ ): void {
+ $preferences = $this->generateUserPreferences();
+ $this->assertEqualsCanonicalizing($result, $preferences->searchUsersByValueBool($app, $key, $value));
+ }
+
+ public function providerGetValueMixed(): array {
+ return [
+ [
+ ['user1'], 'user1', 'app1', 'key0', 'default_because_unknown_key', true,
+ 'default_because_unknown_key'
+ ],
+ [
+ null, 'user1', 'app1', 'key0', 'default_because_unknown_key', true,
+ 'default_because_unknown_key'
+ ],
+ [
+ ['user1'], 'user1', 'app1', 'key0', 'default_because_unknown_key', false,
+ 'default_because_unknown_key'
+ ],
+ [
+ null, 'user1', 'app1', 'key0', 'default_because_unknown_key', false,
+ 'default_because_unknown_key'
+ ],
+ [['user1'], 'user1', 'app1', 'fast_string', 'default_because_unknown_key', false, 'f_value'],
+ [null, 'user1', 'app1', 'fast_string', 'default_because_unknown_key', false, 'f_value'],
+ [['user1'], 'user1', 'app1', 'fast_string', 'default_because_unknown_key', true, 'f_value'],
+ // because non-lazy are already loaded
+ [
+ null, 'user1', 'app1', 'fast_string', 'default_because_unknown_key', true,
+ 'default_because_unknown_key'
+ ],
+ [
+ ['user1'], 'user1', 'app1', 'lazy_string', 'default_because_unknown_key', false,
+ 'default_because_unknown_key'
+ ],
+ [
+ null, 'user1', 'app1', 'lazy_string', 'default_because_unknown_key', false,
+ 'default_because_unknown_key'
+ ],
+ [['user1'], 'user1', 'app1', 'lazy_string', 'default_because_unknown_key', true, 'l_value'],
+ [null, 'user1', 'app1', 'lazy_string', 'default_because_unknown_key', true, 'l_value'],
+ [
+ ['user1'], 'user1', 'app1', 'fast_string_sensitive', 'default_because_unknown_key', false,
+ 'fs_value'
+ ],
+ [
+ null, 'user1', 'app1', 'fast_string_sensitive', 'default_because_unknown_key', false,
+ 'fs_value'
+ ],
+ [
+ ['user1'], 'user1', 'app1', 'fast_string_sensitive', 'default_because_unknown_key', true,
+ 'fs_value'
+ ],
+ [
+ null, 'user1', 'app1', 'fast_string_sensitive', 'default_because_unknown_key', true,
+ 'default_because_unknown_key'
+ ],
+ [
+ ['user1'], 'user1', 'app1', 'lazy_string_sensitive', 'default_because_unknown_key', false,
+ 'default_because_unknown_key'
+ ],
+ [
+ null, 'user1', 'app1', 'lazy_string_sensitive', 'default_because_unknown_key', false,
+ 'default_because_unknown_key'
+ ],
+ [
+ ['user1'], 'user1', 'app1', 'lazy_string_sensitive', 'default_because_unknown_key', true,
+ 'ls_value'
+ ],
+ [null, 'user1', 'app1', 'lazy_string_sensitive', 'default_because_unknown_key', true, 'ls_value'],
+ ];
+ }
+
+ /**
+ * @dataProvider providerGetValueMixed
+ */
+ public function testGetValueMixed(
+ ?array $preload,
+ string $userId,
+ string $app,
+ string $key,
+ string $default,
+ bool $lazy,
+ string $result,
+ ): void {
+ $preferences = $this->generateUserPreferences($preload ?? []);
+ $this->assertEquals($result, $preferences->getValueMixed($userId, $app, $key, $default, $lazy));
+ }
+
+ /**
+ * @dataProvider providerGetValueMixed
+ */
+ public function testGetValueString(
+ ?array $preload,
+ string $userId,
+ string $app,
+ string $key,
+ string $default,
+ bool $lazy,
+ string $result,
+ ): void {
+ $preferences = $this->generateUserPreferences($preload ?? []);
+ $this->assertEquals($result, $preferences->getValueString($userId, $app, $key, $default, $lazy));
+ }
+
+ public function providerGetValueInt(): array {
+ return [
+ [['user1'], 'user1', 'app1', 'key0', 54321, true, 54321],
+ [null, 'user1', 'app1', 'key0', 54321, true, 54321],
+ [['user1'], 'user1', 'app1', 'key0', 54321, false, 54321],
+ [null, 'user1', 'app1', 'key0', 54321, false, 54321],
+ [null, 'user1', 'app1', 'key22', 54321, false, 31],
+ [['user1'], 'user1', 'app1', 'fast_int', 54321, false, 11],
+ [null, 'user1', 'app1', 'fast_int', 54321, false, 11],
+ [['user1'], 'user1', 'app1', 'fast_int', 54321, true, 11],
+ [null, 'user1', 'app1', 'fast_int', 54321, true, 54321],
+ [['user1'], 'user1', 'app1', 'fast_int_sensitive', 54321, false, 2024],
+ [null, 'user1', 'app1', 'fast_int_sensitive', 54321, false, 2024],
+ [['user1'], 'user1', 'app1', 'fast_int_sensitive', 54321, true, 2024],
+ [null, 'user1', 'app1', 'fast_int_sensitive', 54321, true, 54321],
+ [['user1'], 'user1', 'app1', 'lazy_int', 54321, false, 54321],
+ [null, 'user1', 'app1', 'lazy_int', 54321, false, 54321],
+ [['user1'], 'user1', 'app1', 'lazy_int', 54321, true, 12],
+ [null, 'user1', 'app1', 'lazy_int', 54321, true, 12],
+ [['user1'], 'user1', 'app1', 'lazy_int_sensitive', 54321, false, 54321],
+ [null, 'user1', 'app1', 'lazy_int_sensitive', 54321, false, 54321],
+ [['user1'], 'user1', 'app1', 'lazy_int_sensitive', 54321, true, 2048],
+ [null, 'user1', 'app1', 'lazy_int_sensitive', 54321, true, 2048],
+ ];
+ }
+
+ /**
+ * @dataProvider providerGetValueInt
+ */
+ public function testGetValueInt(
+ ?array $preload,
+ string $userId,
+ string $app,
+ string $key,
+ int $default,
+ bool $lazy,
+ int $result,
+ ): void {
+ $preferences = $this->generateUserPreferences($preload ?? []);
+ $this->assertEquals($result, $preferences->getValueInt($userId, $app, $key, $default, $lazy));
+ }
+
+ public function providerGetValueFloat(): array {
+ return [
+ [['user1'], 'user1', 'app1', 'key0', 54.321, true, 54.321],
+ [null, 'user1', 'app1', 'key0', 54.321, true, 54.321],
+ [['user1'], 'user1', 'app1', 'key0', 54.321, false, 54.321],
+ [null, 'user1', 'app1', 'key0', 54.321, false, 54.321],
+ [['user1'], 'user1', 'app1', 'fast_float', 54.321, false, 3.14],
+ [null, 'user1', 'app1', 'fast_float', 54.321, false, 3.14],
+ [['user1'], 'user1', 'app1', 'fast_float', 54.321, true, 3.14],
+ [null, 'user1', 'app1', 'fast_float', 54.321, true, 54.321],
+ [['user1'], 'user1', 'app1', 'fast_float_sensitive', 54.321, false, 1.41],
+ [null, 'user1', 'app1', 'fast_float_sensitive', 54.321, false, 1.41],
+ [['user1'], 'user1', 'app1', 'fast_float_sensitive', 54.321, true, 1.41],
+ [null, 'user1', 'app1', 'fast_float_sensitive', 54.321, true, 54.321],
+ [['user1'], 'user1', 'app1', 'lazy_float', 54.321, false, 54.321],
+ [null, 'user1', 'app1', 'lazy_float', 54.321, false, 54.321],
+ [['user1'], 'user1', 'app1', 'lazy_float', 54.321, true, 3.14159],
+ [null, 'user1', 'app1', 'lazy_float', 54.321, true, 3.14159],
+ [['user1'], 'user1', 'app1', 'lazy_float_sensitive', 54.321, false, 54.321],
+ [null, 'user1', 'app1', 'lazy_float_sensitive', 54.321, false, 54.321],
+ [['user1'], 'user1', 'app1', 'lazy_float_sensitive', 54.321, true, 1.4142],
+ [null, 'user1', 'app1', 'lazy_float_sensitive', 54.321, true, 1.4142],
+ ];
+ }
+
+ /**
+ * @dataProvider providerGetValueFloat
+ */
+ public function testGetValueFloat(
+ ?array $preload,
+ string $userId,
+ string $app,
+ string $key,
+ float $default,
+ bool $lazy,
+ float $result,
+ ): void {
+ $preferences = $this->generateUserPreferences($preload ?? []);
+ $this->assertEquals($result, $preferences->getValueFloat($userId, $app, $key, $default, $lazy));
+ }
+
+ public function providerGetValueBool(): array {
+ return [
+ [['user1'], 'user1', 'app1', 'key0', false, true, false],
+ [null, 'user1', 'app1', 'key0', false, true, false],
+ [['user1'], 'user1', 'app1', 'key0', true, true, true],
+ [null, 'user1', 'app1', 'key0', true, true, true],
+ [['user1'], 'user1', 'app1', 'key0', false, false, false],
+ [null, 'user1', 'app1', 'key0', false, false, false],
+ [['user1'], 'user1', 'app1', 'key0', true, false, true],
+ [null, 'user1', 'app1', 'key0', true, false, true],
+ [['user1'], 'user1', 'app1', 'fast_boolean', false, false, true],
+ [null, 'user1', 'app1', 'fast_boolean', false, false, true],
+ [['user1'], 'user1', 'app1', 'fast_boolean_0', false, false, false],
+ [null, 'user1', 'app1', 'fast_boolean_0', false, false, false],
+ [['user1'], 'user1', 'app1', 'fast_boolean', true, false, true],
+ [null, 'user1', 'app1', 'fast_boolean', true, false, true],
+ [['user1'], 'user1', 'app1', 'fast_boolean_0', true, false, false],
+ [null, 'user1', 'app1', 'fast_boolean_0', true, false, false],
+ [['user1'], 'user1', 'app1', 'fast_boolean', false, true, true],
+ [null, 'user1', 'app1', 'fast_boolean', false, true, false],
+ [['user1'], 'user1', 'app1', 'fast_boolean_0', false, true, false],
+ [null, 'user1', 'app1', 'fast_boolean_0', false, true, false],
+ [['user1'], 'user1', 'app1', 'fast_boolean', true, true, true],
+ [null, 'user1', 'app1', 'fast_boolean', true, true, true],
+ [['user1'], 'user1', 'app1', 'fast_boolean_0', true, true, false],
+ [null, 'user1', 'app1', 'fast_boolean_0', true, true, true],
+ [['user1'], 'user1', 'app1', 'lazy_boolean', false, false, false],
+ [null, 'user1', 'app1', 'lazy_boolean', false, false, false],
+ [['user1'], 'user1', 'app1', 'lazy_boolean_0', false, false, false],
+ [null, 'user1', 'app1', 'lazy_boolean_0', false, false, false],
+ [['user1'], 'user1', 'app1', 'lazy_boolean', true, false, true],
+ [null, 'user1', 'app1', 'lazy_boolean', true, false, true],
+ [['user1'], 'user1', 'app1', 'lazy_boolean_0', true, false, true],
+ [null, 'user1', 'app1', 'lazy_boolean_0', true, false, true],
+ [['user1'], 'user1', 'app1', 'lazy_boolean', false, true, true],
+ [null, 'user1', 'app1', 'lazy_boolean', false, true, true],
+ [['user1'], 'user1', 'app1', 'lazy_boolean_0', false, true, false],
+ [null, 'user1', 'app1', 'lazy_boolean_0', false, true, false],
+ [['user1'], 'user1', 'app1', 'lazy_boolean', true, true, true],
+ [null, 'user1', 'app1', 'lazy_boolean', true, true, true],
+ [['user1'], 'user1', 'app1', 'lazy_boolean_0', true, true, false],
+ [null, 'user1', 'app1', 'lazy_boolean_0', true, true, false],
+ ];
+ }
+
+ /**
+ * @dataProvider providerGetValueBool
+ */
+ public function testGetValueBool(
+ ?array $preload,
+ string $userId,
+ string $app,
+ string $key,
+ bool $default,
+ bool $lazy,
+ bool $result,
+ ): void {
+ $preferences = $this->generateUserPreferences($preload ?? []);
+ $this->assertEquals($result, $preferences->getValueBool($userId, $app, $key, $default, $lazy));
+ }
+
+ public function providerGetValueArray(): array {
+ return [
+ [
+ ['user1'], 'user1', 'app1', 'key0', ['default_because_unknown_key'], true,
+ ['default_because_unknown_key']
+ ],
+ [
+ null, 'user1', 'app1', 'key0', ['default_because_unknown_key'], true,
+ ['default_because_unknown_key']
+ ],
+ [
+ ['user1'], 'user1', 'app1', 'key0', ['default_because_unknown_key'], false,
+ ['default_because_unknown_key']
+ ],
+ [
+ null, 'user1', 'app1', 'key0', ['default_because_unknown_key'], false,
+ ['default_because_unknown_key']
+ ],
+ ];
+ }
+
+ /**
+ * @dataProvider providerGetValueArray
+ */
+ public function testGetValueArray(
+ ?array $preload,
+ string $userId,
+ string $app,
+ string $key,
+ array $default,
+ bool $lazy,
+ array $result,
+ ): void {
+ $preferences = $this->generateUserPreferences($preload ?? []);
+ $this->assertEqualsCanonicalizing(
+ $result, $preferences->getValueArray($userId, $app, $key, $default, $lazy)
+ );
+ }
+
+ public function providerGetValueType(): array {
+ return [
+ [null, 'user1', 'app1', 'key1', false, ValueType::MIXED],
+ [null, 'user1', 'app1', 'key1', true, null, UnknownKeyException::class],
+ [null, 'user1', 'app1', 'fast_string', true, ValueType::STRING, UnknownKeyException::class],
+ [['user1'], 'user1', 'app1', 'fast_string', true, ValueType::STRING],
+ [null, 'user1', 'app1', 'fast_string', false, ValueType::STRING],
+ [null, 'user1', 'app1', 'lazy_string', true, ValueType::STRING],
+ [null, 'user1', 'app1', 'lazy_string', false, ValueType::STRING, UnknownKeyException::class],
+ [
+ null, 'user1', 'app1', 'fast_string_sensitive', true, ValueType::STRING,
+ UnknownKeyException::class
+ ],
+ [['user1'], 'user1', 'app1', 'fast_string_sensitive', true, ValueType::STRING],
+ [null, 'user1', 'app1', 'fast_string_sensitive', false, ValueType::STRING],
+ [null, 'user1', 'app1', 'lazy_string_sensitive', true, ValueType::STRING],
+ [
+ null, 'user1', 'app1', 'lazy_string_sensitive', false, ValueType::STRING,
+ UnknownKeyException::class
+ ],
+ [null, 'user1', 'app1', 'fast_int', true, ValueType::INT, UnknownKeyException::class],
+ [['user1'], 'user1', 'app1', 'fast_int', true, ValueType::INT],
+ [null, 'user1', 'app1', 'fast_int', false, ValueType::INT],
+ [null, 'user1', 'app1', 'lazy_int', true, ValueType::INT],
+ [null, 'user1', 'app1', 'lazy_int', false, ValueType::INT, UnknownKeyException::class],
+ [null, 'user1', 'app1', 'fast_float', true, ValueType::FLOAT, UnknownKeyException::class],
+ [['user1'], 'user1', 'app1', 'fast_float', true, ValueType::FLOAT],
+ [null, 'user1', 'app1', 'fast_float', false, ValueType::FLOAT],
+ [null, 'user1', 'app1', 'lazy_float', true, ValueType::FLOAT],
+ [null, 'user1', 'app1', 'lazy_float', false, ValueType::FLOAT, UnknownKeyException::class],
+ [null, 'user1', 'app1', 'fast_boolean', true, ValueType::BOOL, UnknownKeyException::class],
+ [['user1'], 'user1', 'app1', 'fast_boolean', true, ValueType::BOOL],
+ [null, 'user1', 'app1', 'fast_boolean', false, ValueType::BOOL],
+ [null, 'user1', 'app1', 'lazy_boolean', true, ValueType::BOOL],
+ [null, 'user1', 'app1', 'lazy_boolean', false, ValueType::BOOL, UnknownKeyException::class],
+ ];
+ }
+
+ /**
+ * @dataProvider providerGetValueType
+ */
+ public function testGetValueType(
+ ?array $preload,
+ string $userId,
+ string $app,
+ string $key,
+ ?bool $lazy,
+ ?ValueType $result,
+ ?string $exception = null,
+ ): void {
+ $preferences = $this->generateUserPreferences($preload ?? []);
+ if ($exception !== null) {
+ $this->expectException($exception);
+ }
+
+ $type = $preferences->getValueType($userId, $app, $key, $lazy);
+ if ($exception === null) {
+ $this->assertEquals($result->value, $type->value);
+ }
+ }
+
+ public function providerSetValueMixed(): array {
+ return [
+ [null, 'user1', 'app1', 'key1', 'value', false, false, true],
+ [null, 'user1', 'app1', 'key1', '12345', true, false, true],
+ [null, 'user1', 'app1', 'key1', '12345', true, true, true],
+ [null, 'user1', 'app1', 'key1', 'value1', false, false, false],
+ [null, 'user1', 'app1', 'key1', 'value1', true, false, true],
+ [null, 'user1', 'app1', 'key1', 'value1', false, true, true],
+ [
+ null, 'user1', 'app1', 'fast_string', 'f_value_2', false, false, true,
+ TypeConflictException::class
+ ],
+ [
+ null, 'user1', 'app1', 'fast_string', 'f_value', true, false, true,
+ TypeConflictException::class
+ ],
+ [null, 'user1', 'app1', 'fast_string', 'f_value', true, true, true, TypeConflictException::class],
+ [null, 'user1', 'app1', 'fast_int', 'n_value', false, false, true, TypeConflictException::class],
+ [
+ null, 'user1', 'app1', 'fast_float', 'n_value', false, false, true,
+ TypeConflictException::class
+ ],
+ [
+ null, 'user1', 'app1', 'lazy_string', 'l_value_2', false, false, true,
+ TypeConflictException::class
+ ],
+ [null, 'user1', 'app1', 'lazy_string', 'l_value', true, false, false],
+ [null, 'user1', 'app1', 'lazy_string', 'l_value', true, true, true, TypeConflictException::class],
+ [null, 'user1', 'app1', 'lazy_int', 'l_value', false, false, true, TypeConflictException::class],
+ [
+ null, 'user1', 'app1', 'lazy_float', 'l_value', false, false, true,
+ TypeConflictException::class
+ ],
+ ];
+ }
+
+ /**
+ * @dataProvider providerSetValueMixed
+ */
+ public function testSetValueMixed(
+ ?array $preload,
+ string $userId,
+ string $app,
+ string $key,
+ string $value,
+ bool $lazy,
+ bool $sensitive,
+ bool $result,
+ ?string $exception = null,
+ ): void {
+ $preferences = $this->generateUserPreferences($preload ?? []);
+ if ($exception !== null) {
+ $this->expectException($exception);
+ }
+
+ $edited = $preferences->setValueMixed($userId, $app, $key, $value, $lazy, $sensitive);
+
+ if ($exception === null) {
+ $this->assertEquals($result, $edited);
+ }
+ }
+
+
+ public function providerSetValueString(): array {
+ return [
+ [null, 'user1', 'app1', 'key1', 'value', false, false, true],
+ [null, 'user1', 'app1', 'key1', '12345', true, false, true],
+ [null, 'user1', 'app1', 'key1', '12345', true, true, true],
+ [null, 'user1', 'app1', 'key1', 'value1', false, false, false],
+ [null, 'user1', 'app1', 'key1', 'value1', true, false, true],
+ [null, 'user1', 'app1', 'key1', 'value1', false, true, true],
+ [null, 'user1', 'app1', 'fast_string', 'f_value_2', false, false, true],
+ [null, 'user1', 'app1', 'fast_string', 'f_value', false, false, false],
+ [null, 'user1', 'app1', 'fast_string', 'f_value', true, false, true],
+ [null, 'user1', 'app1', 'fast_string', 'f_value', true, true, true],
+ [null, 'user1', 'app1', 'lazy_string', 'l_value_2', false, false, true],
+ [null, 'user1', 'app1', 'lazy_string', 'l_value', true, false, false],
+ [null, 'user1', 'app1', 'lazy_string', 'l_value', true, true, true],
+ [null, 'user1', 'app1', 'fast_string_sensitive', 'fs_value', false, true, false],
+ [null, 'user1', 'app1', 'fast_string_sensitive', 'fs_value', true, true, true],
+ [null, 'user1', 'app1', 'fast_string_sensitive', 'fs_value', true, false, true],
+ [null, 'user1', 'app1', 'lazy_string_sensitive', 'ls_value', false, true, true],
+ [null, 'user1', 'app1', 'lazy_string_sensitive', 'ls_value', true, true, false],
+ [null, 'user1', 'app1', 'lazy_string_sensitive', 'ls_value', true, false, false],
+ [null, 'user1', 'app1', 'lazy_string_sensitive', 'ls_value_2', true, false, true],
+ [null, 'user1', 'app1', 'fast_int', 'n_value', false, false, true, TypeConflictException::class],
+ [
+ null, 'user1', 'app1', 'fast_float', 'n_value', false, false, true,
+ TypeConflictException::class
+ ],
+ [
+ null, 'user1', 'app1', 'fast_float', 'n_value', false, false, true,
+ TypeConflictException::class
+ ],
+ [null, 'user1', 'app1', 'lazy_int', 'n_value', false, false, true, TypeConflictException::class],
+ [
+ null, 'user1', 'app1', 'lazy_boolean', 'n_value', false, false, true,
+ TypeConflictException::class
+ ],
+ [
+ null, 'user1', 'app1', 'lazy_float', 'n_value', false, false, true,
+ TypeConflictException::class
+ ],
+ ];
+ }
+
+ /**
+ * @dataProvider providerSetValueString
+ */
+ public function testSetValueString(
+ ?array $preload,
+ string $userId,
+ string $app,
+ string $key,
+ string $value,
+ bool $lazy,
+ bool $sensitive,
+ bool $result,
+ ?string $exception = null,
+ ): void {
+ $preferences = $this->generateUserPreferences($preload ?? []);
+ if ($exception !== null) {
+ $this->expectException($exception);
+ }
+
+ $edited = $preferences->setValueString($userId, $app, $key, $value, $lazy, $sensitive);
+ if ($exception !== null) {
+ return;
+ }
+
+ $this->assertEquals($result, $edited);
+ if ($result) {
+ $this->assertEquals($value, $preferences->getValueString($userId, $app, $key, $value, $lazy));
+ $preferences = $this->generateUserPreferences($preload ?? []);
+ $this->assertEquals($value, $preferences->getValueString($userId, $app, $key, $value, $lazy));
+ }
+ }
+
+ public function providerSetValueInt(): array {
+ return [
+ [null, 'user1', 'app1', 'key1', 12345, false, false, true],
+ [null, 'user1', 'app1', 'key1', 12345, true, false, true],
+ [null, 'user1', 'app1', 'key1', 12345, true, true, true],
+ [null, 'user1', 'app1', 'fast_int', 11, false, false, false],
+ [null, 'user1', 'app1', 'fast_int', 111, false, false, true],
+ [null, 'user1', 'app1', 'fast_int', 111, true, false, true],
+ [null, 'user1', 'app1', 'fast_int', 111, false, true, true],
+ [null, 'user1', 'app1', 'fast_int', 11, true, false, true],
+ [null, 'user1', 'app1', 'fast_int', 11, false, true, true],
+ [null, 'user1', 'app1', 'lazy_int', 12, false, false, true],
+ [null, 'user1', 'app1', 'lazy_int', 121, false, false, true],
+ [null, 'user1', 'app1', 'lazy_int', 121, true, false, true],
+ [null, 'user1', 'app1', 'lazy_int', 121, false, true, true],
+ [null, 'user1', 'app1', 'lazy_int', 12, true, false, false],
+ [null, 'user1', 'app1', 'lazy_int', 12, false, true, true],
+ [null, 'user1', 'app1', 'fast_string', 12345, false, false, true, TypeConflictException::class],
+ [null, 'user1', 'app1', 'fast_string', 12345, false, false, false, TypeConflictException::class],
+ [null, 'user1', 'app1', 'fast_string', 12345, true, false, true, TypeConflictException::class],
+ [null, 'user1', 'app1', 'fast_string', 12345, true, true, true, TypeConflictException::class],
+ [null, 'user1', 'app1', 'lazy_string', 12345, false, false, true, TypeConflictException::class],
+ [null, 'user1', 'app1', 'lazy_string', 12345, true, false, false, TypeConflictException::class],
+ [null, 'user1', 'app1', 'lazy_string', 12345, true, true, true, TypeConflictException::class],
+ [null, 'user1', 'app1', 'fast_float', 12345, false, false, true, TypeConflictException::class],
+ [null, 'user1', 'app1', 'fast_float', 12345, false, false, true, TypeConflictException::class],
+ [null, 'user1', 'app1', 'lazy_boolean', 12345, false, false, true, TypeConflictException::class],
+ [null, 'user1', 'app1', 'lazy_float', 12345, false, false, true, TypeConflictException::class],
+ ];
+ }
+
+ /**
+ * @dataProvider providerSetValueInt
+ */
+ public function testSetValueInt(
+ ?array $preload,
+ string $userId,
+ string $app,
+ string $key,
+ int $value,
+ bool $lazy,
+ bool $sensitive,
+ bool $result,
+ ?string $exception = null,
+ ): void {
+ $preferences = $this->generateUserPreferences($preload ?? []);
+ if ($exception !== null) {
+ $this->expectException($exception);
+ }
+
+ $edited = $preferences->setValueInt($userId, $app, $key, $value, $lazy, $sensitive);
+
+ if ($exception !== null) {
+ return;
+ }
+
+ $this->assertEquals($result, $edited);
+ if ($result) {
+ $this->assertEquals($value, $preferences->getValueInt($userId, $app, $key, $value, $lazy));
+ $preferences = $this->generateUserPreferences($preload ?? []);
+ $this->assertEquals($value, $preferences->getValueInt($userId, $app, $key, $value, $lazy));
+ }
+ }
+
+ public function providerSetValueFloat(): array {
+ return [
+ [null, 'user1', 'app1', 'key1', 12.345, false, false, true],
+ [null, 'user1', 'app1', 'key1', 12.345, true, false, true],
+ [null, 'user1', 'app1', 'key1', 12.345, true, true, true],
+ [null, 'user1', 'app1', 'fast_float', 3.14, false, false, false],
+ [null, 'user1', 'app1', 'fast_float', 3.15, false, false, true],
+ [null, 'user1', 'app1', 'fast_float', 3.15, true, false, true],
+ [null, 'user1', 'app1', 'fast_float', 3.15, false, true, true],
+ [null, 'user1', 'app1', 'fast_float', 3.14, true, false, true],
+ [null, 'user1', 'app1', 'fast_float', 3.14, false, true, true],
+ [null, 'user1', 'app1', 'lazy_float', 3.14159, false, false, true],
+ [null, 'user1', 'app1', 'lazy_float', 3.14158, false, false, true],
+ [null, 'user1', 'app1', 'lazy_float', 3.14158, true, false, true],
+ [null, 'user1', 'app1', 'lazy_float', 3.14158, false, true, true],
+ [null, 'user1', 'app1', 'lazy_float', 3.14159, true, false, false],
+ [null, 'user1', 'app1', 'lazy_float', 3.14159, false, true, true],
+ [null, 'user1', 'app1', 'fast_string', 12.345, false, false, true, TypeConflictException::class],
+ [null, 'user1', 'app1', 'fast_string', 12.345, false, false, false, TypeConflictException::class],
+ [null, 'user1', 'app1', 'fast_string', 12.345, true, false, true, TypeConflictException::class],
+ [null, 'user1', 'app1', 'fast_string', 12.345, true, true, true, TypeConflictException::class],
+ [null, 'user1', 'app1', 'lazy_string', 12.345, false, false, true, TypeConflictException::class],
+ [null, 'user1', 'app1', 'lazy_string', 12.345, true, false, false, TypeConflictException::class],
+ [null, 'user1', 'app1', 'fast_array', 12.345, true, true, true, TypeConflictException::class],
+ [null, 'user1', 'app1', 'fast_int', 12.345, false, false, true, TypeConflictException::class],
+ [null, 'user1', 'app1', 'fast_int', 12.345, false, false, true, TypeConflictException::class],
+ [null, 'user1', 'app1', 'lazy_boolean', 12.345, false, false, true, TypeConflictException::class],
+ ];
+ }
+
+ /**
+ * @dataProvider providerSetValueFloat
+ */
+ public function testSetValueFloat(
+ ?array $preload,
+ string $userId,
+ string $app,
+ string $key,
+ float $value,
+ bool $lazy,
+ bool $sensitive,
+ bool $result,
+ ?string $exception = null,
+ ): void {
+ $preferences = $this->generateUserPreferences($preload ?? []);
+ if ($exception !== null) {
+ $this->expectException($exception);
+ }
+
+ $edited = $preferences->setValueFloat($userId, $app, $key, $value, $lazy, $sensitive);
+
+ if ($exception !== null) {
+ return;
+ }
+
+ $this->assertEquals($result, $edited);
+ if ($result) {
+ $this->assertEquals($value, $preferences->getValueFloat($userId, $app, $key, $value, $lazy));
+ $preferences = $this->generateUserPreferences($preload ?? []);
+ $this->assertEquals($value, $preferences->getValueFloat($userId, $app, $key, $value, $lazy));
+ }
+ }
+
+
+ public function providerSetValueArray(): array {
+ return [
+ [null, 'user1', 'app1', 'key1', [], false, false, true],
+ [null, 'user1', 'app1', 'key1', [], true, false, true],
+ [null, 'user1', 'app1', 'key1', [], true, true, true],
+ [null, 'user1', 'app1', 'fast_array', ['year' => 2024], false, false, false],
+ [null, 'user1', 'app1', 'fast_array', [], false, false, true],
+ [null, 'user1', 'app1', 'fast_array', [], true, false, true],
+ [null, 'user1', 'app1', 'fast_array', [], false, true, true],
+ [null, 'user1', 'app1', 'fast_array', ['year' => 2024], true, false, true],
+ [null, 'user1', 'app1', 'fast_array', ['year' => 2024], false, true, true],
+ [null, 'user1', 'app1', 'lazy_array', ['month' => 'October'], false, false, true],
+ [null, 'user1', 'app1', 'lazy_array', ['month' => 'September'], false, false, true],
+ [null, 'user1', 'app1', 'lazy_array', ['month' => 'September'], true, false, true],
+ [null, 'user1', 'app1', 'lazy_array', ['month' => 'September'], false, true, true],
+ [null, 'user1', 'app1', 'lazy_array', ['month' => 'October'], true, false, false],
+ [null, 'user1', 'app1', 'lazy_array', ['month' => 'October'], false, true, true],
+ [null, 'user1', 'app1', 'fast_string', [], false, false, true, TypeConflictException::class],
+ [null, 'user1', 'app1', 'fast_string', [], false, false, false, TypeConflictException::class],
+ [null, 'user1', 'app1', 'fast_string', [], true, false, true, TypeConflictException::class],
+ [null, 'user1', 'app1', 'fast_string', [], true, true, true, TypeConflictException::class],
+ [null, 'user1', 'app1', 'lazy_string', [], false, false, true, TypeConflictException::class],
+ [null, 'user1', 'app1', 'lazy_string', [], true, false, false, TypeConflictException::class],
+ [null, 'user1', 'app1', 'lazy_string', [], true, true, true, TypeConflictException::class],
+ [null, 'user1', 'app1', 'fast_int', [], false, false, true, TypeConflictException::class],
+ [null, 'user1', 'app1', 'fast_int', [], false, false, true, TypeConflictException::class],
+ [null, 'user1', 'app1', 'lazy_boolean', [], false, false, true, TypeConflictException::class],
+ ];
+ }
+
+ /**
+ * @dataProvider providerSetValueArray
+ */
+ public function testSetValueArray(
+ ?array $preload,
+ string $userId,
+ string $app,
+ string $key,
+ array $value,
+ bool $lazy,
+ bool $sensitive,
+ bool $result,
+ ?string $exception = null,
+ ): void {
+ $preferences = $this->generateUserPreferences($preload ?? []);
+ if ($exception !== null) {
+ $this->expectException($exception);
+ }
+
+ $edited = $preferences->setValueArray($userId, $app, $key, $value, $lazy, $sensitive);
+
+ if ($exception !== null) {
+ return;
+ }
+
+ $this->assertEquals($result, $edited);
+ if ($result) {
+ $this->assertEqualsCanonicalizing(
+ $value, $preferences->getValueArray($userId, $app, $key, $value, $lazy)
+ );
+ $preferences = $this->generateUserPreferences($preload ?? []);
+ $this->assertEqualsCanonicalizing(
+ $value, $preferences->getValueArray($userId, $app, $key, $value, $lazy)
+ );
+ }
+ }
+
+ public function providerUpdateSensitive(): array {
+ return [
+ [null, 'user1', 'app1', 'key1', false, false],
+ [['user1'], 'user1', 'app1', 'key1', false, false],
+ [null, 'user1', 'app1', 'key1', true, true],
+ [['user1'], 'user1', 'app1', 'key1', true, true],
+ ];
+ }
+
+ /**
+ * @dataProvider providerUpdateSensitive
+ */
+ public function testUpdateSensitive(
+ ?array $preload,
+ string $userId,
+ string $app,
+ string $key,
+ bool $sensitive,
+ bool $result,
+ ?string $exception = null,
+ ): void {
+ $preferences = $this->generateUserPreferences($preload ?? []);
+ if ($exception !== null) {
+ $this->expectException($exception);
+ }
+
+ $edited = $preferences->updateSensitive($userId, $app, $key, $sensitive);
+ if ($exception !== null) {
+ return;
+ }
+
+ $this->assertEquals($result, $edited);
+ if ($result) {
+ $this->assertEquals($sensitive, $preferences->isSensitive($userId, $app, $key));
+ $preferences = $this->generateUserPreferences($preload ?? []);
+ $this->assertEquals($sensitive, $preferences->isSensitive($userId, $app, $key));
+ if ($sensitive) {
+ $this->assertEquals(true, str_starts_with(
+ $preferences->statusCache()['fastCache'][$userId][$app][$key] ??
+ $preferences->statusCache()['lazyCache'][$userId][$app][$key],
+ '$UserPreferencesEncryption$')
+ );
+ }
+ }
+ }
+
+ public function providerUpdateGlobalSensitive(): array {
+ return [[true], [false]];
+ }
+
+ /**
+ * @dataProvider providerUpdateGlobalSensitive
+ */
+ public function testUpdateGlobalSensitive(bool $sensitive): void {
+ $preferences = $this->generateUserPreferences($preload ?? []);
+ $app = 'app2';
+ if ($sensitive) {
+ $key = 'key2';
+ $value = 'value2a';
+ } else {
+ $key = 'key4';
+ $value = 'value4';
+ }
+
+ $this->assertEquals($value, $preferences->getValueString('user1', $app, $key));
+ foreach (['user1', 'user2', 'user3', 'user4'] as $userId) {
+ $preferences->getValueString($userId, $app, $key); // cache loading for userId
+ $this->assertEquals(
+ !$sensitive, str_starts_with(
+ $preferences->statusCache()['fastCache'][$userId][$app][$key] ??
+ $preferences->statusCache()['lazyCache'][$userId][$app][$key],
+ '$UserPreferencesEncryption$'
+ )
+ );
+ }
+
+ $preferences->updateGlobalSensitive($app, $key, $sensitive);
+
+ $this->assertEquals($value, $preferences->getValueString('user1', $app, $key));
+ foreach (['user1', 'user2', 'user3', 'user4'] as $userId) {
+ $this->assertEquals($sensitive, $preferences->isSensitive($userId, $app, $key));
+ // should only work if updateGlobalSensitive drop cache
+ $this->assertEquals($sensitive, str_starts_with(
+ $preferences->statusCache()['fastCache'][$userId][$app][$key] ??
+ $preferences->statusCache()['lazyCache'][$userId][$app][$key],
+ '$UserPreferencesEncryption$')
+ );
+ }
+ }
+
+ public function providerUpdateLazy(): array {
+ return [
+ [null, 'user1', 'app1', 'key1', false, false],
+ [['user1'], 'user1', 'app1', 'key1', false, false],
+ [null, 'user1', 'app1', 'key1', true, true],
+ [['user1'], 'user1', 'app1', 'key1', true, true],
+ ];
+ }
+
+ /**
+ * @dataProvider providerUpdateLazy
+ */
+ public function testUpdateLazy(
+ ?array $preload,
+ string $userId,
+ string $app,
+ string $key,
+ bool $lazy,
+ bool $result,
+ ?string $exception = null,
+ ): void {
+ $preferences = $this->generateUserPreferences($preload ?? []);
+ if ($exception !== null) {
+ $this->expectException($exception);
+ }
+
+ $edited = $preferences->updateLazy($userId, $app, $key, $lazy);
+ if ($exception !== null) {
+ return;
+ }
+
+ $this->assertEquals($result, $edited);
+ if ($result) {
+ $this->assertEquals($lazy, $preferences->isLazy($userId, $app, $key));
+ $preferences = $this->generateUserPreferences($preload ?? []);
+ $this->assertEquals($lazy, $preferences->isLazy($userId, $app, $key));
+ }
+ }
+
+ public function providerUpdateGlobalLazy(): array {
+ return [[true], [false]];
+ }
+
+ /**
+ * @dataProvider providerUpdateGlobalLazy
+ */
+ public function testUpdateGlobalLazy(bool $lazy): void {
+ $preferences = $this->generateUserPreferences($preload ?? []);
+ $app = 'app2';
+ if ($lazy) {
+ $key = 'key4';
+ $value = 'value4';
+ } else {
+ $key = 'key3';
+ $value = 'value3';
+ }
+
+ $this->assertEquals($value, $preferences->getValueString('user1', $app, $key, '', !$lazy));
+ foreach (['user1', 'user2', 'user3', 'user4'] as $userId) {
+ $this->assertEquals(!$lazy, $preferences->isLazy($userId, $app, $key));
+ }
+
+ $preferences->updateGlobalLazy($app, $key, $lazy);
+ $this->assertEquals($value, $preferences->getValueString('user1', $app, $key, '', $lazy));
+ foreach (['user1', 'user2', 'user3', 'user4'] as $userId) {
+ $this->assertEquals($lazy, $preferences->isLazy($userId, $app, $key));
+ }
+ }
+
+ public function providerGetDetails(): array {
+ return [
+ [
+ 'user3', 'app2', 'key2',
+ [
+ 'userId' => 'user3',
+ 'app' => 'app2',
+ 'key' => 'key2',
+ 'value' => 'value2c',
+ 'type' => 2,
+ 'lazy' => false,
+ 'typeString' => 'mixed',
+ 'sensitive' => false
+ ]
+ ],
+ [
+ 'user1', 'app1', 'lazy_int',
+ [
+ 'userId' => 'user1',
+ 'app' => 'app1',
+ 'key' => 'lazy_int',
+ 'value' => 12,
+ 'type' => 8,
+ 'lazy' => true,
+ 'typeString' => 'int',
+ 'sensitive' => false
+ ]
+ ],
+ [
+ 'user1', 'app1', 'fast_float_sensitive',
+ [
+ 'userId' => 'user1',
+ 'app' => 'app1',
+ 'key' => 'fast_float_sensitive',
+ 'value' => 1.41,
+ 'type' => 16,
+ 'lazy' => false,
+ 'typeString' => 'float',
+ 'sensitive' => true
+ ]
+ ],
+ ];
+ }
+
+ /**
+ * @dataProvider providerGetDetails
+ */
+ public function testGetDetails(string $userId, string $app, string $key, array $result): void {
+ $preferences = $this->generateUserPreferences($preload ?? []);
+ $this->assertEqualsCanonicalizing($result, $preferences->getDetails($userId, $app, $key));
+ }
+
+
+ public function providerDeletePreference(): array {
+ return [
+ [null, 'user1', 'app1', 'key22'],
+ [['user1'], 'user1', 'app1', 'fast_string_sensitive'],
+ [null, 'user1', 'app1', 'lazy_array_sensitive'],
+ [['user2'], 'user1', 'app1', 'lazy_array_sensitive'],
+ [null, 'user2', 'only-lazy', 'key1'],
+ [['user2'], 'user2', 'only-lazy', 'key1'],
+ ];
+ }
+
+ /**
+ * @dataProvider providerDeletePreference
+ */
+ public function testDeletePreference(
+ ?array $preload,
+ string $userId,
+ string $app,
+ string $key,
+ ): void {
+ $preferences = $this->generateUserPreferences($preload ?? []);
+ $lazy = $preferences->isLazy($userId, $app, $key);
+
+ $preferences = $this->generateUserPreferences($preload ?? []);
+ $this->assertEquals(true, $preferences->hasKey($userId, $app, $key, $lazy));
+ $preferences->deletePreference($userId, $app, $key);
+ $this->assertEquals(false, $preferences->hasKey($userId, $app, $key, $lazy));
+ $preferences = $this->generateUserPreferences($preload ?? []);
+ $this->assertEquals(false, $preferences->hasKey($userId, $app, $key, $lazy));
+ }
+
+ public function providerDeleteKey(): array {
+ return [
+ [null, 'app2', 'key3'],
+ [['user1'], 'app2', 'key3'],
+ [null, 'only-lazy', 'key1'],
+ [['user2'], 'only-lazy', 'key1'],
+ [null, 'app2', 'lazy_string_sensitive'],
+ [['user3', 'user1'], 'app2', 'lazy_string_sensitive'],
+ ];
+ }
+
+ /**
+ * @dataProvider providerDeleteKey
+ */
+ public function testDeleteKey(
+ ?array $preload,
+ string $app,
+ string $key,
+ ): void {
+ $preferences = $this->generateUserPreferences($preload ?? []);
+ $preferences->deleteKey($app, $key);
+
+ foreach (['user1', 'user2', 'user3', 'user4'] as $userId) {
+ $this->assertEquals(false, $preferences->hasKey($userId, $app, $key, null));
+ $preferencesTemp = $this->generateUserPreferences($preload ?? []);
+ $this->assertEquals(false, $preferencesTemp->hasKey($userId, $app, $key, null));
+ }
+ }
+
+ public function testDeleteApp(): void {
+ $preferences = $this->generateUserPreferences();
+ $preferences->deleteApp('only-lazy');
+
+ foreach (['user1', 'user2', 'user3', 'user4'] as $userId) {
+ $this->assertEquals(false, in_array('only-lazy', $preferences->getApps($userId)));
+ $preferencesTemp = $this->generateUserPreferences();
+ $this->assertEquals(false, in_array('only-lazy', $preferencesTemp->getApps($userId)));
+ }
+ }
+
+ public function testDeleteAllPreferences(): void {
+ $preferences = $this->generateUserPreferences();
+ $preferences->deleteAllPreferences('user1');
+
+ $this->assertEqualsCanonicalizing([], $preferences->getApps('user1'));
+ $preferences = $this->generateUserPreferences();
+ $this->assertEqualsCanonicalizing([], $preferences->getApps('user1'));
+ }
+
+ public function testClearCache(): void {
+ $preferences = $this->generateUserPreferences(['user1', 'user2']);
+ $preferences->clearCache('user1');
+
+ $this->assertEquals(true, $preferences->statusCache()['fastLoaded']['user2']);
+ $this->assertEquals(false, $preferences->statusCache()['fastLoaded']['user1']);
+ $this->assertEquals('value2a', $preferences->getValueString('user1', 'app2', 'key2'));
+ $this->assertEquals(false, $preferences->statusCache()['lazyLoaded']['user1']);
+ $this->assertEquals(true, $preferences->statusCache()['fastLoaded']['user1']);
+ }
+
+ public function testClearCacheAll(): void {
+ $preferences = $this->generateUserPreferences(['user1', 'user2']);
+ $preferences->clearCacheAll();
+ $this->assertEqualsCanonicalizing(
+ [
+ 'fastLoaded' => [],
+ 'fastCache' => [],
+ 'lazyLoaded' => [],
+ 'lazyCache' => [],
+ 'valueTypes' => [],
+ ],
+ $preferences->statusCache()
+ );
+ }
+}