diff --git a/.github/workflows/laravel-tests.yml b/.github/workflows/laravel-tests.yml index 8384c468..63128f28 100644 --- a/.github/workflows/laravel-tests.yml +++ b/.github/workflows/laravel-tests.yml @@ -54,5 +54,11 @@ jobs: - name: Cache run: php artisan config:cache --env=testing + - name: Run the migrations in the testing environment + run: php artisan migrate --env=testing --database=sqlite_testing + + - name: Run the seeders in the testing environment + run: php artisan db:seed --env=testing --database=sqlite_testing + - name: Execute tests (Unit and Feature tests) via PHPUnit - run: php artisan app:test --migrate --seed \ No newline at end of file + run: php artisan test --env=testing \ No newline at end of file diff --git a/README.md b/README.md index f9582c57..a7306a67 100644 --- a/README.md +++ b/README.md @@ -440,59 +440,7 @@ php artisan cache:clear php artisan config:clear ``` -### Method 1: PHPUnit with the Laravel Custom Command - -There is a custom Laravel Command that runs the test suite for the application, which can be run by executing the -following command: - -```bash -php artisan app:test -``` - -This command is located in the `app/Console/Commands/TestCommand.php` file. - -This command takes the following options: - -- `--coverage`: Run the tests with coverage. -- `--filter`: Filter the tests to run only a specific test class or method. -- `--migrate`: Run the migrations before running the tests. -- `--seed`: Seed the database before running the tests. - -Every time you run this command, the test database will be created (or used) at `storage/database_testing.sqlite`, the -suite -will be executed, and the results will be displayed in the console. - -If you want to only test a specific test class or method, you can run the following command: - -```bash -php artisan app:test --filter={METHOD OR CLASS NAME} -``` - -Note that unlike the PHPUnit command, you need to use the `--filter` flag with the `=` sign. - -Example: - -```bash -php artisan app:test --filter=CrowdSourcingProjectControllerTest -``` - -Example of running the tests with migrations and seeding: - -```bash - php artisan app:test --migrate --seed -``` - -### Run the tests with coverage - -In order to run the tests with coverage, you can use the `--coverage` flag, like so: - -```bash -php artisan app:test --coverage -``` - -### Method 2: PHPUnit with the `php artisan test` command - -You can also run the tests using the `php artisan test` command, which is a built-in Laravel command. +Then, make sure that you have set up the test database: First you need to run the migrations for the test database: @@ -500,12 +448,16 @@ First you need to run the migrations for the test database: php artisan migrate --env=testing --database=sqlite_testing ``` -Then run the seeders: +Then run the seeders for the test database: ```bash php artisan db:seed --env=testing --database=sqlite_testing ``` +### PHPUnit with the `php artisan test` command + +You can run the tests using the `php artisan test` command, which is a built-in Laravel command. + And finally, run the tests: ```bash diff --git a/app/Console/Commands/TestCommand.php b/app/Console/Commands/TestCommand.php deleted file mode 100644 index 13a32e13..00000000 --- a/app/Console/Commands/TestCommand.php +++ /dev/null @@ -1,92 +0,0 @@ -option('migrate')) { - $databaseFilePath = storage_path('database_testing.sqlite'); - $this->info('Creating the testing database in ' . $databaseFilePath); - file_put_contents($databaseFilePath, null); - $this->info('Running the migrations for the testing database...'); - Artisan::call('migrate:fresh', [ - '--env' => 'testing', - '--database' => 'sqlite_testing', - ]); - } - - // Run the seeders if the --seed option is set - if ($this->option('seed')) { - $this->info('Seeding the testing database...'); - Artisan::call('db:seed', [ - '--env' => 'testing', - '--database' => 'sqlite_testing', - '--class' => 'DatabaseSeeder', - ]); - } - - // Clear any configuration cache - $this->info('Clearing the configuration cache...'); - Artisan::call('config:clear'); - - // Prepare the test command options - $testOptions = []; - - if ($filter = $this->option('filter')) { - $testOptions['--filter'] = $filter; - } - - if ($this->option('coverage')) { - $testOptions['--coverage'] = true; - } - - // Clear inherited options to avoid conflicts - $this->info('Running tests with the following options:' . PHP_EOL . json_encode($testOptions, JSON_PRETTY_PRINT)); - $this->unsetConflictOptions(); - - Artisan::call('test', $testOptions); - - // Display the output of the test command - $this->info(Artisan::output()); - - return 0; - } - - /** - * Unset conflicting options to prevent inheritance. - */ - protected function unsetConflictOptions() { - // Manually unset conflicting options - foreach (['--seed', '--migrate', '--filter', '--coverage'] as $option) { - $index = array_search($option, $_SERVER['argv']); - if ($index !== false) { - unset($_SERVER['argv'][$index]); - } - } - } -} diff --git a/app/Repository/LanguageRepository.php b/app/Repository/LanguageRepository.php index 2096d834..3b1098f9 100644 --- a/app/Repository/LanguageRepository.php +++ b/app/Repository/LanguageRepository.php @@ -20,6 +20,6 @@ public function getLanguageByCode(string $code): Language { public function getDefaultLanguage(): Language { // english is the default language - return Language::find(6); + return Language::findOrFail(6); } } diff --git a/database/migrations/2024_12_11_145726_nullable_fields_in_crowd_sourcing_project_translations_table.php b/database/migrations/2024_12_11_145726_nullable_fields_in_crowd_sourcing_project_translations_table.php new file mode 100644 index 00000000..27b7abca --- /dev/null +++ b/database/migrations/2024_12_11_145726_nullable_fields_in_crowd_sourcing_project_translations_table.php @@ -0,0 +1,27 @@ +mediumText('footer')->nullable()->change(); + $table->text('sm_description')->nullable()->change(); + $table->text('sm_keywords')->nullable()->change(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void { + Schema::table('crowd_sourcing_project_translations', function (Blueprint $table) { + // + }); + } +}; diff --git a/tests/Feature/Controllers/CrowdSourcingProject/CrowdSourcingProjectControllerTest.php b/tests/Feature/Controllers/CrowdSourcingProject/CrowdSourcingProjectControllerTest.php index d58efeb5..af2b51fe 100644 --- a/tests/Feature/Controllers/CrowdSourcingProject/CrowdSourcingProjectControllerTest.php +++ b/tests/Feature/Controllers/CrowdSourcingProject/CrowdSourcingProjectControllerTest.php @@ -165,7 +165,7 @@ public function guestCannotAccessCreatePage() { // 302 is the status code for a redirect (to the login page) $response->assertStatus(302); - $response->assertRedirect(route('login', ['locale' => 'en'])); + $response->assertRedirectContains(route('login', ['locale' => 'en'])); } /** @@ -202,7 +202,7 @@ public function guestCannotAccessIndexPage() { $response = $this->get(route('projects.index', ['locale' => 'en'])); $response->assertStatus(302); - $response->assertRedirect(route('login', ['locale' => 'en'])); + $response->assertRedirectContains(route('login', ['locale' => 'en'])); } /** @@ -288,7 +288,7 @@ public function guestCannotStoreProject() { ]); $response->assertStatus(302); - $response->assertRedirect(route('login', ['locale' => 'en'])); + $response->assertRedirectContains(route('login', ['locale' => 'en'])); } /** @@ -410,7 +410,7 @@ public function guestCannotUpdateProject() { ]); $response->assertStatus(302); - $response->assertRedirect(route('login', ['locale' => 'en'])); + $response->assertRedirectContains(route('login', ['locale' => 'en'])); } /** diff --git a/tests/Feature/Controllers/Problem/ProblemControllerTest.php b/tests/Feature/Controllers/Problem/ProblemControllerTest.php index 7c0b6a1f..55b26f6f 100644 --- a/tests/Feature/Controllers/Problem/ProblemControllerTest.php +++ b/tests/Feature/Controllers/Problem/ProblemControllerTest.php @@ -18,7 +18,7 @@ public function guestCannotAccessProblemCreatePage() { // 302 is the status code for a redirect (to the login page) $response->assertStatus(302); - $response->assertRedirect(route('login', ['locale' => 'en'])); + $response->assertRedirectContains(route('login', ['locale' => 'en'])); } /** diff --git a/tests/Feature/Controllers/Questionnaire/QuestionnaireControllerTest.php b/tests/Feature/Controllers/Questionnaire/QuestionnaireControllerTest.php index 0293f8f8..214bd6af 100644 --- a/tests/Feature/Controllers/Questionnaire/QuestionnaireControllerTest.php +++ b/tests/Feature/Controllers/Questionnaire/QuestionnaireControllerTest.php @@ -28,7 +28,7 @@ public function guestCannotSaveQuestionnaireStatus() { ]); $response->assertStatus(302); - $response->assertRedirect(route('login', ['locale' => 'en'])); + $response->assertRedirectContains(route('login', ['locale' => 'en'])); } /** @@ -210,7 +210,7 @@ public function guestCannotUpdateQuestionnaire() { ]); $response->assertStatus(302); - $response->assertRedirect(route('login', ['locale' => 'en'])); + $response->assertRedirectContains(route('login', ['locale' => 'en'])); } /** @@ -302,7 +302,7 @@ public function guestCannotTranslateQuestionnaire() { ]); $response->assertStatus(302); - $response->assertRedirect(route('login', ['locale' => 'en'])); + $response->assertRedirectContains(route('login', ['locale' => 'en'])); } /** diff --git a/tests/Feature/Controllers/Solution/SolutionControllerTest.php b/tests/Feature/Controllers/Solution/SolutionControllerTest.php new file mode 100644 index 00000000..9c95f548 --- /dev/null +++ b/tests/Feature/Controllers/Solution/SolutionControllerTest.php @@ -0,0 +1,26 @@ +get('/en/project-slug/problems/problem-slug/solutions/propose'); + + $response->assertRedirectContains(route('login', ['locale' => 'en'])); + } +} diff --git a/tests/Feature/Controllers/UserControllerTest.php b/tests/Feature/Controllers/UserControllerTest.php index 27080413..c7763c31 100644 --- a/tests/Feature/Controllers/UserControllerTest.php +++ b/tests/Feature/Controllers/UserControllerTest.php @@ -42,7 +42,7 @@ public function myDashboardRedirectsToLoginForUnauthenticatedUser() { $response = $this->get(route('my-dashboard', ['locale' => 'en'])); $response->assertStatus(302); - $response->assertRedirect(route('login', ['locale' => 'en'])); + $response->assertRedirectContains(route('login', ['locale' => 'en'])); } /** @test */ @@ -61,7 +61,7 @@ public function myAccountRedirectsToLoginForUnauthenticatedUser() { $response = $this->get(route('my-account', ['locale' => 'en'])); $response->assertStatus(302); - $response->assertRedirect(route('login', ['locale' => 'en'])); + $response->assertRedirectContains(route('login', ['locale' => 'en'])); } /** @test */ @@ -224,7 +224,7 @@ public function patchRedirectsToLoginForUnauthenticatedUser() { ]); $response->assertStatus(302); - $response->assertRedirect(route('login', ['locale' => 'en'])); + $response->assertRedirectContains(route('login', ['locale' => 'en'])); } /** @test */ @@ -267,7 +267,7 @@ public function deleteRedirectsToLoginForUnauthenticatedUser() { ->post(route('deleteUser'), ['id' => $user->id]); $response->assertStatus(302); - $response->assertRedirect(route('login', ['locale' => 'en'])); + $response->assertRedirectContains(route('login', ['locale' => 'en'])); } /** @test */ @@ -302,7 +302,7 @@ public function showUsersByCriteriaRedirectsToLoginForUnauthenticatedUser() { $response = $this->get(route('api.users.get-filtered', ['name' => 'John'])); $response->assertStatus(302); - $response->assertRedirect(route('login', ['locale' => 'en'])); + $response->assertRedirectContains(route('login', ['locale' => 'en'])); } /** @test */ diff --git a/tests/StorageInitializer.php b/tests/StorageInitializer.php deleted file mode 100644 index 237b3da7..00000000 --- a/tests/StorageInitializer.php +++ /dev/null @@ -1,67 +0,0 @@ -