1
0

DependencyAnalyzerTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2023 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2014-2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace Test\App;
  8. use OC\App\DependencyAnalyzer;
  9. use OC\App\Platform;
  10. use OCP\IL10N;
  11. use Test\TestCase;
  12. class DependencyAnalyzerTest extends TestCase {
  13. /** @var Platform|\PHPUnit\Framework\MockObject\MockObject */
  14. private $platformMock;
  15. /** @var IL10N */
  16. private $l10nMock;
  17. /** @var DependencyAnalyzer */
  18. private $analyser;
  19. protected function setUp(): void {
  20. $this->platformMock = $this->getMockBuilder(Platform::class)
  21. ->disableOriginalConstructor()
  22. ->getMock();
  23. $this->platformMock->expects($this->any())
  24. ->method('getPhpVersion')
  25. ->willReturn('5.4.3');
  26. $this->platformMock->expects($this->any())
  27. ->method('getIntSize')
  28. ->willReturn(4);
  29. $this->platformMock->expects($this->any())
  30. ->method('getDatabase')
  31. ->willReturn('mysql');
  32. $this->platformMock->expects($this->any())
  33. ->method('getOS')
  34. ->willReturn('Linux');
  35. $this->platformMock->expects($this->any())
  36. ->method('isCommandKnown')
  37. ->willReturnCallback(function ($command) {
  38. return ($command === 'grep');
  39. });
  40. $this->platformMock->expects($this->any())
  41. ->method('getLibraryVersion')
  42. ->willReturnCallback(function ($lib) {
  43. if ($lib === 'curl') {
  44. return '2.3.4';
  45. }
  46. return null;
  47. });
  48. $this->platformMock->expects($this->any())
  49. ->method('getOcVersion')
  50. ->willReturn('8.0.2');
  51. $this->l10nMock = $this->getMockBuilder(IL10N::class)
  52. ->disableOriginalConstructor()
  53. ->getMock();
  54. $this->l10nMock->expects($this->any())
  55. ->method('t')
  56. ->willReturnCallback(function ($text, $parameters = []) {
  57. return vsprintf($text, $parameters);
  58. });
  59. $this->analyser = new DependencyAnalyzer($this->platformMock, $this->l10nMock);
  60. }
  61. /**
  62. * @dataProvider providesPhpVersion
  63. *
  64. * @param string $expectedMissing
  65. * @param string $minVersion
  66. * @param string $maxVersion
  67. * @param string $intSize
  68. */
  69. public function testPhpVersion($expectedMissing, $minVersion, $maxVersion, $intSize): void {
  70. $app = [
  71. 'dependencies' => [
  72. 'php' => []
  73. ]
  74. ];
  75. if (!is_null($minVersion)) {
  76. $app['dependencies']['php']['@attributes']['min-version'] = $minVersion;
  77. }
  78. if (!is_null($maxVersion)) {
  79. $app['dependencies']['php']['@attributes']['max-version'] = $maxVersion;
  80. }
  81. if (!is_null($intSize)) {
  82. $app['dependencies']['php']['@attributes']['min-int-size'] = $intSize;
  83. }
  84. $missing = $this->analyser->analyze($app);
  85. $this->assertTrue(is_array($missing));
  86. $this->assertEquals($expectedMissing, $missing);
  87. }
  88. /**
  89. * @dataProvider providesDatabases
  90. * @param $expectedMissing
  91. * @param $databases
  92. */
  93. public function testDatabases($expectedMissing, $databases): void {
  94. $app = [
  95. 'dependencies' => [
  96. ]
  97. ];
  98. if (!is_null($databases)) {
  99. $app['dependencies']['database'] = $databases;
  100. }
  101. $missing = $this->analyser->analyze($app);
  102. $this->assertTrue(is_array($missing));
  103. $this->assertEquals($expectedMissing, $missing);
  104. }
  105. /**
  106. * @dataProvider providesCommands
  107. *
  108. * @param string $expectedMissing
  109. * @param string|null $commands
  110. */
  111. public function testCommand($expectedMissing, $commands): void {
  112. $app = [
  113. 'dependencies' => [
  114. ]
  115. ];
  116. if (!is_null($commands)) {
  117. $app['dependencies']['command'] = $commands;
  118. }
  119. $missing = $this->analyser->analyze($app);
  120. $this->assertTrue(is_array($missing));
  121. $this->assertEquals($expectedMissing, $missing);
  122. }
  123. /**
  124. * @dataProvider providesLibs
  125. * @param $expectedMissing
  126. * @param $libs
  127. */
  128. public function testLibs($expectedMissing, $libs): void {
  129. $app = [
  130. 'dependencies' => [
  131. ]
  132. ];
  133. if (!is_null($libs)) {
  134. $app['dependencies']['lib'] = $libs;
  135. }
  136. $missing = $this->analyser->analyze($app);
  137. $this->assertTrue(is_array($missing));
  138. $this->assertEquals($expectedMissing, $missing);
  139. }
  140. /**
  141. * @dataProvider providesOS
  142. * @param $expectedMissing
  143. * @param $oss
  144. */
  145. public function testOS($expectedMissing, $oss): void {
  146. $app = [
  147. 'dependencies' => []
  148. ];
  149. if (!is_null($oss)) {
  150. $app['dependencies']['os'] = $oss;
  151. }
  152. $missing = $this->analyser->analyze($app);
  153. $this->assertTrue(is_array($missing));
  154. $this->assertEquals($expectedMissing, $missing);
  155. }
  156. /**
  157. * @dataProvider providesOC
  158. * @param $expectedMissing
  159. * @param $oc
  160. */
  161. public function testOC($expectedMissing, $oc): void {
  162. $app = [
  163. 'dependencies' => []
  164. ];
  165. if (!is_null($oc)) {
  166. $app['dependencies'] = $oc;
  167. }
  168. $missing = $this->analyser->analyze($app);
  169. $this->assertTrue(is_array($missing));
  170. $this->assertEquals($expectedMissing, $missing);
  171. }
  172. /**
  173. * @return array
  174. */
  175. public function providesOC() {
  176. return [
  177. // no version -> no missing dependency
  178. [
  179. [],
  180. null,
  181. ],
  182. [
  183. [],
  184. [
  185. 'nextcloud' => [
  186. '@attributes' => [
  187. 'min-version' => '8',
  188. 'max-version' => '8',
  189. ],
  190. ],
  191. ],
  192. ],
  193. [
  194. [],
  195. [
  196. 'nextcloud' => [
  197. '@attributes' => [
  198. 'min-version' => '8.0',
  199. 'max-version' => '8.0',
  200. ],
  201. ],
  202. ],
  203. ],
  204. [
  205. [],
  206. [
  207. 'nextcloud' => [
  208. '@attributes' => [
  209. 'min-version' => '8.0.2',
  210. 'max-version' => '8.0.2'
  211. ],
  212. ],
  213. ],
  214. ],
  215. [
  216. [
  217. 'Server version 8.0.3 or higher is required.',
  218. ],
  219. [
  220. 'nextcloud' => [
  221. '@attributes' => [
  222. 'min-version' => '8.0.3'
  223. ],
  224. ],
  225. ],
  226. ],
  227. [
  228. [
  229. 'Server version 9 or higher is required.',
  230. ],
  231. [
  232. 'nextcloud' => [
  233. '@attributes' => [
  234. 'min-version' => '9'
  235. ],
  236. ],
  237. ],
  238. ],
  239. [
  240. [
  241. 'Server version 10 or higher is required.',
  242. ],
  243. [
  244. 'nextcloud' => [
  245. '@attributes' => [
  246. 'min-version' => '10'
  247. ],
  248. ],
  249. 'owncloud' => [
  250. '@attributes' => [
  251. 'min-version' => '9'
  252. ],
  253. ],
  254. ],
  255. ],
  256. [
  257. [
  258. 'Server version 10 or higher is required.',
  259. ],
  260. [
  261. 'nextcloud' => [
  262. '@attributes' => [
  263. 'min-version' => '9.1',
  264. ],
  265. ],
  266. ],
  267. ],
  268. [
  269. [
  270. 'Server version 9.2 or higher is required.',
  271. ],
  272. [
  273. 'nextcloud' => [
  274. '@attributes' => [
  275. 'min-version' => '9.2',
  276. ],
  277. ],
  278. ],
  279. ],
  280. [
  281. [
  282. 'Server version 11 or higher is required.',
  283. ],
  284. [
  285. 'nextcloud' => [
  286. '@attributes' => [
  287. 'min-version' => '11',
  288. ],
  289. ],
  290. ],
  291. ],
  292. [
  293. [
  294. 'Server version 8.0.1 or lower is required.',
  295. ],
  296. [
  297. 'nextcloud' => [
  298. '@attributes' => [
  299. 'max-version' => '8.0.1',
  300. ],
  301. ],
  302. ],
  303. ],
  304. [
  305. [],
  306. [
  307. 'owncloud' => [
  308. '@attributes' => [
  309. 'min-version' => '8',
  310. 'max-version' => '8',
  311. ],
  312. ],
  313. ],
  314. ],
  315. [
  316. [],
  317. [
  318. 'owncloud' => [
  319. '@attributes' => [
  320. 'min-version' => '8.0',
  321. 'max-version' => '8.0',
  322. ],
  323. ],
  324. ],
  325. ],
  326. [
  327. [],
  328. [
  329. 'owncloud' => [
  330. '@attributes' => [
  331. 'min-version' => '8.0.2',
  332. 'max-version' => '8.0.2'
  333. ],
  334. ],
  335. ],
  336. ],
  337. [
  338. [
  339. 'Server version 8.0.3 or higher is required.',
  340. ],
  341. [
  342. 'owncloud' => [
  343. '@attributes' => [
  344. 'min-version' => '8.0.3'
  345. ],
  346. ],
  347. ],
  348. ],
  349. [
  350. [
  351. 'Server version 9 or higher is required.',
  352. ],
  353. [
  354. 'owncloud' => [
  355. '@attributes' => [
  356. 'min-version' => '9'
  357. ],
  358. ],
  359. ],
  360. ],
  361. [
  362. [
  363. 'Server version 10 or higher is required.',
  364. ],
  365. [
  366. 'owncloud' => [
  367. '@attributes' => [
  368. 'min-version' => '9.1',
  369. ],
  370. ],
  371. ],
  372. ],
  373. [
  374. [
  375. 'Server version 9.2 or higher is required.',
  376. ],
  377. [
  378. 'owncloud' => [
  379. '@attributes' => [
  380. 'min-version' => '9.2',
  381. ],
  382. ],
  383. ],
  384. ],
  385. [
  386. [
  387. 'Server version 8.0.1 or lower is required.',
  388. ],
  389. [
  390. 'owncloud' => [
  391. '@attributes' => [
  392. 'max-version' => '8.0.1',
  393. ],
  394. ],
  395. ],
  396. ],
  397. ];
  398. }
  399. /**
  400. * @return array
  401. */
  402. public function providesOS() {
  403. return [
  404. [[], null],
  405. [[], []],
  406. [['The following platforms are supported: ANDROID'], 'ANDROID'],
  407. [['The following platforms are supported: WINNT'], ['WINNT']]
  408. ];
  409. }
  410. /**
  411. * @return array
  412. */
  413. public function providesLibs() {
  414. return [
  415. // we expect curl to exist
  416. [[], 'curl'],
  417. // we expect abcde to exist
  418. [['The library abcde is not available.'], ['abcde']],
  419. // curl in version 100.0 does not exist
  420. [['Library curl with a version higher than 100.0 is required - available version 2.3.4.'],
  421. [['@attributes' => ['min-version' => '100.0'], '@value' => 'curl']]],
  422. // curl in version 100.0 does not exist
  423. [['Library curl with a version lower than 1.0.0 is required - available version 2.3.4.'],
  424. [['@attributes' => ['max-version' => '1.0.0'], '@value' => 'curl']]],
  425. [['Library curl with a version lower than 2.3.3 is required - available version 2.3.4.'],
  426. [['@attributes' => ['max-version' => '2.3.3'], '@value' => 'curl']]],
  427. [['Library curl with a version higher than 2.3.5 is required - available version 2.3.4.'],
  428. [['@attributes' => ['min-version' => '2.3.5'], '@value' => 'curl']]],
  429. [[],
  430. [['@attributes' => ['min-version' => '2.3.4', 'max-version' => '2.3.4'], '@value' => 'curl']]],
  431. [[],
  432. [['@attributes' => ['min-version' => '2.3', 'max-version' => '2.3'], '@value' => 'curl']]],
  433. [[],
  434. [['@attributes' => ['min-version' => '2', 'max-version' => '2'], '@value' => 'curl']]],
  435. [[],
  436. ['@attributes' => ['min-version' => '2', 'max-version' => '2'], '@value' => 'curl']],
  437. ];
  438. }
  439. /**
  440. * @return array
  441. */
  442. public function providesCommands() {
  443. return [
  444. [[], null],
  445. // grep is known on linux
  446. [[], [['@attributes' => ['os' => 'Linux'], '@value' => 'grep']]],
  447. // grepp is not known on linux
  448. [['The command line tool grepp could not be found'], [['@attributes' => ['os' => 'Linux'], '@value' => 'grepp']]],
  449. // we don't care about tools on Windows - we are on Linux
  450. [[], [['@attributes' => ['os' => 'Windows'], '@value' => 'grepp']]],
  451. // grep is known on all systems
  452. [[], 'grep'],
  453. [[], ['@attributes' => ['os' => 'Linux'], '@value' => 'grep']],
  454. ];
  455. }
  456. /**
  457. * @return array
  458. */
  459. public function providesDatabases() {
  460. return [
  461. // non BC - in case on databases are defined -> all are supported
  462. [[], null],
  463. [[], []],
  464. [['The following databases are supported: mongodb'], 'mongodb'],
  465. [['The following databases are supported: sqlite, postgres'], ['sqlite', ['@value' => 'postgres']]],
  466. ];
  467. }
  468. /**
  469. * @return array
  470. */
  471. public function providesPhpVersion() {
  472. return [
  473. [[], null, null, null],
  474. [[], '5.4', null, null],
  475. [[], null, '5.5', null],
  476. [[], '5.4', '5.5', null],
  477. [['PHP 5.4.4 or higher is required.'], '5.4.4', null, null],
  478. [['PHP with a version lower than 5.4.2 is required.'], null, '5.4.2', null],
  479. [['64bit or higher PHP required.'], null, null, 64],
  480. [[], '5.4', '5.4', null],
  481. ];
  482. }
  483. }