AdminTest.php 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. declare(strict_types = 1);
  3. // {{{ License
  4. // This file is part of GNU social - https://www.gnu.org/software/social
  5. //
  6. // GNU social is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // GNU social is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  18. // }}}
  19. namespace App\Tests\Controller;
  20. use App\Core\DB\DB;
  21. use App\Util\Common;
  22. use App\Util\Formatting;
  23. use App\Util\GNUsocialTestCase;
  24. use InvalidArgumentException;
  25. use Jchook\AssertThrows\AssertThrows;
  26. class AdminTest extends GNUsocialTestCase
  27. {
  28. use AssertThrows;
  29. private function test(array $setting, callable $get_value)
  30. {
  31. $client = static::createClient();
  32. $client->loginUser(DB::findOneBy('local_user', ['nickname' => 'admin']));
  33. copy(INSTALLDIR . '/social.local.yaml', INSTALLDIR . '/social.local.yaml.back');
  34. $old = Common::config(...$setting);
  35. $value = $get_value();
  36. $client->request('GET', '/panel');
  37. $crawler = $client->submitForm('Set site setting', [
  38. 'save_admin[setting]' => implode(':', $setting),
  39. // False gets converted to "", which HTTP doesn't send, so we get null on the other side
  40. 'save_admin[value]' => $value == false ? 'false' : Formatting::toString($value),
  41. ]);
  42. static::assertSame($value, Common::config(...$setting));
  43. // $client->request('GET', '/panel');
  44. $crawler = $client->submitForm('Set site setting', [
  45. 'save_admin[setting]' => implode(':', $setting),
  46. 'save_admin[value]' => Formatting::toString($old),
  47. ]);
  48. static::assertSame($old, Common::config(...$setting));
  49. rename(INSTALLDIR . '/social.local.yaml.back', INSTALLDIR . '/social.local.yaml');
  50. }
  51. public function testSiteString()
  52. {
  53. $this->test(['attachments', 'dir'], fn () => Common::config('storage', 'dir') . 'foo' . \DIRECTORY_SEPARATOR);
  54. }
  55. public function testSiteInt()
  56. {
  57. $this->test(['attachments', 'file_quota'], fn () => 8388608); // 1MB in bits
  58. }
  59. public function testSiteArray()
  60. {
  61. $this->test(['plugins', 'core'], fn () => ['some plugin', 'some other']);
  62. }
  63. public function testSiteBoolTrue()
  64. {
  65. $this->test(['attachments', 'uploads'], fn () => true);
  66. }
  67. public function testSiteBoolFalse()
  68. {
  69. $this->test(['attachments', 'uploads'], fn () => false);
  70. }
  71. public function testSiteInvalidSection()
  72. {
  73. $client = static::createClient();
  74. $client->request('GET', '/panel');
  75. $this->assertThrows(
  76. InvalidArgumentException::class,
  77. fn () => $client->submitForm('Set site setting', [
  78. 'save_admin[setting]' => 'invalid:section',
  79. 'save_admin[value]' => 'false',
  80. ]),
  81. );
  82. }
  83. }