You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AppManagerTest.php 20KB

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