Skip to content

Commit

Permalink
Remove unnecessary logic in storage handler exists method
Browse files Browse the repository at this point in the history
  • Loading branch information
bastianallgeier authored and lukasbestle committed May 21, 2024
1 parent 8c18e60 commit b26d23e
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 65 deletions.
11 changes: 3 additions & 8 deletions src/Content/ContentStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,18 +156,13 @@ public function dynamicVersions(): array
/**
* Checks if a version exists
*
* @param string|null $lang Code `'default'` in a single-lang installation;
* checks for "any language" if not provided
* @param string|null $lang Code `'default'` in a single-lang installation
*/
public function exists(
VersionId $versionId,
string|null $lang
string $lang
): bool {
if ($lang !== null) {
$lang = $this->language($lang);
}

return $this->handler->exists($versionId, $lang);
return $this->handler->exists($versionId, $this->language($lang));
}

/**
Expand Down
5 changes: 2 additions & 3 deletions src/Content/ContentStorageHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,9 @@ public function delete(VersionId $versionId, string $lang): void;
/**
* Checks if a version exists
*
* @param string|null $lang Code `'default'` in a single-lang installation;
* checks for "any language" if not provided
* @param string $lang Code `'default'` in a single-lang installation
*/
public function exists(VersionId $versionId, string|null $lang): bool;
public function exists(VersionId $versionId, string $lang): bool;

/**
* Returns the modification timestamp of a version if it exists
Expand Down
15 changes: 2 additions & 13 deletions src/Content/PlainTextContentStorageHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,21 +205,10 @@ public function delete(VersionId $versionId, string $lang): void
/**
* Checks if a version exists
*
* @param string|null $lang Code `'default'` in a single-lang installation;
* checks for "any language" if not provided
* @param string|null $lang Code `'default'` in a single-lang installation
*/
public function exists(VersionId $versionId, string|null $lang): bool
public function exists(VersionId $versionId, string $lang): bool
{
if ($lang === null) {
foreach ($this->contentFiles($versionId) as $file) {
if (is_file($file) === true) {
return true;
}
}

return false;
}

return is_file($this->contentFile($versionId, $lang)) === true;
}

Expand Down
42 changes: 1 addition & 41 deletions tests/Content/PlainTextContentStorageHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,56 +169,16 @@ public function testDeletePublishedSingleLang()
* @covers ::exists
* @dataProvider existsProvider
*/
public function testExistsNoneExisting(VersionId $id, string|null $language)
public function testExistsNoneExisting(VersionId $id, string $language)
{
$this->assertFalse($this->storage->exists($id, $language));
}

/**
* @covers ::exists
* @dataProvider existsProvider
*/
public function testExistsSomeExistingMultiLang(VersionId $id, string|null $language, array $expected)
{
Dir::make(static::TMP . '/_changes');
touch(static::TMP . '/article.txt');
touch(static::TMP . '/_changes/article.en.txt');

new App([
'languages' => [
[
'code' => 'en',
'default' => true
],
[
'code' => 'de'
]
]
]);

$this->assertSame($expected[0], $this->storage->exists($id, $language));
}

/**
* @covers ::exists
* @dataProvider existsProvider
*/
public function testExistsSomeExistingSingleLang(VersionId $id, string|null $language, array $expected)
{
Dir::make(static::TMP . '/_changes');
touch(static::TMP . '/article.txt');
touch(static::TMP . '/_changes/article.en.txt');

$this->assertSame($expected[1], $this->storage->exists($id, $language));
}

public static function existsProvider(): array
{
return [
[VersionId::changes(), null, [true, false]],
[VersionId::changes(), 'default', [false, false]],
[VersionId::changes(), 'en', [true, true]],
[VersionId::published(), null, [false, true]],
[VersionId::published(), 'default', [true, true]],
[VersionId::published(), 'en', [false, false]]
];
Expand Down

0 comments on commit b26d23e

Please sign in to comment.