-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
4 changed files
with
167 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
|
||
namespace Test\WappMatchers; | ||
|
||
use League\Flysystem\Filesystem; | ||
use League\Flysystem\StorageAttributes; | ||
use org\bovigo\vfs\vfsStream; | ||
use org\bovigo\vfs\vfsStreamDirectory; | ||
use PHPUnit\Framework\Attributes\CoversClass; | ||
use PHPUnit\Framework\TestCase; | ||
use Plesk\Wappspector\FileSystemFactory; | ||
use Plesk\Wappspector\WappMatchers\UpLevelMatcherTrait; | ||
use Plesk\Wappspector\WappMatchers\WappMatcherInterface; | ||
|
||
#[CoversClass(UpLevelMatcherTrait::class)] | ||
class UpLevelMatcherTraitTest extends TestCase | ||
{ | ||
protected vfsStreamDirectory $root; | ||
protected Filesystem $fs; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->root = vfsStream::setup(); | ||
$this->fs = (new FileSystemFactory())($this->root->url()); | ||
} | ||
|
||
public function testNoParentDir(): void | ||
{ | ||
vfsStream::create([ | ||
'readme.txt' => '', | ||
]); | ||
|
||
$result = $this->getTestMatcher()->match($this->fs, ''); | ||
$this->assertIsArray($result); | ||
$this->assertEmpty($result); | ||
} | ||
|
||
public function testInaccessibleParentDir(): void | ||
{ | ||
vfsStream::create([ | ||
'unreadable' => [ | ||
'readable' => [ | ||
'readme.txt' => '', | ||
], | ||
'readme.md' => '', | ||
], | ||
]); | ||
|
||
$this->root->getChild('unreadable')->chmod(0000)->chown(0); | ||
|
||
$result = $this->getTestMatcher()->match($this->fs, 'unreadable/readable'); | ||
$this->assertIsArray($result); | ||
$this->assertEmpty($result); | ||
} | ||
|
||
public function testParentDirNormalization(): void | ||
{ | ||
vfsStream::create([ | ||
'parent' => [ | ||
'child' => [ | ||
'readme.txt' => '', | ||
], | ||
'readme.md' => '', | ||
], | ||
]); | ||
|
||
$result = $this->getTestMatcher()->match($this->fs, 'parent/child'); | ||
$this->assertIsArray($result); | ||
$this->assertArrayHasKey('path', $result); | ||
$this->assertStringNotContainsString('..', $result['path']); | ||
} | ||
|
||
public function getTestMatcher(): WappMatcherInterface | ||
{ | ||
return new class () implements WappMatcherInterface { | ||
use UpLevelMatcherTrait; | ||
|
||
protected function doMatch(Filesystem $fs, string $path): array | ||
{ | ||
$list = $fs->listContents($path); | ||
foreach ($list as $item) { | ||
/** @var StorageAttributes $item */ | ||
if ($item->isFile() && str_ends_with($item->path(), '.md')) { | ||
return [ | ||
'matcher' => 'markdown', | ||
'path' => $path, | ||
'version' => null, | ||
]; | ||
} | ||
} | ||
|
||
return []; | ||
} | ||
}; | ||
} | ||
} |