DelegationControllerTest.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\Settings\Tests\Controller\Admin;
  7. use OC\Settings\AuthorizedGroup;
  8. use OCA\Settings\Controller\AuthorizedGroupController;
  9. use OCA\Settings\Service\AuthorizedGroupService;
  10. use OCP\IRequest;
  11. use Test\TestCase;
  12. class DelegationControllerTest extends TestCase {
  13. /** @var AuthorizedGroupService */
  14. private $service;
  15. /** @var IRequest */
  16. private $request;
  17. /** @var AuthorizedGroupController */
  18. private $controller;
  19. protected function setUp(): void {
  20. parent::setUp();
  21. $this->request = $this->getMockBuilder(IRequest::class)->getMock();
  22. $this->service = $this->getMockBuilder(AuthorizedGroupService::class)->disableOriginalConstructor()->getMock();
  23. $this->controller = new AuthorizedGroupController(
  24. 'settings', $this->request, $this->service
  25. );
  26. }
  27. public function testSaveSettings(): void {
  28. $setting = 'MySecretSetting';
  29. $oldGroups = [];
  30. $oldGroups[] = AuthorizedGroup::fromParams(['groupId' => 'hello', 'class' => $setting]);
  31. $goodbye = AuthorizedGroup::fromParams(['groupId' => 'goodbye', 'class' => $setting, 'id' => 42]);
  32. $oldGroups[] = $goodbye;
  33. $this->service->expects($this->once())
  34. ->method('findExistingGroupsForClass')
  35. ->with('MySecretSetting')
  36. ->will($this->returnValue($oldGroups));
  37. $this->service->expects($this->once())
  38. ->method('delete')
  39. ->with(42);
  40. $this->service->expects($this->once())
  41. ->method('create')
  42. ->with('world', 'MySecretSetting')
  43. ->will($this->returnValue(AuthorizedGroup::fromParams(['groupId' => 'world', 'class' => $setting])));
  44. $result = $this->controller->saveSettings([['gid' => 'hello'], ['gid' => 'world']], 'MySecretSetting');
  45. $this->assertEquals(['valid' => true], $result->getData());
  46. }
  47. }