2016-11-23 17:57:20 -05:00
|
|
|
<?php
|
2025-06-30 09:04:05 -04:00
|
|
|
|
2025-09-29 06:34:49 -04:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2016-11-23 17:57:20 -05:00
|
|
|
/**
|
2024-05-10 09:09:14 -04:00
|
|
|
* SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
2016-11-23 17:57:20 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
namespace Test\Accounts;
|
|
|
|
|
|
|
|
|
|
use OC\Accounts\AccountManager;
|
|
|
|
|
use OC\Accounts\Hooks;
|
2021-06-15 13:32:39 -04:00
|
|
|
use OCP\Accounts\IAccount;
|
2020-12-01 08:33:22 -05:00
|
|
|
use OCP\Accounts\IAccountManager;
|
2021-06-15 13:32:39 -04:00
|
|
|
use OCP\Accounts\IAccountProperty;
|
2016-11-23 17:57:20 -05:00
|
|
|
use OCP\IUser;
|
2020-10-13 10:33:53 -04:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
|
|
|
|
use Psr\Log\LoggerInterface;
|
2016-11-23 17:57:20 -05:00
|
|
|
use Test\TestCase;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class HooksTest
|
|
|
|
|
*
|
|
|
|
|
* @package Test\Accounts
|
|
|
|
|
*/
|
2025-10-20 19:52:40 -04:00
|
|
|
#[\PHPUnit\Framework\Attributes\Group('DB')]
|
2016-11-23 17:57:20 -05:00
|
|
|
class HooksTest extends TestCase {
|
|
|
|
|
|
2025-09-29 06:34:49 -04:00
|
|
|
private LoggerInterface&MockObject $logger;
|
|
|
|
|
private AccountManager&MockObject $accountManager;
|
|
|
|
|
private Hooks $hooks;
|
2016-11-23 17:57:20 -05:00
|
|
|
|
2019-11-27 09:27:18 -05:00
|
|
|
protected function setUp(): void {
|
2016-11-23 17:57:20 -05:00
|
|
|
parent::setUp();
|
|
|
|
|
|
2020-10-13 10:33:53 -04:00
|
|
|
$this->logger = $this->createMock(LoggerInterface::class);
|
2016-11-23 17:57:20 -05:00
|
|
|
$this->accountManager = $this->getMockBuilder(AccountManager::class)
|
|
|
|
|
->disableOriginalConstructor()->getMock();
|
|
|
|
|
|
2021-06-15 13:32:39 -04:00
|
|
|
$this->hooks = new Hooks($this->logger, $this->accountManager);
|
2016-11-23 17:57:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @param $params
|
|
|
|
|
* @param $data
|
|
|
|
|
* @param $setEmail
|
|
|
|
|
* @param $setDisplayName
|
|
|
|
|
* @param $error
|
|
|
|
|
*/
|
2025-06-30 10:56:59 -04:00
|
|
|
#[\PHPUnit\Framework\Attributes\DataProvider('dataTestChangeUserHook')]
|
2016-11-23 17:57:20 -05:00
|
|
|
public function testChangeUserHook($params, $data, $setEmail, $setDisplayName, $error): void {
|
|
|
|
|
if ($error) {
|
2021-06-15 13:32:39 -04:00
|
|
|
$this->accountManager->expects($this->never())->method('updateAccount');
|
2016-11-23 17:57:20 -05:00
|
|
|
} else {
|
2021-06-15 13:32:39 -04:00
|
|
|
$account = $this->createMock(IAccount::class);
|
|
|
|
|
$this->accountManager->expects($this->atLeastOnce())->method('getAccount')->willReturn($account);
|
2016-11-23 17:57:20 -05:00
|
|
|
if ($setEmail) {
|
2021-06-15 13:32:39 -04:00
|
|
|
$property = $this->createMock(IAccountProperty::class);
|
|
|
|
|
$property->expects($this->atLeastOnce())
|
|
|
|
|
->method('getValue')
|
|
|
|
|
->willReturn($data[IAccountManager::PROPERTY_EMAIL]['value']);
|
|
|
|
|
$property->expects($this->atLeastOnce())
|
|
|
|
|
->method('setValue')
|
|
|
|
|
->with($params['value']);
|
|
|
|
|
|
|
|
|
|
$account->expects($this->atLeastOnce())
|
|
|
|
|
->method('getProperty')
|
|
|
|
|
->with(IAccountManager::PROPERTY_EMAIL)
|
|
|
|
|
->willReturn($property);
|
|
|
|
|
|
|
|
|
|
$this->accountManager->expects($this->once())
|
|
|
|
|
->method('updateAccount')
|
|
|
|
|
->with($account);
|
2016-11-23 17:57:20 -05:00
|
|
|
} elseif ($setDisplayName) {
|
2021-06-15 13:32:39 -04:00
|
|
|
$property = $this->createMock(IAccountProperty::class);
|
|
|
|
|
$property->expects($this->atLeastOnce())
|
|
|
|
|
->method('getValue')
|
|
|
|
|
->willReturn($data[IAccountManager::PROPERTY_DISPLAYNAME]['value']);
|
|
|
|
|
$property->expects($this->atLeastOnce())
|
|
|
|
|
->method('setValue')
|
|
|
|
|
->with($params['value']);
|
|
|
|
|
|
|
|
|
|
$account->expects($this->atLeastOnce())
|
|
|
|
|
->method('getProperty')
|
|
|
|
|
->with(IAccountManager::PROPERTY_DISPLAYNAME)
|
|
|
|
|
->willReturn($property);
|
|
|
|
|
|
|
|
|
|
$this->accountManager->expects($this->once())
|
|
|
|
|
->method('updateAccount')
|
|
|
|
|
->with($account);
|
2016-11-23 17:57:20 -05:00
|
|
|
} else {
|
2021-06-15 13:32:39 -04:00
|
|
|
$this->accountManager->expects($this->never())->method('updateAccount');
|
2016-11-23 17:57:20 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-13 04:10:13 -04:00
|
|
|
$params['user'] = $this->createMock(IUser::class);
|
2021-06-15 13:32:39 -04:00
|
|
|
$this->hooks->changeUserHook($params['user'], $params['feature'], $params['value']);
|
2016-11-23 17:57:20 -05:00
|
|
|
}
|
|
|
|
|
|
2025-05-13 04:10:13 -04:00
|
|
|
public static function dataTestChangeUserHook(): array {
|
2016-11-23 17:57:20 -05:00
|
|
|
return [
|
|
|
|
|
[
|
2025-05-13 04:10:13 -04:00
|
|
|
['feature' => '', 'value' => ''],
|
2016-11-23 17:57:20 -05:00
|
|
|
[
|
2020-12-01 08:33:22 -05:00
|
|
|
IAccountManager::PROPERTY_EMAIL => ['value' => ''],
|
|
|
|
|
IAccountManager::PROPERTY_DISPLAYNAME => ['value' => '']
|
2016-11-23 17:57:20 -05:00
|
|
|
],
|
|
|
|
|
false, false, true
|
|
|
|
|
],
|
|
|
|
|
[
|
2025-05-13 04:10:13 -04:00
|
|
|
['feature' => 'foo', 'value' => 'bar'],
|
2016-11-23 17:57:20 -05:00
|
|
|
[
|
2020-12-01 08:33:22 -05:00
|
|
|
IAccountManager::PROPERTY_EMAIL => ['value' => 'oldMail@example.com'],
|
|
|
|
|
IAccountManager::PROPERTY_DISPLAYNAME => ['value' => 'oldDisplayName']
|
2016-11-23 17:57:20 -05:00
|
|
|
],
|
|
|
|
|
false, false, false
|
|
|
|
|
],
|
|
|
|
|
[
|
2025-05-13 04:10:13 -04:00
|
|
|
['feature' => 'eMailAddress', 'value' => 'newMail@example.com'],
|
2016-11-23 17:57:20 -05:00
|
|
|
[
|
2020-12-01 08:33:22 -05:00
|
|
|
IAccountManager::PROPERTY_EMAIL => ['value' => 'oldMail@example.com'],
|
|
|
|
|
IAccountManager::PROPERTY_DISPLAYNAME => ['value' => 'oldDisplayName']
|
2016-11-23 17:57:20 -05:00
|
|
|
],
|
|
|
|
|
true, false, false
|
|
|
|
|
],
|
|
|
|
|
[
|
2025-05-13 04:10:13 -04:00
|
|
|
['feature' => 'displayName', 'value' => 'newDisplayName'],
|
2016-11-23 17:57:20 -05:00
|
|
|
[
|
2020-12-01 08:33:22 -05:00
|
|
|
IAccountManager::PROPERTY_EMAIL => ['value' => 'oldMail@example.com'],
|
|
|
|
|
IAccountManager::PROPERTY_DISPLAYNAME => ['value' => 'oldDisplayName']
|
2016-11-23 17:57:20 -05:00
|
|
|
],
|
|
|
|
|
false, true, false
|
|
|
|
|
],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
}
|