ActionProviderStoreTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. /**
  3. * @copyright 2017 Christoph Wurst <christoph@winzerhof-wurst.at>
  4. *
  5. * @author 2017 Christoph Wurst <christoph@winzerhof-wurst.at>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace Tests\Contacts\ContactsMenu;
  24. use OC\App\AppManager;
  25. use OC\Contacts\ContactsMenu\ActionProviderStore;
  26. use OC\Contacts\ContactsMenu\Providers\EMailProvider;
  27. use OC\Contacts\ContactsMenu\Providers\LocalTimeProvider;
  28. use OC\Contacts\ContactsMenu\Providers\ProfileProvider;
  29. use OCP\App\IAppManager;
  30. use OCP\AppFramework\QueryException;
  31. use OCP\Contacts\ContactsMenu\IProvider;
  32. use OCP\IServerContainer;
  33. use OCP\IUser;
  34. use PHPUnit\Framework\MockObject\MockObject;
  35. use Psr\Log\LoggerInterface;
  36. use Test\TestCase;
  37. class ActionProviderStoreTest extends TestCase {
  38. /** @var IServerContainer|MockObject */
  39. private $serverContainer;
  40. /** @var IAppManager|MockObject */
  41. private $appManager;
  42. private ActionProviderStore $actionProviderStore;
  43. protected function setUp(): void {
  44. parent::setUp();
  45. $this->serverContainer = $this->createMock(IServerContainer::class);
  46. $this->appManager = $this->createMock(AppManager::class);
  47. $logger = $this->createMock(LoggerInterface::class);
  48. $this->actionProviderStore = new ActionProviderStore($this->serverContainer, $this->appManager, $logger);
  49. }
  50. public function testGetProviders() {
  51. $user = $this->createMock(IUser::class);
  52. $provider1 = $this->createMock(ProfileProvider::class);
  53. $provider2 = $this->createMock(LocalTimeProvider::class);
  54. $provider3 = $this->createMock(EMailProvider::class);
  55. $provider4 = $this->createMock(IProvider::class);
  56. $this->appManager->expects($this->once())
  57. ->method('getEnabledAppsForUser')
  58. ->with($user)
  59. ->willReturn(['contacts']);
  60. $this->appManager->expects($this->once())
  61. ->method('getAppInfo')
  62. ->with('contacts')
  63. ->willReturn([
  64. 'contactsmenu' => [
  65. 'OCA\Contacts\Provider1',
  66. ],
  67. ]);
  68. $this->serverContainer->expects($this->exactly(4))
  69. ->method('get')
  70. ->willReturnMap([
  71. [ProfileProvider::class, $provider1],
  72. [LocalTimeProvider::class, $provider2],
  73. [EMailProvider::class, $provider3],
  74. ['OCA\Contacts\Provider1', $provider4]
  75. ]);
  76. $providers = $this->actionProviderStore->getProviders($user);
  77. $this->assertCount(4, $providers);
  78. $this->assertInstanceOf(ProfileProvider::class, $providers[0]);
  79. $this->assertInstanceOf(LocalTimeProvider::class, $providers[1]);
  80. $this->assertInstanceOf(EMailProvider::class, $providers[2]);
  81. }
  82. public function testGetProvidersOfAppWithIncompleInfo() {
  83. $user = $this->createMock(IUser::class);
  84. $provider1 = $this->createMock(ProfileProvider::class);
  85. $provider2 = $this->createMock(LocalTimeProvider::class);
  86. $provider3 = $this->createMock(EMailProvider::class);
  87. $this->appManager->expects($this->once())
  88. ->method('getEnabledAppsForUser')
  89. ->with($user)
  90. ->willReturn(['contacts']);
  91. $this->appManager->expects($this->once())
  92. ->method('getAppInfo')
  93. ->with('contacts')
  94. ->willReturn([/* Empty info.xml */]);
  95. $this->serverContainer->expects($this->exactly(3))
  96. ->method('get')
  97. ->willReturnMap([
  98. [ProfileProvider::class, $provider1],
  99. [LocalTimeProvider::class, $provider2],
  100. [EMailProvider::class, $provider3],
  101. ]);
  102. $providers = $this->actionProviderStore->getProviders($user);
  103. $this->assertCount(3, $providers);
  104. $this->assertInstanceOf(ProfileProvider::class, $providers[0]);
  105. $this->assertInstanceOf(LocalTimeProvider::class, $providers[1]);
  106. $this->assertInstanceOf(EMailProvider::class, $providers[2]);
  107. }
  108. public function testGetProvidersWithQueryException() {
  109. $this->expectException(\Exception::class);
  110. $user = $this->createMock(IUser::class);
  111. $this->appManager->expects($this->once())
  112. ->method('getEnabledAppsForUser')
  113. ->with($user)
  114. ->willReturn([]);
  115. $this->serverContainer->expects($this->once())
  116. ->method('get')
  117. ->willThrowException(new QueryException());
  118. $this->actionProviderStore->getProviders($user);
  119. }
  120. }