StoragesTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace Test\Files\Storage;
  8. use Test\TestCase;
  9. abstract class StoragesTest extends TestCase {
  10. /**
  11. * @var \OC\Files\Storage\Storage
  12. */
  13. protected $storage1;
  14. /**
  15. * @var \OC\Files\Storage\Storage
  16. */
  17. protected $storage2;
  18. protected function tearDown(): void {
  19. if (is_null($this->storage1) && is_null($this->storage2)) {
  20. return;
  21. }
  22. $this->storage1->getCache()->clear();
  23. $this->storage2->getCache()->clear();
  24. parent::tearDown();
  25. }
  26. public function testMoveFileFromStorage() {
  27. $source = 'source.txt';
  28. $target = 'target.txt';
  29. $this->storage2->file_put_contents($source, 'foo');
  30. $this->storage1->moveFromStorage($this->storage2, $source, $target);
  31. $this->assertTrue($this->storage1->file_exists($target), $target . ' was not created');
  32. $this->assertFalse($this->storage2->file_exists($source), $source . ' still exists');
  33. $this->assertEquals('foo', $this->storage1->file_get_contents($target));
  34. }
  35. public function testMoveDirectoryFromStorage() {
  36. $this->storage2->mkdir('source');
  37. $this->storage2->file_put_contents('source/test1.txt', 'foo');
  38. $this->storage2->file_put_contents('source/test2.txt', 'qwerty');
  39. $this->storage2->mkdir('source/subfolder');
  40. $this->storage2->file_put_contents('source/subfolder/test.txt', 'bar');
  41. $this->storage1->moveFromStorage($this->storage2, 'source', 'target');
  42. $this->assertTrue($this->storage1->file_exists('target'));
  43. $this->assertTrue($this->storage1->file_exists('target/test1.txt'));
  44. $this->assertTrue($this->storage1->file_exists('target/test2.txt'));
  45. $this->assertTrue($this->storage1->file_exists('target/subfolder'));
  46. $this->assertTrue($this->storage1->file_exists('target/subfolder/test.txt'));
  47. $this->assertFalse($this->storage2->file_exists('source'));
  48. $this->assertFalse($this->storage2->file_exists('source/test1.txt'));
  49. $this->assertFalse($this->storage2->file_exists('source/test2.txt'));
  50. $this->assertFalse($this->storage2->file_exists('source/subfolder'));
  51. $this->assertFalse($this->storage2->file_exists('source/subfolder/test.txt'));
  52. $this->assertEquals('foo', $this->storage1->file_get_contents('target/test1.txt'));
  53. $this->assertEquals('qwerty', $this->storage1->file_get_contents('target/test2.txt'));
  54. $this->assertEquals('bar', $this->storage1->file_get_contents('target/subfolder/test.txt'));
  55. }
  56. public function testCopyFileFromStorage() {
  57. $source = 'source.txt';
  58. $target = 'target.txt';
  59. $this->storage2->file_put_contents($source, 'foo');
  60. $this->storage1->copyFromStorage($this->storage2, $source, $target);
  61. $this->assertTrue($this->storage1->file_exists($target), $target . ' was not created');
  62. $this->assertTrue($this->storage2->file_exists($source), $source . ' was deleted');
  63. $this->assertEquals('foo', $this->storage1->file_get_contents($target));
  64. }
  65. public function testCopyDirectoryFromStorage() {
  66. $this->storage2->mkdir('source');
  67. $this->storage2->file_put_contents('source/test1.txt', 'foo');
  68. $this->storage2->file_put_contents('source/test2.txt', 'qwerty');
  69. $this->storage2->mkdir('source/subfolder');
  70. $this->storage2->file_put_contents('source/subfolder/test.txt', 'bar');
  71. $this->storage1->copyFromStorage($this->storage2, 'source', 'target');
  72. $this->assertTrue($this->storage1->file_exists('target'));
  73. $this->assertTrue($this->storage1->file_exists('target/test1.txt'));
  74. $this->assertTrue($this->storage1->file_exists('target/test2.txt'));
  75. $this->assertTrue($this->storage1->file_exists('target/subfolder'));
  76. $this->assertTrue($this->storage1->file_exists('target/subfolder/test.txt'));
  77. $this->assertTrue($this->storage2->file_exists('source'));
  78. $this->assertTrue($this->storage2->file_exists('source/test1.txt'));
  79. $this->assertTrue($this->storage2->file_exists('source/test2.txt'));
  80. $this->assertTrue($this->storage2->file_exists('source/subfolder'));
  81. $this->assertTrue($this->storage2->file_exists('source/subfolder/test.txt'));
  82. $this->assertEquals('foo', $this->storage1->file_get_contents('target/test1.txt'));
  83. $this->assertEquals('qwerty', $this->storage1->file_get_contents('target/test2.txt'));
  84. $this->assertEquals('bar', $this->storage1->file_get_contents('target/subfolder/test.txt'));
  85. }
  86. }