Skip to content

Commit

Permalink
Merge pull request #86 from scify/project_lp_cta
Browse files Browse the repository at this point in the history
Fixed tests
  • Loading branch information
PavlosIsaris authored Oct 7, 2024
2 parents 11d6061 + dd66b4a commit f392382
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 53 deletions.
40 changes: 26 additions & 14 deletions tests/Feature/Controllers/CrowdSourcingProjectControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,13 +298,18 @@ public function guestCannotStoreProject() {
public function nonAdminUserCannotStoreProject() {
$user = User::factory()->create();
$this->be($user);

$faker = Faker::create();
// we need a name with no special characters
$name = $faker->name;
$slug = str_replace(' ', '-', strtolower($name));
// remove dots from the slug
$slug = str_replace('.', '', $slug);
$response = $this->withoutMiddleware(VerifyCsrfToken::class) // Disable CSRF only
->post(route('projects.store'), [
'name' => 'Test Project ' . rand(1, 100),
'name' => $name,
'description' => 'Test Description',
'status_id' => 1,
'slug' => 'test-project-' . rand(1, 100),
'slug' => $slug,
'language_id' => 1,
]);

Expand All @@ -321,13 +326,15 @@ public function adminCanStoreProjectWithValidData() {
$this->be($user);

$faker = Faker::create();
$name = $faker->name;
$slug = str_replace(' ', '-', strtolower($name));

$response = $this->withoutMiddleware(VerifyCsrfToken::class)
->post(route('projects.store'), [
'name' => 'Valid Project',
'name' => $name,
'description' => 'Valid Description',
'status_id' => 1,
'slug' => 'valid-project',
'slug' => $slug,
'language_id' => 1,
'color_ids' => [1],
'color_names' => [$faker->name],
Expand All @@ -338,10 +345,10 @@ public function adminCanStoreProjectWithValidData() {
$response->assertStatus(302);
$response->assertSessionHas('flash_message_success', 'The project has been successfully created');
$this->assertDatabaseHas('crowd_sourcing_projects', [
'slug' => 'valid-project',
'slug' => $slug,
]);
$this->assertDatabaseHas('crowd_sourcing_project_translations', [
'name' => 'Valid Project',
'name' => $name,
'description' => 'Valid Description',
]);
}
Expand Down Expand Up @@ -416,12 +423,15 @@ public function authenticatedUserCannotUpdateProject() {
$this->be($user);

$project = CrowdSourcingProject::factory()->create();

$faker = Faker::create();
$name = $faker->name;
$slug = str_replace(' ', '-', strtolower($name));
$slug = str_replace('.', '', $slug);
$response = $this->withoutMiddleware(VerifyCsrfToken::class)->put(route('projects.update', ['project' => $project->id]), [
'name' => 'Updated Project',
'name' => $name,
'description' => 'Updated Description',
'status_id' => 1,
'slug' => 'updated-project',
'slug' => $slug,
'language_id' => 1,
]);

Expand All @@ -439,11 +449,13 @@ public function adminCanUpdateProjectWithValidData() {

$project = CrowdSourcingProject::factory()->create();
$faker = Faker::create();
$name = $faker->name;
$slug = str_replace(' ', '-', strtolower($name));
$response = $this->withoutMiddleware(VerifyCsrfToken::class)->put(route('projects.update', ['project' => $project->id]), [
'name' => 'Updated Project',
'name' => $name,
'description' => 'Updated Description',
'status_id' => 1,
'slug' => 'updated-project',
'slug' => $slug,
'language_id' => 1,
'color_ids' => [1],
'color_names' => [$faker->name],
Expand All @@ -455,11 +467,11 @@ public function adminCanUpdateProjectWithValidData() {
$response->assertSessionHas('flash_message_success', 'The project has been successfully updated');
$this->assertDatabaseHas('crowd_sourcing_projects', [
'id' => $project->id,
'slug' => 'updated-project',
'slug' => $slug,
]);
$this->assertDatabaseHas('crowd_sourcing_project_translations', [
'project_id' => $project->id,
'name' => 'Updated Project',
'name' => $name,
'description' => 'Updated Description',
]);
}
Expand Down
5 changes: 0 additions & 5 deletions tests/Feature/Controllers/FileControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,11 @@
namespace Tests\Feature\Controllers;

use App\Http\Middleware\VerifyCsrfToken;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Tests\TestCase;

class FileControllerTest extends TestCase {
use RefreshDatabase;

protected $seed = true;

/** @test */
public function uploadFilesSuccessfullyUploadsFiles() {
Storage::fake('s3');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,9 @@
use App\Repository\Questionnaire\QuestionnaireRepository;
use Exception;
use Illuminate\Database\QueryException;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class QuestionnaireReportControllerTest extends TestCase {
use RefreshDatabase;

protected $seed = true;

/** @test */
public function viewReportsPageReturnsCorrectView() {
$user = User::factory()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,9 @@
use App\Models\Questionnaire\Questionnaire;
use App\Models\Questionnaire\QuestionnaireResponse;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class QuestionnaireResponseControllerTest extends TestCase {
use RefreshDatabase;

protected $seed = true;

/** @test */
public function testStoreInvalidData() {
$user = User::factory()->create();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,9 @@
use App\Models\Questionnaire\Questionnaire;
use App\Models\User;
use App\Models\UserRole;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class QuestionnaireStatisticsControllerTest extends TestCase {
use RefreshDatabase;

protected $seed = true;

/** @test */
public function showStatisticsPageForQuestionnaireReturnsCorrectView() {
$user = User::factory()->create();
Expand Down
16 changes: 0 additions & 16 deletions tests/Feature/Controllers/UserControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Tests\TestCase;

class UserControllerTest extends TestCase {

use RefreshDatabase;

protected $seed = true;
Expand Down Expand Up @@ -103,21 +102,6 @@ public function patchHandlesInvalidDataCorrectly() {
$response->assertSessionHasErrors(['password']);
}

/** @test */
public function cannotDeleteDeactivatesUserForNonAdminUser() {
$user = User::factory()->create();
$this->be($user);

$response = $this->withoutMiddleware(VerifyCsrfToken::class)
->post(route('deleteUser'), ['id' => $user->id]);

$response->assertStatus(403);
$this->assertDatabaseHas('users', [
'id' => $user->id,
'deleted_at' => null,
]);
}

/** @test */
public function deleteDeactivatesUserForAdminUser() {
$user = User::factory()
Expand Down
17 changes: 15 additions & 2 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,25 @@

namespace Tests;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\RefreshDatabaseState;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;

abstract class TestCase extends BaseTestCase {
use CreatesApplication;
use RefreshDatabase;

protected function setUp(): void {
parent::setUp();
protected function refreshTestDatabase() {
if (!RefreshDatabaseState::$migrated) {
// Run the database migrations specific to testing
$this->artisan('migrate:fresh', ['--env' => 'testing', '--database' => 'sqlite_testing']);

// Run the database seeds specific to testing
$this->artisan('db:seed', ['--env' => 'testing', '--database' => 'sqlite_testing', '--class' => 'DatabaseSeeder']);

RefreshDatabaseState::$migrated = true;
}

$this->beginDatabaseTransaction();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Tests\TestCase;

class QuestionnaireAnswerVoteRepositoryTest extends TestCase {

/** @test
* @throws BindingResolutionException
*/
Expand Down

0 comments on commit f392382

Please sign in to comment.