1
0

AppsControllerTest.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Provisioning_API\Tests\Controller;
  8. use OCA\Provisioning_API\Controller\AppsController;
  9. use OCA\Provisioning_API\Tests\TestCase;
  10. use OCP\App\IAppManager;
  11. use OCP\AppFramework\OCS\OCSException;
  12. use OCP\IRequest;
  13. use OCP\IUserSession;
  14. /**
  15. * Class AppsTest
  16. *
  17. * @group DB
  18. *
  19. * @package OCA\Provisioning_API\Tests
  20. */
  21. class AppsControllerTest extends TestCase {
  22. /** @var IAppManager */
  23. private $appManager;
  24. /** @var AppsController */
  25. private $api;
  26. /** @var IUserSession */
  27. private $userSession;
  28. protected function setUp(): void {
  29. parent::setUp();
  30. $this->appManager = \OC::$server->getAppManager();
  31. $this->groupManager = \OC::$server->getGroupManager();
  32. $this->userSession = \OC::$server->getUserSession();
  33. $request = $this->getMockBuilder(IRequest::class)
  34. ->disableOriginalConstructor()
  35. ->getMock();
  36. $this->api = new AppsController(
  37. 'provisioning_api',
  38. $request,
  39. $this->appManager
  40. );
  41. }
  42. protected function tearDown(): void {
  43. $this->userSession->setUser(null);
  44. }
  45. public function testGetAppInfo(): void {
  46. $result = $this->api->getAppInfo('provisioning_api');
  47. $expected = $this->appManager->getAppInfo('provisioning_api');
  48. $this->assertEquals($expected, $result->getData());
  49. }
  50. public function testGetAppInfoOnBadAppID(): void {
  51. $this->expectException(OCSException::class);
  52. $this->expectExceptionCode(998);
  53. $this->api->getAppInfo('not_provisioning_api');
  54. }
  55. public function testGetApps(): void {
  56. $user = $this->generateUsers();
  57. $this->groupManager->get('admin')->addUser($user);
  58. $this->userSession->setUser($user);
  59. $result = $this->api->getApps();
  60. $data = $result->getData();
  61. $this->assertEquals(count((new \OC_App())->listAllApps()), count($data['apps']));
  62. }
  63. public function testGetAppsEnabled(): void {
  64. $result = $this->api->getApps('enabled');
  65. $data = $result->getData();
  66. $this->assertEquals(count(\OC_App::getEnabledApps()), count($data['apps']));
  67. }
  68. public function testGetAppsDisabled(): void {
  69. $result = $this->api->getApps('disabled');
  70. $data = $result->getData();
  71. $apps = (new \OC_App)->listAllApps();
  72. $list = [];
  73. foreach ($apps as $app) {
  74. $list[] = $app['id'];
  75. }
  76. $disabled = array_diff($list, \OC_App::getEnabledApps());
  77. $this->assertEquals(count($disabled), count($data['apps']));
  78. }
  79. public function testGetAppsInvalidFilter(): void {
  80. $this->expectException(OCSException::class);
  81. $this->expectExceptionCode(101);
  82. $this->api->getApps('foo');
  83. }
  84. }