141 - Validating Poster Images

This commit is contained in:
Adam Wathan
2017-09-28 16:11:02 -04:00
parent b95e75dd6b
commit e18ba35efd
3 changed files with 52 additions and 2 deletions

View File

@@ -5,6 +5,7 @@ namespace App\Http\Controllers\Backstage;
use App\Concert;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
@@ -36,6 +37,7 @@ class ConcertsController extends Controller
'zip' => ['required'],
'ticket_price' => ['required', 'numeric', 'min:5'],
'ticket_quantity' => ['required', 'numeric', 'min:1'],
'poster_image' => ['nullable', 'image', Rule::dimensions()->minWidth(400)->ratio(8.5/11)],
]);
$concert = Auth::user()->concerts()->create([

View File

@@ -27,7 +27,7 @@ class CreateConcertsTable extends Migration
$table->string('zip');
$table->integer('ticket_price');
$table->integer('ticket_quantity');
$table->string('poster_image_path');
$table->string('poster_image_path')->nullable();
$table->datetime('published_at')->nullable();
$table->timestamps();
});

View File

@@ -377,7 +377,7 @@ class AddConcertTest extends TestCase
Storage::fake('s3');
$user = factory(User::class)->create();
$file = File::image('concert-poster.png');
$file = File::image('concert-poster.png', 850, 1100);
$response = $this->actingAs($user)->post('/backstage/concerts', $this->validParams([
'poster_image' => $file,
@@ -392,4 +392,52 @@ class AddConcertTest extends TestCase
);
});
}
/** @test */
function poster_image_must_be_an_image()
{
Storage::fake('s3');
$user = factory(User::class)->create();
$file = File::create('not-a-poster.pdf');
$response = $this->actingAs($user)->from('/backstage/concerts/new')->post('/backstage/concerts', $this->validParams([
'poster_image' => $file,
]));
$response->assertRedirect('/backstage/concerts/new');
$response->assertSessionHasErrors('poster_image');
$this->assertEquals(0, Concert::count());
}
/** @test */
function poster_image_must_be_at_least_400px_wide()
{
Storage::fake('s3');
$user = factory(User::class)->create();
$file = File::image('poster.png', 399, 516);
$response = $this->actingAs($user)->from('/backstage/concerts/new')->post('/backstage/concerts', $this->validParams([
'poster_image' => $file,
]));
$response->assertRedirect('/backstage/concerts/new');
$response->assertSessionHasErrors('poster_image');
$this->assertEquals(0, Concert::count());
}
/** @test */
function poster_image_must_have_letter_aspect_ratio()
{
Storage::fake('s3');
$user = factory(User::class)->create();
$file = File::image('poster.png', 851, 1100);
$response = $this->actingAs($user)->from('/backstage/concerts/new')->post('/backstage/concerts', $this->validParams([
'poster_image' => $file,
]));
$response->assertRedirect('/backstage/concerts/new');
$response->assertSessionHasErrors('poster_image');
$this->assertEquals(0, Concert::count());
}
}