1
0

TestBase.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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\Archive;
  8. abstract class TestBase extends \Test\TestCase {
  9. /**
  10. * @var \OC\Archive\Archive
  11. */
  12. protected $instance;
  13. /**
  14. * get the existing test archive
  15. * @return \OC\Archive\Archive
  16. */
  17. abstract protected function getExisting();
  18. /**
  19. * get a new archive for write testing
  20. * @return \OC\Archive\Archive
  21. */
  22. abstract protected function getNew();
  23. public function testGetFiles(): void {
  24. $this->instance = $this->getExisting();
  25. $allFiles = $this->instance->getFiles();
  26. $expected = ['lorem.txt','logo-wide.png','dir/', 'dir/lorem.txt'];
  27. $this->assertEquals(4, count($allFiles), 'only found '.count($allFiles).' out of 4 expected files');
  28. foreach ($expected as $file) {
  29. $this->assertContains($file, $allFiles, 'cant find '. $file . ' in archive');
  30. $this->assertTrue($this->instance->fileExists($file), 'file '.$file.' does not exist in archive');
  31. }
  32. $this->assertFalse($this->instance->fileExists('non/existing/file'));
  33. $rootContent = $this->instance->getFolder('');
  34. $expected = ['lorem.txt','logo-wide.png', 'dir/'];
  35. $this->assertEquals(3, count($rootContent));
  36. foreach ($expected as $file) {
  37. $this->assertContains($file, $rootContent, 'cant find '. $file . ' in archive');
  38. }
  39. $dirContent = $this->instance->getFolder('dir/');
  40. $expected = ['lorem.txt'];
  41. $this->assertEquals(1, count($dirContent));
  42. foreach ($expected as $file) {
  43. $this->assertContains($file, $dirContent, 'cant find '. $file . ' in archive');
  44. }
  45. }
  46. public function testContent(): void {
  47. $this->instance = $this->getExisting();
  48. $dir = \OC::$SERVERROOT.'/tests/data';
  49. $textFile = $dir.'/lorem.txt';
  50. $this->assertEquals(file_get_contents($textFile), $this->instance->getFile('lorem.txt'));
  51. $tmpFile = \OC::$server->getTempManager()->getTemporaryFile('.txt');
  52. $this->instance->extractFile('lorem.txt', $tmpFile);
  53. $this->assertEquals(file_get_contents($textFile), file_get_contents($tmpFile));
  54. }
  55. public function testWrite(): void {
  56. $dir = \OC::$SERVERROOT.'/tests/data';
  57. $textFile = $dir.'/lorem.txt';
  58. $this->instance = $this->getNew();
  59. $this->assertEquals(0, count($this->instance->getFiles()));
  60. $this->instance->addFile('lorem.txt', $textFile);
  61. $this->assertEquals(1, count($this->instance->getFiles()));
  62. $this->assertTrue($this->instance->fileExists('lorem.txt'));
  63. $this->assertFalse($this->instance->fileExists('lorem.txt/'));
  64. $this->assertEquals(file_get_contents($textFile), $this->instance->getFile('lorem.txt'));
  65. $this->instance->addFile('lorem.txt', 'foobar');
  66. $this->assertEquals('foobar', $this->instance->getFile('lorem.txt'));
  67. }
  68. public function testReadStream(): void {
  69. $dir = \OC::$SERVERROOT.'/tests/data';
  70. $this->instance = $this->getExisting();
  71. $fh = $this->instance->getStream('lorem.txt', 'r');
  72. $this->assertTrue((bool)$fh);
  73. $content = fread($fh, $this->instance->filesize('lorem.txt'));
  74. fclose($fh);
  75. $this->assertEquals(file_get_contents($dir.'/lorem.txt'), $content);
  76. }
  77. public function testWriteStream(): void {
  78. $dir = \OC::$SERVERROOT.'/tests/data';
  79. $this->instance = $this->getNew();
  80. $fh = $this->instance->getStream('lorem.txt', 'w');
  81. $source = fopen($dir.'/lorem.txt', 'r');
  82. \OCP\Files::streamCopy($source, $fh);
  83. fclose($source);
  84. fclose($fh);
  85. $this->assertTrue($this->instance->fileExists('lorem.txt'));
  86. $this->assertEquals(file_get_contents($dir.'/lorem.txt'), $this->instance->getFile('lorem.txt'));
  87. }
  88. public function testFolder(): void {
  89. $this->instance = $this->getNew();
  90. $this->assertFalse($this->instance->fileExists('/test'));
  91. $this->assertFalse($this->instance->fileExists('/test/'));
  92. $this->instance->addFolder('/test');
  93. $this->assertTrue($this->instance->fileExists('/test'));
  94. $this->assertTrue($this->instance->fileExists('/test/'));
  95. $this->instance->remove('/test');
  96. $this->assertFalse($this->instance->fileExists('/test'));
  97. $this->assertFalse($this->instance->fileExists('/test/'));
  98. }
  99. public function testExtract(): void {
  100. $dir = \OC::$SERVERROOT.'/tests/data';
  101. $this->instance = $this->getExisting();
  102. $tmpDir = \OC::$server->getTempManager()->getTemporaryFolder();
  103. $this->instance->extract($tmpDir);
  104. $this->assertEquals(true, file_exists($tmpDir.'lorem.txt'));
  105. $this->assertEquals(true, file_exists($tmpDir.'dir/lorem.txt'));
  106. $this->assertEquals(true, file_exists($tmpDir.'logo-wide.png'));
  107. $this->assertEquals(file_get_contents($dir.'/lorem.txt'), file_get_contents($tmpDir.'lorem.txt'));
  108. \OCP\Files::rmdirr($tmpDir);
  109. }
  110. public function testMoveRemove(): void {
  111. $dir = \OC::$SERVERROOT.'/tests/data';
  112. $textFile = $dir.'/lorem.txt';
  113. $this->instance = $this->getNew();
  114. $this->instance->addFile('lorem.txt', $textFile);
  115. $this->assertFalse($this->instance->fileExists('target.txt'));
  116. $this->instance->rename('lorem.txt', 'target.txt');
  117. $this->assertTrue($this->instance->fileExists('target.txt'));
  118. $this->assertFalse($this->instance->fileExists('lorem.txt'));
  119. $this->assertEquals(file_get_contents($textFile), $this->instance->getFile('target.txt'));
  120. $this->instance->remove('target.txt');
  121. $this->assertFalse($this->instance->fileExists('target.txt'));
  122. }
  123. public function testRecursive(): void {
  124. $dir = \OC::$SERVERROOT.'/tests/data';
  125. $this->instance = $this->getNew();
  126. $this->instance->addRecursive('/dir', $dir);
  127. $this->assertTrue($this->instance->fileExists('/dir/lorem.txt'));
  128. $this->assertTrue($this->instance->fileExists('/dir/data.zip'));
  129. $this->assertTrue($this->instance->fileExists('/dir/data.tar.gz'));
  130. }
  131. }