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.

manager.php 12KB

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