HooksTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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\ILogger;
  25. use OCP\IUser;
  26. use Test\TestCase;
  27. /**
  28. * Class HooksTest
  29. *
  30. * @package Test\Accounts
  31. * @group DB
  32. */
  33. class HooksTest extends TestCase {
  34. /** @var ILogger | \PHPUnit_Framework_MockObject_MockObject */
  35. private $logger;
  36. /** @var AccountManager | \PHPUnit_Framework_MockObject_MockObject */
  37. private $accountManager;
  38. /** @var Hooks | \PHPUnit_Framework_MockObject_MockObject */
  39. private $hooks;
  40. public function setUp() {
  41. parent::setUp();
  42. $this->logger = $this->createMock(ILogger::class);
  43. $this->accountManager = $this->getMockBuilder(AccountManager::class)
  44. ->disableOriginalConstructor()->getMock();
  45. $this->hooks = $this->getMockBuilder(Hooks::class)
  46. ->setConstructorArgs([$this->logger])
  47. ->setMethods(['getAccountManager'])
  48. ->getMock();
  49. $this->hooks->method('getAccountManager')->willReturn($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('getUser');
  63. $this->accountManager->expects($this->never())->method('updateUser');
  64. } else {
  65. $this->accountManager->expects($this->once())->method('getUser')->willReturn($data);
  66. $newData = $data;
  67. if ($setEmail) {
  68. $newData[AccountManager::PROPERTY_EMAIL]['value'] = $params['value'];
  69. $this->accountManager->expects($this->once())->method('updateUser')
  70. ->with($params['user'], $newData);
  71. } elseif ($setDisplayName) {
  72. $newData[AccountManager::PROPERTY_DISPLAYNAME]['value'] = $params['value'];
  73. $this->accountManager->expects($this->once())->method('updateUser')
  74. ->with($params['user'], $newData);
  75. } else {
  76. $this->accountManager->expects($this->never())->method('updateUser');
  77. }
  78. }
  79. $this->hooks->changeUserHook($params);
  80. }
  81. public function dataTestChangeUserHook() {
  82. $user = $this->createMock(IUser::class);
  83. return [
  84. [
  85. ['feature' => '', 'value' => ''],
  86. [
  87. AccountManager::PROPERTY_EMAIL => ['value' => ''],
  88. AccountManager::PROPERTY_DISPLAYNAME => ['value' => '']
  89. ],
  90. false, false, true
  91. ],
  92. [
  93. ['user' => $user, 'value' => ''],
  94. [
  95. AccountManager::PROPERTY_EMAIL => ['value' => ''],
  96. AccountManager::PROPERTY_DISPLAYNAME => ['value' => '']
  97. ],
  98. false, false, true
  99. ],
  100. [
  101. ['user' => $user, 'feature' => ''],
  102. [
  103. AccountManager::PROPERTY_EMAIL => ['value' => ''],
  104. AccountManager::PROPERTY_DISPLAYNAME => ['value' => '']
  105. ],
  106. false, false, true
  107. ],
  108. [
  109. ['user' => $user, 'feature' => 'foo', 'value' => 'bar'],
  110. [
  111. AccountManager::PROPERTY_EMAIL => ['value' => 'oldMail@example.com'],
  112. AccountManager::PROPERTY_DISPLAYNAME => ['value' => 'oldDisplayName']
  113. ],
  114. false, false, false
  115. ],
  116. [
  117. ['user' => $user, 'feature' => 'eMailAddress', 'value' => 'newMail@example.com'],
  118. [
  119. AccountManager::PROPERTY_EMAIL => ['value' => 'oldMail@example.com'],
  120. AccountManager::PROPERTY_DISPLAYNAME => ['value' => 'oldDisplayName']
  121. ],
  122. true, false, false
  123. ],
  124. [
  125. ['user' => $user, 'feature' => 'displayName', 'value' => 'newDisplayName'],
  126. [
  127. AccountManager::PROPERTY_EMAIL => ['value' => 'oldMail@example.com'],
  128. AccountManager::PROPERTY_DISPLAYNAME => ['value' => 'oldDisplayName']
  129. ],
  130. false, true, false
  131. ],
  132. ];
  133. }
  134. public function testGetAccountManager() {
  135. $hooks = new Hooks($this->logger);
  136. $result = $this->invokePrivate($hooks, 'getAccountManager');
  137. $this->assertInstanceOf(AccountManager::class, $result);
  138. }
  139. }