LocalTimeProviderTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 IURLGenerator|MockObject */
  45. private $urlGenerator;
  46. /** @var IUserManager|MockObject */
  47. private $userManager;
  48. /** @var ITimeFactory|MockObject */
  49. private $timeFactory;
  50. /** @var IDateTimeFormatter|MockObject */
  51. private $dateTimeFormatter;
  52. /** @var IConfig|MockObject */
  53. private $config;
  54. private LocalTimeProvider $provider;
  55. protected function setUp(): void {
  56. parent::setUp();
  57. $this->actionFactory = $this->createMock(IActionFactory::class);
  58. $this->l10nFactory = $this->createMock(IL10NFactory::class);
  59. $this->l = $this->createMock(IL10N::class);
  60. $this->l->expects($this->any())
  61. ->method('t')
  62. ->will($this->returnCallback(function ($text, $parameters = []) {
  63. return vsprintf($text, $parameters);
  64. }));
  65. $this->urlGenerator = $this->createMock(IURLGenerator::class);
  66. $this->userManager = $this->createMock(IUserManager::class);
  67. $this->timeFactory = $this->createMock(ITimeFactory::class);
  68. $this->dateTimeFormatter = $this->createMock(IDateTimeFormatter::class);
  69. $this->config = $this->createMock(IConfig::class);
  70. $this->provider = new LocalTimeProvider(
  71. $this->actionFactory,
  72. $this->l10nFactory,
  73. $this->urlGenerator,
  74. $this->userManager,
  75. $this->timeFactory,
  76. $this->dateTimeFormatter,
  77. $this->config
  78. );
  79. }
  80. public function testProcess(): void {
  81. $entry = $this->createMock(IEntry::class);
  82. $entry->expects($this->once())
  83. ->method('getProperty')
  84. ->with('UID')
  85. ->willReturn('user1');
  86. $user = $this->createMock(IUser::class);
  87. $user->method('getUID')
  88. ->willReturn('user1');
  89. $this->userManager->expects($this->once())
  90. ->method('get')
  91. ->with('user1')
  92. ->willReturn($user);
  93. $this->l10nFactory->method('get')
  94. ->with('lib')
  95. ->willReturn($this->l);
  96. $this->config->method('getUserValue')
  97. ->with('user1', 'core', 'timezone')
  98. ->willReturn('America/Los_Angeles');
  99. $now = new \DateTime('2023-01-04 10:24:43');
  100. $this->timeFactory->method('getDateTime')
  101. ->willReturn($now);
  102. $now = new \DateTime('2023-01-04 10:24:43');
  103. $this->dateTimeFormatter->method('formatTime')
  104. ->with($now, 'short', $this->anything())
  105. ->willReturn('01:24');
  106. $this->urlGenerator->method('imagePath')
  107. ->willReturn('actions/recent.svg');
  108. $this->urlGenerator->method('getAbsoluteURL')
  109. ->with('actions/recent.svg')
  110. ->willReturn('https://localhost/actions/recent.svg');
  111. $action = $this->createMock(ILinkAction::class);
  112. $this->actionFactory->expects($this->once())
  113. ->method('newLinkAction')
  114. ->with(
  115. 'https://localhost/actions/recent.svg',
  116. 'Local time: 01:24',
  117. '#',
  118. 'timezone'
  119. )
  120. ->willReturn($action);
  121. $entry->expects($this->once())
  122. ->method('addAction')
  123. ->with($action);
  124. $this->provider->process($entry);
  125. }
  126. public function testProcessNoUser(): void {
  127. $entry = $this->createMock(IEntry::class);
  128. $entry->expects($this->once())
  129. ->method('getProperty')
  130. ->with('UID')
  131. ->willReturn('user1');
  132. $user = $this->createMock(IUser::class);
  133. $user->method('getUID')
  134. ->willReturn(null);
  135. $entry->expects($this->never())
  136. ->method('addAction');
  137. $this->provider->process($entry);
  138. }
  139. }