PredefinedStatusServiceTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\UserStatus\Tests\Service;
  8. use OCA\UserStatus\Service\PredefinedStatusService;
  9. use OCP\IL10N;
  10. use Test\TestCase;
  11. class PredefinedStatusServiceTest extends TestCase {
  12. /** @var IL10N|\PHPUnit\Framework\MockObject\MockObject */
  13. protected $l10n;
  14. /** @var PredefinedStatusService */
  15. protected $service;
  16. protected function setUp(): void {
  17. parent::setUp();
  18. $this->l10n = $this->createMock(IL10N::class);
  19. $this->service = new PredefinedStatusService($this->l10n);
  20. }
  21. public function testGetDefaultStatuses(): void {
  22. $this->l10n->expects($this->exactly(7))
  23. ->method('t')
  24. ->willReturnCallback(function ($text, $parameters = []) {
  25. return vsprintf($text, $parameters);
  26. });
  27. $actual = $this->service->getDefaultStatuses();
  28. $this->assertEquals([
  29. [
  30. 'id' => 'meeting',
  31. 'icon' => '📅',
  32. 'message' => 'In a meeting',
  33. 'clearAt' => [
  34. 'type' => 'period',
  35. 'time' => 3600,
  36. ],
  37. ],
  38. [
  39. 'id' => 'commuting',
  40. 'icon' => '🚌',
  41. 'message' => 'Commuting',
  42. 'clearAt' => [
  43. 'type' => 'period',
  44. 'time' => 1800,
  45. ],
  46. ],
  47. [
  48. 'id' => 'remote-work',
  49. 'icon' => '🏡',
  50. 'message' => 'Working remotely',
  51. 'clearAt' => [
  52. 'type' => 'end-of',
  53. 'time' => 'day',
  54. ],
  55. ],
  56. [
  57. 'id' => 'sick-leave',
  58. 'icon' => '🤒',
  59. 'message' => 'Out sick',
  60. 'clearAt' => [
  61. 'type' => 'end-of',
  62. 'time' => 'day',
  63. ],
  64. ],
  65. [
  66. 'id' => 'vacationing',
  67. 'icon' => '🌴',
  68. 'message' => 'Vacationing',
  69. 'clearAt' => null,
  70. ],
  71. [
  72. 'id' => 'call',
  73. 'icon' => '💬',
  74. 'message' => 'In a call',
  75. 'clearAt' => null,
  76. 'visible' => false,
  77. ],
  78. [
  79. 'id' => 'out-of-office',
  80. 'icon' => '🛑',
  81. 'message' => 'Out of office',
  82. 'clearAt' => null,
  83. 'visible' => false,
  84. ],
  85. ], $actual);
  86. }
  87. /**
  88. * @param string $id
  89. * @param string|null $expectedIcon
  90. *
  91. * @dataProvider getIconForIdDataProvider
  92. */
  93. public function testGetIconForId(string $id, ?string $expectedIcon): void {
  94. $actual = $this->service->getIconForId($id);
  95. $this->assertEquals($expectedIcon, $actual);
  96. }
  97. /**
  98. * @return array
  99. */
  100. public function getIconForIdDataProvider(): array {
  101. return [
  102. ['meeting', '📅'],
  103. ['commuting', '🚌'],
  104. ['sick-leave', '🤒'],
  105. ['vacationing', '🌴'],
  106. ['remote-work', '🏡'],
  107. ['call', '💬'],
  108. ['unknown-id', null],
  109. ];
  110. }
  111. /**
  112. * @param string $id
  113. * @param string|null $expected
  114. *
  115. * @dataProvider getTranslatedStatusForIdDataProvider
  116. */
  117. public function testGetTranslatedStatusForId(string $id, ?string $expected): void {
  118. $this->l10n->method('t')
  119. ->willReturnArgument(0);
  120. $actual = $this->service->getTranslatedStatusForId($id);
  121. $this->assertEquals($expected, $actual);
  122. }
  123. /**
  124. * @return array
  125. */
  126. public function getTranslatedStatusForIdDataProvider(): array {
  127. return [
  128. ['meeting', 'In a meeting'],
  129. ['commuting', 'Commuting'],
  130. ['sick-leave', 'Out sick'],
  131. ['vacationing', 'Vacationing'],
  132. ['remote-work', 'Working remotely'],
  133. ['call', 'In a call'],
  134. ['unknown-id', null],
  135. ];
  136. }
  137. /**
  138. * @param string $id
  139. * @param bool $expected
  140. *
  141. * @dataProvider isValidIdDataProvider
  142. */
  143. public function testIsValidId(string $id, bool $expected): void {
  144. $actual = $this->service->isValidId($id);
  145. $this->assertEquals($expected, $actual);
  146. }
  147. /**
  148. * @return array
  149. */
  150. public function isValidIdDataProvider(): array {
  151. return [
  152. ['meeting', true],
  153. ['commuting', true],
  154. ['sick-leave', true],
  155. ['vacationing', true],
  156. ['remote-work', true],
  157. ['call', true],
  158. ['unknown-id', false],
  159. ];
  160. }
  161. public function testGetDefaultStatusById(): void {
  162. $this->l10n->expects($this->exactly(7))
  163. ->method('t')
  164. ->willReturnCallback(function ($text, $parameters = []) {
  165. return vsprintf($text, $parameters);
  166. });
  167. $this->assertEquals([
  168. 'id' => 'call',
  169. 'icon' => '💬',
  170. 'message' => 'In a call',
  171. 'clearAt' => null,
  172. 'visible' => false,
  173. ], $this->service->getDefaultStatusById('call'));
  174. }
  175. public function testGetDefaultStatusByUnknownId(): void {
  176. $this->assertNull($this->service->getDefaultStatusById('unknown'));
  177. }
  178. }