You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
2.5 KiB

  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\AppFramework\Services\IAppConfig;
  12. use OCP\IConfig;
  13. use OCP\IUser;
  14. use OCP\IUserManager;
  15. use PHPUnit\Framework\MockObject\MockObject;
  16. use Test\TestCase;
  17. class DashboardServiceTest extends TestCase {
  18. private IConfig&MockObject $config;
  19. private IAppConfig&MockObject $appConfig;
  20. private IUserManager&MockObject $userManager;
  21. private IAccountManager&MockObject $accountManager;
  22. private DashboardService $service;
  23. protected function setUp(): void {
  24. parent::setUp();
  25. $this->config = $this->createMock(IConfig::class);
  26. $this->appConfig = $this->createMock(IAppConfig::class);
  27. $this->userManager = $this->createMock(IUserManager::class);
  28. $this->accountManager = $this->createMock(IAccountManager::class);
  29. $this->service = new DashboardService(
  30. $this->config,
  31. $this->appConfig,
  32. 'alice',
  33. $this->userManager,
  34. $this->accountManager,
  35. );
  36. }
  37. public function testGetBirthdate(): void {
  38. $user = $this->createMock(IUser::class);
  39. $this->userManager->method('get')
  40. ->willReturn($user);
  41. $account = new Account($user);
  42. $account->setProperty(
  43. IAccountManager::PROPERTY_BIRTHDATE,
  44. '2024-12-10T00:00:00.000Z',
  45. IAccountManager::SCOPE_LOCAL,
  46. IAccountManager::VERIFIED,
  47. );
  48. $this->accountManager->method('getAccount')
  49. ->willReturn($account);
  50. $birthdate = $this->service->getBirthdate();
  51. $this->assertEquals('2024-12-10T00:00:00.000Z', $birthdate);
  52. }
  53. public function testGetBirthdatePropertyDoesNotExist(): void {
  54. $user = $this->createMock(IUser::class);
  55. $this->userManager->method('get')
  56. ->willReturn($user);
  57. $account = new Account($user);
  58. $this->accountManager->method('getAccount')
  59. ->willReturn($account);
  60. $birthdate = $this->service->getBirthdate();
  61. $this->assertEquals('', $birthdate);
  62. }
  63. public function testGetBirthdateUserNotFound(): void {
  64. $this->userManager->method('get')
  65. ->willReturn(null);
  66. $birthdate = $this->service->getBirthdate();
  67. $this->assertEquals('', $birthdate);
  68. }
  69. public function testGetBirthdateNoUserId(): void {
  70. $service = new DashboardService(
  71. $this->config,
  72. $this->appConfig,
  73. null,
  74. $this->userManager,
  75. $this->accountManager,
  76. );
  77. $birthdate = $service->getBirthdate();
  78. $this->assertEquals('', $birthdate);
  79. }
  80. }