FileAccessHelperTest.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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-only
  6. */
  7. namespace Test\IntegrityCheck\Helpers;
  8. use OC\IntegrityCheck\Helpers\FileAccessHelper;
  9. use Test\TestCase;
  10. class FileAccessHelperTest extends TestCase {
  11. /** @var FileAccessHelper */
  12. private $fileAccessHelper;
  13. protected function setUp(): void {
  14. parent::setUp();
  15. $this->fileAccessHelper = new FileAccessHelper();
  16. }
  17. public function testReadAndWrite() {
  18. $tempManager = \OC::$server->getTempManager();
  19. $filePath = $tempManager->getTemporaryFile();
  20. $data = 'SomeDataGeneratedByIntegrityCheck';
  21. $this->fileAccessHelper->file_put_contents($filePath, $data);
  22. $this->assertSame($data, $this->fileAccessHelper->file_get_contents($filePath));
  23. }
  24. public function testFile_put_contentsWithException() {
  25. $this->expectException(\Exception::class);
  26. $this->expectExceptionMessage('Failed to write into /anabsolutelynotexistingfolder/on/the/system.txt');
  27. $this->fileAccessHelper->file_put_contents('/anabsolutelynotexistingfolder/on/the/system.txt', 'MyFiles');
  28. }
  29. public function testIs_writable() {
  30. $this->assertFalse($this->fileAccessHelper->is_writable('/anabsolutelynotexistingfolder/on/the/system.txt'));
  31. $this->assertTrue($this->fileAccessHelper->is_writable(\OC::$server->getTempManager()->getTemporaryFile('MyFile')));
  32. }
  33. public function testAssertDirectoryExistsWithException() {
  34. $this->expectException(\Exception::class);
  35. $this->expectExceptionMessage('Directory /anabsolutelynotexistingfolder/on/the/system does not exist.');
  36. $this->fileAccessHelper->assertDirectoryExists('/anabsolutelynotexistingfolder/on/the/system');
  37. }
  38. public function testAssertDirectoryExists() {
  39. $this->fileAccessHelper->assertDirectoryExists(\OC::$server->getTempManager()->getTemporaryFolder('/testfolder/'));
  40. $this->addToAssertionCount(1);
  41. }
  42. }