1
0

AppTest.php 13 KB

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