FtpTests.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
  1. <?php
  2. namespace League\Flysystem\Adapter;
  3. use DateTime;
  4. use ErrorException;
  5. use League\Flysystem\Config;
  6. use League\Flysystem\NotSupportedException;
  7. use LogicException;
  8. use PHPUnit\Framework\TestCase;
  9. use RuntimeException;
  10. use const PHP_VERSION;
  11. function ftp_systype($connection)
  12. {
  13. static $connections = [
  14. 'reconnect.me',
  15. 'dont.reconnect.me',
  16. ];
  17. if (is_string($connection) && array_key_exists($connection, $connections)) {
  18. $connections[$connection]++;
  19. if (strpos($connection, 'dont') !== false || $connections[$connection] < 2) {
  20. return false;
  21. }
  22. }
  23. return 'LINUX';
  24. }
  25. function ftp_ssl_connect($host)
  26. {
  27. if ($host === 'fail.me') {
  28. return false;
  29. }
  30. if ($host === 'disconnect.check') {
  31. return tmpfile();
  32. }
  33. return $host;
  34. }
  35. function ftp_delete($conn, $path)
  36. {
  37. return ! strpos($path, 'rm.fail.txt');
  38. }
  39. function ftp_rmdir($connection, $dirname)
  40. {
  41. return strpos($dirname, 'rmdir.fail') === false;
  42. }
  43. function ftp_connect($host)
  44. {
  45. return ftp_ssl_connect($host);
  46. }
  47. function ftp_pasv($connection)
  48. {
  49. return $connection !== 'pasv.fail';
  50. }
  51. function ftp_rename()
  52. {
  53. return true;
  54. }
  55. function ftp_close($connection)
  56. {
  57. if (is_resource($connection)) {
  58. return fclose($connection);
  59. }
  60. return true;
  61. }
  62. function ftp_login($connection)
  63. {
  64. if ($connection === 'login.fail') {
  65. trigger_error('FTP login failed!!', E_WARNING);
  66. return false;
  67. }
  68. return true;
  69. }
  70. function ftp_chdir($connection, $directory)
  71. {
  72. if ($connection === 'chdir.fail') {
  73. return false;
  74. }
  75. if (in_array($directory, [
  76. 'not.found',
  77. 'windows.not.found',
  78. 'syno.not.found',
  79. 'rawlist-total-0.txt',
  80. 'file1.txt',
  81. 'file2.txt',
  82. 'file3.txt',
  83. 'file4.txt',
  84. 'file5WithPadding.txt',
  85. 'dir1',
  86. 'file1.with-total-line.txt',
  87. 'file1.with-invalid-line.txt',
  88. ], true)) {
  89. return false;
  90. }
  91. return $directory !== '0';
  92. }
  93. function ftp_pwd($connection)
  94. {
  95. return 'dirname';
  96. }
  97. function ftp_raw($connection, $command)
  98. {
  99. if ($connection === 'utf8.fail') {
  100. return [0 => '421 Service not available, closing control connection'];
  101. }
  102. if ($command === 'OPTS UTF8 ON') {
  103. if ($connection === 'utf8.alreadyActive') {
  104. return [0 => '202 UTF8 mode is always enabled. No need to send this command.'];
  105. }
  106. return [0 => '200 UTF8 set to on'];
  107. }
  108. if ($command === 'NOOP') {
  109. if (getenv('FTP_CLOSE_THROW') === 'DISCONNECT_CATCH') {
  110. return [0 => '500 Internal error'];
  111. }
  112. return [0 => '200 Zzz...'];
  113. }
  114. if ($command === 'STAT syno.not.found') {
  115. return [0 => '211- status of syno.not.found:', 1 => 'ftpd: assd: No such file or directory.' ,2 => '211 End of status'];
  116. }
  117. if ($command === 'syno.unknowndir') {
  118. return [0 => '211- status of syno.unknowndir:', 1 => 'ftpd: assd: No such file or directory.' ,2 => '211 End of status'];
  119. }
  120. if (strpos($command, 'unknowndir') !== false) {
  121. return false;
  122. }
  123. return [
  124. 0 => '211-Status of somewhere/folder/dummy.txt:',
  125. 1 => ' -rw-r--r-- 1 ftp ftp 0 Nov 24 13:59 somewhere/folder/dummy.txt',
  126. 2 => '211 End of status'
  127. ];
  128. }
  129. function ftp_rawlist($connection, $directory)
  130. {
  131. $directory = str_replace("-A ", "", $directory);
  132. if (getenv('FTP_CLOSE_THROW') === 'DISCONNECT_CATCH') {
  133. throw new ErrorException('ftp_rawlist');
  134. }
  135. if (getenv('FTP_CLOSE_THROW') === 'DISCONNECT_RETHROW') {
  136. throw new ErrorException('does not contain the correct message');
  137. }
  138. if (strpos($directory, 'recurse.manually/recurse.folder') !== false) {
  139. return [
  140. '-rw-r--r-- 1 ftp ftp 409 Aug 19 09:01 file1.txt',
  141. ];
  142. }
  143. if (strpos($directory, 'recurse.manually') !== false) {
  144. return [
  145. 'drwxr-xr-x 2 ftp ftp 4096 Nov 24 13:59 recurse.folder',
  146. ];
  147. }
  148. if (strpos($directory, 'recurse.folder') !== false) {
  149. return false;
  150. }
  151. if (strpos($directory, 'fail.rawlist') !== false) {
  152. return false;
  153. }
  154. if ($directory === 'not.found') {
  155. return false;
  156. }
  157. if ($directory === 'windows.not.found') {
  158. return ["File not found"];
  159. }
  160. if (strpos($directory, 'file1.txt') !== false) {
  161. return [
  162. '-rw-r--r-- 1 ftp ftp 409 Aug 19 09:01 file1.txt',
  163. ];
  164. }
  165. if ($directory === '0') {
  166. return [
  167. '-rw-r--r-- 1 ftp ftp 409 Aug 19 09:01 0',
  168. ];
  169. }
  170. if (strpos($directory, 'file2.txt') !== false) {
  171. return [
  172. '05-23-15 12:09PM 684 file2.txt',
  173. ];
  174. }
  175. if (strpos($directory, 'file3.txt') !== false) {
  176. return [
  177. '06-09-2016 12:09PM 684 file3.txt',
  178. ];
  179. }
  180. if (strpos($directory, 'file4.txt') !== false) {
  181. return [
  182. '2016-05-23 12:09PM 684 file4.txt',
  183. ];
  184. }
  185. if (strpos($directory, 'file5WithPadding.txt') !== false) {
  186. return [
  187. ' 2016-05-23 12:09PM 685 file5WithPadding.txt',
  188. ];
  189. }
  190. if (strpos($directory, 'dir1') !== false) {
  191. return [
  192. '2015-05-23 12:09 <DIR> dir1',
  193. ];
  194. }
  195. if (strpos($directory, 'rmdir.nested.fail') !== false) {
  196. return [
  197. 'drwxr-xr-x 2 ftp ftp 4096 Oct 13 2012 .',
  198. 'drwxr-xr-x 4 ftp ftp 4096 Nov 24 13:58 ..',
  199. '-rw-r--r-- 1 ftp ftp 409 Oct 13 2012 rm.fail.txt',
  200. ];
  201. }
  202. if (strpos($directory, 'lastfiledir') !== false) {
  203. return [
  204. 'drwxr-xr-x 2 ftp ftp 4096 Feb 6 2012 .',
  205. 'drwxr-xr-x 4 ftp ftp 4096 Feb 6 13:58 ..',
  206. '-rw-r--r-- 1 ftp ftp 409 Aug 19 09:01 file1.txt',
  207. '-rw-r--r-- 1 ftp ftp 409 Aug 14 09:01 file2.txt',
  208. '-rw-r--r-- 1 ftp ftp 409 Feb 6 10:06 file3.txt',
  209. '-rw-r--r-- 1 ftp ftp 409 Mar 20 2014 file4.txt',
  210. ];
  211. }
  212. if (strpos($directory, 'spaced.files') !== false) {
  213. return [
  214. 'drwxr-xr-x 2 ftp ftp 4096 Feb 6 2012 .',
  215. 'drwxr-xr-x 4 ftp ftp 4096 Feb 6 13:58 ..',
  216. '-rw-r--r-- 1 ftp ftp 409 Aug 19 09:01 file1.txt',
  217. ];
  218. }
  219. if (strpos($directory, 'file1.with-total-line.txt') !== false) {
  220. return [
  221. 'total 1',
  222. '-rw-r--r-- 1 ftp ftp 409 Aug 19 09:01 file1.txt',
  223. ];
  224. }
  225. if (strpos($directory, 'rawlist-total-0.txt') !== false) {
  226. return [
  227. 'total 0',
  228. ];
  229. }
  230. if (strpos($directory, 'file1.with-invalid-line.txt') !== false) {
  231. return [
  232. 'invalid line',
  233. '-rw-r--r-- 1 ftp ftp 409 Aug 19 09:01 file1.txt',
  234. ];
  235. }
  236. if (strpos($directory, 'some.nested/rmdir.fail') !== false || strpos($directory, 'somewhere/cgi-bin') !== false) {
  237. return [
  238. 'drwxr-xr-x 2 ftp ftp 4096 Oct 13 2012 .',
  239. 'drwxr-xr-x 4 ftp ftp 4096 Nov 24 13:58 ..',
  240. ];
  241. }
  242. if (strpos($directory, 'some.nested') !== false) {
  243. return ['drwxr-xr-x 1 ftp ftp 409 Aug 19 09:01 rmdir.fail'];
  244. }
  245. if (strpos($directory, 'somewhere/folder') !== false) {
  246. return ['-rw-r--r-- 1 ftp ftp 0 Nov 24 13:59 dummy.txt'];
  247. }
  248. return [
  249. 'drwxr-xr-x 4 ftp ftp 4096 Nov 24 13:58 .',
  250. 'drwxr-xr-x 16 ftp ftp 4096 Sep 2 13:01 ..',
  251. 'drwxr-xr-x 2 ftp ftp 4096 Oct 13 2012 cgi-bin',
  252. 'drwxr-xr-x 2 ftp ftp 4096 Nov 24 13:59 folder',
  253. '-rw-r--r-- 1 ftp ftp 409 Oct 13 2012 index.html',
  254. '',
  255. 'somewhere/cgi-bin:',
  256. 'drwxr-xr-x 2 ftp ftp 4096 Oct 13 2012 .',
  257. 'drwxr-xr-x 4 ftp ftp 4096 Nov 24 13:58 ..',
  258. '',
  259. 'somewhere/folder:',
  260. 'drwxr-xr-x 2 ftp ftp 4096 Nov 24 13:59 .',
  261. 'drwxr-xr-x 4 ftp ftp 4096 Nov 24 13:58 ..',
  262. '-rw-r--r-- 1 ftp ftp 0 Nov 24 13:59 dummy.txt',
  263. ];
  264. }
  265. function ftp_mdtm($connection, $path)
  266. {
  267. switch ($path) {
  268. case 'lastfiledir/file1.txt':
  269. return 1408438882;
  270. break;
  271. case 'lastfiledir/file2.txt':
  272. return 1408006883;
  273. break;
  274. case 'lastfiledir/file3.txt':
  275. return 1423217165;
  276. break;
  277. case 'lastfiledir/file4.txt':
  278. return 1395305765;
  279. break;
  280. case 'some/file.ext':
  281. return 1408438882;
  282. break;
  283. default:
  284. return -1;
  285. break;
  286. }
  287. }
  288. function ftp_mkdir($connection, $dirname)
  289. {
  290. return strpos($dirname, 'mkdir.fail') === false;
  291. }
  292. function ftp_fput($connection, $path)
  293. {
  294. return strpos($path, 'write.fail') === false;
  295. }
  296. function ftp_fget($connection, $resource, $path)
  297. {
  298. if (strpos($path, 'not.found') !== false) {
  299. return false;
  300. }
  301. \fwrite($resource, 'contents');
  302. rewind($resource);
  303. return true;
  304. }
  305. function ftp_nlist($connection, $directory)
  306. {
  307. return ['./some.nested'];
  308. }
  309. function ftp_chmod($connection, $mode, $path)
  310. {
  311. return strpos($path, 'chmod.fail') === false;
  312. }
  313. function ftp_set_option($connection, $option, $value)
  314. {
  315. putenv('USE_PASSV_ADDREESS' . $option . '=' . ($value ? 'YES' : 'NO'));
  316. return true;
  317. }
  318. class FtpTests extends TestCase
  319. {
  320. protected $options = [
  321. 'host' => 'example.org',
  322. 'port' => 40,
  323. 'ssl' => true,
  324. 'timeout' => 35,
  325. 'root' => '/somewhere',
  326. 'permPublic' => 0777,
  327. 'permPrivate' => 0000,
  328. 'passive' => false,
  329. 'username' => 'user',
  330. 'password' => 'password',
  331. 'recurseManually' => false,
  332. ];
  333. public function setUp(): void
  334. {
  335. putenv('FTP_CLOSE_THROW=nope');
  336. }
  337. public function testInstantiable()
  338. {
  339. if ( ! defined('FTP_BINARY')) {
  340. $this->markTestSkipped('The FTP_BINARY constant is not defined');
  341. }
  342. $adapter = new Ftp($this->options);
  343. $this->assertOptionsAreRetrievable($adapter);
  344. $listing = $adapter->listContents('', true);
  345. $this->assertIsArray($listing);
  346. $this->assertGetterFailuresReturnFalse($adapter);
  347. $this->assertTrue($adapter->rename('a', 'b'));
  348. $this->assertTrue($adapter->delete('a'));
  349. $this->assertFalse($adapter->deleteDir('some.nested/rmdir.fail'));
  350. $this->assertFalse($adapter->deleteDir('rmdir.nested.fail'));
  351. $this->assertTrue($adapter->deleteDir('somewhere'));
  352. $result = $adapter->read('something.txt');
  353. $this->assertEquals('contents', $result['contents']);
  354. $result = $adapter->getMimetype('something.txt');
  355. $this->assertEquals('text/plain', $result['mimetype']);
  356. $this->assertFalse($adapter->createDir('some.nested/mkdir.fail', new Config()));
  357. $this->assertIsArray($adapter->write('unknowndir/file.txt', 'contents', new Config(['visibility' => 'public'])));
  358. $this->assertIsArray($adapter->writeStream('unknowndir/file.txt', tmpfile(), new Config(['visibility' => 'public'])));
  359. $this->assertIsArray($adapter->updateStream('unknowndir/file.txt', tmpfile(), new Config()));
  360. $this->assertIsArray($adapter->getTimestamp('some/file.ext'));
  361. }
  362. /**
  363. * @depends testInstantiable
  364. */
  365. public function testManualRecursion()
  366. {
  367. $adapter = new Ftp($this->options);
  368. $adapter->setRecurseManually(true);
  369. $result = $adapter->listContents('recurse.manually', true);
  370. $this->assertCount(2, $result);
  371. $this->assertEquals('recurse.manually/recurse.folder', $result[0]['path']);
  372. $this->assertEquals('recurse.manually/recurse.folder/file1.txt', $result[1]['path']);
  373. }
  374. /**
  375. * @depends testInstantiable
  376. */
  377. public function testDisconnect()
  378. {
  379. $adapter = new Ftp(array_merge($this->options, ['host' => 'disconnect.check']));
  380. $adapter->connect();
  381. $this->assertTrue($adapter->isConnected());
  382. $adapter->disconnect();
  383. $this->assertFalse($adapter->isConnected());
  384. }
  385. /**
  386. * @depends testInstantiable
  387. */
  388. public function testIsConnectedTimeout()
  389. {
  390. putenv('FTP_CLOSE_THROW=DISCONNECT_CATCH');
  391. $adapter = new Ftp(array_merge($this->options, ['host' => 'disconnect.check']));
  392. $adapter->connect();
  393. $this->assertFalse($adapter->isConnected());
  394. }
  395. /**
  396. * @depends testInstantiable
  397. */
  398. public function testIgnorePassiveAddress()
  399. {
  400. if ( ! defined('FTP_USEPASVADDRESS')) {
  401. define('FTP_USEPASVADDRESS', 2);
  402. }
  403. $this->assertFalse(getenv('USE_PASSV_ADDREESS' . FTP_USEPASVADDRESS));
  404. $adapter = new Ftp(array_merge($this->options, ['ignorePassiveAddress' => true]));
  405. $adapter->connect();
  406. $this->assertEquals('NO', getenv('USE_PASSV_ADDREESS' . FTP_USEPASVADDRESS));
  407. }
  408. /**
  409. * @depends testInstantiable
  410. */
  411. public function testGetMetadataForRoot()
  412. {
  413. $adapter = new Ftp($this->options);
  414. $metadata = $adapter->getMetadata('');
  415. $expected = ['type' => 'dir', 'path' => ''];
  416. $this->assertEquals($expected, $metadata);
  417. }
  418. /**
  419. * @depends testInstantiable
  420. */
  421. public function testGetMetadata()
  422. {
  423. $adapter = new Ftp($this->options);
  424. $metadata = $adapter->getMetadata('file1.txt');
  425. $this->assertIsArray($metadata);
  426. $this->assertEquals('file', $metadata['type']);
  427. $this->assertEquals('file1.txt', $metadata['path']);
  428. }
  429. /**
  430. * @depends testInstantiable
  431. */
  432. public function testHasWithTotalZero()
  433. {
  434. $adapter = new Ftp($this->options);
  435. $this->assertFalse($adapter->getMetadata('rawlist-total-0.txt'));
  436. }
  437. /**
  438. * @depends testInstantiable
  439. */
  440. public function testGetMetadataForRootFileNamedZero()
  441. {
  442. $adapter = new Ftp($this->options);
  443. $metadata = $adapter->getMetadata('0');
  444. $this->assertIsArray($metadata);
  445. $this->assertEquals('file', $metadata['type']);
  446. $this->assertEquals('0', $metadata['path']);
  447. }
  448. /**
  449. * @depends testInstantiable
  450. */
  451. public function testGetMetadataIgnoresInvalidTotalLine()
  452. {
  453. $adapter = new Ftp($this->options);
  454. $metadata = $adapter->getMetadata('file1.with-total-line.txt');
  455. $this->assertEquals('file1.txt', $metadata['path']);
  456. }
  457. /**
  458. * @depends testInstantiable
  459. */
  460. public function testGetWindowsMetadata()
  461. {
  462. $adapter = new Ftp($this->options);
  463. $metadata = $adapter->getMetadata('file2.txt');
  464. $this->assertIsArray($metadata);
  465. $this->assertEquals('file', $metadata['type']);
  466. $this->assertEquals('file2.txt', $metadata['path']);
  467. $this->assertEquals(1432382940, $metadata['timestamp']);
  468. $this->assertEquals('public', $metadata['visibility']);
  469. $this->assertEquals(684, $metadata['size']);
  470. $metadata = $adapter->getMetadata('file3.txt');
  471. $this->assertIsArray($metadata);
  472. $this->assertEquals('file', $metadata['type']);
  473. $this->assertEquals('file3.txt', $metadata['path']);
  474. $this->assertEquals(1473163740, $metadata['timestamp']);
  475. $this->assertEquals('public', $metadata['visibility']);
  476. $this->assertEquals(684, $metadata['size']);
  477. $metadata = $adapter->getMetadata('file4.txt');
  478. $this->assertIsArray($metadata);
  479. $this->assertEquals('file', $metadata['type']);
  480. $this->assertEquals('file4.txt', $metadata['path']);
  481. $this->assertEquals(1464005340, $metadata['timestamp']);
  482. $this->assertEquals('public', $metadata['visibility']);
  483. $this->assertEquals(684, $metadata['size']);
  484. $metadata = $adapter->getMetadata('file5WithPadding.txt');
  485. $this->assertIsArray($metadata);
  486. $this->assertEquals('file', $metadata['type']);
  487. $this->assertEquals('file5WithPadding.txt', $metadata['path']);
  488. $this->assertEquals(1464005340, $metadata['timestamp']);
  489. $this->assertEquals('public', $metadata['visibility']);
  490. $this->assertEquals(685, $metadata['size']);
  491. $metadata = $adapter->getMetadata('dir1');
  492. $this->assertEquals('dir', $metadata['type']);
  493. $this->assertEquals('dir1', $metadata['path']);
  494. $this->assertEquals(1432382940, $metadata['timestamp']);
  495. }
  496. /**
  497. * @depends testInstantiable
  498. *
  499. * Some Windows FTP server return a 500 error with the message "File not found" instead of false
  500. * when calling ftp_rawlist() on invalid dir
  501. */
  502. public function testFileNotFoundWindowMetadata()
  503. {
  504. $adapter = new Ftp($this->options);
  505. $metadata = $adapter->getMetadata('windows.not.found');
  506. $this->assertFalse($metadata);
  507. }
  508. /**
  509. * @depends testInstantiable
  510. */
  511. public function testFileNotFoundWindows()
  512. {
  513. $adapter = new Ftp($this->options);
  514. $this->assertFalse($adapter->has('windows.not.found'));
  515. $this->assertFalse($adapter->getVisibility('windows.not.found'));
  516. $this->assertFalse($adapter->getSize('windows.not.found'));
  517. $this->assertFalse($adapter->getMimetype('windows.not.found'));
  518. $this->assertFalse($adapter->getTimestamp('windows.not.found'));
  519. $this->assertFalse($adapter->write('write.fail', 'contents', new Config()));
  520. $this->assertFalse($adapter->writeStream('write.fail', tmpfile(), new Config()));
  521. $this->assertFalse($adapter->update('write.fail', 'contents', new Config()));
  522. $this->assertFalse($adapter->setVisibility('chmod.fail', 'private'));
  523. }
  524. /**
  525. * @depends testInstantiable
  526. */
  527. public function testGetLastFile()
  528. {
  529. $adapter = new Ftp($this->options);
  530. $listing = $adapter->listContents('lastfiledir');
  531. $last_modified_file = reset($listing);
  532. foreach ($listing as $file) {
  533. $file_time = $adapter->getTimestamp($file['path'])['timestamp'];
  534. $last_file_time = $adapter->getTimestamp($last_modified_file['path'])['timestamp'];
  535. if ($last_file_time < $file_time) {
  536. $last_modified_file = $file;
  537. }
  538. }
  539. $this->assertEquals('lastfiledir/file3.txt', $last_modified_file['path']);
  540. }
  541. /**
  542. * @depends testInstantiable
  543. */
  544. public function testListDirWithFileWithLeadingSpace()
  545. {
  546. $adapter = new Ftp($this->options);
  547. $listing = $adapter->listContents('spaced.files');
  548. $file = array_pop($listing);
  549. $this->assertEquals('spaced.files/ file1.txt', $file['path']);
  550. }
  551. /**
  552. * @depends testInstantiable
  553. */
  554. public function testListingNotEmpty()
  555. {
  556. $adapter = new Ftp($this->options);
  557. $listing = $adapter->listContents('');
  558. $this->assertNotEmpty($listing);
  559. }
  560. public function expectedUnixListings()
  561. {
  562. return [
  563. [
  564. /*$directory=*/ '',
  565. /*$recursive=*/ false,
  566. /*$enableTimestamps=*/ true,
  567. /*'expectedListing'=>*/ [
  568. [
  569. 'type' => 'dir',
  570. 'path' => 'cgi-bin',
  571. 'timestamp' => 1350086400,
  572. ],
  573. [
  574. 'type' => 'dir',
  575. 'path' => 'folder',
  576. 'timestamp' => DateTime::createFromFormat('M d H:i', 'Nov 24 13:59')->getTimestamp(),
  577. ],
  578. [
  579. 'type' => 'file',
  580. 'path' => 'index.html',
  581. 'visibility' => 'public',
  582. 'size' => 409,
  583. 'timestamp' => 1350086400,
  584. ],
  585. [
  586. 'type' => 'file',
  587. 'path' => 'somewhere/folder/dummy.txt',
  588. 'visibility' => 'public',
  589. 'size' => 0,
  590. 'timestamp' => DateTime::createFromFormat('M d H:i', 'Nov 24 13:59')->getTimestamp(),
  591. ],
  592. ]
  593. ],
  594. [
  595. /*$directory=*/ '',
  596. /*$recursive=*/ true,
  597. /*$enableTimestamps=*/ true,
  598. /*'expectedListing'=>*/ [
  599. [
  600. 'type' => 'dir',
  601. 'path' => 'cgi-bin',
  602. 'timestamp' => 1350086400,
  603. ],
  604. [
  605. 'type' => 'dir',
  606. 'path' => 'folder',
  607. 'timestamp' => DateTime::createFromFormat('M d H:i', 'Nov 24 13:59')->getTimestamp(),
  608. ],
  609. [
  610. 'type' => 'file',
  611. 'path' => 'index.html',
  612. 'visibility' => 'public',
  613. 'size' => 409,
  614. 'timestamp' => 1350086400,
  615. ],
  616. [
  617. 'type' => 'file',
  618. 'path' => 'somewhere/folder/dummy.txt',
  619. 'visibility' => 'public',
  620. 'size' => 0,
  621. 'timestamp' => DateTime::createFromFormat('M d H:i', 'Nov 24 13:59')->getTimestamp(),
  622. ],
  623. ]
  624. ],
  625. [
  626. /*$directory=*/ 'lastfiledir',
  627. /*$recursive=*/ true,
  628. /*$enableTimestamps=*/ true,
  629. /*'expectedListing'=>*/ [
  630. [
  631. 'type' => 'file',
  632. 'path' => 'lastfiledir/file1.txt',
  633. 'visibility' => 'public',
  634. 'size' => 409,
  635. 'timestamp' => DateTime::createFromFormat('M d H:i', 'Aug 19 09:01')->getTimestamp(),
  636. ],
  637. [
  638. 'type' => 'file',
  639. 'path' => 'lastfiledir/file2.txt',
  640. 'visibility' => 'public',
  641. 'size' => 409,
  642. 'timestamp' => DateTime::createFromFormat('M d H:i', 'Aug 14 09:01')->getTimestamp(),
  643. ],
  644. [
  645. 'type' => 'file',
  646. 'path' => 'lastfiledir/file3.txt',
  647. 'visibility' => 'public',
  648. 'size' => 409,
  649. 'timestamp' => DateTime::createFromFormat('M d H:i', 'Feb 6 10:06')->getTimestamp(),
  650. ],
  651. [
  652. 'type' => 'file',
  653. 'path' => 'lastfiledir/file4.txt',
  654. 'visibility' => 'public',
  655. 'size' => 409,
  656. 'timestamp' => 1395273600,
  657. ],
  658. ]
  659. ],
  660. [
  661. /*$directory=*/ 'lastfiledir',
  662. /*$recursive=*/ true,
  663. /*$enableTimestamps=*/ false,
  664. /*'expectedListing'=>*/ [
  665. [
  666. 'type' => 'file',
  667. 'path' => 'lastfiledir/file1.txt',
  668. 'visibility' => 'public',
  669. 'size' => 409,
  670. ],
  671. [
  672. 'type' => 'file',
  673. 'path' => 'lastfiledir/file2.txt',
  674. 'visibility' => 'public',
  675. 'size' => 409,
  676. ],
  677. [
  678. 'type' => 'file',
  679. 'path' => 'lastfiledir/file3.txt',
  680. 'visibility' => 'public',
  681. 'size' => 409,
  682. ],
  683. [
  684. 'type' => 'file',
  685. 'path' => 'lastfiledir/file4.txt',
  686. 'visibility' => 'public',
  687. 'size' => 409,
  688. ],
  689. ]
  690. ]
  691. ];
  692. }
  693. /**
  694. * @depends testInstantiable
  695. * @dataProvider expectedUnixListings
  696. */
  697. public function testListingFromUnixFormat($directory, $recursive, $enableTimestamps, $expectedListing)
  698. {
  699. $adapter = new Ftp($this->options += ['enableTimestampsOnUnixListings' => $enableTimestamps]);
  700. $listing = $adapter->listContents($directory, $recursive);
  701. $this->assertEquals($listing, $expectedListing);
  702. }
  703. /**
  704. * @depends testInstantiable
  705. */
  706. public function testConnectFail()
  707. {
  708. $this->expectException(RuntimeException::class);
  709. $adapter = new Ftp(['host' => 'fail.me', 'ssl' => false, 'transferMode' => FTP_BINARY]);
  710. $adapter->connect();
  711. }
  712. /**
  713. * @depends testInstantiable
  714. */
  715. public function testRawlistFail()
  716. {
  717. $adapter = new Ftp($this->options);
  718. $result = $adapter->listContents('fail.rawlist');
  719. $this->assertEquals([], $result);
  720. }
  721. /**
  722. * @depends testInstantiable
  723. */
  724. public function testConnectFailSsl()
  725. {
  726. $this->expectException(RuntimeException::class);
  727. $adapter = new Ftp(['host' => 'fail.me', 'ssl' => true]);
  728. $adapter->connect();
  729. }
  730. /**
  731. * @depends testInstantiable
  732. */
  733. public function testLoginFailSsl()
  734. {
  735. if (PHP_MAJOR_VERSION > 7) {
  736. $this->markTestSkipped('PHP 8.0.0 does not accept non resource arguments.');
  737. }
  738. $this->expectException(RuntimeException::class);
  739. $adapter = new Ftp(['host' => 'login.fail', 'ssl' => true]);
  740. $adapter->connect();
  741. }
  742. /**
  743. * @depends testInstantiable
  744. */
  745. public function testRootFailSsl()
  746. {
  747. $this->expectException(RuntimeException::class);
  748. $adapter = new Ftp(['host' => 'chdir.fail', 'ssl' => true, 'root' => 'somewhere']);
  749. $adapter->connect();
  750. }
  751. /**
  752. * @depends testInstantiable
  753. */
  754. public function testPassiveFailSsl()
  755. {
  756. $this->expectException(RuntimeException::class);
  757. $adapter = new Ftp(['host' => 'pasv.fail', 'ssl' => true, 'root' => 'somewhere']);
  758. $adapter->connect();
  759. }
  760. /**
  761. * @depends testInstantiable
  762. */
  763. public function testItReconnects()
  764. {
  765. $adapter = new Ftp(['host' => 'reconnect.me', 'ssl' => true, 'root' => 'somewhere']);
  766. $this->assertFalse($adapter->isConnected());
  767. $this->assertNotNull($adapter->getConnection());
  768. }
  769. /**
  770. * @depends testInstantiable
  771. */
  772. public function testItCanSetSystemType()
  773. {
  774. $adapter = new Ftp($this->options);
  775. $this->assertNull($adapter->getSystemType());
  776. $adapter->setSystemType('unix');
  777. $this->assertEquals('unix', $adapter->getSystemType());
  778. }
  779. /**
  780. * @depends testInstantiable
  781. */
  782. public function testItThrowsAnExceptionWhenAnInvalidSystemTypeIsSet()
  783. {
  784. $this->expectException(NotSupportedException::class);
  785. $adapter = new Ftp($this->options + ['systemType' => 'unknown']);
  786. $adapter->listContents();
  787. }
  788. /**
  789. * @depends testInstantiable
  790. */
  791. public function testItThrowsAnExceptionWhenAnInvalidUnixListingIsFound()
  792. {
  793. $this->expectException(RuntimeException::class);
  794. $adapter = new Ftp($this->options + ['systemType' => 'unix']);
  795. $adapter->getMetadata('file1.with-invalid-line.txt');
  796. }
  797. /**
  798. * @depends testInstantiable
  799. */
  800. public function testReadFailure()
  801. {
  802. $adapter = new Ftp($this->options + ['systemType' => 'unix']);
  803. $this->assertFalse($adapter->read('not.found'));
  804. }
  805. /**
  806. * @depends testInstantiable
  807. */
  808. public function testItThrowsAnExceptionWhenAnInvalidWindowsListingIsFound()
  809. {
  810. $this->expectException(RuntimeException::class);
  811. $adapter = new Ftp($this->options + ['systemType' => 'windows']);
  812. $metadata = $adapter->getMetadata('file1.with-invalid-line.txt');
  813. $this->assertEquals('file1.txt', $metadata['path']);
  814. }
  815. /**
  816. * @depends testInstantiable
  817. */
  818. public function testItSetUtf8Mode()
  819. {
  820. $adapter = new Ftp($this->options + ['utf8' => true]);
  821. $adapter->setUtf8(true);
  822. $this->assertNull($adapter->connect());
  823. }
  824. /**
  825. * @depends testInstantiable
  826. */
  827. public function testItSetUtf8ModeWhenAlreadySetByServer()
  828. {
  829. $adapter = new Ftp(['host' => 'utf8.alreadyActive', 'utf8' => true]);
  830. $adapter->setUtf8(true);
  831. $this->assertNull($adapter->connect());
  832. }
  833. /**
  834. * @depends testInstantiable
  835. */
  836. public function testItThrowsAnExceptionWhenItCouldNotSetUtf8ModeForConnection()
  837. {
  838. $this->expectException(RuntimeException::class);
  839. $adapter = new Ftp(['host' => 'utf8.fail', 'utf8' => true]);
  840. $adapter->setUtf8(true);
  841. $adapter->connect();
  842. }
  843. /**
  844. * @param $adapter
  845. */
  846. protected function assertOptionsAreRetrievable($adapter)
  847. {
  848. $this->assertEquals('example.org', $adapter->getHost());
  849. $this->assertEquals(40, $adapter->getPort());
  850. $this->assertEquals(35, $adapter->getTimeout());
  851. $this->assertEquals('/somewhere/', $adapter->getRoot());
  852. $this->assertEquals(0777, $adapter->getPermPublic());
  853. $this->assertEquals(0000, $adapter->getPermPrivate());
  854. $this->assertEquals('user', $adapter->getUsername());
  855. $this->assertEquals('password', $adapter->getPassword());
  856. }
  857. /**
  858. * @param $adapter
  859. */
  860. protected function assertGetterFailuresReturnFalse($adapter)
  861. {
  862. $this->assertFalse($adapter->has('not.found'));
  863. $this->assertFalse($adapter->getVisibility('not.found'));
  864. $this->assertFalse($adapter->getSize('not.found'));
  865. $this->assertFalse($adapter->getMimetype('not.found'));
  866. $this->assertFalse($adapter->getTimestamp('not.found'));
  867. $this->assertFalse($adapter->write('write.fail', 'contents', new Config()));
  868. $this->assertFalse($adapter->writeStream('write.fail', tmpfile(), new Config()));
  869. $this->assertFalse($adapter->update('write.fail', 'contents', new Config()));
  870. $this->assertFalse($adapter->setVisibility('chmod.fail', 'private'));
  871. }
  872. }