AccountsManagerTest.php 9.6 KB

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