AdminSettingsControllerTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Lukas Reschke <lukas@statuscode.ch>
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Jan C. Borchardt <hey@jancborchardt.net>
  8. * @author Lukas Reschke <lukas@statuscode.ch>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. *
  11. * @license GNU AGPL version 3 or any later version
  12. *
  13. * This program is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License as
  15. * published by the Free Software Foundation, either version 3 of the
  16. * License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. *
  26. */
  27. namespace OCA\Settings\Tests\Controller;
  28. use OCA\Settings\Controller\AdminSettingsController;
  29. use OCA\Settings\Settings\Personal\ServerDevNotice;
  30. use OCP\AppFramework\Http\TemplateResponse;
  31. use OCP\AppFramework\Services\IInitialState;
  32. use OCP\Group\ISubAdmin;
  33. use OCP\IGroupManager;
  34. use OCP\INavigationManager;
  35. use OCP\IRequest;
  36. use OCP\IUser;
  37. use OCP\IUserSession;
  38. use OCP\Settings\IDeclarativeManager;
  39. use OCP\Settings\IManager;
  40. use PHPUnit\Framework\MockObject\MockObject;
  41. use Test\TestCase;
  42. /**
  43. * Class AdminSettingsControllerTest
  44. *
  45. * @group DB
  46. *
  47. * @package Tests\Settings\Controller
  48. */
  49. class AdminSettingsControllerTest extends TestCase {
  50. /** @var AdminSettingsController */
  51. private $adminSettingsController;
  52. /** @var IRequest|MockObject */
  53. private $request;
  54. /** @var INavigationManager|MockObject */
  55. private $navigationManager;
  56. /** @var IManager|MockObject */
  57. private $settingsManager;
  58. /** @var IUserSession|MockObject */
  59. private $userSession;
  60. /** @var IGroupManager|MockObject */
  61. private $groupManager;
  62. /** @var ISubAdmin|MockObject */
  63. private $subAdmin;
  64. /** @var IDeclarativeManager|MockObject */
  65. private $declarativeSettingsManager;
  66. /** @var IInitialState|MockObject */
  67. private $initialState;
  68. /** @var string */
  69. private $adminUid = 'lololo';
  70. protected function setUp(): void {
  71. parent::setUp();
  72. $this->request = $this->createMock(IRequest::class);
  73. $this->navigationManager = $this->createMock(INavigationManager::class);
  74. $this->settingsManager = $this->createMock(IManager::class);
  75. $this->userSession = $this->createMock(IUserSession::class);
  76. $this->groupManager = $this->createMock(IGroupManager::class);
  77. $this->subAdmin = $this->createMock(ISubAdmin::class);
  78. $this->declarativeSettingsManager = $this->createMock(IDeclarativeManager::class);
  79. $this->initialState = $this->createMock(IInitialState::class);
  80. $this->adminSettingsController = new AdminSettingsController(
  81. 'settings',
  82. $this->request,
  83. $this->navigationManager,
  84. $this->settingsManager,
  85. $this->userSession,
  86. $this->groupManager,
  87. $this->subAdmin,
  88. $this->declarativeSettingsManager,
  89. $this->initialState,
  90. );
  91. $user = \OC::$server->getUserManager()->createUser($this->adminUid, 'mylongrandompassword');
  92. \OC_User::setUserId($user->getUID());
  93. \OC::$server->getGroupManager()->createGroup('admin')->addUser($user);
  94. }
  95. protected function tearDown(): void {
  96. \OC::$server->getUserManager()->get($this->adminUid)->delete();
  97. parent::tearDown();
  98. }
  99. public function testIndex() {
  100. $user = $this->createMock(IUser::class);
  101. $this->userSession
  102. ->method('getUser')
  103. ->willReturn($user);
  104. $user->method('getUID')->willReturn('user123');
  105. $this->groupManager
  106. ->method('isAdmin')
  107. ->with('user123')
  108. ->willReturn(true);
  109. $this->subAdmin
  110. ->method('isSubAdmin')
  111. ->with($user)
  112. ->willReturn(false);
  113. $this->settingsManager
  114. ->expects($this->once())
  115. ->method('getAdminSections')
  116. ->willReturn([]);
  117. $this->settingsManager
  118. ->expects($this->once())
  119. ->method('getPersonalSections')
  120. ->willReturn([]);
  121. $this->settingsManager
  122. ->expects($this->once())
  123. ->method('getAllowedAdminSettings')
  124. ->with('test')
  125. ->willReturn([5 => $this->createMock(ServerDevNotice::class)]);
  126. $this->declarativeSettingsManager
  127. ->expects($this->any())
  128. ->method('getFormIDs')
  129. ->with($user, 'admin', 'test')
  130. ->willReturn([]);
  131. $idx = $this->adminSettingsController->index('test');
  132. $expected = new TemplateResponse('settings', 'settings/frame', [
  133. 'forms' => ['personal' => [], 'admin' => []],
  134. 'content' => ''
  135. ]);
  136. $this->assertEquals($expected, $idx);
  137. }
  138. }