AdminSettingsControllerTest.php 4.0 KB

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