LocalTimeProviderTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2023, Joas Schilling <coding@schilljs.com>
  5. *
  6. * @author Joas Schilling <coding@schilljs.com>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace lib\Contacts\ContactsMenu\Providers;
  25. use OC\Contacts\ContactsMenu\Providers\LocalTimeProvider;
  26. use OCP\AppFramework\Utility\ITimeFactory;
  27. use OCP\Contacts\ContactsMenu\IActionFactory;
  28. use OCP\Contacts\ContactsMenu\IEntry;
  29. use OCP\Contacts\ContactsMenu\ILinkAction;
  30. use OCP\IConfig;
  31. use OCP\IDateTimeFormatter;
  32. use OCP\IL10N;
  33. use OCP\IURLGenerator;
  34. use OCP\IUser;
  35. use OCP\IUserManager;
  36. use OCP\L10N\IFactory as IL10NFactory;
  37. use PHPUnit\Framework\MockObject\MockObject;
  38. use Test\TestCase;
  39. class LocalTimeProviderTest extends TestCase {
  40. /** @var IActionFactory|MockObject */
  41. private $actionFactory;
  42. /** @var IL10N|MockObject */
  43. private $l;
  44. /** @var IL10NFactory|MockObject */
  45. private $l10nFactory;
  46. /** @var IURLGenerator|MockObject */
  47. private $urlGenerator;
  48. /** @var IUserManager|MockObject */
  49. private $userManager;
  50. /** @var ITimeFactory|MockObject */
  51. private $timeFactory;
  52. /** @var IDateTimeFormatter|MockObject */
  53. private $dateTimeFormatter;
  54. /** @var IConfig|MockObject */
  55. private $config;
  56. private LocalTimeProvider $provider;
  57. protected function setUp(): void {
  58. parent::setUp();
  59. $this->actionFactory = $this->createMock(IActionFactory::class);
  60. $this->l10nFactory = $this->createMock(IL10NFactory::class);
  61. $this->l = $this->createMock(IL10N::class);
  62. $this->l->expects($this->any())
  63. ->method('t')
  64. ->will($this->returnCallback(function ($text, $parameters = []) {
  65. return vsprintf($text, $parameters);
  66. }));
  67. $this->urlGenerator = $this->createMock(IURLGenerator::class);
  68. $this->userManager = $this->createMock(IUserManager::class);
  69. $this->timeFactory = $this->createMock(ITimeFactory::class);
  70. $this->dateTimeFormatter = $this->createMock(IDateTimeFormatter::class);
  71. $this->config = $this->createMock(IConfig::class);
  72. $this->provider = new LocalTimeProvider(
  73. $this->actionFactory,
  74. $this->l10nFactory,
  75. $this->urlGenerator,
  76. $this->userManager,
  77. $this->timeFactory,
  78. $this->dateTimeFormatter,
  79. $this->config
  80. );
  81. }
  82. public function testProcess(): void {
  83. $entry = $this->createMock(IEntry::class);
  84. $entry->expects($this->once())
  85. ->method('getProperty')
  86. ->with('UID')
  87. ->willReturn('user1');
  88. $user = $this->createMock(IUser::class);
  89. $user->method('getUID')
  90. ->willReturn('user1');
  91. $this->userManager->expects($this->once())
  92. ->method('get')
  93. ->with('user1')
  94. ->willReturn($user);
  95. $this->l10nFactory->method('get')
  96. ->with('lib')
  97. ->willReturn($this->l);
  98. $this->config->method('getUserValue')
  99. ->with('user1', 'core', 'timezone')
  100. ->willReturn('America/Los_Angeles');
  101. $now = new \DateTime('2023-01-04 10:24:43');
  102. $this->timeFactory->method('getDateTime')
  103. ->willReturn($now);
  104. $now = new \DateTime('2023-01-04 10:24:43');
  105. $this->dateTimeFormatter->method('formatTime')
  106. ->with($now, 'short', $this->anything())
  107. ->willReturn('01:24');
  108. $this->urlGenerator->method('imagePath')
  109. ->willReturn('actions/recent.svg');
  110. $this->urlGenerator->method('getAbsoluteURL')
  111. ->with('actions/recent.svg')
  112. ->willReturn('https://localhost/actions/recent.svg');
  113. $action = $this->createMock(ILinkAction::class);
  114. $this->actionFactory->expects($this->once())
  115. ->method('newLinkAction')
  116. ->with(
  117. 'https://localhost/actions/recent.svg',
  118. 'Local time: 01:24',
  119. '#',
  120. 'timezone'
  121. )
  122. ->willReturn($action);
  123. $entry->expects($this->once())
  124. ->method('addAction')
  125. ->with($action);
  126. $this->provider->process($entry);
  127. }
  128. public function testProcessNoUser(): void {
  129. $entry = $this->createMock(IEntry::class);
  130. $entry->expects($this->once())
  131. ->method('getProperty')
  132. ->with('UID')
  133. ->willReturn('user1');
  134. $user = $this->createMock(IUser::class);
  135. $user->method('getUID')
  136. ->willReturn(null);
  137. $entry->expects($this->never())
  138. ->method('addAction');
  139. $this->provider->process($entry);
  140. }
  141. }