1
0

AppTest.php 13 KB

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