AppTest.php 13 KB

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