AppTest.php 13 KB

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