AppTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  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. 'dav',
  334. 'federatedfilesharing',
  335. 'lookup_server_connector',
  336. 'oauth2',
  337. 'provisioning_api',
  338. 'twofactor_backupcodes',
  339. 'workflowengine',
  340. ),
  341. false
  342. ),
  343. // logged in user2
  344. array(
  345. self::TEST_USER2,
  346. array(
  347. 'files',
  348. 'app1',
  349. 'app3',
  350. 'appforgroup12',
  351. 'appforgroup2',
  352. 'dav',
  353. 'federatedfilesharing',
  354. 'lookup_server_connector',
  355. 'oauth2',
  356. 'provisioning_api',
  357. 'twofactor_backupcodes',
  358. 'workflowengine',
  359. ),
  360. false
  361. ),
  362. // logged in user3
  363. array(
  364. self::TEST_USER3,
  365. array(
  366. 'files',
  367. 'app1',
  368. 'app3',
  369. 'appforgroup1',
  370. 'appforgroup12',
  371. 'appforgroup2',
  372. 'dav',
  373. 'federatedfilesharing',
  374. 'lookup_server_connector',
  375. 'oauth2',
  376. 'provisioning_api',
  377. 'twofactor_backupcodes',
  378. 'workflowengine',
  379. ),
  380. false
  381. ),
  382. // no user, returns all apps
  383. array(
  384. null,
  385. array(
  386. 'files',
  387. 'app1',
  388. 'app3',
  389. 'appforgroup1',
  390. 'appforgroup12',
  391. 'appforgroup2',
  392. 'dav',
  393. 'federatedfilesharing',
  394. 'lookup_server_connector',
  395. 'oauth2',
  396. 'provisioning_api',
  397. 'twofactor_backupcodes',
  398. 'workflowengine',
  399. ),
  400. false,
  401. ),
  402. // user given, but ask for all
  403. array(
  404. self::TEST_USER1,
  405. array(
  406. 'files',
  407. 'app1',
  408. 'app3',
  409. 'appforgroup1',
  410. 'appforgroup12',
  411. 'appforgroup2',
  412. 'dav',
  413. 'federatedfilesharing',
  414. 'lookup_server_connector',
  415. 'oauth2',
  416. 'provisioning_api',
  417. 'twofactor_backupcodes',
  418. 'workflowengine',
  419. ),
  420. true,
  421. ),
  422. );
  423. }
  424. /**
  425. * Test enabled apps
  426. *
  427. * @dataProvider appConfigValuesProvider
  428. */
  429. public function testEnabledApps($user, $expectedApps, $forceAll) {
  430. $userManager = \OC::$server->getUserManager();
  431. $groupManager = \OC::$server->getGroupManager();
  432. $user1 = $userManager->createUser(self::TEST_USER1, self::TEST_USER1);
  433. $user2 = $userManager->createUser(self::TEST_USER2, self::TEST_USER2);
  434. $user3 = $userManager->createUser(self::TEST_USER3, self::TEST_USER3);
  435. $group1 = $groupManager->createGroup(self::TEST_GROUP1);
  436. $group1->addUser($user1);
  437. $group1->addUser($user3);
  438. $group2 = $groupManager->createGroup(self::TEST_GROUP2);
  439. $group2->addUser($user2);
  440. $group2->addUser($user3);
  441. \OC_User::setUserId($user);
  442. $this->setupAppConfigMock()->expects($this->once())
  443. ->method('getValues')
  444. ->will($this->returnValue(
  445. array(
  446. 'app3' => 'yes',
  447. 'app2' => 'no',
  448. 'app1' => 'yes',
  449. 'appforgroup1' => '["group1"]',
  450. 'appforgroup2' => '["group2"]',
  451. 'appforgroup12' => '["group2","group1"]',
  452. )
  453. )
  454. );
  455. $apps = \OC_App::getEnabledApps(false, $forceAll);
  456. $this->restoreAppConfig();
  457. \OC_User::setUserId(null);
  458. $user1->delete();
  459. $user2->delete();
  460. $user3->delete();
  461. $group1->delete();
  462. $group2->delete();
  463. $this->assertEquals($expectedApps, $apps);
  464. }
  465. /**
  466. * Test isEnabledApps() with cache, not re-reading the list of
  467. * enabled apps more than once when a user is set.
  468. */
  469. public function testEnabledAppsCache() {
  470. $userManager = \OC::$server->getUserManager();
  471. $user1 = $userManager->createUser(self::TEST_USER1, self::TEST_USER1);
  472. \OC_User::setUserId(self::TEST_USER1);
  473. $this->setupAppConfigMock()->expects($this->once())
  474. ->method('getValues')
  475. ->will($this->returnValue(
  476. array(
  477. 'app3' => 'yes',
  478. 'app2' => 'no',
  479. )
  480. )
  481. );
  482. $apps = \OC_App::getEnabledApps();
  483. $this->assertEquals(array('files', 'app3', 'dav', 'federatedfilesharing', 'lookup_server_connector', 'oauth2', 'provisioning_api', 'twofactor_backupcodes', 'workflowengine'), $apps);
  484. // mock should not be called again here
  485. $apps = \OC_App::getEnabledApps();
  486. $this->assertEquals(array('files', 'app3', 'dav', 'federatedfilesharing', 'lookup_server_connector', 'oauth2', 'provisioning_api', 'twofactor_backupcodes', 'workflowengine'), $apps);
  487. $this->restoreAppConfig();
  488. \OC_User::setUserId(null);
  489. $user1->delete();
  490. }
  491. private function setupAppConfigMock() {
  492. $appConfig = $this->getMockBuilder(AppConfig::class)
  493. ->setMethods(['getValues'])
  494. ->setConstructorArgs([\OC::$server->getDatabaseConnection()])
  495. ->disableOriginalConstructor()
  496. ->getMock();
  497. $this->registerAppConfig($appConfig);
  498. return $appConfig;
  499. }
  500. /**
  501. * Register an app config mock for testing purposes.
  502. *
  503. * @param IAppConfig $appConfig app config mock
  504. */
  505. private function registerAppConfig(AppConfig $appConfig) {
  506. $this->overwriteService('AppConfig', $appConfig);
  507. $this->overwriteService('AppManager', new \OC\App\AppManager(
  508. \OC::$server->getUserSession(),
  509. $appConfig,
  510. \OC::$server->getGroupManager(),
  511. \OC::$server->getMemCacheFactory(),
  512. \OC::$server->getEventDispatcher()
  513. ));
  514. }
  515. /**
  516. * Restore the original app config service.
  517. */
  518. private function restoreAppConfig() {
  519. $this->restoreService('AppConfig');
  520. $this->restoreService('AppManager');
  521. // Remove the cache of the mocked apps list with a forceRefresh
  522. \OC_App::getEnabledApps();
  523. }
  524. /**
  525. * Providers for the app data values
  526. */
  527. public function appDataProvider() {
  528. return [
  529. [
  530. ['description' => " \t This is a multiline \n test with \n \t \n \n some new lines "],
  531. ['description' => "This is a multiline \n test with \n \t \n \n some new lines"],
  532. ],
  533. [
  534. ['description' => " \t This is a multiline \n test with \n \t some new lines "],
  535. ['description' => "This is a multiline \n test with \n \t some new lines"],
  536. ],
  537. [
  538. ['description' => hex2bin('5065726d657420646520732761757468656e7469666965722064616e732070697769676f20646972656374656d656e74206176656320736573206964656e74696669616e7473206f776e636c6f75642073616e73206c65732072657461706572206574206d657420c3a0206a6f757273206365757820636920656e20636173206465206368616e67656d656e74206465206d6f742064652070617373652e0d0a0d')],
  539. ['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."],
  540. ],
  541. [
  542. ['not-a-description' => " \t This is a multiline \n test with \n \t some new lines "],
  543. [
  544. 'not-a-description' => " \t This is a multiline \n test with \n \t some new lines ",
  545. 'description' => '',
  546. ],
  547. ],
  548. [
  549. ['description' => [100, 'bla']],
  550. ['description' => ''],
  551. ],
  552. ];
  553. }
  554. /**
  555. * Test app info parser
  556. *
  557. * @dataProvider appDataProvider
  558. * @param array $data
  559. * @param array $expected
  560. */
  561. public function testParseAppInfo(array $data, array $expected) {
  562. $this->assertSame($expected, \OC_App::parseAppInfo($data));
  563. }
  564. public function testParseAppInfoL10N() {
  565. $parser = new InfoParser();
  566. $data = $parser->parse(\OC::$SERVERROOT. "/tests/data/app/description-multi-lang.xml");
  567. $this->assertEquals('English', \OC_App::parseAppInfo($data, 'en')['description']);
  568. $this->assertEquals('German', \OC_App::parseAppInfo($data, 'de')['description']);
  569. }
  570. public function testParseAppInfoL10NSingleLanguage() {
  571. $parser = new InfoParser();
  572. $data = $parser->parse(\OC::$SERVERROOT. "/tests/data/app/description-single-lang.xml");
  573. $this->assertEquals('English', \OC_App::parseAppInfo($data, 'en')['description']);
  574. }
  575. }