AppTest.php 12 KB

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