CommonTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Robin Appelman
  6. * @copyright 2012 Robin Appelman icewind@owncloud.com
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. namespace Test\Files\Storage;
  23. use OC\Files\Storage\Wrapper\Jail;
  24. use OC\Files\Storage\Wrapper\Wrapper;
  25. use PHPUnit\Framework\MockObject\MockObject;
  26. /**
  27. * Class CommonTest
  28. *
  29. * @group DB
  30. *
  31. * @package Test\Files\Storage
  32. */
  33. class CommonTest extends Storage {
  34. /**
  35. * @var string tmpDir
  36. */
  37. private $tmpDir;
  38. protected function setUp(): void {
  39. parent::setUp();
  40. $this->tmpDir = \OC::$server->getTempManager()->getTemporaryFolder();
  41. $this->instance = new \OC\Files\Storage\CommonTest(['datadir' => $this->tmpDir]);
  42. }
  43. protected function tearDown(): void {
  44. \OC_Helper::rmdirr($this->tmpDir);
  45. parent::tearDown();
  46. }
  47. public function testMoveFromStorageWrapped() {
  48. /** @var \OC\Files\Storage\CommonTest|MockObject $instance */
  49. $instance = $this->getMockBuilder(\OC\Files\Storage\CommonTest::class)
  50. ->setMethods(['copyFromStorage', 'rmdir', 'unlink'])
  51. ->setConstructorArgs([['datadir' => $this->tmpDir]])
  52. ->getMock();
  53. $instance->method('copyFromStorage')
  54. ->willThrowException(new \Exception('copy'));
  55. $source = new Wrapper([
  56. 'storage' => $instance,
  57. ]);
  58. $instance->file_put_contents('foo.txt', 'bar');
  59. $instance->moveFromStorage($source, 'foo.txt', 'bar.txt');
  60. $this->assertTrue($instance->file_exists('bar.txt'));
  61. }
  62. public function testMoveFromStorageJailed() {
  63. /** @var \OC\Files\Storage\CommonTest|MockObject $instance */
  64. $instance = $this->getMockBuilder(\OC\Files\Storage\CommonTest::class)
  65. ->setMethods(['copyFromStorage', 'rmdir', 'unlink'])
  66. ->setConstructorArgs([['datadir' => $this->tmpDir]])
  67. ->getMock();
  68. $instance->method('copyFromStorage')
  69. ->willThrowException(new \Exception('copy'));
  70. $source = new Jail([
  71. 'storage' => $instance,
  72. 'root' => 'foo'
  73. ]);
  74. $source = new Wrapper([
  75. 'storage' => $source
  76. ]);
  77. $instance->mkdir('foo');
  78. $instance->file_put_contents('foo/foo.txt', 'bar');
  79. $instance->moveFromStorage($source, 'foo.txt', 'bar.txt');
  80. $this->assertTrue($instance->file_exists('bar.txt'));
  81. }
  82. public function testMoveFromStorageNestedJail() {
  83. /** @var \OC\Files\Storage\CommonTest|MockObject $instance */
  84. $instance = $this->getMockBuilder(\OC\Files\Storage\CommonTest::class)
  85. ->setMethods(['copyFromStorage', 'rmdir', 'unlink'])
  86. ->setConstructorArgs([['datadir' => $this->tmpDir]])
  87. ->getMock();
  88. $instance->method('copyFromStorage')
  89. ->willThrowException(new \Exception('copy'));
  90. $source = new Jail([
  91. 'storage' => $instance,
  92. 'root' => 'foo'
  93. ]);
  94. $source = new Wrapper([
  95. 'storage' => $source
  96. ]);
  97. $source = new Jail([
  98. 'storage' => $source,
  99. 'root' => 'bar'
  100. ]);
  101. $source = new Wrapper([
  102. 'storage' => $source
  103. ]);
  104. $instance->mkdir('foo');
  105. $instance->mkdir('foo/bar');
  106. $instance->file_put_contents('foo/bar/foo.txt', 'bar');
  107. $instance->moveFromStorage($source, 'foo.txt', 'bar.txt');
  108. $this->assertTrue($instance->file_exists('bar.txt'));
  109. }
  110. }