Skip to content

Commit

Permalink
Move html from server/select to template
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Tiringer <tiringerdaniel@tutanota.com>

Factor out logic from server listing

Signed-off-by: Daniel Tiringer <tiringerdaniel@tutanota.com>

Create the base twig skeleton

Signed-off-by: Daniel Tiringer <tiringerdaniel@tutanota.com>

Insert template and variables to form part

Signed-off-by: Daniel Tiringer <tiringerdaniel@tutanota.com>

Insert list view template part

Signed-off-by: Daniel Tiringer <tiringerdaniel@tutanota.com>

Insert just options fork

Signed-off-by: Daniel Tiringer <tiringerdaniel@tutanota.com>

Factor out repeating code into server_options template

Signed-off-by: Daniel Tiringer <tiringerdaniel@tutanota.com>

Render the template

Signed-off-by: Daniel Tiringer <tiringerdaniel@tutanota.com>

Comment out test parts that don't work due to translate

Signed-off-by: Daniel Tiringer <tiringerdaniel@tutanota.com>

Fix the typos in the translatable elements

Signed-off-by: Daniel Tiringer <tiringerdaniel@tutanota.com>

Refactor the test into separate scenarios

Signed-off-by: Daniel Tiringer <tiringerdaniel@tutanota.com>

Remove indentation spaces

Signed-off-by: Daniel Tiringer <tiringerdaniel@tutanota.com>

Codesniffer cleanup

Signed-off-by: Daniel Tiringer <tiringerdaniel@tutanota.com>

Add test for fieldset existence

Signed-off-by: Daniel Tiringer <tiringerdaniel@tutanota.com>

Code style fixes

Signed-off-by: Daniel Tiringer <tiringerdaniel@tutanota.com>

Reorder lines in test

Signed-off-by: Daniel Tiringer <tiringerdaniel@tutanota.com>

Replace include function with tag

Signed-off-by: Daniel Tiringer <tiringerdaniel@tutanota.com>

Removing the escape calls

Signed-off-by: Daniel Tiringer <tiringerdaniel@tutanota.com>
  • Loading branch information
Daniel Tiringer authored and danielTiringer committed May 5, 2021
1 parent d260e34 commit debb6af
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 99 deletions.
76 changes: 33 additions & 43 deletions libraries/classes/Server/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@

namespace PhpMyAdmin\Server;

use PhpMyAdmin\Template;
use PhpMyAdmin\Url;
use PhpMyAdmin\Util;

use function count;
use function htmlspecialchars;
use function implode;
use function is_array;
use function strpos;
Expand All @@ -31,8 +31,6 @@ class Select
*/
public static function render($not_only_options, $omit_fieldset)
{
$retval = '';

// Show as list?
if ($not_only_options) {
$list = $GLOBALS['cfg']['DisplayServersList'];
Expand All @@ -41,29 +39,15 @@ public static function render($not_only_options, $omit_fieldset)
$list = false;
}

$form_action = '';
if ($not_only_options) {
$retval .= '<form method="post" action="'
. Util::getScriptNameForOption(
$GLOBALS['cfg']['DefaultTabServer'],
'server'
)
. '" class="disableAjax">';

if (! $omit_fieldset) {
$retval .= '<fieldset class="pma-fieldset">';
}

$retval .= Url::getHiddenFields([]);
$retval .= '<label for="select_server">'
. __('Current server:') . '</label> ';

$retval .= '<select name="server" id="select_server" class="autosubmit">';
$retval .= '<option value="">(' . __('Servers') . ') ...</option>' . "\n";
} elseif ($list) {
$retval .= __('Current server:') . '<br>';
$retval .= '<ul id="list_server">';
$form_action = Util::getScriptNameForOption(
$GLOBALS['cfg']['DefaultTabServer'],
'server'
);
}

$servers = [];
foreach ($GLOBALS['cfg']['Servers'] as $key => $server) {
if (empty($server['host'])) {
continue;
Expand Down Expand Up @@ -98,39 +82,45 @@ public static function render($not_only_options, $omit_fieldset)
}

if ($list) {
$retval .= '<li>';
if ($selected) {
$retval .= '<strong>' . htmlspecialchars($label) . '</strong>';
$servers['list'][] = [
'selected' => true,
'label' => $label,
];
} else {
$scriptName = Util::getScriptNameForOption(
$GLOBALS['cfg']['DefaultTabServer'],
'server'
);
$retval .= '<a class="disableAjax item" href="'
. $scriptName
. Url::getCommon(['server' => $key], strpos($scriptName, '?') === false ? '?' : '&')
. '" >' . htmlspecialchars($label) . '</a>';
$href = $scriptName . Url::getCommon(
['server' => $key],
strpos($scriptName, '?') === false ? '?' : '&'
);
$servers['list'][] = [
'href' => $href,
'label' => $label,
];
}

$retval .= '</li>';
} else {
$retval .= '<option value="' . $key . '" '
. ($selected ? ' selected="selected"' : '') . '>'
. htmlspecialchars($label) . '</option>' . "\n";
$servers['select'][] = [
'value' => $key,
'selected' => $selected,
'label' => $label,
];
}
}

$renderDetails = [
'not_only_options' => $not_only_options,
'omit_fieldset' => $omit_fieldset,
'servers' => $servers,
];
if ($not_only_options) {
$retval .= '</select>';
if (! $omit_fieldset) {
$retval .= '</fieldset>';
}

$retval .= '</form>';
} elseif ($list) {
$retval .= '</ul>';
$renderDetails['form_action'] = $form_action;
}

return $retval;
$template = new Template();

return $template->render('server/select/index', $renderDetails);
}
}
37 changes: 37 additions & 0 deletions templates/server/select/index.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{% if not_only_options %}
<form class="disableAjax" method="post" action="{{ form_action|raw }}">
{% if omit_fieldset == false %}
<fieldset class="pma-fieldset">
{% endif %}
{{ get_hidden_fields([]) }}
<label for="select_server">{% trans 'Current server:' %}</label>
<select id="select_server" class="autosubmit" name="server">
<option value="">({% trans 'Servers' %}) ...</option>
{% include 'server/select/server_options.twig' with {
'select': servers.select
} only %}
</select>
{% if omit_fieldset == false %}
</fieldset>
{% endif %}
</form>
{% elseif servers.list %}
{% trans 'Current server:' %}<br>
<ul id="list_server">
<li>
{% for server in servers.list %}
{% if server.selected %}
<strong>{{ server.label }}</strong>
{% else %}
<a class="disableAjax item" href="{{ server.href }}">
{{- server.label -}}
</a>
{% endif %}
{% endfor %}
</li>
</ul>
{% else %}
{% include 'server/select/server_options.twig' with {
'select': servers.select
} only %}
{% endif %}
8 changes: 8 additions & 0 deletions templates/server/select/server_options.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{% for server in select %}
<option
value="{{ server.value }}"
{% if server.selected %}selected="selected"{% endif %}
>
{{- server.label -}}
</option>
{% endfor %}
112 changes: 56 additions & 56 deletions test/classes/Server/SelectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,6 @@ protected function setUp(): void

$GLOBALS['table'] = 'table';

//$_SESSION
}

/**
* Test for Select::render
*/
public function testRender(): void
{
$not_only_options = false;
$omit_fieldset = false;

$GLOBALS['cfg']['DefaultTabServer'] = 'welcome';

$GLOBALS['cfg']['Servers'] = [
Expand All @@ -67,57 +56,50 @@ public function testRender(): void
'auth_type' => 'config',
],
];
//$_SESSION
}

//$not_only_options=false & $omit_fieldset=false
$html = Select::render($not_only_options, $omit_fieldset);
$server = $GLOBALS['cfg']['Servers']['0'];

//server items
$this->assertStringContainsString(
$server['host'],
$html
);
$this->assertStringContainsString(
$server['port'],
$html
);
$this->assertStringContainsString(
$server['only_db'],
$html
);
$this->assertStringContainsString(
$server['user'],
$html
);

$not_only_options = true;
$omit_fieldset = true;
$GLOBALS['cfg']['DisplayServersList'] = null;
/**
* Test for Select::render
*
* @dataProvider renderDataProvider
*/
public function testRender(bool $not_only_options, bool $omit_fieldset): void
{
if ($not_only_options) {
$GLOBALS['cfg']['DisplayServersList'] = null;
}

//$not_only_options=true & $omit_fieldset=true
$html = Select::render($not_only_options, $omit_fieldset);
$server = $GLOBALS['cfg']['Servers']['0'];

//$GLOBALS['cfg']['DefaultTabServer']
$this->assertStringContainsString(
Util::getScriptNameForOption(
$GLOBALS['cfg']['DefaultTabServer'],
'server'
),
$html
);

//labels
$this->assertStringContainsString(
__('Current server:'),
$html
);
$this->assertStringContainsString(
'(' . __('Servers') . ')',
$html
);
if ($not_only_options) {
if (! $omit_fieldset) {
$this->assertStringContainsString(
'</fieldset>',
$html
);
}

$this->assertStringContainsString(
Util::getScriptNameForOption(
$GLOBALS['cfg']['DefaultTabServer'],
'server'
),
$html
);

$this->assertStringContainsString(
__('Current server:'),
$html
);
$this->assertStringContainsString(
'(' . __('Servers') . ')',
$html
);
}

//server items
$server = $GLOBALS['cfg']['Servers']['0'];
$this->assertStringContainsString(
$server['host'],
$html
Expand All @@ -135,4 +117,22 @@ public function testRender(): void
$html
);
}

public function renderDataProvider(): array
{
return [
'only options, don\'t omit fieldset' => [
false,
false,
],
'not only options, omits fieldset' => [
true,
true,
],
'not only options, don\'t omit fieldset' => [
true,
false,
],
];
}
}

0 comments on commit debb6af

Please sign in to comment.