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.

ManagerTest.php 13KB

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