FtpTest.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2019-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Files_External\Tests\Storage;
  8. use OCA\Files_External\Lib\Storage\FTP;
  9. /**
  10. * Class FtpTest
  11. *
  12. * @group DB
  13. *
  14. * @package OCA\Files_External\Tests\Storage
  15. */
  16. class FtpTest extends \Test\Files\Storage\Storage {
  17. private $config;
  18. protected function setUp(): void {
  19. parent::setUp();
  20. $id = $this->getUniqueID();
  21. $this->config = include('files_external/tests/config.ftp.php');
  22. if (! is_array($this->config) or ! $this->config['run']) {
  23. $this->markTestSkipped('FTP backend not configured');
  24. }
  25. $rootInstance = new FTP($this->config);
  26. $rootInstance->mkdir($id);
  27. $this->config['root'] .= '/' . $id; //make sure we have an new empty folder to work in
  28. $this->instance = new FTP($this->config);
  29. }
  30. protected function tearDown(): void {
  31. if ($this->instance) {
  32. $this->instance->rmdir('');
  33. }
  34. $this->instance = null;
  35. parent::tearDown();
  36. }
  37. /**
  38. * ftp has no proper way to handle spaces at the end of file names
  39. */
  40. public function directoryProvider() {
  41. return array_filter(parent::directoryProvider(), function ($item) {
  42. return substr($item[0], -1) !== ' ';
  43. });
  44. }
  45. /**
  46. * mtime for folders is only with a minute resolution
  47. */
  48. public function testStat(): void {
  49. $textFile = \OC::$SERVERROOT . '/tests/data/lorem.txt';
  50. $ctimeStart = time();
  51. $this->instance->file_put_contents('/lorem.txt', file_get_contents($textFile));
  52. $this->assertTrue($this->instance->isReadable('/lorem.txt'));
  53. $ctimeEnd = time();
  54. $mTime = $this->instance->filemtime('/lorem.txt');
  55. $this->assertTrue($this->instance->hasUpdated('/lorem.txt', $ctimeStart - 5));
  56. $this->assertTrue($this->instance->hasUpdated('/', $ctimeStart - 61));
  57. // check that ($ctimeStart - 5) <= $mTime <= ($ctimeEnd + 1)
  58. $this->assertGreaterThanOrEqual(($ctimeStart - 5), $mTime);
  59. $this->assertLessThanOrEqual(($ctimeEnd + 1), $mTime);
  60. $this->assertEquals(filesize($textFile), $this->instance->filesize('/lorem.txt'));
  61. $stat = $this->instance->stat('/lorem.txt');
  62. //only size and mtime are required in the result
  63. $this->assertEquals($stat['size'], $this->instance->filesize('/lorem.txt'));
  64. $this->assertEquals($stat['mtime'], $mTime);
  65. if ($this->instance->touch('/lorem.txt', 100) !== false) {
  66. $mTime = $this->instance->filemtime('/lorem.txt');
  67. $this->assertEquals($mTime, 100);
  68. }
  69. $mtimeStart = time();
  70. $this->instance->unlink('/lorem.txt');
  71. $this->assertTrue($this->instance->hasUpdated('/', $mtimeStart - 61));
  72. }
  73. }