app.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. <?php
  2. /**
  3. * Copyright (c) 2012 Bernhard Posselt <dev@bernhard-posselt.com>
  4. * Copyright (c) 2014 Vincent Petry <pvince81@owncloud.com>
  5. * This file is licensed under the Affero General Public License version 3 or
  6. * later.
  7. * See the COPYING-README file.
  8. */
  9. class Test_App extends PHPUnit_Framework_TestCase {
  10. private $oldAppConfigService;
  11. const TEST_USER1 = 'user1';
  12. const TEST_USER2 = 'user2';
  13. const TEST_USER3 = 'user3';
  14. const TEST_GROUP1 = 'group1';
  15. const TEST_GROUP2 = 'group2';
  16. function appVersionsProvider() {
  17. return array(
  18. // exact match
  19. array(
  20. '6.0.0.0',
  21. array(
  22. 'requiremin' => '6.0',
  23. 'requiremax' => '6.0',
  24. ),
  25. true
  26. ),
  27. // in-between match
  28. array(
  29. '6.0.0.0',
  30. array(
  31. 'requiremin' => '5.0',
  32. 'requiremax' => '7.0',
  33. ),
  34. true
  35. ),
  36. // app too old
  37. array(
  38. '6.0.0.0',
  39. array(
  40. 'requiremin' => '5.0',
  41. 'requiremax' => '5.0',
  42. ),
  43. false
  44. ),
  45. // app too new
  46. array(
  47. '5.0.0.0',
  48. array(
  49. 'requiremin' => '6.0',
  50. 'requiremax' => '6.0',
  51. ),
  52. false
  53. ),
  54. // only min specified
  55. array(
  56. '6.0.0.0',
  57. array(
  58. 'requiremin' => '6.0',
  59. ),
  60. true
  61. ),
  62. // only min specified fail
  63. array(
  64. '5.0.0.0',
  65. array(
  66. 'requiremin' => '6.0',
  67. ),
  68. false
  69. ),
  70. // only min specified legacy
  71. array(
  72. '6.0.0.0',
  73. array(
  74. 'require' => '6.0',
  75. ),
  76. true
  77. ),
  78. // only min specified legacy fail
  79. array(
  80. '4.0.0.0',
  81. array(
  82. 'require' => '6.0',
  83. ),
  84. false
  85. ),
  86. // only max specified
  87. array(
  88. '5.0.0.0',
  89. array(
  90. 'requiremax' => '6.0',
  91. ),
  92. true
  93. ),
  94. // only max specified fail
  95. array(
  96. '7.0.0.0',
  97. array(
  98. 'requiremax' => '6.0',
  99. ),
  100. false
  101. ),
  102. // variations of versions
  103. // single OC number
  104. array(
  105. '4',
  106. array(
  107. 'require' => '4.0',
  108. ),
  109. true
  110. ),
  111. // multiple OC number
  112. array(
  113. '4.3.1',
  114. array(
  115. 'require' => '4.3',
  116. ),
  117. true
  118. ),
  119. // single app number
  120. array(
  121. '4',
  122. array(
  123. 'require' => '4',
  124. ),
  125. true
  126. ),
  127. // single app number fail
  128. array(
  129. '4.3',
  130. array(
  131. 'require' => '5',
  132. ),
  133. false
  134. ),
  135. // complex
  136. array(
  137. '5.0.0',
  138. array(
  139. 'require' => '4.5.1',
  140. ),
  141. true
  142. ),
  143. // complex fail
  144. array(
  145. '4.3.1',
  146. array(
  147. 'require' => '4.3.2',
  148. ),
  149. false
  150. ),
  151. // two numbers
  152. array(
  153. '4.3.1',
  154. array(
  155. 'require' => '4.4',
  156. ),
  157. false
  158. ),
  159. // one number fail
  160. array(
  161. '4.3.1',
  162. array(
  163. 'require' => '5',
  164. ),
  165. false
  166. ),
  167. // pre-alpha app
  168. array(
  169. '5.0.3',
  170. array(
  171. 'require' => '4.93',
  172. ),
  173. true
  174. ),
  175. // pre-alpha OC
  176. array(
  177. '6.90.0.2',
  178. array(
  179. 'require' => '6.90',
  180. ),
  181. true
  182. ),
  183. // pre-alpha OC max
  184. array(
  185. '6.90.0.2',
  186. array(
  187. 'requiremax' => '7',
  188. ),
  189. true
  190. ),
  191. // expect same major number match
  192. array(
  193. '5.0.3',
  194. array(
  195. 'require' => '5',
  196. ),
  197. true
  198. ),
  199. // expect same major number match
  200. array(
  201. '5.0.3',
  202. array(
  203. 'requiremax' => '5',
  204. ),
  205. true
  206. ),
  207. );
  208. }
  209. /**
  210. * @dataProvider appVersionsProvider
  211. */
  212. public function testIsAppCompatible($ocVersion, $appInfo, $expectedResult) {
  213. $this->assertEquals($expectedResult, OC_App::isAppCompatible($ocVersion, $appInfo));
  214. }
  215. /**
  216. * Test that the isAppCompatible method also supports passing an array
  217. * as $ocVersion
  218. */
  219. public function testIsAppCompatibleWithArray() {
  220. $ocVersion = array(6);
  221. $appInfo = array(
  222. 'requiremin' => '6',
  223. 'requiremax' => '6',
  224. );
  225. $this->assertTrue(OC_App::isAppCompatible($ocVersion, $appInfo));
  226. }
  227. /**
  228. * Tests that the app order is correct
  229. */
  230. public function testGetEnabledAppsIsSorted() {
  231. $apps = \OC_App::getEnabledApps(true);
  232. // copy array
  233. $sortedApps = $apps;
  234. sort($sortedApps);
  235. // 'files' is always on top
  236. unset($sortedApps[array_search('files', $sortedApps)]);
  237. array_unshift($sortedApps, 'files');
  238. $this->assertEquals($sortedApps, $apps);
  239. }
  240. /**
  241. * Providers for the app config values
  242. */
  243. function appConfigValuesProvider() {
  244. return array(
  245. // logged in user1
  246. array(
  247. self::TEST_USER1,
  248. array(
  249. 'files',
  250. 'app1',
  251. 'app3',
  252. 'appforgroup1',
  253. 'appforgroup12',
  254. ),
  255. false
  256. ),
  257. // logged in user2
  258. array(
  259. self::TEST_USER2,
  260. array(
  261. 'files',
  262. 'app1',
  263. 'app3',
  264. 'appforgroup12',
  265. 'appforgroup2',
  266. ),
  267. false
  268. ),
  269. // logged in user3
  270. array(
  271. self::TEST_USER3,
  272. array(
  273. 'files',
  274. 'app1',
  275. 'app3',
  276. 'appforgroup1',
  277. 'appforgroup12',
  278. 'appforgroup2',
  279. ),
  280. false
  281. ),
  282. // no user, returns all apps
  283. array(
  284. null,
  285. array(
  286. 'files',
  287. 'app1',
  288. 'app3',
  289. 'appforgroup1',
  290. 'appforgroup12',
  291. 'appforgroup2',
  292. ),
  293. false,
  294. ),
  295. // user given, but ask for all
  296. array(
  297. self::TEST_USER1,
  298. array(
  299. 'files',
  300. 'app1',
  301. 'app3',
  302. 'appforgroup1',
  303. 'appforgroup12',
  304. 'appforgroup2',
  305. ),
  306. true,
  307. ),
  308. );
  309. }
  310. /**
  311. * Test enabled apps
  312. *
  313. * @dataProvider appConfigValuesProvider
  314. */
  315. public function testEnabledApps($user, $expectedApps, $forceAll) {
  316. $userManager = \OC::$server->getUserManager();
  317. $groupManager = \OC::$server->getGroupManager();
  318. $user1 = $userManager->createUser(self::TEST_USER1, self::TEST_USER1);
  319. $user2 = $userManager->createUser(self::TEST_USER2, self::TEST_USER2);
  320. $user3 = $userManager->createUser(self::TEST_USER3, self::TEST_USER3);
  321. $group1 = $groupManager->createGroup(self::TEST_GROUP1);
  322. $group1->addUser($user1);
  323. $group1->addUser($user3);
  324. $group2 = $groupManager->createGroup(self::TEST_GROUP2);
  325. $group2->addUser($user2);
  326. $group2->addUser($user3);
  327. \OC_User::setUserId($user);
  328. $this->setupAppConfigMock()->expects($this->once())
  329. ->method('getValues')
  330. ->will($this->returnValue(
  331. array(
  332. 'app3' => 'yes',
  333. 'app2' => 'no',
  334. 'app1' => 'yes',
  335. 'appforgroup1' => '["group1"]',
  336. 'appforgroup2' => '["group2"]',
  337. 'appforgroup12' => '["group2","group1"]',
  338. )
  339. )
  340. );
  341. $apps = \OC_App::getEnabledApps(true, $forceAll);
  342. $this->assertEquals($expectedApps, $apps);
  343. $this->restoreAppConfig();
  344. \OC_User::setUserId(null);
  345. $user1->delete();
  346. $user2->delete();
  347. $user3->delete();
  348. // clear user cache...
  349. $userManager->delete(self::TEST_USER1);
  350. $userManager->delete(self::TEST_USER2);
  351. $userManager->delete(self::TEST_USER3);
  352. $group1->delete();
  353. $group2->delete();
  354. }
  355. /**
  356. * Test isEnabledApps() with cache, not re-reading the list of
  357. * enabled apps more than once when a user is set.
  358. */
  359. public function testEnabledAppsCache() {
  360. $userManager = \OC::$server->getUserManager();
  361. $user1 = $userManager->createUser(self::TEST_USER1, self::TEST_USER1);
  362. \OC_User::setUserId(self::TEST_USER1);
  363. $this->setupAppConfigMock()->expects($this->once())
  364. ->method('getValues')
  365. ->will($this->returnValue(
  366. array(
  367. 'app3' => 'yes',
  368. 'app2' => 'no',
  369. )
  370. )
  371. );
  372. $apps = \OC_App::getEnabledApps(true);
  373. $this->assertEquals(array('files', 'app3'), $apps);
  374. // mock should not be called again here
  375. $apps = \OC_App::getEnabledApps(false);
  376. $this->assertEquals(array('files', 'app3'), $apps);
  377. $this->restoreAppConfig();
  378. \OC_User::setUserId(null);
  379. $user1->delete();
  380. // clear user cache...
  381. $userManager->delete(self::TEST_USER1);
  382. }
  383. /**
  384. * Tests that the apps list is re-requested (not cached) when
  385. * no user is set.
  386. */
  387. public function testEnabledAppsNoCache() {
  388. $this->setupAppConfigMock()->expects($this->exactly(2))
  389. ->method('getValues')
  390. ->will($this->returnValue(
  391. array(
  392. 'app3' => 'yes',
  393. 'app2' => 'no',
  394. )
  395. )
  396. );
  397. $apps = \OC_App::getEnabledApps(true);
  398. $this->assertEquals(array('files', 'app3'), $apps);
  399. // mock should be called again here
  400. $apps = \OC_App::getEnabledApps(false);
  401. $this->assertEquals(array('files', 'app3'), $apps);
  402. $this->restoreAppConfig();
  403. }
  404. private function setupAppConfigMock() {
  405. $appConfig = $this->getMock(
  406. '\OC\AppConfig',
  407. array('getValues'),
  408. array(\OC_DB::getConnection()),
  409. '',
  410. false
  411. );
  412. $this->registerAppConfig($appConfig);
  413. return $appConfig;
  414. }
  415. /**
  416. * Register an app config mock for testing purposes.
  417. * @param $appConfig app config mock
  418. */
  419. private function registerAppConfig($appConfig) {
  420. $this->oldAppConfigService = \OC::$server->query('AppConfig');
  421. \OC::$server->registerService('AppConfig', function ($c) use ($appConfig) {
  422. return $appConfig;
  423. });
  424. }
  425. /**
  426. * Restore the original app config service.
  427. */
  428. private function restoreAppConfig() {
  429. $oldService = $this->oldAppConfigService;
  430. \OC::$server->registerService('AppConfig', function ($c) use ($oldService){
  431. return $oldService;
  432. });
  433. }
  434. }