AccountsManagerTest.php 8.2 KB

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