1
0

FileAccessHelperTest.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. /**
  3. * @author Lukas Reschke <lukas@owncloud.com>
  4. *
  5. * @copyright Copyright (c) 2015, ownCloud, Inc.
  6. * @license AGPL-3.0
  7. *
  8. * This code is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License, version 3,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License, version 3,
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>
  19. *
  20. */
  21. namespace Test\IntegrityCheck\Helpers;
  22. use OC\IntegrityCheck\Helpers\FileAccessHelper;
  23. use Test\TestCase;
  24. class FileAccessHelperTest extends TestCase {
  25. /** @var FileAccessHelper */
  26. private $fileAccessHelper;
  27. protected function setUp(): void {
  28. parent::setUp();
  29. $this->fileAccessHelper = new FileAccessHelper();
  30. }
  31. public function testReadAndWrite() {
  32. $tempManager = \OC::$server->getTempManager();
  33. $filePath = $tempManager->getTemporaryFile();
  34. $data = 'SomeDataGeneratedByIntegrityCheck';
  35. $this->fileAccessHelper->file_put_contents($filePath, $data);
  36. $this->assertSame($data, $this->fileAccessHelper->file_get_contents($filePath));
  37. }
  38. public function testFile_put_contentsWithException() {
  39. $this->expectException(\Exception::class);
  40. $this->expectExceptionMessage('Failed to write into /anabsolutelynotexistingfolder/on/the/system.txt');
  41. $this->fileAccessHelper->file_put_contents('/anabsolutelynotexistingfolder/on/the/system.txt', 'MyFiles');
  42. }
  43. public function testIs_writable() {
  44. $this->assertFalse($this->fileAccessHelper->is_writable('/anabsolutelynotexistingfolder/on/the/system.txt'));
  45. $this->assertTrue($this->fileAccessHelper->is_writable(\OC::$server->getTempManager()->getTemporaryFile('MyFile')));
  46. }
  47. public function testAssertDirectoryExistsWithException() {
  48. $this->expectException(\Exception::class);
  49. $this->expectExceptionMessage('Directory /anabsolutelynotexistingfolder/on/the/system does not exist.');
  50. $this->fileAccessHelper->assertDirectoryExists('/anabsolutelynotexistingfolder/on/the/system');
  51. }
  52. public function testAssertDirectoryExists() {
  53. $this->fileAccessHelper->assertDirectoryExists(\OC::$server->getTempManager()->getTemporaryFolder('/testfolder/'));
  54. $this->addToAssertionCount(1);
  55. }
  56. }