[TESTS] Raise test coverage for AdminPanel controller to 100%

This commit is contained in:
Hugo Sales 2021-08-03 17:56:25 +00:00
parent f9c1d14c7a
commit 960675b459
Signed by untrusted user: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0
2 changed files with 110 additions and 11 deletions

View File

@ -36,7 +36,7 @@ use App\Core\Controller;
use App\Core\Form; use App\Core\Form;
use function App\Core\I18n\_m; use function App\Core\I18n\_m;
use App\Util\Common; use App\Util\Common;
use App\Util\Exceptiion\InvalidFormException; use App\Util\Exception\InvalidFormException;
use App\Util\Formatting; use App\Util\Formatting;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType; use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\Form\Extension\Core\Type\SubmitType;
@ -71,19 +71,28 @@ class AdminPanel extends Controller
if ($form->isSubmitted()) { if ($form->isSubmitted()) {
$data = $form->getData(); $data = $form->getData();
if ($form->isValid() && array_key_exists('setting', $data)) { if ($form->isValid() && array_key_exists('setting', $data)) {
list($section, $setting) = explode(':', $data['setting']); [$section, $setting] = explode(':', $data['setting']);
$value = $data['value']; foreach ([
if (preg_match('/^[0-9]+$/', $value)) { 'int' => FILTER_VALIDATE_INT,
$value = (int) $value; 'bool' => FILTER_VALIDATE_BOOL,
} elseif (strstr($value, ',') === false) { 'string' => [fn ($v) => strstr($v, ',') === false, fn ($v) => $v],
// empty, string 'array' => [fn ($v) => strstr($v, ',') !== false, function ($v) { Formatting::toArray($v, $v); return $v; }],
} elseif (Formatting::toArray($value, $value)) { ] as $type => $validator) {
// empty if (!is_array($validator)) {
} elseif (preg_match('/true|false/i', $value)) { $value = filter_var($data['value'], $validator, FILTER_NULL_ON_FAILURE);
$value = ($value == 'true'); if ($value !== null) {
break;
}
} else {
[$check, $convert] = $validator;
if ($check($data['value'])) {
$value = $convert($data['value']);
}
}
} }
$default = $defaults[$section][$setting]; $default = $defaults[$section][$setting];
// Sanity check // Sanity check
if (gettype($default) === gettype($value)) { if (gettype($default) === gettype($value)) {
$old_value = Common::config($section, $setting); $old_value = Common::config($section, $setting);

View File

@ -0,0 +1,90 @@
<?php
// {{{ License
// This file is part of GNU social - https://www.gnu.org/software/social
//
// GNU social is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GNU social is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
// }}}
namespace App\Tests\Core;
use App\Util\Common;
use App\Util\Formatting;
use App\Util\GNUsocialTestCase;
use Jchook\AssertThrows\AssertThrows;
class AdminTest extends GNUsocialTestCase
{
use AssertThrows;
private function test(array $setting, callable $get_value)
{
$client = static::createClient();
copy(INSTALLDIR . '/social.local.yaml', INSTALLDIR . '/social.local.yaml.back');
$old = Common::config(...$setting);
$value = $get_value();
$client->request('GET', '/panel');
$crawler = $client->submitForm('Set site setting', [
'save[setting]' => implode(':', $setting),
// False gets converted to "", which HTTP doesn't send, so we get null on the other side
'save[value]' => $value == false ? 'fAlse' : Formatting::toString($value),
]);
static::assertSame($value, Common::config(...$setting));
// $client->request('GET', '/panel');
$crawler = $client->submitForm('Set site setting', [
'save[setting]' => implode(':', $setting),
'save[value]' => Formatting::toString($old),
]);
static::assertSame($old, Common::config(...$setting));
rename(INSTALLDIR . '/social.local.yaml.back', INSTALLDIR . '/social.local.yaml');
}
public function testSiteString()
{
$this->test(['attachments', 'dir'], fn () => INSTALLDIR . '/foo');
}
public function testSiteInt()
{
$this->test(['attachments', 'max_width'], fn () => 1024);
}
public function testSiteArray()
{
$this->test(['plugins', 'core'], fn () => ['some plugin', 'some other']);
}
public function testSiteBoolTrue()
{
$this->test(['attachments', 'uploads'], fn () => true);
}
public function testSiteBoolFalse()
{
$this->test(['attachments', 'uploads'], fn () => false);
}
public function testSiteInvalidSection()
{
$client = static::createClient();
$client->request('GET', '/panel');
$this->assertThrows(\InvalidArgumentException::class,
fn () => $client->submitForm('Set site setting', [
'save[setting]' => 'invalid:section',
'save[value]' => 'false',
]));
}
}