Skip to content

Commit

Permalink
UHF-8570: Test coverage for mkdir call
Browse files Browse the repository at this point in the history
  • Loading branch information
tuutti committed Mar 26, 2024
1 parent 3285c55 commit 3fc8c27
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 10 deletions.
5 changes: 2 additions & 3 deletions src/AzureFileSystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Drupal\Core\File\FileSystem;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Site\Settings;
use Drupal\Core\StreamWrapper\StreamWrapperManager;
use Drupal\Core\StreamWrapper\StreamWrapperManagerInterface;
use Psr\Log\LoggerInterface;

Expand Down Expand Up @@ -78,7 +77,7 @@ public function mkdir(

// If the URI has a scheme, don't override the umask - schemes can handle
// this issue in their own implementation.
if (StreamWrapperManager::getScheme($uri)) {
if ($this->streamWrapperManager::getScheme($uri)) {
return $this->mkdirCall($uri, 0777, $recursive, $context);
}

Expand Down Expand Up @@ -108,7 +107,7 @@ public function mkdir(
$recursive_path .= $component;

if (!file_exists($recursive_path)) {
$success = $this->mkdirCall($recursive_path, $mode, FALSE, $context);
$success = $this->mkdirCall($recursive_path, 0777, FALSE, $context);
// If the operation failed, check again if the directory was created
// by another process/server, only report a failure if not.
if (!$success && !file_exists($recursive_path)) {
Expand Down
131 changes: 124 additions & 7 deletions tests/src/Unit/AzureFileSystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@

namespace Drupal\Tests\helfi_azure_fs\Unit;

use Drupal\Core\File\FileSystem;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Site\Settings;
use Drupal\Core\StreamWrapper\StreamWrapperManager;
use Drupal\Core\StreamWrapper\StreamWrapperManagerInterface;
use Drupal\helfi_azure_fs\AzureFileSystem;
use Drupal\Tests\UnitTestCase;
use org\bovigo\vfs\vfsStream;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use Psr\Log\LoggerInterface;

/**
Expand All @@ -23,22 +25,79 @@ class AzureFileSystemTest extends UnitTestCase {

use ProphecyTrait;

/**
* Mocks stream wrapper manager.
*
* @param bool $expectedValue
* The expected return value.
*
* @return \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface
* The stream wrapper mock.
*/
private function getStreamWrapperManagerMock(bool $expectedValue) : StreamWrapperManagerInterface {
return new class($expectedValue) extends StreamWrapperManager {

// phpcs:ignore
private static bool $expectedReturnValue;

/**
* Constructs a new instance.
*
* @param bool $value
* The value.
*/
public function __construct(bool $value) {
static::$expectedReturnValue = $value;
}

/**
* {@inheritdoc}
*/
public static function getScheme($uri) {
return static::$expectedReturnValue;
}

};
}

/**
* Asserts that the file permissions of a given URI matches.
*
* @param int $expected_mode
* The expected file mode.
* @param string $uri
* The URI to test.
* @param string $message
* An optional error message.
*
* @internal
*/
private function assertFilePermissions(int $expected_mode, string $uri, string $message = ''): void {
// Mask out all but the last three octets.
$actual_mode = fileperms($uri) & 0777;
$this->assertSame($expected_mode, $actual_mode, $message);
}

/**
* Gets the file system to test.
*
* @param \Prophecy\Prophecy\ObjectProphecy $decorated
* @param \Drupal\Core\File\FileSystemInterface $decorated
* The prophesized FS.
* @param \Drupal\Core\Site\Settings $settings
* The settings.
* @param \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface|null $streamWrapperManager
* The stream wrapper manager.
*
* @return \Drupal\helfi_azure_fs\AzureFileSystem
* The SUT.
*/
private function getSut(ObjectProphecy $decorated, Settings $settings) : AzureFileSystem {
$streamWrapperManager = $this->createMock(StreamWrapperManagerInterface::class);
private function getSut(FileSystemInterface $decorated, Settings $settings, ?StreamWrapperManagerInterface $streamWrapperManager = NULL) : AzureFileSystem {
if (!$streamWrapperManager) {
$streamWrapperManager = $this->createMock(StreamWrapperManagerInterface::class);
}
$logger = $this->createMock(LoggerInterface::class);

return new AzureFileSystem($decorated->reveal(), $streamWrapperManager, $settings, $logger);
return new AzureFileSystem($decorated, $streamWrapperManager, $settings, $logger);
}

/**
Expand All @@ -54,7 +113,7 @@ public function testChmodSkipFsOperations(array $structure, string $uri) : void
$decorated = $this->prophesize(FileSystemInterface::class);
$decorated->chmod($uri, NULL)->shouldNotBeCalled();

$fs = $this->getSut($decorated, new Settings(['is_azure' => TRUE]));
$fs = $this->getSut($decorated->reveal(), new Settings(['is_azure' => TRUE]));
$this->assertTrue($fs->chmod($uri));
}

Expand All @@ -74,7 +133,7 @@ public function testSkipOperationsFallback(array $structure, string $uri) : void
->shouldBeCalled()
->willReturn(TRUE);

$this->getSut($decorated, new Settings([]))->chmod($uri);
$this->getSut($decorated->reveal(), new Settings([]))->chmod($uri);
}

/**
Expand All @@ -96,4 +155,62 @@ public function chmodFolderData() : array {
];
}

/**
* Tests mkdir with skipFsOperations.
*
* @dataProvider chmodFolderData
*/
public function testMkdirSkipFsOperations(array $structure, string $uri) : void {
vfsStream::setup('dir');
vfsStream::create($structure);

// Make sure decorated service is called when 'skipFsOperations'
// is disabled.
$decorated = $this->prophesize(FileSystemInterface::class);
$decorated->mkdir($uri, NULL, FALSE, NULL)
->shouldBeCalled()
->willReturn(TRUE);

$this->getSut($decorated->reveal(), new Settings([]))->mkdir($uri);
}

/**
* Tests mkdir with scheme.
*/
public function testMkdirWithScheme() : void {
$streamWrapperManager = $this->getStreamWrapperManagerMock(TRUE);
$uri = 'vfs://dir/subdir';
$decorated = $this->prophesize(FileSystemInterface::class);
$decorated->mkdir($uri, NULL, FALSE, NULL)
->shouldNotBeCalled();
$sut = $this->getSut($decorated->reveal(), new Settings(['is_azure' => TRUE]), $streamWrapperManager);
$this->assertTrue($sut->mkdir($uri));
$this->assertTrue(file_exists($uri));
$this->assertFilePermissions(0777, $uri);
}

/**
* Tests mkdir without skipFsOperations.
*/
public function testMkDirNoScheme() : void {
$streamWrapperManager = $this->getStreamWrapperManagerMock(FALSE);
vfsStream::setup('dir');
$decorated = $this->prophesize(FileSystem::class);
$decorated->mkdir(Argument::any(), Argument::any(), Argument::any(), Argument::any())
->shouldNotBeCalled();

$settings = new Settings(['is_azure' => TRUE]);
$sut = $this->getSut($decorated->reveal(), $settings, $streamWrapperManager);

$uri = 'vfs://dir/subdir/';
$this->assertTrue($sut->mkdir($uri));
$this->assertTrue(file_exists($uri));
$this->assertFilePermissions(0777, $uri);

$uri = 'vfs://dir/subdir/subdir2';
$this->assertTrue($sut->mkdir($uri, recursive: TRUE));
$this->assertTrue(file_exists($uri));
$this->assertFilePermissions(0777, $uri);
}

}

0 comments on commit 3fc8c27

Please sign in to comment.