From fe7c2b51153cea5cdf0c8e0371b6bd3c0fa3fbb5 Mon Sep 17 00:00:00 2001 From: Hugo Sales Date: Tue, 3 Aug 2021 17:56:25 +0000 Subject: [PATCH] [TESTS] Raise test coverage for AdminPanel controller to 100% --- src/Controller/AdminPanel.php | 31 +++++++----- tests/Controller/AdminTest.php | 90 ++++++++++++++++++++++++++++++++++ 2 files changed, 110 insertions(+), 11 deletions(-) create mode 100644 tests/Controller/AdminTest.php diff --git a/src/Controller/AdminPanel.php b/src/Controller/AdminPanel.php index 94e75d411c..a5d948a816 100644 --- a/src/Controller/AdminPanel.php +++ b/src/Controller/AdminPanel.php @@ -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); diff --git a/tests/Controller/AdminTest.php b/tests/Controller/AdminTest.php new file mode 100644 index 0000000000..1f171bbfa9 --- /dev/null +++ b/tests/Controller/AdminTest.php @@ -0,0 +1,90 @@ +. + +// }}} + +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', + ])); + } +}