ActionProviderStoreTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace Tests\Contacts\ContactsMenu;
  7. use OC\App\AppManager;
  8. use OC\Contacts\ContactsMenu\ActionProviderStore;
  9. use OC\Contacts\ContactsMenu\Providers\EMailProvider;
  10. use OC\Contacts\ContactsMenu\Providers\LocalTimeProvider;
  11. use OC\Contacts\ContactsMenu\Providers\ProfileProvider;
  12. use OCP\App\IAppManager;
  13. use OCP\AppFramework\QueryException;
  14. use OCP\Contacts\ContactsMenu\IProvider;
  15. use OCP\IServerContainer;
  16. use OCP\IUser;
  17. use PHPUnit\Framework\MockObject\MockObject;
  18. use Psr\Log\LoggerInterface;
  19. use Test\TestCase;
  20. class ActionProviderStoreTest extends TestCase {
  21. /** @var IServerContainer|MockObject */
  22. private $serverContainer;
  23. /** @var IAppManager|MockObject */
  24. private $appManager;
  25. private ActionProviderStore $actionProviderStore;
  26. protected function setUp(): void {
  27. parent::setUp();
  28. $this->serverContainer = $this->createMock(IServerContainer::class);
  29. $this->appManager = $this->createMock(AppManager::class);
  30. $logger = $this->createMock(LoggerInterface::class);
  31. $this->actionProviderStore = new ActionProviderStore($this->serverContainer, $this->appManager, $logger);
  32. }
  33. public function testGetProviders(): void {
  34. $user = $this->createMock(IUser::class);
  35. $provider1 = $this->createMock(ProfileProvider::class);
  36. $provider2 = $this->createMock(LocalTimeProvider::class);
  37. $provider3 = $this->createMock(EMailProvider::class);
  38. $provider4 = $this->createMock(IProvider::class);
  39. $this->appManager->expects($this->once())
  40. ->method('getEnabledAppsForUser')
  41. ->with($user)
  42. ->willReturn(['contacts']);
  43. $this->appManager->expects($this->once())
  44. ->method('getAppInfo')
  45. ->with('contacts')
  46. ->willReturn([
  47. 'contactsmenu' => [
  48. 'OCA\Contacts\Provider1',
  49. ],
  50. ]);
  51. $this->serverContainer->expects($this->exactly(4))
  52. ->method('get')
  53. ->willReturnMap([
  54. [ProfileProvider::class, $provider1],
  55. [LocalTimeProvider::class, $provider2],
  56. [EMailProvider::class, $provider3],
  57. ['OCA\Contacts\Provider1', $provider4]
  58. ]);
  59. $providers = $this->actionProviderStore->getProviders($user);
  60. $this->assertCount(4, $providers);
  61. $this->assertInstanceOf(ProfileProvider::class, $providers[0]);
  62. $this->assertInstanceOf(LocalTimeProvider::class, $providers[1]);
  63. $this->assertInstanceOf(EMailProvider::class, $providers[2]);
  64. }
  65. public function testGetProvidersOfAppWithIncompleInfo(): void {
  66. $user = $this->createMock(IUser::class);
  67. $provider1 = $this->createMock(ProfileProvider::class);
  68. $provider2 = $this->createMock(LocalTimeProvider::class);
  69. $provider3 = $this->createMock(EMailProvider::class);
  70. $this->appManager->expects($this->once())
  71. ->method('getEnabledAppsForUser')
  72. ->with($user)
  73. ->willReturn(['contacts']);
  74. $this->appManager->expects($this->once())
  75. ->method('getAppInfo')
  76. ->with('contacts')
  77. ->willReturn([/* Empty info.xml */]);
  78. $this->serverContainer->expects($this->exactly(3))
  79. ->method('get')
  80. ->willReturnMap([
  81. [ProfileProvider::class, $provider1],
  82. [LocalTimeProvider::class, $provider2],
  83. [EMailProvider::class, $provider3],
  84. ]);
  85. $providers = $this->actionProviderStore->getProviders($user);
  86. $this->assertCount(3, $providers);
  87. $this->assertInstanceOf(ProfileProvider::class, $providers[0]);
  88. $this->assertInstanceOf(LocalTimeProvider::class, $providers[1]);
  89. $this->assertInstanceOf(EMailProvider::class, $providers[2]);
  90. }
  91. public function testGetProvidersWithQueryException(): void {
  92. $this->expectException(\Exception::class);
  93. $user = $this->createMock(IUser::class);
  94. $this->appManager->expects($this->once())
  95. ->method('getEnabledAppsForUser')
  96. ->with($user)
  97. ->willReturn([]);
  98. $this->serverContainer->expects($this->once())
  99. ->method('get')
  100. ->willThrowException(new QueryException());
  101. $this->actionProviderStore->getProviders($user);
  102. }
  103. }