AppManagerTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. <?php
  2. /**
  3. * Copyright (c) 2014 Robin Appelman <icewind@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace Test\App;
  9. use OC\App\AppManager;
  10. use OC\Group\Group;
  11. use OC\User\User;
  12. use OCP\App\AppPathNotFoundException;
  13. use OCP\App\IAppManager;
  14. use OCP\ICache;
  15. use OCP\ICacheFactory;
  16. use OCP\IGroupManager;
  17. use OCP\IUserSession;
  18. use OCP\IAppConfig;
  19. use OCP\IConfig;
  20. use OCP\IURLGenerator;
  21. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  22. use Test\TestCase;
  23. /**
  24. * Class AppManagerTest
  25. *
  26. * @package Test\App
  27. */
  28. class AppManagerTest extends TestCase {
  29. /**
  30. * @return IAppConfig|\PHPUnit_Framework_MockObject_MockObject
  31. */
  32. protected function getAppConfig() {
  33. $appConfig = array();
  34. $config = $this->createMock(IAppConfig::class);
  35. $config->expects($this->any())
  36. ->method('getValue')
  37. ->will($this->returnCallback(function ($app, $key, $default) use (&$appConfig) {
  38. return (isset($appConfig[$app]) and isset($appConfig[$app][$key])) ? $appConfig[$app][$key] : $default;
  39. }));
  40. $config->expects($this->any())
  41. ->method('setValue')
  42. ->will($this->returnCallback(function ($app, $key, $value) use (&$appConfig) {
  43. if (!isset($appConfig[$app])) {
  44. $appConfig[$app] = array();
  45. }
  46. $appConfig[$app][$key] = $value;
  47. }));
  48. $config->expects($this->any())
  49. ->method('getValues')
  50. ->will($this->returnCallback(function ($app, $key) use (&$appConfig) {
  51. if ($app) {
  52. return $appConfig[$app];
  53. } else {
  54. $values = array();
  55. foreach ($appConfig as $appid => $appData) {
  56. if (isset($appData[$key])) {
  57. $values[$appid] = $appData[$key];
  58. }
  59. }
  60. return $values;
  61. }
  62. }));
  63. return $config;
  64. }
  65. /** @var IUserSession|\PHPUnit_Framework_MockObject_MockObject */
  66. protected $userSession;
  67. /** @var IGroupManager|\PHPUnit_Framework_MockObject_MockObject */
  68. protected $groupManager;
  69. /** @var IAppConfig|\PHPUnit_Framework_MockObject_MockObject */
  70. protected $appConfig;
  71. /** @var ICache|\PHPUnit_Framework_MockObject_MockObject */
  72. protected $cache;
  73. /** @var ICacheFactory|\PHPUnit_Framework_MockObject_MockObject */
  74. protected $cacheFactory;
  75. /** @var EventDispatcherInterface|\PHPUnit_Framework_MockObject_MockObject */
  76. protected $eventDispatcher;
  77. /** @var IAppManager */
  78. protected $manager;
  79. protected function setUp() {
  80. parent::setUp();
  81. $this->userSession = $this->createMock(IUserSession::class);
  82. $this->groupManager = $this->createMock(IGroupManager::class);
  83. $this->appConfig = $this->getAppConfig();
  84. $this->cacheFactory = $this->createMock(ICacheFactory::class);
  85. $this->cache = $this->createMock(ICache::class);
  86. $this->eventDispatcher = $this->createMock(EventDispatcherInterface::class);
  87. $this->cacheFactory->expects($this->any())
  88. ->method('createDistributed')
  89. ->with('settings')
  90. ->willReturn($this->cache);
  91. $this->manager = new AppManager($this->userSession, $this->appConfig, $this->groupManager, $this->cacheFactory, $this->eventDispatcher);
  92. }
  93. protected function expectClearCache() {
  94. $this->cache->expects($this->once())
  95. ->method('clear')
  96. ->with('listApps');
  97. }
  98. public function testEnableApp() {
  99. $this->expectClearCache();
  100. // making sure "files_trashbin" is disabled
  101. if ($this->manager->isEnabledForUser('files_trashbin')) {
  102. $this->manager->disableApp('files_trashbin');
  103. }
  104. $this->manager->enableApp('files_trashbin');
  105. $this->assertEquals('yes', $this->appConfig->getValue('files_trashbin', 'enabled', 'no'));
  106. }
  107. public function testDisableApp() {
  108. $this->expectClearCache();
  109. $this->manager->disableApp('files_trashbin');
  110. $this->assertEquals('no', $this->appConfig->getValue('files_trashbin', 'enabled', 'no'));
  111. }
  112. public function testNotEnableIfNotInstalled() {
  113. try {
  114. $this->manager->enableApp('some_random_name_which_i_hope_is_not_an_app');
  115. $this->assertFalse(true, 'If this line is reached the expected exception is not thrown.');
  116. } catch (AppPathNotFoundException $e) {
  117. // Exception is expected
  118. $this->assertEquals('Could not find path for some_random_name_which_i_hope_is_not_an_app', $e->getMessage());
  119. }
  120. $this->assertEquals('no', $this->appConfig->getValue(
  121. 'some_random_name_which_i_hope_is_not_an_app', 'enabled', 'no'
  122. ));
  123. }
  124. public function testEnableAppForGroups() {
  125. $groups = array(
  126. new Group('group1', array(), null),
  127. new Group('group2', array(), null)
  128. );
  129. $this->expectClearCache();
  130. $this->manager->enableAppForGroups('test', $groups);
  131. $this->assertEquals('["group1","group2"]', $this->appConfig->getValue('test', 'enabled', 'no'));
  132. }
  133. public function dataEnableAppForGroupsAllowedTypes() {
  134. return [
  135. [[]],
  136. [[
  137. 'types' => [],
  138. ]],
  139. [[
  140. 'types' => ['nickvergessen'],
  141. ]],
  142. ];
  143. }
  144. /**
  145. * @dataProvider dataEnableAppForGroupsAllowedTypes
  146. *
  147. * @param array $appInfo
  148. */
  149. public function testEnableAppForGroupsAllowedTypes(array $appInfo) {
  150. $groups = array(
  151. new Group('group1', array(), null),
  152. new Group('group2', array(), null)
  153. );
  154. $this->expectClearCache();
  155. /** @var AppManager|\PHPUnit_Framework_MockObject_MockObject $manager */
  156. $manager = $this->getMockBuilder(AppManager::class)
  157. ->setConstructorArgs([
  158. $this->userSession, $this->appConfig, $this->groupManager, $this->cacheFactory, $this->eventDispatcher
  159. ])
  160. ->setMethods([
  161. 'getAppInfo'
  162. ])
  163. ->getMock();
  164. $manager->expects($this->once())
  165. ->method('getAppInfo')
  166. ->with('test')
  167. ->willReturn($appInfo);
  168. $manager->enableAppForGroups('test', $groups);
  169. $this->assertEquals('["group1","group2"]', $this->appConfig->getValue('test', 'enabled', 'no'));
  170. }
  171. public function dataEnableAppForGroupsForbiddenTypes() {
  172. return [
  173. ['filesystem'],
  174. ['prelogin'],
  175. ['authentication'],
  176. ['logging'],
  177. ['prevent_group_restriction'],
  178. ];
  179. }
  180. /**
  181. * @dataProvider dataEnableAppForGroupsForbiddenTypes
  182. *
  183. * @param string $type
  184. *
  185. * @expectedException \Exception
  186. * @expectedExceptionMessage test can't be enabled for groups.
  187. */
  188. public function testEnableAppForGroupsForbiddenTypes($type) {
  189. $groups = array(
  190. new Group('group1', array(), null),
  191. new Group('group2', array(), null)
  192. );
  193. /** @var AppManager|\PHPUnit_Framework_MockObject_MockObject $manager */
  194. $manager = $this->getMockBuilder(AppManager::class)
  195. ->setConstructorArgs([
  196. $this->userSession, $this->appConfig, $this->groupManager, $this->cacheFactory, $this->eventDispatcher
  197. ])
  198. ->setMethods([
  199. 'getAppInfo'
  200. ])
  201. ->getMock();
  202. $manager->expects($this->once())
  203. ->method('getAppInfo')
  204. ->with('test')
  205. ->willReturn([
  206. 'types' => [$type],
  207. ]);
  208. $manager->enableAppForGroups('test', $groups);
  209. }
  210. public function testIsInstalledEnabled() {
  211. $this->appConfig->setValue('test', 'enabled', 'yes');
  212. $this->assertTrue($this->manager->isInstalled('test'));
  213. }
  214. public function testIsInstalledDisabled() {
  215. $this->appConfig->setValue('test', 'enabled', 'no');
  216. $this->assertFalse($this->manager->isInstalled('test'));
  217. }
  218. public function testIsInstalledEnabledForGroups() {
  219. $this->appConfig->setValue('test', 'enabled', '["foo"]');
  220. $this->assertTrue($this->manager->isInstalled('test'));
  221. }
  222. private function newUser($uid) {
  223. $config = $this->createMock(IConfig::class);
  224. $urlgenerator = $this->createMock(IURLGenerator::class);
  225. return new User($uid, null, null, $config, $urlgenerator);
  226. }
  227. public function testIsEnabledForUserEnabled() {
  228. $this->appConfig->setValue('test', 'enabled', 'yes');
  229. $user = $this->newUser('user1');
  230. $this->assertTrue($this->manager->isEnabledForUser('test', $user));
  231. }
  232. public function testIsEnabledForUserDisabled() {
  233. $this->appConfig->setValue('test', 'enabled', 'no');
  234. $user = $this->newUser('user1');
  235. $this->assertFalse($this->manager->isEnabledForUser('test', $user));
  236. }
  237. public function testGetAppPath() {
  238. $this->assertEquals(\OC::$SERVERROOT . '/apps/files', $this->manager->getAppPath('files'));
  239. }
  240. public function testGetAppPathFail() {
  241. $this->expectException(AppPathNotFoundException::class);
  242. $this->manager->getAppPath('testnotexisting');
  243. }
  244. public function testIsEnabledForUserEnabledForGroup() {
  245. $user = $this->newUser('user1');
  246. $this->groupManager->expects($this->once())
  247. ->method('getUserGroupIds')
  248. ->with($user)
  249. ->will($this->returnValue(array('foo', 'bar')));
  250. $this->appConfig->setValue('test', 'enabled', '["foo"]');
  251. $this->assertTrue($this->manager->isEnabledForUser('test', $user));
  252. }
  253. public function testIsEnabledForUserDisabledForGroup() {
  254. $user = $this->newUser('user1');
  255. $this->groupManager->expects($this->once())
  256. ->method('getUserGroupIds')
  257. ->with($user)
  258. ->will($this->returnValue(array('bar')));
  259. $this->appConfig->setValue('test', 'enabled', '["foo"]');
  260. $this->assertFalse($this->manager->isEnabledForUser('test', $user));
  261. }
  262. public function testIsEnabledForUserLoggedOut() {
  263. $this->appConfig->setValue('test', 'enabled', '["foo"]');
  264. $this->assertFalse($this->manager->isEnabledForUser('test'));
  265. }
  266. public function testIsEnabledForUserLoggedIn() {
  267. $user = $this->newUser('user1');
  268. $this->userSession->expects($this->once())
  269. ->method('getUser')
  270. ->will($this->returnValue($user));
  271. $this->groupManager->expects($this->once())
  272. ->method('getUserGroupIds')
  273. ->with($user)
  274. ->will($this->returnValue(array('foo', 'bar')));
  275. $this->appConfig->setValue('test', 'enabled', '["foo"]');
  276. $this->assertTrue($this->manager->isEnabledForUser('test'));
  277. }
  278. public function testGetInstalledApps() {
  279. $this->appConfig->setValue('test1', 'enabled', 'yes');
  280. $this->appConfig->setValue('test2', 'enabled', 'no');
  281. $this->appConfig->setValue('test3', 'enabled', '["foo"]');
  282. $apps = [
  283. 'dav',
  284. 'federatedfilesharing',
  285. 'files',
  286. 'lookup_server_connector',
  287. 'oauth2',
  288. 'provisioning_api',
  289. 'test1',
  290. 'test3',
  291. 'twofactor_backupcodes',
  292. 'workflowengine',
  293. ];
  294. $this->assertEquals($apps, $this->manager->getInstalledApps());
  295. }
  296. public function testGetAppsForUser() {
  297. $user = $this->newUser('user1');
  298. $this->groupManager->expects($this->any())
  299. ->method('getUserGroupIds')
  300. ->with($user)
  301. ->will($this->returnValue(array('foo', 'bar')));
  302. $this->appConfig->setValue('test1', 'enabled', 'yes');
  303. $this->appConfig->setValue('test2', 'enabled', 'no');
  304. $this->appConfig->setValue('test3', 'enabled', '["foo"]');
  305. $this->appConfig->setValue('test4', 'enabled', '["asd"]');
  306. $enabled = [
  307. 'dav',
  308. 'federatedfilesharing',
  309. 'files',
  310. 'lookup_server_connector',
  311. 'oauth2',
  312. 'provisioning_api',
  313. 'test1',
  314. 'test3',
  315. 'twofactor_backupcodes',
  316. 'workflowengine',
  317. ];
  318. $this->assertEquals($enabled, $this->manager->getEnabledAppsForUser($user));
  319. }
  320. public function testGetAppsNeedingUpgrade() {
  321. /** @var AppManager|\PHPUnit_Framework_MockObject_MockObject $manager */
  322. $manager = $this->getMockBuilder(AppManager::class)
  323. ->setConstructorArgs([$this->userSession, $this->appConfig, $this->groupManager, $this->cacheFactory, $this->eventDispatcher])
  324. ->setMethods(['getAppInfo'])
  325. ->getMock();
  326. $appInfos = [
  327. 'dav' => ['id' => 'dav'],
  328. 'files' => ['id' => 'files'],
  329. 'federatedfilesharing' => ['id' => 'federatedfilesharing'],
  330. 'provisioning_api' => ['id' => 'provisioning_api'],
  331. 'lookup_server_connector' => ['id' => 'lookup_server_connector'],
  332. 'test1' => ['id' => 'test1', 'version' => '1.0.1', 'requiremax' => '9.0.0'],
  333. 'test2' => ['id' => 'test2', 'version' => '1.0.0', 'requiremin' => '8.2.0'],
  334. 'test3' => ['id' => 'test3', 'version' => '1.2.4', 'requiremin' => '9.0.0'],
  335. 'test4' => ['id' => 'test4', 'version' => '3.0.0', 'requiremin' => '8.1.0'],
  336. 'testnoversion' => ['id' => 'testnoversion', 'requiremin' => '8.2.0'],
  337. 'twofactor_backupcodes' => ['id' => 'twofactor_backupcodes'],
  338. 'workflowengine' => ['id' => 'workflowengine'],
  339. 'oauth2' => ['id' => 'oauth2'],
  340. ];
  341. $manager->expects($this->any())
  342. ->method('getAppInfo')
  343. ->will($this->returnCallback(
  344. function($appId) use ($appInfos) {
  345. return $appInfos[$appId];
  346. }
  347. ));
  348. $this->appConfig->setValue('test1', 'enabled', 'yes');
  349. $this->appConfig->setValue('test1', 'installed_version', '1.0.0');
  350. $this->appConfig->setValue('test2', 'enabled', 'yes');
  351. $this->appConfig->setValue('test2', 'installed_version', '1.0.0');
  352. $this->appConfig->setValue('test3', 'enabled', 'yes');
  353. $this->appConfig->setValue('test3', 'installed_version', '1.0.0');
  354. $this->appConfig->setValue('test4', 'enabled', 'yes');
  355. $this->appConfig->setValue('test4', 'installed_version', '2.4.0');
  356. $apps = $manager->getAppsNeedingUpgrade('8.2.0');
  357. $this->assertCount(2, $apps);
  358. $this->assertEquals('test1', $apps[0]['id']);
  359. $this->assertEquals('test4', $apps[1]['id']);
  360. }
  361. public function testGetIncompatibleApps() {
  362. /** @var AppManager|\PHPUnit_Framework_MockObject_MockObject $manager */
  363. $manager = $this->getMockBuilder(AppManager::class)
  364. ->setConstructorArgs([$this->userSession, $this->appConfig, $this->groupManager, $this->cacheFactory, $this->eventDispatcher])
  365. ->setMethods(['getAppInfo'])
  366. ->getMock();
  367. $appInfos = [
  368. 'dav' => ['id' => 'dav'],
  369. 'files' => ['id' => 'files'],
  370. 'federatedfilesharing' => ['id' => 'federatedfilesharing'],
  371. 'provisioning_api' => ['id' => 'provisioning_api'],
  372. 'lookup_server_connector' => ['id' => 'lookup_server_connector'],
  373. 'test1' => ['id' => 'test1', 'version' => '1.0.1', 'requiremax' => '8.0.0'],
  374. 'test2' => ['id' => 'test2', 'version' => '1.0.0', 'requiremin' => '8.2.0'],
  375. 'test3' => ['id' => 'test3', 'version' => '1.2.4', 'requiremin' => '9.0.0'],
  376. 'testnoversion' => ['id' => 'testnoversion', 'requiremin' => '8.2.0'],
  377. 'twofactor_backupcodes' => ['id' => 'twofactor_backupcodes'],
  378. 'workflowengine' => ['id' => 'workflowengine'],
  379. 'oauth2' => ['id' => 'oauth2'],
  380. ];
  381. $manager->expects($this->any())
  382. ->method('getAppInfo')
  383. ->will($this->returnCallback(
  384. function($appId) use ($appInfos) {
  385. return $appInfos[$appId];
  386. }
  387. ));
  388. $this->appConfig->setValue('test1', 'enabled', 'yes');
  389. $this->appConfig->setValue('test2', 'enabled', 'yes');
  390. $this->appConfig->setValue('test3', 'enabled', 'yes');
  391. $apps = $manager->getIncompatibleApps('8.2.0');
  392. $this->assertCount(2, $apps);
  393. $this->assertEquals('test1', $apps[0]['id']);
  394. $this->assertEquals('test3', $apps[1]['id']);
  395. }
  396. }