AdminSettingsControllerTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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\IUserSession;
  17. use OCP\Settings\IDeclarativeManager;
  18. use OCP\Settings\IManager;
  19. use PHPUnit\Framework\MockObject\MockObject;
  20. use Test\TestCase;
  21. /**
  22. * Class AdminSettingsControllerTest
  23. *
  24. * @group DB
  25. *
  26. * @package Tests\Settings\Controller
  27. */
  28. class AdminSettingsControllerTest extends TestCase {
  29. /** @var AdminSettingsController */
  30. private $adminSettingsController;
  31. /** @var IRequest|MockObject */
  32. private $request;
  33. /** @var INavigationManager|MockObject */
  34. private $navigationManager;
  35. /** @var IManager|MockObject */
  36. private $settingsManager;
  37. /** @var IUserSession|MockObject */
  38. private $userSession;
  39. /** @var IGroupManager|MockObject */
  40. private $groupManager;
  41. /** @var ISubAdmin|MockObject */
  42. private $subAdmin;
  43. /** @var IDeclarativeManager|MockObject */
  44. private $declarativeSettingsManager;
  45. /** @var IInitialState|MockObject */
  46. private $initialState;
  47. /** @var string */
  48. private $adminUid = 'lololo';
  49. protected function setUp(): void {
  50. parent::setUp();
  51. $this->request = $this->createMock(IRequest::class);
  52. $this->navigationManager = $this->createMock(INavigationManager::class);
  53. $this->settingsManager = $this->createMock(IManager::class);
  54. $this->userSession = $this->createMock(IUserSession::class);
  55. $this->groupManager = $this->createMock(IGroupManager::class);
  56. $this->subAdmin = $this->createMock(ISubAdmin::class);
  57. $this->declarativeSettingsManager = $this->createMock(IDeclarativeManager::class);
  58. $this->initialState = $this->createMock(IInitialState::class);
  59. $this->adminSettingsController = new AdminSettingsController(
  60. 'settings',
  61. $this->request,
  62. $this->navigationManager,
  63. $this->settingsManager,
  64. $this->userSession,
  65. $this->groupManager,
  66. $this->subAdmin,
  67. $this->declarativeSettingsManager,
  68. $this->initialState,
  69. );
  70. $user = \OC::$server->getUserManager()->createUser($this->adminUid, 'mylongrandompassword');
  71. \OC_User::setUserId($user->getUID());
  72. \OC::$server->getGroupManager()->createGroup('admin')->addUser($user);
  73. }
  74. protected function tearDown(): void {
  75. \OC::$server->getUserManager()->get($this->adminUid)->delete();
  76. parent::tearDown();
  77. }
  78. public function testIndex() {
  79. $user = $this->createMock(IUser::class);
  80. $this->userSession
  81. ->method('getUser')
  82. ->willReturn($user);
  83. $user->method('getUID')->willReturn('user123');
  84. $this->groupManager
  85. ->method('isAdmin')
  86. ->with('user123')
  87. ->willReturn(true);
  88. $this->subAdmin
  89. ->method('isSubAdmin')
  90. ->with($user)
  91. ->willReturn(false);
  92. $this->settingsManager
  93. ->expects($this->once())
  94. ->method('getAdminSections')
  95. ->willReturn([]);
  96. $this->settingsManager
  97. ->expects($this->once())
  98. ->method('getPersonalSections')
  99. ->willReturn([]);
  100. $this->settingsManager
  101. ->expects($this->once())
  102. ->method('getAllowedAdminSettings')
  103. ->with('test')
  104. ->willReturn([5 => $this->createMock(ServerDevNotice::class)]);
  105. $this->declarativeSettingsManager
  106. ->expects($this->any())
  107. ->method('getFormIDs')
  108. ->with($user, 'admin', 'test')
  109. ->willReturn([]);
  110. $idx = $this->adminSettingsController->index('test');
  111. $expected = new TemplateResponse('settings', 'settings/frame', [
  112. 'forms' => ['personal' => [], 'admin' => []],
  113. 'content' => ''
  114. ]);
  115. $this->assertEquals($expected, $idx);
  116. }
  117. }