2021-11-29 12:39:04 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Tests\Feature;
|
|
|
|
|
|
2021-11-29 12:52:36 +02:00
|
|
|
use App\Models\User;
|
2021-11-29 12:39:04 +02:00
|
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
|
|
class ValidationTest extends TestCase
|
|
|
|
|
{
|
|
|
|
|
use RefreshDatabase;
|
|
|
|
|
|
|
|
|
|
public function test_simple_validation_rules()
|
|
|
|
|
{
|
|
|
|
|
// Post without any title should fail because title is required
|
|
|
|
|
$response = $this->post('posts');
|
|
|
|
|
$response->assertSessionHasErrors('title')->assertStatus(302);
|
|
|
|
|
|
|
|
|
|
// Post with title should succeed
|
|
|
|
|
$response = $this->post('posts', ['title' => 'Some title']);
|
|
|
|
|
$response->assertStatus(200);
|
|
|
|
|
|
|
|
|
|
// Post with the same title should fail because it's not unique
|
|
|
|
|
$response = $this->post('posts', ['title' => 'Some title']);
|
|
|
|
|
$response->assertSessionHasErrors('title')->assertStatus(302);
|
|
|
|
|
}
|
2021-11-29 12:52:36 +02:00
|
|
|
|
|
|
|
|
public function test_array_validation()
|
|
|
|
|
{
|
|
|
|
|
$user = User::factory()->create();
|
|
|
|
|
|
|
|
|
|
// Post without name and email should fail
|
|
|
|
|
$response = $this->actingAs($user)->post('profile');
|
|
|
|
|
$response->assertStatus(302);
|
|
|
|
|
|
|
|
|
|
// Post with name and email should succeed
|
|
|
|
|
$response = $this->actingAs($user)->post('profile', [
|
|
|
|
|
'profile' => [
|
|
|
|
|
'name' => 'Some name',
|
|
|
|
|
'email' => 'some@email.com'
|
|
|
|
|
]
|
|
|
|
|
]);
|
|
|
|
|
$response->assertStatus(200);
|
|
|
|
|
}
|
2021-11-29 13:17:49 +02:00
|
|
|
|
|
|
|
|
public function test_validation_errors_shown_in_blade()
|
|
|
|
|
{
|
|
|
|
|
// Post without name and description should fail
|
|
|
|
|
$response = $this->followingRedirects()->post('projects');
|
|
|
|
|
$response->assertStatus(200);
|
|
|
|
|
$response->assertSee('The name field is required.');
|
|
|
|
|
$response->assertSee('The description field is required.');
|
|
|
|
|
}
|
2021-11-29 13:34:42 +02:00
|
|
|
|
|
|
|
|
public function test_validation_specific_error_shown_in_blade()
|
|
|
|
|
{
|
|
|
|
|
// Post without name should fail
|
|
|
|
|
$response = $this->followingRedirects()->post('products');
|
|
|
|
|
$response->assertStatus(200);
|
|
|
|
|
$response->assertSee('The name field is required.');
|
|
|
|
|
}
|
2021-11-29 13:41:46 +02:00
|
|
|
|
|
|
|
|
public function test_old_value_stays_in_form_after_validation_error()
|
|
|
|
|
{
|
|
|
|
|
// Post without name should fail
|
|
|
|
|
$response = $this->followingRedirects()->post('teams', ['name' => 'Abc']);
|
|
|
|
|
$response->assertStatus(200);
|
|
|
|
|
$response->assertSee('Abc');
|
|
|
|
|
}
|
2021-11-29 15:21:45 +02:00
|
|
|
|
|
|
|
|
public function test_form_request_validation()
|
|
|
|
|
{
|
|
|
|
|
// Post with no name/description should fail
|
|
|
|
|
$response = $this->post('items');
|
|
|
|
|
$response->assertStatus(302);
|
|
|
|
|
|
|
|
|
|
// Post with all the fields should succeed
|
|
|
|
|
$response = $this->post('items', [
|
|
|
|
|
'name' => 'Abc',
|
|
|
|
|
'description' => 'Xyz',
|
|
|
|
|
]);
|
|
|
|
|
$response->assertStatus(200);
|
|
|
|
|
}
|
2021-11-29 15:31:28 +02:00
|
|
|
|
|
|
|
|
public function test_update_forbidden_field()
|
|
|
|
|
{
|
|
|
|
|
$user = User::factory()->create();
|
|
|
|
|
|
|
|
|
|
// field is_admin should not be possible to update
|
|
|
|
|
$updatedUser = [
|
|
|
|
|
'name' => 'Updated name',
|
|
|
|
|
'email' => 'updated@email.com',
|
|
|
|
|
'is_admin' => 1
|
|
|
|
|
];
|
|
|
|
|
$response = $this->put('users/' . $user->id, $updatedUser);
|
|
|
|
|
$response->assertStatus(200);
|
|
|
|
|
|
|
|
|
|
$user = User::where('name', $updatedUser['name'])->first();
|
|
|
|
|
$this->assertNotNull($user);
|
|
|
|
|
$this->assertEquals(false, $user->is_admin);
|
|
|
|
|
}
|
2021-11-29 12:39:04 +02:00
|
|
|
}
|