FtpTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Robin Appelman <robin@icewind.nl>
  9. * @author Robin McCorkell <robin@mccorkell.me.uk>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. * @author Thomas Müller <thomas.mueller@tmit.eu>
  12. * @author Vincent Petry <vincent@nextcloud.com>
  13. *
  14. * @license AGPL-3.0
  15. *
  16. * This code is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License, version 3,
  18. * as published by the Free Software Foundation.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License, version 3,
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>
  27. *
  28. */
  29. namespace OCA\Files_External\Tests\Storage;
  30. use OCA\Files_External\Lib\Storage\FTP;
  31. /**
  32. * Class FtpTest
  33. *
  34. * @group DB
  35. *
  36. * @package OCA\Files_External\Tests\Storage
  37. */
  38. class FtpTest extends \Test\Files\Storage\Storage {
  39. private $config;
  40. protected function setUp(): void {
  41. parent::setUp();
  42. $id = $this->getUniqueID();
  43. $this->config = include('files_external/tests/config.ftp.php');
  44. if (! is_array($this->config) or ! $this->config['run']) {
  45. $this->markTestSkipped('FTP backend not configured');
  46. }
  47. $rootInstance = new FTP($this->config);
  48. $rootInstance->mkdir($id);
  49. $this->config['root'] .= '/' . $id; //make sure we have an new empty folder to work in
  50. $this->instance = new FTP($this->config);
  51. }
  52. protected function tearDown(): void {
  53. if ($this->instance) {
  54. $this->instance->rmdir('');
  55. }
  56. $this->instance = null;
  57. parent::tearDown();
  58. }
  59. /**
  60. * ftp has no proper way to handle spaces at the end of file names
  61. */
  62. public function directoryProvider() {
  63. return array_filter(parent::directoryProvider(), function ($item) {
  64. return substr($item[0], -1) !== ' ';
  65. });
  66. }
  67. /**
  68. * mtime for folders is only with a minute resolution
  69. */
  70. public function testStat() {
  71. $textFile = \OC::$SERVERROOT . '/tests/data/lorem.txt';
  72. $ctimeStart = time();
  73. $this->instance->file_put_contents('/lorem.txt', file_get_contents($textFile));
  74. $this->assertTrue($this->instance->isReadable('/lorem.txt'));
  75. $ctimeEnd = time();
  76. $mTime = $this->instance->filemtime('/lorem.txt');
  77. $this->assertTrue($this->instance->hasUpdated('/lorem.txt', $ctimeStart - 5));
  78. $this->assertTrue($this->instance->hasUpdated('/', $ctimeStart - 61));
  79. // check that ($ctimeStart - 5) <= $mTime <= ($ctimeEnd + 1)
  80. $this->assertGreaterThanOrEqual(($ctimeStart - 5), $mTime);
  81. $this->assertLessThanOrEqual(($ctimeEnd + 1), $mTime);
  82. $this->assertEquals(filesize($textFile), $this->instance->filesize('/lorem.txt'));
  83. $stat = $this->instance->stat('/lorem.txt');
  84. //only size and mtime are required in the result
  85. $this->assertEquals($stat['size'], $this->instance->filesize('/lorem.txt'));
  86. $this->assertEquals($stat['mtime'], $mTime);
  87. if ($this->instance->touch('/lorem.txt', 100) !== false) {
  88. $mTime = $this->instance->filemtime('/lorem.txt');
  89. $this->assertEquals($mTime, 100);
  90. }
  91. $mtimeStart = time();
  92. $this->instance->unlink('/lorem.txt');
  93. $this->assertTrue($this->instance->hasUpdated('/', $mtimeStart - 61));
  94. }
  95. }