AccountsManagerTest.php 6.7 KB

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