AppManagerTest.php 14 KB

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