AccountsManagerTest.php 11 KB

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