HooksTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace Test\Accounts;
  7. use OC\Accounts\AccountManager;
  8. use OC\Accounts\Hooks;
  9. use OCP\Accounts\IAccount;
  10. use OCP\Accounts\IAccountManager;
  11. use OCP\Accounts\IAccountProperty;
  12. use OCP\IUser;
  13. use PHPUnit\Framework\MockObject\MockObject;
  14. use Psr\Log\LoggerInterface;
  15. use Test\TestCase;
  16. /**
  17. * Class HooksTest
  18. *
  19. * @package Test\Accounts
  20. * @group DB
  21. */
  22. class HooksTest extends TestCase {
  23. /** @var LoggerInterface|MockObject */
  24. private $logger;
  25. /** @var AccountManager|MockObject */
  26. private $accountManager;
  27. /** @var Hooks */
  28. private $hooks;
  29. protected function setUp(): void {
  30. parent::setUp();
  31. $this->logger = $this->createMock(LoggerInterface::class);
  32. $this->accountManager = $this->getMockBuilder(AccountManager::class)
  33. ->disableOriginalConstructor()->getMock();
  34. $this->hooks = new Hooks($this->logger, $this->accountManager);
  35. }
  36. /**
  37. * @dataProvider dataTestChangeUserHook
  38. *
  39. * @param $params
  40. * @param $data
  41. * @param $setEmail
  42. * @param $setDisplayName
  43. * @param $error
  44. */
  45. public function testChangeUserHook($params, $data, $setEmail, $setDisplayName, $error): void {
  46. if ($error) {
  47. $this->accountManager->expects($this->never())->method('updateAccount');
  48. } else {
  49. $account = $this->createMock(IAccount::class);
  50. $this->accountManager->expects($this->atLeastOnce())->method('getAccount')->willReturn($account);
  51. if ($setEmail) {
  52. $property = $this->createMock(IAccountProperty::class);
  53. $property->expects($this->atLeastOnce())
  54. ->method('getValue')
  55. ->willReturn($data[IAccountManager::PROPERTY_EMAIL]['value']);
  56. $property->expects($this->atLeastOnce())
  57. ->method('setValue')
  58. ->with($params['value']);
  59. $account->expects($this->atLeastOnce())
  60. ->method('getProperty')
  61. ->with(IAccountManager::PROPERTY_EMAIL)
  62. ->willReturn($property);
  63. $this->accountManager->expects($this->once())
  64. ->method('updateAccount')
  65. ->with($account);
  66. } elseif ($setDisplayName) {
  67. $property = $this->createMock(IAccountProperty::class);
  68. $property->expects($this->atLeastOnce())
  69. ->method('getValue')
  70. ->willReturn($data[IAccountManager::PROPERTY_DISPLAYNAME]['value']);
  71. $property->expects($this->atLeastOnce())
  72. ->method('setValue')
  73. ->with($params['value']);
  74. $account->expects($this->atLeastOnce())
  75. ->method('getProperty')
  76. ->with(IAccountManager::PROPERTY_DISPLAYNAME)
  77. ->willReturn($property);
  78. $this->accountManager->expects($this->once())
  79. ->method('updateAccount')
  80. ->with($account);
  81. } else {
  82. $this->accountManager->expects($this->never())->method('updateAccount');
  83. }
  84. }
  85. $this->hooks->changeUserHook($params['user'], $params['feature'], $params['value']);
  86. }
  87. public function dataTestChangeUserHook() {
  88. $user = $this->createMock(IUser::class);
  89. return [
  90. [
  91. ['user' => $user, 'feature' => '', 'value' => ''],
  92. [
  93. IAccountManager::PROPERTY_EMAIL => ['value' => ''],
  94. IAccountManager::PROPERTY_DISPLAYNAME => ['value' => '']
  95. ],
  96. false, false, true
  97. ],
  98. [
  99. ['user' => $user, 'feature' => 'foo', 'value' => 'bar'],
  100. [
  101. IAccountManager::PROPERTY_EMAIL => ['value' => 'oldMail@example.com'],
  102. IAccountManager::PROPERTY_DISPLAYNAME => ['value' => 'oldDisplayName']
  103. ],
  104. false, false, false
  105. ],
  106. [
  107. ['user' => $user, 'feature' => 'eMailAddress', 'value' => 'newMail@example.com'],
  108. [
  109. IAccountManager::PROPERTY_EMAIL => ['value' => 'oldMail@example.com'],
  110. IAccountManager::PROPERTY_DISPLAYNAME => ['value' => 'oldDisplayName']
  111. ],
  112. true, false, false
  113. ],
  114. [
  115. ['user' => $user, 'feature' => 'displayName', 'value' => 'newDisplayName'],
  116. [
  117. IAccountManager::PROPERTY_EMAIL => ['value' => 'oldMail@example.com'],
  118. IAccountManager::PROPERTY_DISPLAYNAME => ['value' => 'oldDisplayName']
  119. ],
  120. false, true, false
  121. ],
  122. ];
  123. }
  124. }