1
0

AppTest.php 12 KB

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