Merge pull request #15530 from seyfer/extract-twig-file-listing

extract html for FileListing into twig and add tests #14801
This commit is contained in:
Maurício Meneghini Fauth 2019-10-28 19:59:11 -03:00 committed by GitHub
commit 1b59e2d7f3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 66 additions and 9 deletions

View File

@ -64,15 +64,13 @@ class FileListing
if ($list === false) {
return false;
}
$result = '';
foreach ($list as $val) {
$result .= '<option value="' . htmlspecialchars($val) . '"';
if ($val == $active) {
$result .= ' selected="selected"';
}
$result .= '>' . htmlspecialchars($val) . '</option>' . "\n";
}
return $result;
$template = new Template();
return $template->render('file_select_options', [
'filesList' => $list,
'active' => $active
]);
}
/**

View File

@ -0,0 +1,5 @@
{% for file in filesList %}
<option value="{{ file }}"{% if file == active %} selected="selected"{% endif %}>
{{ file }}
</option>
{% endfor %}

View File

@ -34,6 +34,16 @@ class FileListingTest extends TestCase
public function testGetDirContent(): void
{
$this->assertFalse($this->fileListing->getDirContent('nonexistent directory'));
$fixturesDir = ROOT_PATH . 'test/classes/_data/file_listing';
$this->assertSame(
array_values([
'one.txt',
'two.md',
]),
array_values($this->fileListing->getDirContent($fixturesDir))
);
}
/**
@ -41,7 +51,51 @@ class FileListingTest extends TestCase
*/
public function testGetFileSelectOptions(): void
{
$fixturesDir = ROOT_PATH . 'test/classes/_data/file_listing';
$this->assertFalse($this->fileListing->getFileSelectOptions('nonexistent directory'));
$expectedHtmlWithoutActive = <<<HTML
<option value="one.txt">
one.txt
</option>
<option value="two.md">
two.md
</option>
HTML;
$this->assertSame(
$expectedHtmlWithoutActive,
$this->fileListing->getFileSelectOptions($fixturesDir)
);
$expectedHtmlWithActive = <<<HTML
<option value="one.txt">
one.txt
</option>
<option value="two.md" selected="selected">
two.md
</option>
HTML;
$this->assertSame(
$expectedHtmlWithActive,
$this->fileListing->getFileSelectOptions($fixturesDir, '', 'two.md')
);
$expectedFilteredHtml = <<<HTML
<option value="one.txt">
one.txt
</option>
HTML;
$this->assertSame(
$expectedFilteredHtml,
$this->fileListing->getFileSelectOptions($fixturesDir, '/.*\.txt/')
);
}
/**

View File

View File