UserStatusWidgetTest.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2020, Georg Ehrke
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Georg Ehrke <oc.list@georgehrke.com>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OCA\UserStatus\Tests\Dashboard;
  26. use OCA\UserStatus\Dashboard\UserStatusWidget;
  27. use OCA\UserStatus\Db\UserStatus;
  28. use OCA\UserStatus\Service\StatusService;
  29. use OCP\AppFramework\Services\IInitialState;
  30. use OCP\IDateTimeFormatter;
  31. use OCP\IL10N;
  32. use OCP\IURLGenerator;
  33. use OCP\IUser;
  34. use OCP\IUserManager;
  35. use OCP\IUserSession;
  36. use Test\TestCase;
  37. class UserStatusWidgetTest extends TestCase {
  38. /** @var IL10N|\PHPUnit\Framework\MockObject\MockObject */
  39. private $l10n;
  40. /** @var IDateTimeFormatter|\PHPUnit\Framework\MockObject\MockObject */
  41. private $dateTimeFormatter;
  42. /** @var IURLGenerator|\PHPUnit\Framework\MockObject\MockObject */
  43. private $urlGenerator;
  44. /** @var IInitialState|\PHPUnit\Framework\MockObject\MockObject */
  45. private $initialState;
  46. /** @var IUserManager|\PHPUnit\Framework\MockObject\MockObject */
  47. private $userManager;
  48. /** @var IUserSession|\PHPUnit\Framework\MockObject\MockObject */
  49. private $userSession;
  50. /** @var StatusService|\PHPUnit\Framework\MockObject\MockObject */
  51. private $service;
  52. /** @var UserStatusWidget */
  53. private $widget;
  54. protected function setUp(): void {
  55. parent::setUp();
  56. $this->l10n = $this->createMock(IL10N::class);
  57. $this->dateTimeFormatter = $this->createMock(IDateTimeFormatter::class);
  58. $this->urlGenerator = $this->createMock(IURLGenerator::class);
  59. $this->initialState = $this->createMock(IInitialState::class);
  60. $this->userManager = $this->createMock(IUserManager::class);
  61. $this->userSession = $this->createMock(IUserSession::class);
  62. $this->service = $this->createMock(StatusService::class);
  63. $this->widget = new UserStatusWidget($this->l10n, $this->dateTimeFormatter, $this->urlGenerator, $this->initialState, $this->userManager, $this->userSession, $this->service);
  64. }
  65. public function testGetId(): void {
  66. $this->assertEquals('user_status', $this->widget->getId());
  67. }
  68. public function testGetTitle(): void {
  69. $this->l10n->expects($this->exactly(1))
  70. ->method('t')
  71. ->willReturnArgument(0);
  72. $this->assertEquals('Recent statuses', $this->widget->getTitle());
  73. }
  74. public function testGetOrder(): void {
  75. $this->assertEquals(5, $this->widget->getOrder());
  76. }
  77. public function testGetIconClass(): void {
  78. $this->assertEquals('icon-user-status', $this->widget->getIconClass());
  79. }
  80. public function testGetUrl(): void {
  81. $this->assertNull($this->widget->getUrl());
  82. }
  83. public function testLoadNoUserSession(): void {
  84. $this->userSession->expects($this->once())
  85. ->method('getUser')
  86. ->willReturn(null);
  87. $this->initialState->expects($this->once())
  88. ->method('provideInitialState')
  89. ->with('dashboard_data', []);
  90. $this->service->expects($this->never())
  91. ->method('findAllRecentStatusChanges');
  92. $this->widget->load();
  93. }
  94. public function testLoadWithCurrentUser(): void {
  95. $user = $this->createMock(IUser::class);
  96. $user->method('getUid')->willReturn('john.doe');
  97. $this->userSession->expects($this->once())
  98. ->method('getUser')
  99. ->willReturn($user);
  100. $user1 = $this->createMock(IUser::class);
  101. $user1->method('getDisplayName')->willReturn('User No. 1');
  102. $this->userManager
  103. ->method('get')
  104. ->willReturnMap([
  105. ['user_1', $user1],
  106. ['user_2', null],
  107. ['user_3', null],
  108. ['user_4', null],
  109. ['user_5', null],
  110. ['user_6', null],
  111. ['user_7', null],
  112. ]);
  113. $userStatuses = [
  114. UserStatus::fromParams([
  115. 'userId' => 'user_1',
  116. 'status' => 'online',
  117. 'customIcon' => '💻',
  118. 'customMessage' => 'Working',
  119. 'statusTimestamp' => 5000,
  120. ]),
  121. UserStatus::fromParams([
  122. 'userId' => 'user_2',
  123. 'status' => 'away',
  124. 'customIcon' => '☕️',
  125. 'customMessage' => 'Office Hangout',
  126. 'statusTimestamp' => 6000,
  127. ]),
  128. UserStatus::fromParams([
  129. 'userId' => 'user_3',
  130. 'status' => 'dnd',
  131. 'customIcon' => null,
  132. 'customMessage' => null,
  133. 'statusTimestamp' => 7000,
  134. ]),
  135. UserStatus::fromParams([
  136. 'userId' => 'john.doe',
  137. 'status' => 'away',
  138. 'customIcon' => '☕️',
  139. 'customMessage' => 'Office Hangout',
  140. 'statusTimestamp' => 90000,
  141. ]),
  142. UserStatus::fromParams([
  143. 'userId' => 'user_4',
  144. 'status' => 'dnd',
  145. 'customIcon' => null,
  146. 'customMessage' => null,
  147. 'statusTimestamp' => 7000,
  148. ]),
  149. UserStatus::fromParams([
  150. 'userId' => 'user_5',
  151. 'status' => 'invisible',
  152. 'customIcon' => '🏝',
  153. 'customMessage' => 'On vacation',
  154. 'statusTimestamp' => 7000,
  155. ]),
  156. UserStatus::fromParams([
  157. 'userId' => 'user_6',
  158. 'status' => 'offline',
  159. 'customIcon' => null,
  160. 'customMessage' => null,
  161. 'statusTimestamp' => 7000,
  162. ]),
  163. UserStatus::fromParams([
  164. 'userId' => 'user_7',
  165. 'status' => 'invisible',
  166. 'customIcon' => null,
  167. 'customMessage' => null,
  168. 'statusTimestamp' => 7000,
  169. ]),
  170. ];
  171. $this->service->expects($this->once())
  172. ->method('findAllRecentStatusChanges')
  173. ->with(8, 0)
  174. ->willReturn($userStatuses);
  175. $this->initialState->expects($this->once())
  176. ->method('provideInitialState')
  177. ->with('dashboard_data', $this->callback(function ($data): bool {
  178. $this->assertEquals([
  179. [
  180. 'userId' => 'user_1',
  181. 'displayName' => 'User No. 1',
  182. 'status' => 'online',
  183. 'icon' => '💻',
  184. 'message' => 'Working',
  185. 'timestamp' => 5000,
  186. ],
  187. [
  188. 'userId' => 'user_2',
  189. 'displayName' => 'user_2',
  190. 'status' => 'away',
  191. 'icon' => '☕️',
  192. 'message' => 'Office Hangout',
  193. 'timestamp' => 6000,
  194. ],
  195. [
  196. 'userId' => 'user_3',
  197. 'displayName' => 'user_3',
  198. 'status' => 'dnd',
  199. 'icon' => null,
  200. 'message' => null,
  201. 'timestamp' => 7000,
  202. ],
  203. [
  204. 'userId' => 'user_4',
  205. 'displayName' => 'user_4',
  206. 'status' => 'dnd',
  207. 'icon' => null,
  208. 'message' => null,
  209. 'timestamp' => 7000,
  210. ],
  211. [
  212. 'userId' => 'user_5',
  213. 'displayName' => 'user_5',
  214. 'status' => 'offline',
  215. 'icon' => '🏝',
  216. 'message' => 'On vacation',
  217. 'timestamp' => 7000,
  218. ],
  219. [
  220. 'userId' => 'user_6',
  221. 'displayName' => 'user_6',
  222. 'status' => 'offline',
  223. 'icon' => null,
  224. 'message' => null,
  225. 'timestamp' => 7000,
  226. ],
  227. [
  228. 'userId' => 'user_7',
  229. 'displayName' => 'user_7',
  230. 'status' => 'offline',
  231. 'icon' => null,
  232. 'message' => null,
  233. 'timestamp' => 7000,
  234. ],
  235. ], $data);
  236. return true;
  237. }));
  238. $this->widget->load();
  239. }
  240. }