aboutsummaryrefslogtreecommitdiffstats
path: root/tests/lib/Accounts/HooksTest.php
diff options
context:
space:
mode:
Diffstat (limited to 'tests/lib/Accounts/HooksTest.php')
-rw-r--r--tests/lib/Accounts/HooksTest.php117
1 files changed, 51 insertions, 66 deletions
diff --git a/tests/lib/Accounts/HooksTest.php b/tests/lib/Accounts/HooksTest.php
index 8af9e209034..622fb3c7461 100644
--- a/tests/lib/Accounts/HooksTest.php
+++ b/tests/lib/Accounts/HooksTest.php
@@ -1,29 +1,17 @@
<?php
+
/**
- * @copyright Copyright (c) 2016 Bjoern Schiessle <bjoern@schiessle.org>
- *
- * @license GNU AGPL version 3 or any later version
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
+ * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace Test\Accounts;
use OC\Accounts\AccountManager;
use OC\Accounts\Hooks;
+use OCP\Accounts\IAccount;
use OCP\Accounts\IAccountManager;
+use OCP\Accounts\IAccountProperty;
use OCP\IUser;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
@@ -36,14 +24,13 @@ use Test\TestCase;
* @group DB
*/
class HooksTest extends TestCase {
-
/** @var LoggerInterface|MockObject */
private $logger;
/** @var AccountManager|MockObject */
private $accountManager;
- /** @var Hooks|MockObject */
+ /** @var Hooks */
private $hooks;
protected function setUp(): void {
@@ -53,16 +40,10 @@ class HooksTest extends TestCase {
$this->accountManager = $this->getMockBuilder(AccountManager::class)
->disableOriginalConstructor()->getMock();
- $this->hooks = $this->getMockBuilder(Hooks::class)
- ->setConstructorArgs([$this->logger])
- ->setMethods(['getAccountManager'])
- ->getMock();
-
- $this->hooks->method('getAccountManager')->willReturn($this->accountManager);
+ $this->hooks = new Hooks($this->logger, $this->accountManager);
}
/**
- * @dataProvider dataTestChangeUserHook
*
* @param $params
* @param $data
@@ -70,31 +51,57 @@ class HooksTest extends TestCase {
* @param $setDisplayName
* @param $error
*/
- public function testChangeUserHook($params, $data, $setEmail, $setDisplayName, $error) {
+ #[\PHPUnit\Framework\Attributes\DataProvider('dataTestChangeUserHook')]
+ public function testChangeUserHook($params, $data, $setEmail, $setDisplayName, $error): void {
if ($error) {
- $this->accountManager->expects($this->never())->method('getUser');
- $this->accountManager->expects($this->never())->method('updateUser');
+ $this->accountManager->expects($this->never())->method('updateAccount');
} else {
- $this->accountManager->expects($this->once())->method('getUser')->willReturn($data);
- $newData = $data;
+ $account = $this->createMock(IAccount::class);
+ $this->accountManager->expects($this->atLeastOnce())->method('getAccount')->willReturn($account);
if ($setEmail) {
- $newData[IAccountManager::PROPERTY_EMAIL]['value'] = $params['value'];
- $this->accountManager->expects($this->once())->method('updateUser')
- ->with($params['user'], $newData);
+ $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);
} elseif ($setDisplayName) {
- $newData[IAccountManager::PROPERTY_DISPLAYNAME]['value'] = $params['value'];
- $this->accountManager->expects($this->once())->method('updateUser')
- ->with($params['user'], $newData);
+ $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);
} else {
- $this->accountManager->expects($this->never())->method('updateUser');
+ $this->accountManager->expects($this->never())->method('updateAccount');
}
}
- $this->hooks->changeUserHook($params);
+ $params['user'] = $this->createMock(IUser::class);
+ $this->hooks->changeUserHook($params['user'], $params['feature'], $params['value']);
}
- public function dataTestChangeUserHook() {
- $user = $this->createMock(IUser::class);
+ public static function dataTestChangeUserHook(): array {
return [
[
['feature' => '', 'value' => ''],
@@ -105,23 +112,7 @@ class HooksTest extends TestCase {
false, false, true
],
[
- ['user' => $user, 'value' => ''],
- [
- IAccountManager::PROPERTY_EMAIL => ['value' => ''],
- IAccountManager::PROPERTY_DISPLAYNAME => ['value' => '']
- ],
- false, false, true
- ],
- [
- ['user' => $user, 'feature' => ''],
- [
- IAccountManager::PROPERTY_EMAIL => ['value' => ''],
- IAccountManager::PROPERTY_DISPLAYNAME => ['value' => '']
- ],
- false, false, true
- ],
- [
- ['user' => $user, 'feature' => 'foo', 'value' => 'bar'],
+ ['feature' => 'foo', 'value' => 'bar'],
[
IAccountManager::PROPERTY_EMAIL => ['value' => 'oldMail@example.com'],
IAccountManager::PROPERTY_DISPLAYNAME => ['value' => 'oldDisplayName']
@@ -129,7 +120,7 @@ class HooksTest extends TestCase {
false, false, false
],
[
- ['user' => $user, 'feature' => 'eMailAddress', 'value' => 'newMail@example.com'],
+ ['feature' => 'eMailAddress', 'value' => 'newMail@example.com'],
[
IAccountManager::PROPERTY_EMAIL => ['value' => 'oldMail@example.com'],
IAccountManager::PROPERTY_DISPLAYNAME => ['value' => 'oldDisplayName']
@@ -137,7 +128,7 @@ class HooksTest extends TestCase {
true, false, false
],
[
- ['user' => $user, 'feature' => 'displayName', 'value' => 'newDisplayName'],
+ ['feature' => 'displayName', 'value' => 'newDisplayName'],
[
IAccountManager::PROPERTY_EMAIL => ['value' => 'oldMail@example.com'],
IAccountManager::PROPERTY_DISPLAYNAME => ['value' => 'oldDisplayName']
@@ -146,10 +137,4 @@ class HooksTest extends TestCase {
],
];
}
-
- public function testGetAccountManager() {
- $hooks = new Hooks($this->logger);
- $result = $this->invokePrivate($hooks, 'getAccountManager');
- $this->assertInstanceOf(AccountManager::class, $result);
- }
}