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.

193 lines
5.9 KiB

  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2017 Joas Schilling <coding@schilljs.com>
  5. *
  6. * @license GNU AGPL version 3 or any later version
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as
  10. * published by the Free Software Foundation, either version 3 of the
  11. * License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. namespace OCA\Talk\Tests\php\Settings\Admin;
  23. use OCA\Talk\Config;
  24. use OCA\Talk\MatterbridgeManager;
  25. use OCA\Talk\Service\CommandService;
  26. use OCA\Talk\Settings\Admin\AdminSettings;
  27. use OCP\AppFramework\Services\IAppConfig;
  28. use OCP\AppFramework\Services\IInitialState;
  29. use OCP\ICacheFactory;
  30. use OCP\IConfig;
  31. use OCP\IGroupManager;
  32. use OCP\IL10N;
  33. use OCP\IUserSession;
  34. use OCP\L10N\IFactory;
  35. use PHPUnit\Framework\Assert;
  36. use PHPUnit\Framework\MockObject\MockObject;
  37. use Test\TestCase;
  38. class AdminSettingsTest extends TestCase {
  39. protected Config&MockObject $talkConfig;
  40. protected IConfig&MockObject $serverConfig;
  41. protected IAppConfig&MockObject $appConfig;
  42. protected CommandService&MockObject $commandService;
  43. protected IInitialState&MockObject $initialState;
  44. protected ICacheFactory&MockObject $cacheFactory;
  45. protected IGroupManager&MockObject $groupManager;
  46. protected MatterbridgeManager&MockObject $matterbridgeManager;
  47. protected IUserSession&MockObject $userSession;
  48. protected IL10N&MockObject $l10n;
  49. protected IFactory&MockObject $l10nFactory;
  50. protected ?AdminSettings $admin = null;
  51. public function setUp(): void {
  52. parent::setUp();
  53. $this->talkConfig = $this->createMock(Config::class);
  54. $this->serverConfig = $this->createMock(IConfig::class);
  55. $this->appConfig = $this->createMock(IAppConfig::class);
  56. $this->commandService = $this->createMock(CommandService::class);
  57. $this->initialState = $this->createMock(IInitialState::class);
  58. $this->cacheFactory = $this->createMock(ICacheFactory::class);
  59. $this->groupManager = $this->createMock(IGroupManager::class);
  60. $this->matterbridgeManager = $this->createMock(MatterbridgeManager::class);
  61. $this->userSession = $this->createMock(IUserSession::class);
  62. $this->l10n = $this->createMock(IL10N::class);
  63. $this->l10nFactory = $this->createMock(IFactory::class);
  64. $this->admin = $this->getAdminSettings();
  65. }
  66. /**
  67. * @param string[] $methods
  68. * @return AdminSettings|MockObject
  69. */
  70. protected function getAdminSettings(array $methods = []): AdminSettings {
  71. if (empty($methods)) {
  72. return new AdminSettings(
  73. $this->talkConfig,
  74. $this->serverConfig,
  75. $this->appConfig,
  76. $this->commandService,
  77. $this->initialState,
  78. $this->cacheFactory,
  79. $this->groupManager,
  80. $this->matterbridgeManager,
  81. $this->userSession,
  82. $this->l10n,
  83. $this->l10nFactory
  84. );
  85. }
  86. return $this->getMockBuilder(AdminSettings::class)
  87. ->setConstructorArgs([
  88. $this->talkConfig,
  89. $this->serverConfig,
  90. $this->appConfig,
  91. $this->commandService,
  92. $this->initialState,
  93. $this->cacheFactory,
  94. $this->groupManager,
  95. $this->matterbridgeManager,
  96. $this->userSession,
  97. $this->l10n,
  98. $this->l10nFactory,
  99. ])
  100. ->onlyMethods($methods)
  101. ->getMock();
  102. }
  103. public function testGetSection(): void {
  104. $admin = $this->getAdminSettings();
  105. $this->assertNotEmpty($admin->getSection());
  106. }
  107. public function testGetPriority(): void {
  108. $admin = $this->getAdminSettings();
  109. $this->assertEquals(0, $admin->getPriority());
  110. }
  111. public function testGetForm(): void {
  112. $admin = $this->getAdminSettings([
  113. 'initGeneralSettings',
  114. 'initAllowedGroups',
  115. 'initCommands',
  116. 'initStunServers',
  117. 'initTurnServers',
  118. 'initSignalingServers',
  119. 'initRequestSignalingServerTrial',
  120. ]);
  121. $admin->expects($this->once())
  122. ->method('initGeneralSettings');
  123. $admin->expects($this->once())
  124. ->method('initAllowedGroups');
  125. $admin->expects($this->once())
  126. ->method('initCommands');
  127. $admin->expects($this->once())
  128. ->method('initStunServers');
  129. $admin->expects($this->once())
  130. ->method('initTurnServers');
  131. $admin->expects($this->once())
  132. ->method('initSignalingServers');
  133. $admin->expects($this->once())
  134. ->method('initRequestSignalingServerTrial');
  135. $form = $admin->getForm();
  136. $this->assertSame('settings/admin-settings', $form->getTemplateName());
  137. $this->assertSame('', $form->getRenderAs());
  138. $this->assertCount(0, $form->getParams());
  139. }
  140. public function testInitStunServers(): void {
  141. $this->talkConfig->expects($this->once())
  142. ->method('getStunServers')
  143. ->willReturn(['getStunServers']);
  144. $this->serverConfig->expects($this->once())
  145. ->method('getSystemValueBool')
  146. ->with('has_internet_connection', true)
  147. ->willReturn(true);
  148. $i = 0;
  149. $expectedCalls = [
  150. ['stun_servers', ['getStunServers']],
  151. ['has_internet_connection', true],
  152. ];
  153. $this->initialState->expects($this->exactly(2))
  154. ->method('provideInitialState')
  155. ->willReturnCallback(function () use ($expectedCalls, &$i) {
  156. Assert::assertArrayHasKey($i, $expectedCalls);
  157. Assert::assertSame($expectedCalls[$i], func_get_args());
  158. $i++;
  159. });
  160. $admin = $this->getAdminSettings();
  161. self::invokePrivate($admin, 'initStunServers');
  162. }
  163. public function testInitTurnServers(): void {
  164. $this->talkConfig->expects($this->once())
  165. ->method('getTurnServers')
  166. ->willReturn(['getTurnServers']);
  167. $this->initialState->expects($this->once())
  168. ->method('provideInitialState')
  169. ->with('turn_servers', ['getTurnServers']);
  170. $admin = $this->getAdminSettings();
  171. self::invokePrivate($admin, 'initTurnServers');
  172. }
  173. }