AppManagerTest.php 19 KB

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