HooksTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Bjoern Schiessle <bjoern@schiessle.org>
  4. *
  5. * @license GNU AGPL version 3 or any later version
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as
  9. * published by the Free Software Foundation, either version 3 of the
  10. * License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. namespace Test\Accounts;
  22. use OC\Accounts\AccountManager;
  23. use OC\Accounts\Hooks;
  24. use OCP\Accounts\IAccount;
  25. use OCP\Accounts\IAccountManager;
  26. use OCP\Accounts\IAccountProperty;
  27. use OCP\IUser;
  28. use PHPUnit\Framework\MockObject\MockObject;
  29. use Psr\Log\LoggerInterface;
  30. use Test\TestCase;
  31. /**
  32. * Class HooksTest
  33. *
  34. * @package Test\Accounts
  35. * @group DB
  36. */
  37. class HooksTest extends TestCase {
  38. /** @var LoggerInterface|MockObject */
  39. private $logger;
  40. /** @var AccountManager|MockObject */
  41. private $accountManager;
  42. /** @var Hooks */
  43. private $hooks;
  44. protected function setUp(): void {
  45. parent::setUp();
  46. $this->logger = $this->createMock(LoggerInterface::class);
  47. $this->accountManager = $this->getMockBuilder(AccountManager::class)
  48. ->disableOriginalConstructor()->getMock();
  49. $this->hooks = new Hooks($this->logger, $this->accountManager);
  50. }
  51. /**
  52. * @dataProvider dataTestChangeUserHook
  53. *
  54. * @param $params
  55. * @param $data
  56. * @param $setEmail
  57. * @param $setDisplayName
  58. * @param $error
  59. */
  60. public function testChangeUserHook($params, $data, $setEmail, $setDisplayName, $error) {
  61. if ($error) {
  62. $this->accountManager->expects($this->never())->method('updateAccount');
  63. } else {
  64. $account = $this->createMock(IAccount::class);
  65. $this->accountManager->expects($this->atLeastOnce())->method('getAccount')->willReturn($account);
  66. if ($setEmail) {
  67. $property = $this->createMock(IAccountProperty::class);
  68. $property->expects($this->atLeastOnce())
  69. ->method('getValue')
  70. ->willReturn($data[IAccountManager::PROPERTY_EMAIL]['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_EMAIL)
  77. ->willReturn($property);
  78. $this->accountManager->expects($this->once())
  79. ->method('updateAccount')
  80. ->with($account);
  81. } elseif ($setDisplayName) {
  82. $property = $this->createMock(IAccountProperty::class);
  83. $property->expects($this->atLeastOnce())
  84. ->method('getValue')
  85. ->willReturn($data[IAccountManager::PROPERTY_DISPLAYNAME]['value']);
  86. $property->expects($this->atLeastOnce())
  87. ->method('setValue')
  88. ->with($params['value']);
  89. $account->expects($this->atLeastOnce())
  90. ->method('getProperty')
  91. ->with(IAccountManager::PROPERTY_DISPLAYNAME)
  92. ->willReturn($property);
  93. $this->accountManager->expects($this->once())
  94. ->method('updateAccount')
  95. ->with($account);
  96. } else {
  97. $this->accountManager->expects($this->never())->method('updateAccount');
  98. }
  99. }
  100. $this->hooks->changeUserHook($params['user'], $params['feature'], $params['value']);
  101. }
  102. public function dataTestChangeUserHook() {
  103. $user = $this->createMock(IUser::class);
  104. return [
  105. [
  106. ['user' => $user, 'feature' => '', 'value' => ''],
  107. [
  108. IAccountManager::PROPERTY_EMAIL => ['value' => ''],
  109. IAccountManager::PROPERTY_DISPLAYNAME => ['value' => '']
  110. ],
  111. false, false, true
  112. ],
  113. [
  114. ['user' => $user, 'feature' => 'foo', 'value' => 'bar'],
  115. [
  116. IAccountManager::PROPERTY_EMAIL => ['value' => 'oldMail@example.com'],
  117. IAccountManager::PROPERTY_DISPLAYNAME => ['value' => 'oldDisplayName']
  118. ],
  119. false, false, false
  120. ],
  121. [
  122. ['user' => $user, 'feature' => 'eMailAddress', 'value' => 'newMail@example.com'],
  123. [
  124. IAccountManager::PROPERTY_EMAIL => ['value' => 'oldMail@example.com'],
  125. IAccountManager::PROPERTY_DISPLAYNAME => ['value' => 'oldDisplayName']
  126. ],
  127. true, false, false
  128. ],
  129. [
  130. ['user' => $user, 'feature' => 'displayName', 'value' => 'newDisplayName'],
  131. [
  132. IAccountManager::PROPERTY_EMAIL => ['value' => 'oldMail@example.com'],
  133. IAccountManager::PROPERTY_DISPLAYNAME => ['value' => 'oldDisplayName']
  134. ],
  135. false, true, false
  136. ],
  137. ];
  138. }
  139. }