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.

427 lines
13 KiB

10 years ago
10 years ago
10 years ago
10 years ago
  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 = [
  265. 'dav',
  266. 'federatedfilesharing',
  267. 'files',
  268. 'provisioning_api',
  269. 'test1',
  270. 'test3',
  271. 'twofactor_backupcodes',
  272. 'workflowengine',
  273. ];
  274. $this->assertEquals($apps, $this->manager->getInstalledApps());
  275. }
  276. public function testGetAppsForUser() {
  277. $user = $this->newUser('user1');
  278. $this->groupManager->expects($this->any())
  279. ->method('getUserGroupIds')
  280. ->with($user)
  281. ->will($this->returnValue(array('foo', 'bar')));
  282. $this->appConfig->setValue('test1', 'enabled', 'yes');
  283. $this->appConfig->setValue('test2', 'enabled', 'no');
  284. $this->appConfig->setValue('test3', 'enabled', '["foo"]');
  285. $this->appConfig->setValue('test4', 'enabled', '["asd"]');
  286. $enabled = [
  287. 'dav',
  288. 'federatedfilesharing',
  289. 'files',
  290. 'provisioning_api',
  291. 'test1',
  292. 'test3',
  293. 'twofactor_backupcodes',
  294. 'workflowengine'
  295. ];
  296. $this->assertEquals($enabled, $this->manager->getEnabledAppsForUser($user));
  297. }
  298. public function testGetAppsNeedingUpgrade() {
  299. $this->manager = $this->getMockBuilder('\OC\App\AppManager')
  300. ->setConstructorArgs([$this->userSession, $this->appConfig, $this->groupManager, $this->cacheFactory, $this->eventDispatcher])
  301. ->setMethods(['getAppInfo'])
  302. ->getMock();
  303. $appInfos = [
  304. 'dav' => ['id' => 'dav'],
  305. 'files' => ['id' => 'files'],
  306. 'federatedfilesharing' => ['id' => 'federatedfilesharing'],
  307. 'provisioning_api' => ['id' => 'provisioning_api'],
  308. 'test1' => ['id' => 'test1', 'version' => '1.0.1', 'requiremax' => '9.0.0'],
  309. 'test2' => ['id' => 'test2', 'version' => '1.0.0', 'requiremin' => '8.2.0'],
  310. 'test3' => ['id' => 'test3', 'version' => '1.2.4', 'requiremin' => '9.0.0'],
  311. 'test4' => ['id' => 'test4', 'version' => '3.0.0', 'requiremin' => '8.1.0'],
  312. 'testnoversion' => ['id' => 'testnoversion', 'requiremin' => '8.2.0'],
  313. 'twofactor_backupcodes' => ['id' => 'twofactor_backupcodes'],
  314. 'workflowengine' => ['id' => 'workflowengine'],
  315. ];
  316. $this->manager->expects($this->any())
  317. ->method('getAppInfo')
  318. ->will($this->returnCallback(
  319. function($appId) use ($appInfos) {
  320. return $appInfos[$appId];
  321. }
  322. ));
  323. $this->appConfig->setValue('test1', 'enabled', 'yes');
  324. $this->appConfig->setValue('test1', 'installed_version', '1.0.0');
  325. $this->appConfig->setValue('test2', 'enabled', 'yes');
  326. $this->appConfig->setValue('test2', 'installed_version', '1.0.0');
  327. $this->appConfig->setValue('test3', 'enabled', 'yes');
  328. $this->appConfig->setValue('test3', 'installed_version', '1.0.0');
  329. $this->appConfig->setValue('test4', 'enabled', 'yes');
  330. $this->appConfig->setValue('test4', 'installed_version', '2.4.0');
  331. $apps = $this->manager->getAppsNeedingUpgrade('8.2.0');
  332. $this->assertCount(2, $apps);
  333. $this->assertEquals('test1', $apps[0]['id']);
  334. $this->assertEquals('test4', $apps[1]['id']);
  335. }
  336. public function testGetIncompatibleApps() {
  337. $this->manager = $this->getMockBuilder('\OC\App\AppManager')
  338. ->setConstructorArgs([$this->userSession, $this->appConfig, $this->groupManager, $this->cacheFactory, $this->eventDispatcher])
  339. ->setMethods(['getAppInfo'])
  340. ->getMock();
  341. $appInfos = [
  342. 'dav' => ['id' => 'dav'],
  343. 'files' => ['id' => 'files'],
  344. 'federatedfilesharing' => ['id' => 'federatedfilesharing'],
  345. 'provisioning_api' => ['id' => 'provisioning_api'],
  346. 'test1' => ['id' => 'test1', 'version' => '1.0.1', 'requiremax' => '8.0.0'],
  347. 'test2' => ['id' => 'test2', 'version' => '1.0.0', 'requiremin' => '8.2.0'],
  348. 'test3' => ['id' => 'test3', 'version' => '1.2.4', 'requiremin' => '9.0.0'],
  349. 'testnoversion' => ['id' => 'testnoversion', 'requiremin' => '8.2.0'],
  350. 'twofactor_backupcodes' => ['id' => 'twofactor_backupcodes'],
  351. 'workflowengine' => ['id' => 'workflowengine'],
  352. ];
  353. $this->manager->expects($this->any())
  354. ->method('getAppInfo')
  355. ->will($this->returnCallback(
  356. function($appId) use ($appInfos) {
  357. return $appInfos[$appId];
  358. }
  359. ));
  360. $this->appConfig->setValue('test1', 'enabled', 'yes');
  361. $this->appConfig->setValue('test2', 'enabled', 'yes');
  362. $this->appConfig->setValue('test3', 'enabled', 'yes');
  363. $apps = $this->manager->getIncompatibleApps('8.2.0');
  364. $this->assertCount(2, $apps);
  365. $this->assertEquals('test1', $apps[0]['id']);
  366. $this->assertEquals('test3', $apps[1]['id']);
  367. }
  368. }