LocalTimeProviderTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace lib\Contacts\ContactsMenu\Providers;
  8. use OC\Contacts\ContactsMenu\Providers\LocalTimeProvider;
  9. use OCP\AppFramework\Utility\ITimeFactory;
  10. use OCP\Contacts\ContactsMenu\IActionFactory;
  11. use OCP\Contacts\ContactsMenu\IEntry;
  12. use OCP\Contacts\ContactsMenu\ILinkAction;
  13. use OCP\IConfig;
  14. use OCP\IDateTimeFormatter;
  15. use OCP\IL10N;
  16. use OCP\IURLGenerator;
  17. use OCP\IUser;
  18. use OCP\IUserManager;
  19. use OCP\L10N\IFactory as IL10NFactory;
  20. use PHPUnit\Framework\MockObject\MockObject;
  21. use Test\TestCase;
  22. class LocalTimeProviderTest extends TestCase {
  23. /** @var IActionFactory|MockObject */
  24. private $actionFactory;
  25. /** @var IL10N|MockObject */
  26. private $l;
  27. /** @var IL10NFactory|MockObject */
  28. private $l10nFactory;
  29. /** @var IURLGenerator|MockObject */
  30. private $urlGenerator;
  31. /** @var IUserManager|MockObject */
  32. private $userManager;
  33. /** @var ITimeFactory|MockObject */
  34. private $timeFactory;
  35. /** @var IDateTimeFormatter|MockObject */
  36. private $dateTimeFormatter;
  37. /** @var IConfig|MockObject */
  38. private $config;
  39. private LocalTimeProvider $provider;
  40. protected function setUp(): void {
  41. parent::setUp();
  42. $this->actionFactory = $this->createMock(IActionFactory::class);
  43. $this->l10nFactory = $this->createMock(IL10NFactory::class);
  44. $this->l = $this->createMock(IL10N::class);
  45. $this->l->expects($this->any())
  46. ->method('t')
  47. ->will($this->returnCallback(function ($text, $parameters = []) {
  48. return vsprintf($text, $parameters);
  49. }));
  50. $this->urlGenerator = $this->createMock(IURLGenerator::class);
  51. $this->userManager = $this->createMock(IUserManager::class);
  52. $this->timeFactory = $this->createMock(ITimeFactory::class);
  53. $this->dateTimeFormatter = $this->createMock(IDateTimeFormatter::class);
  54. $this->config = $this->createMock(IConfig::class);
  55. $this->provider = new LocalTimeProvider(
  56. $this->actionFactory,
  57. $this->l10nFactory,
  58. $this->urlGenerator,
  59. $this->userManager,
  60. $this->timeFactory,
  61. $this->dateTimeFormatter,
  62. $this->config
  63. );
  64. }
  65. public function testProcess(): void {
  66. $entry = $this->createMock(IEntry::class);
  67. $entry->expects($this->once())
  68. ->method('getProperty')
  69. ->with('UID')
  70. ->willReturn('user1');
  71. $user = $this->createMock(IUser::class);
  72. $user->method('getUID')
  73. ->willReturn('user1');
  74. $this->userManager->expects($this->once())
  75. ->method('get')
  76. ->with('user1')
  77. ->willReturn($user);
  78. $this->l10nFactory->method('get')
  79. ->with('lib')
  80. ->willReturn($this->l);
  81. $this->config->method('getUserValue')
  82. ->with('user1', 'core', 'timezone')
  83. ->willReturn('America/Los_Angeles');
  84. $now = new \DateTime('2023-01-04 10:24:43');
  85. $this->timeFactory->method('getDateTime')
  86. ->willReturn($now);
  87. $now = new \DateTime('2023-01-04 10:24:43');
  88. $this->dateTimeFormatter->method('formatTime')
  89. ->with($now, 'short', $this->anything())
  90. ->willReturn('01:24');
  91. $this->urlGenerator->method('imagePath')
  92. ->willReturn('actions/recent.svg');
  93. $this->urlGenerator->method('getAbsoluteURL')
  94. ->with('actions/recent.svg')
  95. ->willReturn('https://localhost/actions/recent.svg');
  96. $action = $this->createMock(ILinkAction::class);
  97. $this->actionFactory->expects($this->once())
  98. ->method('newLinkAction')
  99. ->with(
  100. 'https://localhost/actions/recent.svg',
  101. 'Local time: 01:24',
  102. '#',
  103. 'timezone'
  104. )
  105. ->willReturn($action);
  106. $entry->expects($this->once())
  107. ->method('addAction')
  108. ->with($action);
  109. $this->provider->process($entry);
  110. }
  111. public function testProcessNoUser(): void {
  112. $entry = $this->createMock(IEntry::class);
  113. $entry->expects($this->once())
  114. ->method('getProperty')
  115. ->with('UID')
  116. ->willReturn('user1');
  117. $user = $this->createMock(IUser::class);
  118. $user->method('getUID')
  119. ->willReturn(null);
  120. $entry->expects($this->never())
  121. ->method('addAction');
  122. $this->provider->process($entry);
  123. }
  124. }