diff --git a/tests/integration/TestCase.php b/tests/integration/TestCase.php index 9fa641df0..e705a8967 100644 --- a/tests/integration/TestCase.php +++ b/tests/integration/TestCase.php @@ -82,12 +82,26 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase // First, truncate all referenced tables so that they are empty. foreach (array_keys($tableData) as $table) { - $this->database()->table($table)->truncate(); + if ($table !== 'settings') { + $this->database()->table($table)->truncate(); + } } // Then, insert all rows required for this test case. foreach ($tableData as $table => $rows) { - $this->database()->table($table)->insert($rows); + foreach ($rows as $row) { + if ($table === 'settings') { + $this->database()->table($table)->updateOrInsert( + ['key' => $row['key']], + $row + ); + } else { + $this->database()->table($table)->updateOrInsert( + isset($row['id']) ? ['id' => $row['id']] : $row, + $row + ); + } + } } // And finally, turn on foreign key checks again. diff --git a/tests/integration/api/Controller/CreateUserControllerTest.php b/tests/integration/api/Controller/CreateUserControllerTest.php index ee9979aa8..c5f8e80b0 100644 --- a/tests/integration/api/Controller/CreateUserControllerTest.php +++ b/tests/integration/api/Controller/CreateUserControllerTest.php @@ -40,6 +40,9 @@ class CreateUserControllerTest extends ApiControllerTestCase 'group_user' => [ ['user_id' => 1, 'group_id' => 1], ], + 'settings' => [ + ['key' => 'mail_driver', 'value' => 'log'] + ] ]); } diff --git a/tests/integration/api/csrf_protection/RequireCsrfTokenTest.php b/tests/integration/api/csrf_protection/RequireCsrfTokenTest.php index 83980c6e1..d52ec519c 100644 --- a/tests/integration/api/csrf_protection/RequireCsrfTokenTest.php +++ b/tests/integration/api/csrf_protection/RequireCsrfTokenTest.php @@ -11,7 +11,6 @@ namespace Flarum\Tests\integration\api\csrf_protection; -use Flarum\Foundation\Application; use Flarum\Tests\integration\RetrievesAuthorizedUsers; use Flarum\Tests\integration\TestCase; @@ -40,8 +39,7 @@ class RequireCsrfTokenTest extends TestCase ['user_id' => 1, 'key' => 'superadmin'], ], 'settings' => [ - ['key' => 'mail_driver', 'value' => 'mail'], - ['key' => 'version', 'value' => Application::VERSION], + ['key' => 'csrf_test', 'value' => 1], ], ]); } @@ -65,7 +63,7 @@ class RequireCsrfTokenTest extends TestCase 'POST', '/api/settings', [ 'cookiesFrom' => $auth, - 'json' => ['mail_driver' => 'log'], + 'json' => ['csrf_test' => 2], ] ) ); @@ -111,7 +109,7 @@ class RequireCsrfTokenTest extends TestCase 'POST', '/api/settings', [ 'cookiesFrom' => $auth, - 'json' => ['mail_driver' => 'log'], + 'json' => ['csrf_test' => 2], ] )->withHeader('X-CSRF-Token', $token) ); @@ -121,8 +119,8 @@ class RequireCsrfTokenTest extends TestCase // Was the setting actually changed in the database? $this->assertEquals( - 'log', - $this->database()->table('settings')->where('key', 'mail_driver')->first()->value + 2, + $this->database()->table('settings')->where('key', 'csrf_test')->first()->value ); } @@ -154,7 +152,7 @@ class RequireCsrfTokenTest extends TestCase 'POST', '/api/settings', [ 'cookiesFrom' => $auth, - 'json' => ['mail_driver' => 'log', 'csrfToken' => $token], + 'json' => ['csrf_test' => 2, 'csrfToken' => $token], ] ) ); @@ -164,8 +162,8 @@ class RequireCsrfTokenTest extends TestCase // Was the setting actually changed in the database? $this->assertEquals( - 'log', - $this->database()->table('settings')->where('key', 'mail_driver')->first()->value + 2, + $this->database()->table('settings')->where('key', 'csrf_test')->first()->value ); } @@ -178,7 +176,7 @@ class RequireCsrfTokenTest extends TestCase $this->request( 'POST', '/api/settings', [ - 'json' => ['mail_driver' => 'log'], + 'json' => ['csrf_test' => 2], ] )->withHeader('Authorization', 'Token superadmin') ); @@ -188,8 +186,8 @@ class RequireCsrfTokenTest extends TestCase // Was the setting actually changed in the database? $this->assertEquals( - 'log', - $this->database()->table('settings')->where('key', 'mail_driver')->first()->value + 2, + $this->database()->table('settings')->where('key', 'csrf_test')->first()->value ); } }