ObjectStoreStorageTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. /**
  3. * @author Jörn Friedrich Dreyer
  4. * @copyright (c) 2014 Jörn Friedrich Dreyer <jfd@owncloud.com>
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public
  17. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. namespace Test\Files\ObjectStore;
  21. use OC\Files\ObjectStore\ObjectStoreStorage;
  22. use OC\Files\ObjectStore\StorageObjectStore;
  23. use OC\Files\Storage\Temporary;
  24. use OCP\Files\ObjectStore\IObjectStore;
  25. use Test\Files\Storage\Storage;
  26. /**
  27. * @group DB
  28. */
  29. class ObjectStoreStorageTest extends Storage {
  30. /**
  31. * @var IObjectStore
  32. */
  33. private $objectStorage;
  34. protected function setUp() {
  35. parent::setUp();
  36. $baseStorage = new Temporary();
  37. $this->objectStorage = new StorageObjectStore($baseStorage);
  38. $config['objectstore'] = $this->objectStorage;
  39. $this->instance = new ObjectStoreStorage($config);
  40. }
  41. protected function tearDown() {
  42. if (is_null($this->instance)) {
  43. return;
  44. }
  45. $this->instance->getCache()->clear();
  46. parent::tearDown();
  47. }
  48. public function testStat() {
  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. // check that ($ctimeStart - 5) <= $mTime <= ($ctimeEnd + 1)
  56. $this->assertGreaterThanOrEqual(($ctimeStart - 5), $mTime);
  57. $this->assertLessThanOrEqual(($ctimeEnd + 1), $mTime);
  58. $this->assertEquals(filesize($textFile), $this->instance->filesize('/lorem.txt'));
  59. $stat = $this->instance->stat('/lorem.txt');
  60. //only size and mtime are required in the result
  61. $this->assertEquals($stat['size'], $this->instance->filesize('/lorem.txt'));
  62. $this->assertEquals($stat['mtime'], $mTime);
  63. if ($this->instance->touch('/lorem.txt', 100) !== false) {
  64. $mTime = $this->instance->filemtime('/lorem.txt');
  65. $this->assertEquals($mTime, 100);
  66. }
  67. }
  68. public function testCheckUpdate() {
  69. $this->markTestSkipped('Detecting external changes is not supported on object storages');
  70. }
  71. /**
  72. * @dataProvider copyAndMoveProvider
  73. */
  74. public function testMove($source, $target) {
  75. $this->initSourceAndTarget($source);
  76. $sourceId = $this->instance->getCache()->getId(ltrim('/',$source));
  77. $this->assertNotEquals(-1, $sourceId);
  78. $this->instance->rename($source, $target);
  79. $this->assertTrue($this->instance->file_exists($target), $target.' was not created');
  80. $this->assertFalse($this->instance->file_exists($source), $source.' still exists');
  81. $this->assertSameAsLorem($target);
  82. $targetId = $this->instance->getCache()->getId(ltrim('/',$target));
  83. $this->assertSame($sourceId, $targetId, 'fileid must be stable on move or shares will break');
  84. }
  85. public function testRenameDirectory() {
  86. $this->instance->mkdir('source');
  87. $this->instance->file_put_contents('source/test1.txt', 'foo');
  88. $this->instance->file_put_contents('source/test2.txt', 'qwerty');
  89. $this->instance->mkdir('source/subfolder');
  90. $this->instance->file_put_contents('source/subfolder/test.txt', 'bar');
  91. $sourceId = $this->instance->getCache()->getId('source');
  92. $this->assertNotEquals(-1, $sourceId);
  93. $this->instance->rename('source', 'target');
  94. $this->assertFalse($this->instance->file_exists('source'));
  95. $this->assertFalse($this->instance->file_exists('source/test1.txt'));
  96. $this->assertFalse($this->instance->file_exists('source/test2.txt'));
  97. $this->assertFalse($this->instance->file_exists('source/subfolder'));
  98. $this->assertFalse($this->instance->file_exists('source/subfolder/test.txt'));
  99. $this->assertTrue($this->instance->file_exists('target'));
  100. $this->assertTrue($this->instance->file_exists('target/test1.txt'));
  101. $this->assertTrue($this->instance->file_exists('target/test2.txt'));
  102. $this->assertTrue($this->instance->file_exists('target/subfolder'));
  103. $this->assertTrue($this->instance->file_exists('target/subfolder/test.txt'));
  104. $this->assertEquals('foo', $this->instance->file_get_contents('target/test1.txt'));
  105. $this->assertEquals('qwerty', $this->instance->file_get_contents('target/test2.txt'));
  106. $this->assertEquals('bar', $this->instance->file_get_contents('target/subfolder/test.txt'));
  107. $targetId = $this->instance->getCache()->getId('target');
  108. $this->assertSame($sourceId, $targetId, 'fileid must be stable on move or shares will break');
  109. }
  110. public function testRenameOverWriteDirectory() {
  111. $this->instance->mkdir('source');
  112. $this->instance->file_put_contents('source/test1.txt', 'foo');
  113. $sourceId = $this->instance->getCache()->getId('source');
  114. $this->assertNotEquals(-1, $sourceId);
  115. $this->instance->mkdir('target');
  116. $this->instance->file_put_contents('target/test1.txt', 'bar');
  117. $this->instance->file_put_contents('target/test2.txt', 'bar');
  118. $this->instance->rename('source', 'target');
  119. $this->assertFalse($this->instance->file_exists('source'));
  120. $this->assertFalse($this->instance->file_exists('source/test1.txt'));
  121. $this->assertFalse($this->instance->file_exists('target/test2.txt'));
  122. $this->assertEquals('foo', $this->instance->file_get_contents('target/test1.txt'));
  123. $targetId = $this->instance->getCache()->getId('target');
  124. $this->assertSame($sourceId, $targetId, 'fileid must be stable on move or shares will break');
  125. }
  126. public function testRenameOverWriteDirectoryOverFile() {
  127. $this->instance->mkdir('source');
  128. $this->instance->file_put_contents('source/test1.txt', 'foo');
  129. $sourceId = $this->instance->getCache()->getId('source');
  130. $this->assertNotEquals(-1, $sourceId);
  131. $this->instance->file_put_contents('target', 'bar');
  132. $this->instance->rename('source', 'target');
  133. $this->assertFalse($this->instance->file_exists('source'));
  134. $this->assertFalse($this->instance->file_exists('source/test1.txt'));
  135. $this->assertEquals('foo', $this->instance->file_get_contents('target/test1.txt'));
  136. $targetId = $this->instance->getCache()->getId('target');
  137. $this->assertSame($sourceId, $targetId, 'fileid must be stable on move or shares will break');
  138. }
  139. }