AppTest.php 12 KB

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