DashboardServiceTest.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\Dashboard\Tests;
  8. use OC\Accounts\Account;
  9. use OCA\Dashboard\Service\DashboardService;
  10. use OCP\Accounts\IAccountManager;
  11. use OCP\IConfig;
  12. use OCP\IUser;
  13. use OCP\IUserManager;
  14. use PHPUnit\Framework\MockObject\MockObject;
  15. use Test\TestCase;
  16. class DashboardServiceTest extends TestCase {
  17. private IConfig&MockObject $config;
  18. private IUserManager&MockObject $userManager;
  19. private IAccountManager&MockObject $accountManager;
  20. private DashboardService $service;
  21. protected function setUp(): void {
  22. parent::setUp();
  23. $this->config = $this->createMock(IConfig::class);
  24. $this->userManager = $this->createMock(IUserManager::class);
  25. $this->accountManager = $this->createMock(IAccountManager::class);
  26. $this->service = new DashboardService(
  27. $this->config,
  28. 'alice',
  29. $this->userManager,
  30. $this->accountManager,
  31. );
  32. }
  33. public function testGetBirthdate() {
  34. $user = $this->createMock(IUser::class);
  35. $this->userManager->method('get')
  36. ->willReturn($user);
  37. $account = new Account($user);
  38. $account->setProperty(
  39. IAccountManager::PROPERTY_BIRTHDATE,
  40. '2024-12-10T00:00:00.000Z',
  41. IAccountManager::SCOPE_LOCAL,
  42. IAccountManager::VERIFIED,
  43. );
  44. $this->accountManager->method('getAccount')
  45. ->willReturn($account);
  46. $birthdate = $this->service->getBirthdate();
  47. $this->assertEquals('2024-12-10T00:00:00.000Z', $birthdate);
  48. }
  49. public function testGetBirthdatePropertyDoesNotExist() {
  50. $user = $this->createMock(IUser::class);
  51. $this->userManager->method('get')
  52. ->willReturn($user);
  53. $account = new Account($user);
  54. $this->accountManager->method('getAccount')
  55. ->willReturn($account);
  56. $birthdate = $this->service->getBirthdate();
  57. $this->assertEquals('', $birthdate);
  58. }
  59. public function testGetBirthdateUserNotFound() {
  60. $this->userManager->method('get')
  61. ->willReturn(null);
  62. $birthdate = $this->service->getBirthdate();
  63. $this->assertEquals('', $birthdate);
  64. }
  65. public function testGetBirthdateNoUserId() {
  66. $service = new DashboardService(
  67. $this->config,
  68. null,
  69. $this->userManager,
  70. $this->accountManager,
  71. );
  72. $birthdate = $service->getBirthdate();
  73. $this->assertEquals('', $birthdate);
  74. }
  75. }