AppManagerTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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. 'cloud_federation_api',
  285. 'dav',
  286. 'federatedfilesharing',
  287. 'files',
  288. 'lookup_server_connector',
  289. 'oauth2',
  290. 'provisioning_api',
  291. 'test1',
  292. 'test3',
  293. 'twofactor_backupcodes',
  294. 'workflowengine',
  295. ];
  296. $this->assertEquals($apps, $this->manager->getInstalledApps());
  297. }
  298. public function testGetAppsForUser() {
  299. $user = $this->newUser('user1');
  300. $this->groupManager->expects($this->any())
  301. ->method('getUserGroupIds')
  302. ->with($user)
  303. ->will($this->returnValue(array('foo', 'bar')));
  304. $this->appConfig->setValue('test1', 'enabled', 'yes');
  305. $this->appConfig->setValue('test2', 'enabled', 'no');
  306. $this->appConfig->setValue('test3', 'enabled', '["foo"]');
  307. $this->appConfig->setValue('test4', 'enabled', '["asd"]');
  308. $enabled = [
  309. 'cloud_federation_api',
  310. 'dav',
  311. 'federatedfilesharing',
  312. 'files',
  313. 'lookup_server_connector',
  314. 'oauth2',
  315. 'provisioning_api',
  316. 'test1',
  317. 'test3',
  318. 'twofactor_backupcodes',
  319. 'workflowengine',
  320. ];
  321. $this->assertEquals($enabled, $this->manager->getEnabledAppsForUser($user));
  322. }
  323. public function testGetAppsNeedingUpgrade() {
  324. /** @var AppManager|\PHPUnit_Framework_MockObject_MockObject $manager */
  325. $manager = $this->getMockBuilder(AppManager::class)
  326. ->setConstructorArgs([$this->userSession, $this->appConfig, $this->groupManager, $this->cacheFactory, $this->eventDispatcher])
  327. ->setMethods(['getAppInfo'])
  328. ->getMock();
  329. $appInfos = [
  330. 'cloud_federation_api' => ['id' => 'cloud_federation_api'],
  331. 'dav' => ['id' => 'dav'],
  332. 'files' => ['id' => 'files'],
  333. 'federatedfilesharing' => ['id' => 'federatedfilesharing'],
  334. 'provisioning_api' => ['id' => 'provisioning_api'],
  335. 'lookup_server_connector' => ['id' => 'lookup_server_connector'],
  336. 'test1' => ['id' => 'test1', 'version' => '1.0.1', 'requiremax' => '9.0.0'],
  337. 'test2' => ['id' => 'test2', 'version' => '1.0.0', 'requiremin' => '8.2.0'],
  338. 'test3' => ['id' => 'test3', 'version' => '1.2.4', 'requiremin' => '9.0.0'],
  339. 'test4' => ['id' => 'test4', 'version' => '3.0.0', 'requiremin' => '8.1.0'],
  340. 'testnoversion' => ['id' => 'testnoversion', 'requiremin' => '8.2.0'],
  341. 'twofactor_backupcodes' => ['id' => 'twofactor_backupcodes'],
  342. 'workflowengine' => ['id' => 'workflowengine'],
  343. 'oauth2' => ['id' => 'oauth2'],
  344. ];
  345. $manager->expects($this->any())
  346. ->method('getAppInfo')
  347. ->will($this->returnCallback(
  348. function($appId) use ($appInfos) {
  349. return $appInfos[$appId];
  350. }
  351. ));
  352. $this->appConfig->setValue('test1', 'enabled', 'yes');
  353. $this->appConfig->setValue('test1', 'installed_version', '1.0.0');
  354. $this->appConfig->setValue('test2', 'enabled', 'yes');
  355. $this->appConfig->setValue('test2', 'installed_version', '1.0.0');
  356. $this->appConfig->setValue('test3', 'enabled', 'yes');
  357. $this->appConfig->setValue('test3', 'installed_version', '1.0.0');
  358. $this->appConfig->setValue('test4', 'enabled', 'yes');
  359. $this->appConfig->setValue('test4', 'installed_version', '2.4.0');
  360. $apps = $manager->getAppsNeedingUpgrade('8.2.0');
  361. $this->assertCount(2, $apps);
  362. $this->assertEquals('test1', $apps[0]['id']);
  363. $this->assertEquals('test4', $apps[1]['id']);
  364. }
  365. public function testGetIncompatibleApps() {
  366. /** @var AppManager|\PHPUnit_Framework_MockObject_MockObject $manager */
  367. $manager = $this->getMockBuilder(AppManager::class)
  368. ->setConstructorArgs([$this->userSession, $this->appConfig, $this->groupManager, $this->cacheFactory, $this->eventDispatcher])
  369. ->setMethods(['getAppInfo'])
  370. ->getMock();
  371. $appInfos = [
  372. 'cloud_federation_api' => ['id' => 'cloud_federation_api'],
  373. 'dav' => ['id' => 'dav'],
  374. 'files' => ['id' => 'files'],
  375. 'federatedfilesharing' => ['id' => 'federatedfilesharing'],
  376. 'provisioning_api' => ['id' => 'provisioning_api'],
  377. 'lookup_server_connector' => ['id' => 'lookup_server_connector'],
  378. 'test1' => ['id' => 'test1', 'version' => '1.0.1', 'requiremax' => '8.0.0'],
  379. 'test2' => ['id' => 'test2', 'version' => '1.0.0', 'requiremin' => '8.2.0'],
  380. 'test3' => ['id' => 'test3', 'version' => '1.2.4', 'requiremin' => '9.0.0'],
  381. 'testnoversion' => ['id' => 'testnoversion', 'requiremin' => '8.2.0'],
  382. 'twofactor_backupcodes' => ['id' => 'twofactor_backupcodes'],
  383. 'workflowengine' => ['id' => 'workflowengine'],
  384. 'oauth2' => ['id' => 'oauth2'],
  385. ];
  386. $manager->expects($this->any())
  387. ->method('getAppInfo')
  388. ->will($this->returnCallback(
  389. function($appId) use ($appInfos) {
  390. return $appInfos[$appId];
  391. }
  392. ));
  393. $this->appConfig->setValue('test1', 'enabled', 'yes');
  394. $this->appConfig->setValue('test2', 'enabled', 'yes');
  395. $this->appConfig->setValue('test3', 'enabled', 'yes');
  396. $apps = $manager->getIncompatibleApps('8.2.0');
  397. $this->assertCount(2, $apps);
  398. $this->assertEquals('test1', $apps[0]['id']);
  399. $this->assertEquals('test3', $apps[1]['id']);
  400. }
  401. }