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