1
0

AdminSettingsControllerTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\Settings\Tests\Controller;
  7. use OCA\Settings\Controller\AdminSettingsController;
  8. use OCA\Settings\Settings\Personal\ServerDevNotice;
  9. use OCP\AppFramework\Http\TemplateResponse;
  10. use OCP\AppFramework\Services\IInitialState;
  11. use OCP\Group\ISubAdmin;
  12. use OCP\IGroupManager;
  13. use OCP\INavigationManager;
  14. use OCP\IRequest;
  15. use OCP\IUser;
  16. use OCP\IUserManager;
  17. use OCP\IUserSession;
  18. use OCP\Server;
  19. use OCP\Settings\IDeclarativeManager;
  20. use OCP\Settings\IManager;
  21. use PHPUnit\Framework\MockObject\MockObject;
  22. use Test\TestCase;
  23. /**
  24. * Class AdminSettingsControllerTest
  25. *
  26. * @group DB
  27. *
  28. * @package Tests\Settings\Controller
  29. */
  30. class AdminSettingsControllerTest extends TestCase {
  31. private IRequest&MockObject $request;
  32. private INavigationManager&MockObject $navigationManager;
  33. private IManager&MockObject $settingsManager;
  34. private IUserSession&MockObject $userSession;
  35. private IGroupManager&MockObject $groupManager;
  36. private ISubAdmin&MockObject $subAdmin;
  37. private IDeclarativeManager&MockObject $declarativeSettingsManager;
  38. private IInitialState&MockObject $initialState;
  39. private string $adminUid = 'lololo';
  40. private AdminSettingsController $adminSettingsController;
  41. protected function setUp(): void {
  42. parent::setUp();
  43. $this->request = $this->createMock(IRequest::class);
  44. $this->navigationManager = $this->createMock(INavigationManager::class);
  45. $this->settingsManager = $this->createMock(IManager::class);
  46. $this->userSession = $this->createMock(IUserSession::class);
  47. $this->groupManager = $this->createMock(IGroupManager::class);
  48. $this->subAdmin = $this->createMock(ISubAdmin::class);
  49. $this->declarativeSettingsManager = $this->createMock(IDeclarativeManager::class);
  50. $this->initialState = $this->createMock(IInitialState::class);
  51. $this->adminSettingsController = new AdminSettingsController(
  52. 'settings',
  53. $this->request,
  54. $this->navigationManager,
  55. $this->settingsManager,
  56. $this->userSession,
  57. $this->groupManager,
  58. $this->subAdmin,
  59. $this->declarativeSettingsManager,
  60. $this->initialState,
  61. );
  62. $user = Server::get(IUserManager::class)->createUser($this->adminUid, 'mylongrandompassword');
  63. \OC_User::setUserId($user->getUID());
  64. Server::get(IGroupManager::class)->createGroup('admin')->addUser($user);
  65. }
  66. protected function tearDown(): void {
  67. Server::get(IUserManager::class)
  68. ->get($this->adminUid)
  69. ->delete();
  70. \OC_User::setUserId(null);
  71. Server::get(IUserSession::class)->setUser(null);
  72. parent::tearDown();
  73. }
  74. public function testIndex(): void {
  75. $user = $this->createMock(IUser::class);
  76. $this->userSession
  77. ->method('getUser')
  78. ->willReturn($user);
  79. $user->method('getUID')->willReturn('user123');
  80. $this->groupManager
  81. ->method('isAdmin')
  82. ->with('user123')
  83. ->willReturn(true);
  84. $this->subAdmin
  85. ->method('isSubAdmin')
  86. ->with($user)
  87. ->willReturn(false);
  88. $form = new TemplateResponse('settings', 'settings/empty');
  89. $setting = $this->createMock(ServerDevNotice::class);
  90. $setting->expects(self::any())
  91. ->method('getForm')
  92. ->willReturn($form);
  93. $this->settingsManager
  94. ->expects($this->once())
  95. ->method('getAdminSections')
  96. ->willReturn([]);
  97. $this->settingsManager
  98. ->expects($this->once())
  99. ->method('getPersonalSections')
  100. ->willReturn([]);
  101. $this->settingsManager
  102. ->expects($this->once())
  103. ->method('getAllowedAdminSettings')
  104. ->with('test')
  105. ->willReturn([5 => [$setting]]);
  106. $this->declarativeSettingsManager
  107. ->expects($this->any())
  108. ->method('getFormIDs')
  109. ->with($user, 'admin', 'test')
  110. ->willReturn([]);
  111. $idx = $this->adminSettingsController->index('test');
  112. $expected = new TemplateResponse('settings', 'settings/frame', [
  113. 'forms' => ['personal' => [], 'admin' => []],
  114. 'content' => ''
  115. ]);
  116. $this->assertEquals($expected, $idx);
  117. }
  118. }