DelegationControllerTest.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2021 Nextcloud GmbH
  4. *
  5. * @author Carl Schwan <carl@carlschwan.eu>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace OCA\Settings\Tests\Controller\Admin;
  24. use OC\Settings\AuthorizedGroup;
  25. use OCA\Settings\Controller\AuthorizedGroupController;
  26. use OCA\Settings\Service\AuthorizedGroupService;
  27. use OCP\IRequest;
  28. use Test\TestCase;
  29. class DelegationControllerTest extends TestCase {
  30. /** @var AuthorizedGroupService */
  31. private $service;
  32. /** @var IRequest */
  33. private $request;
  34. /** @var AuthorizedGroupController */
  35. private $controller;
  36. protected function setUp(): void {
  37. parent::setUp();
  38. $this->request = $this->getMockBuilder(IRequest::class)->getMock();
  39. $this->service = $this->getMockBuilder(AuthorizedGroupService::class)->disableOriginalConstructor()->getMock();
  40. $this->controller = new AuthorizedGroupController(
  41. 'settings', $this->request, $this->service
  42. );
  43. }
  44. public function testSaveSettings() {
  45. $setting = 'MySecretSetting';
  46. $oldGroups = [];
  47. $oldGroups[] = AuthorizedGroup::fromParams(['groupId' => 'hello', 'class' => $setting]);
  48. $goodbye = AuthorizedGroup::fromParams(['groupId' => 'goodbye', 'class' => $setting, 'id' => 42]);
  49. $oldGroups[] = $goodbye;
  50. $this->service->expects($this->once())
  51. ->method('findExistingGroupsForClass')
  52. ->with('MySecretSetting')
  53. ->will($this->returnValue($oldGroups));
  54. $this->service->expects($this->once())
  55. ->method('delete')
  56. ->with(42);
  57. $this->service->expects($this->once())
  58. ->method('create')
  59. ->with('world', 'MySecretSetting')
  60. ->will($this->returnValue(AuthorizedGroup::fromParams(['groupId' => 'world', 'class' => $setting])));
  61. $result = $this->controller->saveSettings([['gid' => 'hello'], ['gid' => 'world']], 'MySecretSetting');
  62. $this->assertEquals(['valid' => true], $result->getData());
  63. }
  64. }