Browse Source

Add form validation to user and agency profile update page

pull/1/head
Nafies Luthfi 8 years ago
parent
commit
8823836f29
  1. 11
      app/Http/Controllers/Users/AgencyController.php
  2. 7
      app/Http/Controllers/Users/ProfileController.php
  3. BIN
      public/assets/imgs/default-logo.png
  4. 85
      tests/Feature/AgencyProfileTest.php
  5. 76
      tests/Feature/Users/UserProfileTest.php

11
app/Http/Controllers/Users/AgencyController.php

@ -6,6 +6,8 @@ use App\Http\Controllers\Controller;
use Option;
/**
* Agency Profile Controller
*
* @author Nafies Luthfi <nafiesL@gmail.com>
*/
class AgencyController extends Controller
@ -22,6 +24,15 @@ class AgencyController extends Controller
public function update()
{
request()->validate([
'name' => 'required|string|max:100',
'tagline' => 'required|string|max:255',
'email' => 'required|email|max:255',
'website' => 'required|url|max:255',
'address' => 'required|string|max:255',
'phone' => 'required|string|max:255',
]);
Option::set('agency_name', request('name'));
Option::set('agency_tagline', request('tagline'));
Option::set('agency_email', request('email'));

7
app/Http/Controllers/Users/ProfileController.php

@ -5,6 +5,8 @@ namespace App\Http\Controllers\Users;
use App\Http\Controllers\Controller;
/**
* User Profile Controller
*
* @author Nafies Luthfi <nafiesL@gmail.com>
*/
class ProfileController extends Controller
@ -22,6 +24,11 @@ class ProfileController extends Controller
public function update()
{
request()->validate([
'name' => 'required|string|max:100',
'email' => 'required|email|max:255',
]);
$user = auth()->user();
$user->name = request('name');

BIN
public/assets/imgs/default-logo.png

Before

Width: 400  |  Height: 400  |  Size: 58 KiB

After

Width: 200  |  Height: 200  |  Size: 22 KiB

85
tests/Feature/AgencyProfileTest.php

@ -0,0 +1,85 @@
<?php
namespace Tests\Feature\Users;
use Tests\TestCase;
/**
* Agency Profile Controller
*
* @author Nafies Luthfi <nafiesL@gmail.com>
*/
class AgencyProfileTest extends TestCase
{
/** @test */
public function admin_user_can_visit_agency_page()
{
$user = $this->adminUserSigningIn();
$this->visit(route('users.agency.show'));
$this->seePageIs(route('users.agency.show'));
}
/** @test */
public function admin_user_can_update_agency_data()
{
$user = $this->adminUserSigningIn();
$this->visit(route('users.agency.edit'));
$this->submitForm(trans('agency.update'), [
'name' => 'Nama Agensi Saya',
'tagline' => 'Tagline agensi saya',
'email' => 'nama_agensi@domain.com',
'address' => 'Jln. Kalimantan, No. 20, Kota',
'phone' => '081234567890',
'website' => 'https://example.com',
]);
$this->see(trans('agency.updated'));
$this->seePageIs(route('users.agency.show'));
$this->seeInDatabase('site_options', [
'key' => 'agency_name',
'value' => 'Nama Agensi Saya',
]);
$this->seeInDatabase('site_options', [
'key' => 'agency_email',
'value' => 'nama_agensi@domain.com',
]);
$this->seeInDatabase('site_options', [
'key' => 'agency_address',
'value' => 'Jln. Kalimantan, No. 20, Kota',
]);
$this->seeInDatabase('site_options', [
'key' => 'agency_phone',
'value' => '081234567890',
]);
$this->seeInDatabase('site_options', [
'key' => 'agency_website',
'value' => 'https://example.com',
]);
$this->seeInDatabase('site_options', [
'key' => 'agency_tagline',
'value' => 'Tagline agensi saya',
]);
}
/** @test */
public function admin_user_can_update_agency_logo_image()
{
$user = $this->adminUserSigningIn();
$this->visit(route('users.agency.edit'));
$this->attach(storage_path('app/sample-image.jpg'), 'logo');
$this->press(trans('agency.logo_upload'));
$this->see(trans('agency.updated'));
$this->seePageIs(route('users.agency.show'));
$this->seeInDatabase('site_options', [
'key' => 'agency_logo_path',
'value' => 'sample-image.jpg',
]);
$this->assertFileExistsThenDelete(public_path('assets/imgs/sample-image.jpg'));
}
}

76
tests/Feature/Users/UserProfileTest.php

@ -4,6 +4,11 @@ namespace Tests\Feature\Users;
use Tests\TestCase;
/**
* User Profile Feature Test
*
* @author Nafies Luthfi <nafiesL@gmail.com>
*/
class UserProfileTest extends TestCase
{
/** @test */
@ -42,75 +47,4 @@ class UserProfileTest extends TestCase
'email' => 'me@domain.com',
]);
}
/** @test */
public function a_user_can_visit_their_agency_page()
{
$user = $this->adminUserSigningIn();
$this->visit(route('users.agency.show'));
$this->seePageIs(route('users.agency.show'));
}
/** @test */
public function a_user_can_update_their_agency_data()
{
$user = $this->adminUserSigningIn();
$this->visit(route('users.agency.edit'));
$this->submitForm(trans('agency.update'), [
'name' => 'Nama Agensi Saya',
'tagline' => 'Tagline agensi saya',
'email' => 'nama_agensi@domain.com',
'address' => 'Jln. Kalimantan, No. 20, Kota',
'phone' => '081234567890',
'website' => 'https://example.com',
]);
$this->see(trans('agency.updated'));
$this->seePageIs(route('users.agency.show'));
$this->seeInDatabase('site_options', [
'key' => 'agency_name',
'value' => 'Nama Agensi Saya',
]);
$this->seeInDatabase('site_options', [
'key' => 'agency_email',
'value' => 'nama_agensi@domain.com',
]);
$this->seeInDatabase('site_options', [
'key' => 'agency_address',
'value' => 'Jln. Kalimantan, No. 20, Kota',
]);
$this->seeInDatabase('site_options', [
'key' => 'agency_phone',
'value' => '081234567890',
]);
$this->seeInDatabase('site_options', [
'key' => 'agency_website',
'value' => 'https://example.com',
]);
$this->seeInDatabase('site_options', [
'key' => 'agency_tagline',
'value' => 'Tagline agensi saya',
]);
}
/** @test */
public function admin_user_can_update_agency_logo_image()
{
$user = $this->adminUserSigningIn();
$this->visit(route('users.agency.edit'));
$this->attach(storage_path('app/sample-image.jpg'), 'logo');
$this->press(trans('agency.logo_upload'));
$this->see(trans('agency.updated'));
$this->seePageIs(route('users.agency.show'));
$this->seeInDatabase('site_options', [
'key' => 'agency_logo_path',
'value' => 'sample-image.jpg',
]);
$this->assertFileExistsThenDelete(public_path('assets/imgs/sample-image.jpg'));
}
}
Loading…
Cancel
Save