app.php 10 KB

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