AccountsManagerTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 OC\Mail\Mailer;
  24. use OCP\IUser;
  25. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  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. * @param bool $userAlreadyExists
  67. */
  68. public function testUpdateUser($newData, $oldData, $insertNew, $updateExisitng) {
  69. $accountManager = $this->getInstance(['getUser', 'insertNewUser', 'updateExistingUser']);
  70. $user = $this->getMockBuilder('OCP\IUser')->getMock();
  71. $accountManager->expects($this->once())->method('getUser')->with($user)->willReturn($oldData);
  72. if ($updateExisitng) {
  73. $accountManager->expects($this->once())->method('updateExistingUser')
  74. ->with($user, $newData);
  75. $accountManager->expects($this->never())->method('insertNewUser');
  76. }
  77. if ($insertNew) {
  78. $accountManager->expects($this->once())->method('insertNewUser')
  79. ->with($user, $newData);
  80. $accountManager->expects($this->never())->method('updateExistingUser');
  81. }
  82. if (!$insertNew && !$updateExisitng) {
  83. $accountManager->expects($this->never())->method('updateExistingUser');
  84. $accountManager->expects($this->never())->method('insertNewUser');
  85. $this->eventDispatcher->expects($this->never())->method('dispatch');
  86. } else {
  87. $this->eventDispatcher->expects($this->once())->method('dispatch')
  88. ->willReturnCallback(
  89. function ($eventName, $event) use ($user) {
  90. $this->assertSame('OC\AccountManager::userUpdated', $eventName);
  91. $this->assertInstanceOf('Symfony\Component\EventDispatcher\GenericEvent', $event);
  92. }
  93. );
  94. }
  95. $accountManager->updateUser($user, $newData);
  96. }
  97. public function dataTrueFalse() {
  98. return [
  99. [['newData'], ['oldData'], false, true],
  100. [['newData'], [], true, false],
  101. [['oldData'], ['oldData'], false, false]
  102. ];
  103. }
  104. /**
  105. * @dataProvider dataTestGetUser
  106. *
  107. * @param string $setUser
  108. * @param array $setData
  109. * @param IUser $askUser
  110. * @param array $expectedData
  111. * @param book $userAlreadyExists
  112. */
  113. public function testGetUser($setUser, $setData, $askUser, $expectedData, $userAlreadyExists) {
  114. $accountManager = $this->getInstance(['buildDefaultUserRecord', 'insertNewUser']);
  115. if (!$userAlreadyExists) {
  116. $accountManager->expects($this->once())->method('buildDefaultUserRecord')
  117. ->with($askUser)->willReturn($expectedData);
  118. $accountManager->expects($this->once())->method('insertNewUser')
  119. ->with($askUser, $expectedData);
  120. }
  121. $this->addDummyValuesToTable($setUser, $setData);
  122. $this->assertEquals($expectedData,
  123. $accountManager->getUser($askUser)
  124. );
  125. }
  126. public function dataTestGetUser() {
  127. $user1 = $this->getMockBuilder('OCP\IUser')->getMock();
  128. $user1->expects($this->any())->method('getUID')->willReturn('user1');
  129. $user2 = $this->getMockBuilder('OCP\IUser')->getMock();
  130. $user2->expects($this->any())->method('getUID')->willReturn('user2');
  131. return [
  132. ['user1', ['key' => 'value'], $user1, ['key' => 'value'], true],
  133. ['user1', ['key' => 'value'], $user2, [], false],
  134. ];
  135. }
  136. public function testUpdateExistingUser() {
  137. $user = $this->getMockBuilder('OCP\IUser')->getMock();
  138. $user->expects($this->once())->method('getUID')->willReturn('uid');
  139. $oldData = ['key' => 'value'];
  140. $newData = ['newKey' => 'newValue'];
  141. $accountManager = $this->getInstance();
  142. $this->addDummyValuesToTable('uid', $oldData);
  143. $this->invokePrivate($accountManager, 'updateExistingUser', [$user, $newData]);
  144. $newDataFromTable = $this->getDataFromTable('uid');
  145. $this->assertEquals($newData, $newDataFromTable);
  146. }
  147. public function testInsertNewUser() {
  148. $user = $this->getMockBuilder('OCP\IUser')->getMock();
  149. $uid = 'uid';
  150. $data = ['key' => 'value'];
  151. $accountManager = $this->getInstance();
  152. $user->expects($this->once())->method('getUID')->willReturn($uid);
  153. $this->assertNull($this->getDataFromTable($uid));
  154. $this->invokePrivate($accountManager, 'insertNewUser', [$user, $data]);
  155. $dataFromDb = $this->getDataFromTable($uid);
  156. $this->assertEquals($data, $dataFromDb);
  157. }
  158. private function addDummyValuesToTable($uid, $data) {
  159. $query = $this->connection->getQueryBuilder();
  160. $query->insert($this->table)
  161. ->values(
  162. [
  163. 'uid' => $query->createNamedParameter($uid),
  164. 'data' => $query->createNamedParameter(json_encode($data)),
  165. ]
  166. )
  167. ->execute();
  168. }
  169. private function getDataFromTable($uid) {
  170. $query = $this->connection->getQueryBuilder();
  171. $query->select('data')->from($this->table)
  172. ->where($query->expr()->eq('uid', $query->createParameter('uid')))
  173. ->setParameter('uid', $uid);
  174. $query->execute();
  175. $result = $query->execute()->fetchAll();
  176. if (!empty($result)) {
  177. return json_decode($result[0]['data'], true);
  178. }
  179. }
  180. }