1
0

AccountsManagerTest.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. <?php
  2. /**
  3. * @author Björn Schießle <schiessle@owncloud.com>
  4. *
  5. * @copyright Copyright (c) 2016, ownCloud, Inc.
  6. * @license AGPL-3.0
  7. *
  8. * This code is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License, version 3,
  10. * as published by the Free Software Foundation.
  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, version 3,
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>
  19. *
  20. */
  21. namespace Test\Accounts;
  22. use OC\Accounts\Account;
  23. use OC\Accounts\AccountManager;
  24. use OCP\Accounts\IAccountManager;
  25. use OCP\BackgroundJob\IJobList;
  26. use OCP\ILogger;
  27. use OCP\IUser;
  28. use PHPUnit\Framework\MockObject\MockObject;
  29. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  30. use Symfony\Component\EventDispatcher\GenericEvent;
  31. use Test\TestCase;
  32. /**
  33. * Class AccountsManagerTest
  34. *
  35. * @group DB
  36. * @package Test\Accounts
  37. */
  38. class AccountsManagerTest extends TestCase {
  39. /** @var \OCP\IDBConnection */
  40. private $connection;
  41. /** @var EventDispatcherInterface|MockObject */
  42. private $eventDispatcher;
  43. /** @var IJobList|MockObject */
  44. private $jobList;
  45. /** @var string accounts table name */
  46. private $table = 'accounts';
  47. /** @var ILogger|MockObject */
  48. private $logger;
  49. protected function setUp(): void {
  50. parent::setUp();
  51. $this->eventDispatcher = $this->createMock(EventDispatcherInterface::class);
  52. $this->connection = \OC::$server->getDatabaseConnection();
  53. $this->jobList = $this->createMock(IJobList::class);
  54. $this->logger = $this->createMock(ILogger::class);
  55. }
  56. protected function tearDown(): void {
  57. parent::tearDown();
  58. $query = $this->connection->getQueryBuilder();
  59. $query->delete($this->table)->execute();
  60. }
  61. /**
  62. * get a instance of the accountManager
  63. *
  64. * @param array $mockedMethods list of methods which should be mocked
  65. * @return MockObject | AccountManager
  66. */
  67. public function getInstance($mockedMethods = null) {
  68. return $this->getMockBuilder(AccountManager::class)
  69. ->setConstructorArgs([$this->connection, $this->eventDispatcher, $this->jobList, $this->logger])
  70. ->setMethods($mockedMethods)
  71. ->getMock();
  72. }
  73. /**
  74. * @dataProvider dataTrueFalse
  75. *
  76. * @param array $newData
  77. * @param array $oldData
  78. * @param bool $insertNew
  79. * @param bool $updateExisting
  80. */
  81. public function testUpdateUser($newData, $oldData, $insertNew, $updateExisting) {
  82. $accountManager = $this->getInstance(['getUser', 'insertNewUser', 'updateExistingUser', 'updateVerifyStatus', 'checkEmailVerification']);
  83. /** @var IUser $user */
  84. $user = $this->createMock(IUser::class);
  85. $accountManager->expects($this->once())->method('getUser')->with($user)->willReturn($oldData);
  86. if ($updateExisting) {
  87. $accountManager->expects($this->once())->method('checkEmailVerification')
  88. ->with($oldData, $newData, $user)->willReturn($newData);
  89. $accountManager->expects($this->once())->method('updateVerifyStatus')
  90. ->with($oldData, $newData)->willReturn($newData);
  91. $accountManager->expects($this->once())->method('updateExistingUser')
  92. ->with($user, $newData);
  93. $accountManager->expects($this->never())->method('insertNewUser');
  94. }
  95. if ($insertNew) {
  96. $accountManager->expects($this->once())->method('insertNewUser')
  97. ->with($user, $newData);
  98. $accountManager->expects($this->never())->method('updateExistingUser');
  99. }
  100. if (!$insertNew && !$updateExisting) {
  101. $accountManager->expects($this->never())->method('updateExistingUser');
  102. $accountManager->expects($this->never())->method('checkEmailVerification');
  103. $accountManager->expects($this->never())->method('updateVerifyStatus');
  104. $accountManager->expects($this->never())->method('insertNewUser');
  105. $this->eventDispatcher->expects($this->never())->method('dispatch');
  106. } else {
  107. $this->eventDispatcher->expects($this->once())->method('dispatch')
  108. ->willReturnCallback(
  109. function ($eventName, $event) use ($user, $newData) {
  110. $this->assertSame('OC\AccountManager::userUpdated', $eventName);
  111. $this->assertInstanceOf(GenericEvent::class, $event);
  112. /** @var GenericEvent $event */
  113. $this->assertSame($user, $event->getSubject());
  114. $this->assertSame($newData, $event->getArguments());
  115. }
  116. );
  117. }
  118. $accountManager->updateUser($user, $newData);
  119. }
  120. public function dataTrueFalse() {
  121. return [
  122. [['newData'], ['oldData'], false, true],
  123. [['newData'], [], true, false],
  124. [['oldData'], ['oldData'], false, false]
  125. ];
  126. }
  127. /**
  128. * @dataProvider dataTestGetUser
  129. *
  130. * @param string $setUser
  131. * @param array $setData
  132. * @param IUser $askUser
  133. * @param array $expectedData
  134. * @param bool $userAlreadyExists
  135. */
  136. public function testGetUser($setUser, $setData, $askUser, $expectedData, $userAlreadyExists) {
  137. $accountManager = $this->getInstance(['buildDefaultUserRecord', 'insertNewUser', 'addMissingDefaultValues']);
  138. if (!$userAlreadyExists) {
  139. $accountManager->expects($this->once())->method('buildDefaultUserRecord')
  140. ->with($askUser)->willReturn($expectedData);
  141. $accountManager->expects($this->once())->method('insertNewUser')
  142. ->with($askUser, $expectedData);
  143. }
  144. if (empty($expectedData)) {
  145. $accountManager->expects($this->never())->method('addMissingDefaultValues');
  146. } else {
  147. $accountManager->expects($this->once())->method('addMissingDefaultValues')->with($expectedData)
  148. ->willReturn($expectedData);
  149. }
  150. $this->addDummyValuesToTable($setUser, $setData);
  151. $this->assertEquals($expectedData,
  152. $accountManager->getUser($askUser)
  153. );
  154. }
  155. public function dataTestGetUser() {
  156. $user1 = $this->getMockBuilder(IUser::class)->getMock();
  157. $user1->expects($this->any())->method('getUID')->willReturn('user1');
  158. $user2 = $this->getMockBuilder(IUser::class)->getMock();
  159. $user2->expects($this->any())->method('getUID')->willReturn('user2');
  160. return [
  161. ['user1', ['key' => 'value'], $user1, ['key' => 'value'], true],
  162. ['user1', ['key' => 'value'], $user2, [], false],
  163. ];
  164. }
  165. public function testUpdateExistingUser() {
  166. $user = $this->getMockBuilder(IUser::class)->getMock();
  167. $user->expects($this->once())->method('getUID')->willReturn('uid');
  168. $oldData = ['key' => 'value'];
  169. $newData = ['newKey' => 'newValue'];
  170. $accountManager = $this->getInstance();
  171. $this->addDummyValuesToTable('uid', $oldData);
  172. $this->invokePrivate($accountManager, 'updateExistingUser', [$user, $newData]);
  173. $newDataFromTable = $this->getDataFromTable('uid');
  174. $this->assertEquals($newData, $newDataFromTable);
  175. }
  176. public function testInsertNewUser() {
  177. $user = $this->getMockBuilder(IUser::class)->getMock();
  178. $uid = 'uid';
  179. $data = ['key' => 'value'];
  180. $accountManager = $this->getInstance();
  181. $user->expects($this->once())->method('getUID')->willReturn($uid);
  182. $this->assertNull($this->getDataFromTable($uid));
  183. $this->invokePrivate($accountManager, 'insertNewUser', [$user, $data]);
  184. $dataFromDb = $this->getDataFromTable($uid);
  185. $this->assertEquals($data, $dataFromDb);
  186. }
  187. public function testAddMissingDefaultValues() {
  188. $accountManager = $this->getInstance();
  189. $input = [
  190. 'key1' => ['value' => 'value1', 'verified' => '0'],
  191. 'key2' => ['value' => 'value1'],
  192. ];
  193. $expected = [
  194. 'key1' => ['value' => 'value1', 'verified' => '0'],
  195. 'key2' => ['value' => 'value1', 'verified' => '0'],
  196. ];
  197. $result = $this->invokePrivate($accountManager, 'addMissingDefaultValues', [$input]);
  198. $this->assertSame($expected, $result);
  199. }
  200. private function addDummyValuesToTable($uid, $data) {
  201. $query = $this->connection->getQueryBuilder();
  202. $query->insert($this->table)
  203. ->values(
  204. [
  205. 'uid' => $query->createNamedParameter($uid),
  206. 'data' => $query->createNamedParameter(json_encode($data)),
  207. ]
  208. )
  209. ->execute();
  210. }
  211. private function getDataFromTable($uid) {
  212. $query = $this->connection->getQueryBuilder();
  213. $query->select('data')->from($this->table)
  214. ->where($query->expr()->eq('uid', $query->createParameter('uid')))
  215. ->setParameter('uid', $uid);
  216. $query->execute();
  217. $qResult = $query->execute();
  218. $result = $qResult->fetchAll();
  219. $qResult->closeCursor();
  220. if (!empty($result)) {
  221. return json_decode($result[0]['data'], true);
  222. }
  223. }
  224. public function testGetAccount() {
  225. $accountManager = $this->getInstance(['getUser']);
  226. /** @var IUser $user */
  227. $user = $this->createMock(IUser::class);
  228. $data = [
  229. IAccountManager::PROPERTY_TWITTER =>
  230. [
  231. 'value' => '@twitterhandle',
  232. 'scope' => IAccountManager::VISIBILITY_PRIVATE,
  233. 'verified' => IAccountManager::NOT_VERIFIED,
  234. ],
  235. IAccountManager::PROPERTY_EMAIL =>
  236. [
  237. 'value' => 'test@example.com',
  238. 'scope' => IAccountManager::VISIBILITY_PUBLIC,
  239. 'verified' => IAccountManager::VERIFICATION_IN_PROGRESS,
  240. ],
  241. IAccountManager::PROPERTY_WEBSITE =>
  242. [
  243. 'value' => 'https://example.com',
  244. 'scope' => IAccountManager::VISIBILITY_CONTACTS_ONLY,
  245. 'verified' => IAccountManager::VERIFIED,
  246. ],
  247. ];
  248. $expected = new Account($user);
  249. $expected->setProperty(IAccountManager::PROPERTY_TWITTER, '@twitterhandle', IAccountManager::VISIBILITY_PRIVATE, IAccountManager::NOT_VERIFIED);
  250. $expected->setProperty(IAccountManager::PROPERTY_EMAIL, 'test@example.com', IAccountManager::VISIBILITY_PUBLIC, IAccountManager::VERIFICATION_IN_PROGRESS);
  251. $expected->setProperty(IAccountManager::PROPERTY_WEBSITE, 'https://example.com', IAccountManager::VISIBILITY_CONTACTS_ONLY, IAccountManager::VERIFIED);
  252. $accountManager->expects($this->once())
  253. ->method('getUser')
  254. ->willReturn($data);
  255. $this->assertEquals($expected, $accountManager->getAccount($user));
  256. }
  257. }