AppTest.php 13 KB

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