AppManagerTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * Copyright (c) 2014 Robin Appelman <icewind@owncloud.com>
  5. * This file is licensed under the Affero General Public License version 3 or
  6. * later.
  7. * See the COPYING-README file.
  8. */
  9. namespace Test\App;
  10. use OC\App\AppManager;
  11. use OC\AppConfig;
  12. use OCP\App\AppPathNotFoundException;
  13. use OCP\App\IAppManager;
  14. use OCP\ICache;
  15. use OCP\ICacheFactory;
  16. use OCP\IConfig;
  17. use OCP\IGroup;
  18. use OCP\IGroupManager;
  19. use OCP\IUser;
  20. use OCP\IUserSession;
  21. use PHPUnit\Framework\MockObject\MockObject;
  22. use Psr\Log\LoggerInterface;
  23. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  24. use Test\TestCase;
  25. /**
  26. * Class AppManagerTest
  27. *
  28. * @package Test\App
  29. */
  30. class AppManagerTest extends TestCase {
  31. /**
  32. * @return AppConfig|MockObject
  33. */
  34. protected function getAppConfig() {
  35. $appConfig = [];
  36. $config = $this->createMock(AppConfig::class);
  37. $config->expects($this->any())
  38. ->method('getValue')
  39. ->willReturnCallback(function ($app, $key, $default) use (&$appConfig) {
  40. return (isset($appConfig[$app]) and isset($appConfig[$app][$key])) ? $appConfig[$app][$key] : $default;
  41. });
  42. $config->expects($this->any())
  43. ->method('setValue')
  44. ->willReturnCallback(function ($app, $key, $value) use (&$appConfig) {
  45. if (!isset($appConfig[$app])) {
  46. $appConfig[$app] = [];
  47. }
  48. $appConfig[$app][$key] = $value;
  49. });
  50. $config->expects($this->any())
  51. ->method('getValues')
  52. ->willReturnCallback(function ($app, $key) use (&$appConfig) {
  53. if ($app) {
  54. return $appConfig[$app];
  55. } else {
  56. $values = [];
  57. foreach ($appConfig as $appid => $appData) {
  58. if (isset($appData[$key])) {
  59. $values[$appid] = $appData[$key];
  60. }
  61. }
  62. return $values;
  63. }
  64. });
  65. return $config;
  66. }
  67. /** @var IUserSession|MockObject */
  68. protected $userSession;
  69. /** @var IConfig|MockObject */
  70. private $config;
  71. /** @var IGroupManager|MockObject */
  72. protected $groupManager;
  73. /** @var AppConfig|MockObject */
  74. protected $appConfig;
  75. /** @var ICache|MockObject */
  76. protected $cache;
  77. /** @var ICacheFactory|MockObject */
  78. protected $cacheFactory;
  79. /** @var EventDispatcherInterface|MockObject */
  80. protected $eventDispatcher;
  81. /** @var LoggerInterface|MockObject */
  82. protected $logger;
  83. /** @var IAppManager */
  84. protected $manager;
  85. protected function setUp(): void {
  86. parent::setUp();
  87. $this->userSession = $this->createMock(IUserSession::class);
  88. $this->groupManager = $this->createMock(IGroupManager::class);
  89. $this->config = $this->createMock(IConfig::class);
  90. $this->appConfig = $this->getAppConfig();
  91. $this->cacheFactory = $this->createMock(ICacheFactory::class);
  92. $this->cache = $this->createMock(ICache::class);
  93. $this->eventDispatcher = $this->createMock(EventDispatcherInterface::class);
  94. $this->logger = $this->createMock(LoggerInterface::class);
  95. $this->cacheFactory->expects($this->any())
  96. ->method('createDistributed')
  97. ->with('settings')
  98. ->willReturn($this->cache);
  99. $this->manager = new AppManager(
  100. $this->userSession,
  101. $this->config,
  102. $this->appConfig,
  103. $this->groupManager,
  104. $this->cacheFactory,
  105. $this->eventDispatcher,
  106. $this->logger
  107. );
  108. }
  109. protected function expectClearCache() {
  110. $this->cache->expects($this->once())
  111. ->method('clear')
  112. ->with('listApps');
  113. }
  114. public function testEnableApp() {
  115. $this->expectClearCache();
  116. // making sure "files_trashbin" is disabled
  117. if ($this->manager->isEnabledForUser('files_trashbin')) {
  118. $this->manager->disableApp('files_trashbin');
  119. }
  120. $this->manager->enableApp('files_trashbin');
  121. $this->assertEquals('yes', $this->appConfig->getValue('files_trashbin', 'enabled', 'no'));
  122. }
  123. public function testDisableApp() {
  124. $this->expectClearCache();
  125. $this->manager->disableApp('files_trashbin');
  126. $this->assertEquals('no', $this->appConfig->getValue('files_trashbin', 'enabled', 'no'));
  127. }
  128. public function testNotEnableIfNotInstalled() {
  129. try {
  130. $this->manager->enableApp('some_random_name_which_i_hope_is_not_an_app');
  131. $this->assertFalse(true, 'If this line is reached the expected exception is not thrown.');
  132. } catch (AppPathNotFoundException $e) {
  133. // Exception is expected
  134. $this->assertEquals('Could not find path for some_random_name_which_i_hope_is_not_an_app', $e->getMessage());
  135. }
  136. $this->assertEquals('no', $this->appConfig->getValue(
  137. 'some_random_name_which_i_hope_is_not_an_app', 'enabled', 'no'
  138. ));
  139. }
  140. public function testEnableAppForGroups() {
  141. $group1 = $this->createMock(IGroup::class);
  142. $group1->method('getGID')
  143. ->willReturn('group1');
  144. $group2 = $this->createMock(IGroup::class);
  145. $group2->method('getGID')
  146. ->willReturn('group2');
  147. $groups = [$group1, $group2];
  148. $this->expectClearCache();
  149. /** @var AppManager|MockObject $manager */
  150. $manager = $this->getMockBuilder(AppManager::class)
  151. ->setConstructorArgs([
  152. $this->userSession, $this->config, $this->appConfig, $this->groupManager, $this->cacheFactory, $this->eventDispatcher, $this->logger
  153. ])
  154. ->setMethods([
  155. 'getAppPath',
  156. ])
  157. ->getMock();
  158. $manager->expects($this->exactly(2))
  159. ->method('getAppPath')
  160. ->with('test')
  161. ->willReturn('apps/test');
  162. $manager->enableAppForGroups('test', $groups);
  163. $this->assertEquals('["group1","group2"]', $this->appConfig->getValue('test', 'enabled', 'no'));
  164. }
  165. public function dataEnableAppForGroupsAllowedTypes() {
  166. return [
  167. [[]],
  168. [[
  169. 'types' => [],
  170. ]],
  171. [[
  172. 'types' => ['nickvergessen'],
  173. ]],
  174. ];
  175. }
  176. /**
  177. * @dataProvider dataEnableAppForGroupsAllowedTypes
  178. *
  179. * @param array $appInfo
  180. */
  181. public function testEnableAppForGroupsAllowedTypes(array $appInfo) {
  182. $group1 = $this->createMock(IGroup::class);
  183. $group1->method('getGID')
  184. ->willReturn('group1');
  185. $group2 = $this->createMock(IGroup::class);
  186. $group2->method('getGID')
  187. ->willReturn('group2');
  188. $groups = [$group1, $group2];
  189. $this->expectClearCache();
  190. /** @var AppManager|MockObject $manager */
  191. $manager = $this->getMockBuilder(AppManager::class)
  192. ->setConstructorArgs([
  193. $this->userSession, $this->config, $this->appConfig, $this->groupManager, $this->cacheFactory, $this->eventDispatcher, $this->logger
  194. ])
  195. ->setMethods([
  196. 'getAppPath',
  197. 'getAppInfo',
  198. ])
  199. ->getMock();
  200. $manager->expects($this->once())
  201. ->method('getAppPath')
  202. ->with('test')
  203. ->willReturn(null);
  204. $manager->expects($this->once())
  205. ->method('getAppInfo')
  206. ->with('test')
  207. ->willReturn($appInfo);
  208. $manager->enableAppForGroups('test', $groups);
  209. $this->assertEquals('["group1","group2"]', $this->appConfig->getValue('test', 'enabled', 'no'));
  210. }
  211. public function dataEnableAppForGroupsForbiddenTypes() {
  212. return [
  213. ['filesystem'],
  214. ['prelogin'],
  215. ['authentication'],
  216. ['logging'],
  217. ['prevent_group_restriction'],
  218. ];
  219. }
  220. /**
  221. * @dataProvider dataEnableAppForGroupsForbiddenTypes
  222. *
  223. * @param string $type
  224. *
  225. */
  226. public function testEnableAppForGroupsForbiddenTypes($type) {
  227. $this->expectException(\Exception::class);
  228. $this->expectExceptionMessage('test can\'t be enabled for groups.');
  229. $group1 = $this->createMock(IGroup::class);
  230. $group1->method('getGID')
  231. ->willReturn('group1');
  232. $group2 = $this->createMock(IGroup::class);
  233. $group2->method('getGID')
  234. ->willReturn('group2');
  235. $groups = [$group1, $group2];
  236. /** @var AppManager|MockObject $manager */
  237. $manager = $this->getMockBuilder(AppManager::class)
  238. ->setConstructorArgs([
  239. $this->userSession, $this->config, $this->appConfig, $this->groupManager, $this->cacheFactory, $this->eventDispatcher, $this->logger
  240. ])
  241. ->setMethods([
  242. 'getAppPath',
  243. 'getAppInfo',
  244. ])
  245. ->getMock();
  246. $manager->expects($this->once())
  247. ->method('getAppPath')
  248. ->with('test')
  249. ->willReturn(null);
  250. $manager->expects($this->once())
  251. ->method('getAppInfo')
  252. ->with('test')
  253. ->willReturn([
  254. 'types' => [$type],
  255. ]);
  256. $manager->enableAppForGroups('test', $groups);
  257. }
  258. public function testIsInstalledEnabled() {
  259. $this->appConfig->setValue('test', 'enabled', 'yes');
  260. $this->assertTrue($this->manager->isInstalled('test'));
  261. }
  262. public function testIsInstalledDisabled() {
  263. $this->appConfig->setValue('test', 'enabled', 'no');
  264. $this->assertFalse($this->manager->isInstalled('test'));
  265. }
  266. public function testIsInstalledEnabledForGroups() {
  267. $this->appConfig->setValue('test', 'enabled', '["foo"]');
  268. $this->assertTrue($this->manager->isInstalled('test'));
  269. }
  270. private function newUser($uid) {
  271. $user = $this->createMock(IUser::class);
  272. $user->method('getUID')
  273. ->willReturn($uid);
  274. return $user;
  275. }
  276. public function testIsEnabledForUserEnabled() {
  277. $this->appConfig->setValue('test', 'enabled', 'yes');
  278. $user = $this->newUser('user1');
  279. $this->assertTrue($this->manager->isEnabledForUser('test', $user));
  280. }
  281. public function testIsEnabledForUserDisabled() {
  282. $this->appConfig->setValue('test', 'enabled', 'no');
  283. $user = $this->newUser('user1');
  284. $this->assertFalse($this->manager->isEnabledForUser('test', $user));
  285. }
  286. public function testGetAppPath() {
  287. $this->assertEquals(\OC::$SERVERROOT . '/apps/files', $this->manager->getAppPath('files'));
  288. }
  289. public function testGetAppPathSymlink() {
  290. $fakeAppDirname = sha1(uniqid('test', true));
  291. $fakeAppPath = sys_get_temp_dir() . '/' . $fakeAppDirname;
  292. $fakeAppLink = \OC::$SERVERROOT . '/' . $fakeAppDirname;
  293. mkdir($fakeAppPath);
  294. if (symlink($fakeAppPath, $fakeAppLink) === false) {
  295. $this->markTestSkipped('Failed to create symlink');
  296. }
  297. // Use the symlink as the app path
  298. \OC::$APPSROOTS[] = [
  299. 'path' => $fakeAppLink,
  300. 'url' => \OC::$WEBROOT . '/' . $fakeAppDirname,
  301. 'writable' => false,
  302. ];
  303. $fakeTestAppPath = $fakeAppPath . '/' . 'test-test-app';
  304. mkdir($fakeTestAppPath);
  305. $generatedAppPath = $this->manager->getAppPath('test-test-app');
  306. rmdir($fakeTestAppPath);
  307. unlink($fakeAppLink);
  308. rmdir($fakeAppPath);
  309. $this->assertEquals($fakeAppLink . '/test-test-app', $generatedAppPath);
  310. }
  311. public function testGetAppPathFail() {
  312. $this->expectException(AppPathNotFoundException::class);
  313. $this->manager->getAppPath('testnotexisting');
  314. }
  315. public function testIsEnabledForUserEnabledForGroup() {
  316. $user = $this->newUser('user1');
  317. $this->groupManager->expects($this->once())
  318. ->method('getUserGroupIds')
  319. ->with($user)
  320. ->willReturn(['foo', 'bar']);
  321. $this->appConfig->setValue('test', 'enabled', '["foo"]');
  322. $this->assertTrue($this->manager->isEnabledForUser('test', $user));
  323. }
  324. public function testIsEnabledForUserDisabledForGroup() {
  325. $user = $this->newUser('user1');
  326. $this->groupManager->expects($this->once())
  327. ->method('getUserGroupIds')
  328. ->with($user)
  329. ->willReturn(['bar']);
  330. $this->appConfig->setValue('test', 'enabled', '["foo"]');
  331. $this->assertFalse($this->manager->isEnabledForUser('test', $user));
  332. }
  333. public function testIsEnabledForUserLoggedOut() {
  334. $this->appConfig->setValue('test', 'enabled', '["foo"]');
  335. $this->assertFalse($this->manager->isEnabledForUser('test'));
  336. }
  337. public function testIsEnabledForUserLoggedIn() {
  338. $user = $this->newUser('user1');
  339. $this->userSession->expects($this->once())
  340. ->method('getUser')
  341. ->willReturn($user);
  342. $this->groupManager->expects($this->once())
  343. ->method('getUserGroupIds')
  344. ->with($user)
  345. ->willReturn(['foo', 'bar']);
  346. $this->appConfig->setValue('test', 'enabled', '["foo"]');
  347. $this->assertTrue($this->manager->isEnabledForUser('test'));
  348. }
  349. public function testGetInstalledApps() {
  350. $this->appConfig->setValue('test1', 'enabled', 'yes');
  351. $this->appConfig->setValue('test2', 'enabled', 'no');
  352. $this->appConfig->setValue('test3', 'enabled', '["foo"]');
  353. $apps = [
  354. 'cloud_federation_api',
  355. 'dav',
  356. 'federatedfilesharing',
  357. 'files',
  358. 'lookup_server_connector',
  359. 'oauth2',
  360. 'provisioning_api',
  361. 'settings',
  362. 'test1',
  363. 'test3',
  364. 'twofactor_backupcodes',
  365. 'viewer',
  366. 'workflowengine',
  367. ];
  368. $this->assertEquals($apps, $this->manager->getInstalledApps());
  369. }
  370. public function testGetAppsForUser() {
  371. $user = $this->newUser('user1');
  372. $this->groupManager->expects($this->any())
  373. ->method('getUserGroupIds')
  374. ->with($user)
  375. ->willReturn(['foo', 'bar']);
  376. $this->appConfig->setValue('test1', 'enabled', 'yes');
  377. $this->appConfig->setValue('test2', 'enabled', 'no');
  378. $this->appConfig->setValue('test3', 'enabled', '["foo"]');
  379. $this->appConfig->setValue('test4', 'enabled', '["asd"]');
  380. $enabled = [
  381. 'cloud_federation_api',
  382. 'dav',
  383. 'federatedfilesharing',
  384. 'files',
  385. 'lookup_server_connector',
  386. 'oauth2',
  387. 'provisioning_api',
  388. 'settings',
  389. 'test1',
  390. 'test3',
  391. 'twofactor_backupcodes',
  392. 'viewer',
  393. 'workflowengine',
  394. ];
  395. $this->assertEquals($enabled, $this->manager->getEnabledAppsForUser($user));
  396. }
  397. public function testGetAppsNeedingUpgrade() {
  398. /** @var AppManager|MockObject $manager */
  399. $manager = $this->getMockBuilder(AppManager::class)
  400. ->setConstructorArgs([$this->userSession, $this->config, $this->appConfig, $this->groupManager, $this->cacheFactory, $this->eventDispatcher, $this->logger])
  401. ->setMethods(['getAppInfo'])
  402. ->getMock();
  403. $appInfos = [
  404. 'cloud_federation_api' => ['id' => 'cloud_federation_api'],
  405. 'dav' => ['id' => 'dav'],
  406. 'files' => ['id' => 'files'],
  407. 'federatedfilesharing' => ['id' => 'federatedfilesharing'],
  408. 'provisioning_api' => ['id' => 'provisioning_api'],
  409. 'lookup_server_connector' => ['id' => 'lookup_server_connector'],
  410. 'test1' => ['id' => 'test1', 'version' => '1.0.1', 'requiremax' => '9.0.0'],
  411. 'test2' => ['id' => 'test2', 'version' => '1.0.0', 'requiremin' => '8.2.0'],
  412. 'test3' => ['id' => 'test3', 'version' => '1.2.4', 'requiremin' => '9.0.0'],
  413. 'test4' => ['id' => 'test4', 'version' => '3.0.0', 'requiremin' => '8.1.0'],
  414. 'testnoversion' => ['id' => 'testnoversion', 'requiremin' => '8.2.0'],
  415. 'settings' => ['id' => 'settings'],
  416. 'twofactor_backupcodes' => ['id' => 'twofactor_backupcodes'],
  417. 'viewer' => ['id' => 'viewer'],
  418. 'workflowengine' => ['id' => 'workflowengine'],
  419. 'oauth2' => ['id' => 'oauth2'],
  420. ];
  421. $manager->expects($this->any())
  422. ->method('getAppInfo')
  423. ->willReturnCallback(
  424. function ($appId) use ($appInfos) {
  425. return $appInfos[$appId];
  426. }
  427. );
  428. $this->appConfig->setValue('test1', 'enabled', 'yes');
  429. $this->appConfig->setValue('test1', 'installed_version', '1.0.0');
  430. $this->appConfig->setValue('test2', 'enabled', 'yes');
  431. $this->appConfig->setValue('test2', 'installed_version', '1.0.0');
  432. $this->appConfig->setValue('test3', 'enabled', 'yes');
  433. $this->appConfig->setValue('test3', 'installed_version', '1.0.0');
  434. $this->appConfig->setValue('test4', 'enabled', 'yes');
  435. $this->appConfig->setValue('test4', 'installed_version', '2.4.0');
  436. $apps = $manager->getAppsNeedingUpgrade('8.2.0');
  437. $this->assertCount(2, $apps);
  438. $this->assertEquals('test1', $apps[0]['id']);
  439. $this->assertEquals('test4', $apps[1]['id']);
  440. }
  441. public function testGetIncompatibleApps() {
  442. /** @var AppManager|MockObject $manager */
  443. $manager = $this->getMockBuilder(AppManager::class)
  444. ->setConstructorArgs([$this->userSession, $this->config, $this->appConfig, $this->groupManager, $this->cacheFactory, $this->eventDispatcher, $this->logger])
  445. ->setMethods(['getAppInfo'])
  446. ->getMock();
  447. $appInfos = [
  448. 'cloud_federation_api' => ['id' => 'cloud_federation_api'],
  449. 'dav' => ['id' => 'dav'],
  450. 'files' => ['id' => 'files'],
  451. 'federatedfilesharing' => ['id' => 'federatedfilesharing'],
  452. 'provisioning_api' => ['id' => 'provisioning_api'],
  453. 'lookup_server_connector' => ['id' => 'lookup_server_connector'],
  454. 'test1' => ['id' => 'test1', 'version' => '1.0.1', 'requiremax' => '8.0.0'],
  455. 'test2' => ['id' => 'test2', 'version' => '1.0.0', 'requiremin' => '8.2.0'],
  456. 'test3' => ['id' => 'test3', 'version' => '1.2.4', 'requiremin' => '9.0.0'],
  457. 'settings' => ['id' => 'settings'],
  458. 'testnoversion' => ['id' => 'testnoversion', 'requiremin' => '8.2.0'],
  459. 'twofactor_backupcodes' => ['id' => 'twofactor_backupcodes'],
  460. 'workflowengine' => ['id' => 'workflowengine'],
  461. 'oauth2' => ['id' => 'oauth2'],
  462. 'viewer' => ['id' => 'viewer'],
  463. ];
  464. $manager->expects($this->any())
  465. ->method('getAppInfo')
  466. ->willReturnCallback(
  467. function ($appId) use ($appInfos) {
  468. return $appInfos[$appId];
  469. }
  470. );
  471. $this->appConfig->setValue('test1', 'enabled', 'yes');
  472. $this->appConfig->setValue('test2', 'enabled', 'yes');
  473. $this->appConfig->setValue('test3', 'enabled', 'yes');
  474. $apps = $manager->getIncompatibleApps('8.2.0');
  475. $this->assertCount(2, $apps);
  476. $this->assertEquals('test1', $apps[0]['id']);
  477. $this->assertEquals('test3', $apps[1]['id']);
  478. }
  479. public function testGetEnabledAppsForGroup() {
  480. $group = $this->createMock(IGroup::class);
  481. $group->expects($this->any())
  482. ->method('getGID')
  483. ->willReturn('foo');
  484. $this->appConfig->setValue('test1', 'enabled', 'yes');
  485. $this->appConfig->setValue('test2', 'enabled', 'no');
  486. $this->appConfig->setValue('test3', 'enabled', '["foo"]');
  487. $this->appConfig->setValue('test4', 'enabled', '["asd"]');
  488. $enabled = [
  489. 'cloud_federation_api',
  490. 'dav',
  491. 'federatedfilesharing',
  492. 'files',
  493. 'lookup_server_connector',
  494. 'oauth2',
  495. 'provisioning_api',
  496. 'settings',
  497. 'test1',
  498. 'test3',
  499. 'twofactor_backupcodes',
  500. 'viewer',
  501. 'workflowengine',
  502. ];
  503. $this->assertEquals($enabled, $this->manager->getEnabledAppsForGroup($group));
  504. }
  505. public function testGetAppRestriction() {
  506. $this->appConfig->setValue('test1', 'enabled', 'yes');
  507. $this->appConfig->setValue('test2', 'enabled', 'no');
  508. $this->appConfig->setValue('test3', 'enabled', '["foo"]');
  509. $this->assertEquals([], $this->manager->getAppRestriction('test1'));
  510. $this->assertEquals([], $this->manager->getAppRestriction('test2'));
  511. $this->assertEquals(['foo'], $this->manager->getAppRestriction('test3'));
  512. }
  513. }