From 161623c85ebb55cfaf8f122a0e33498c4c39ca48 Mon Sep 17 00:00:00 2001 From: Nabeel Shahzad Date: Fri, 22 Dec 2017 12:46:46 -0600 Subject: [PATCH] Fixes for emails; send user registered/pending depending on state --- app/Database/factories/AirlineFactory.php | 17 ++++++++ app/Database/factories/UserFactory.php | 4 +- .../Controllers/Auth/RegisterController.php | 27 ++++++++---- app/Listeners/EmailEventListener.php | 25 +++++++---- app/Mail/UserPending.php | 32 +++++++++++++++ app/Mail/UserRegistered.php | 34 +++++++++++++++ app/Models/Enums/PilotState.php | 25 +++++++++++ app/Models/User.php | 3 +- app/Services/UserService.php | 23 ++++------- config/enums.php | 8 ---- config/mail.php | 2 +- resources/views/emails/user/pending.blade.php | 8 ++++ .../views/emails/user/registered.blade.php | 12 ++++++ .../mail/html/button.blade.php | 0 .../mail/html/footer.blade.php | 0 .../mail/html/header.blade.php | 0 .../mail/html/layout.blade.php | 0 .../mail/html/message.blade.php | 0 .../mail/html/panel.blade.php | 0 .../mail/html/promotion.blade.php | 0 .../mail/html/promotion/button.blade.php | 0 .../mail/html/subcopy.blade.php | 0 .../mail/html/table.blade.php | 0 .../mail/html/themes/default.css | 0 .../mail/markdown/button.blade.php | 0 .../mail/markdown/footer.blade.php | 0 .../mail/markdown/header.blade.php | 0 .../mail/markdown/layout.blade.php | 0 .../mail/markdown/message.blade.php | 0 .../mail/markdown/panel.blade.php | 0 .../mail/markdown/promotion.blade.php | 0 .../mail/markdown/promotion/button.blade.php | 0 .../mail/markdown/subcopy.blade.php | 0 .../mail/markdown/table.blade.php | 0 tests/Browser/Pages/HomePage.php | 41 ------------------- tests/Browser/Pages/Page.php | 20 --------- tests/Browser/RegistrationTest.php | 21 ---------- tests/Browser/console/.gitignore | 2 - tests/Browser/screenshots/.gitignore | 2 - tests/CreatesApplication.php | 22 ---------- tests/RegistrationTest.php | 38 +++++++++++++++++ tests/TestCase.php | 2 - 42 files changed, 215 insertions(+), 153 deletions(-) create mode 100644 app/Database/factories/AirlineFactory.php create mode 100644 app/Mail/UserPending.php create mode 100644 app/Mail/UserRegistered.php create mode 100644 app/Models/Enums/PilotState.php create mode 100644 resources/views/emails/user/pending.blade.php create mode 100644 resources/views/emails/user/registered.blade.php rename resources/views/{layouts/default => vendor}/mail/html/button.blade.php (100%) rename resources/views/{layouts/default => vendor}/mail/html/footer.blade.php (100%) rename resources/views/{layouts/default => vendor}/mail/html/header.blade.php (100%) rename resources/views/{layouts/default => vendor}/mail/html/layout.blade.php (100%) rename resources/views/{layouts/default => vendor}/mail/html/message.blade.php (100%) rename resources/views/{layouts/default => vendor}/mail/html/panel.blade.php (100%) rename resources/views/{layouts/default => vendor}/mail/html/promotion.blade.php (100%) rename resources/views/{layouts/default => vendor}/mail/html/promotion/button.blade.php (100%) rename resources/views/{layouts/default => vendor}/mail/html/subcopy.blade.php (100%) rename resources/views/{layouts/default => vendor}/mail/html/table.blade.php (100%) rename resources/views/{layouts/default => vendor}/mail/html/themes/default.css (100%) rename resources/views/{layouts/default => vendor}/mail/markdown/button.blade.php (100%) rename resources/views/{layouts/default => vendor}/mail/markdown/footer.blade.php (100%) rename resources/views/{layouts/default => vendor}/mail/markdown/header.blade.php (100%) rename resources/views/{layouts/default => vendor}/mail/markdown/layout.blade.php (100%) rename resources/views/{layouts/default => vendor}/mail/markdown/message.blade.php (100%) rename resources/views/{layouts/default => vendor}/mail/markdown/panel.blade.php (100%) rename resources/views/{layouts/default => vendor}/mail/markdown/promotion.blade.php (100%) rename resources/views/{layouts/default => vendor}/mail/markdown/promotion/button.blade.php (100%) rename resources/views/{layouts/default => vendor}/mail/markdown/subcopy.blade.php (100%) rename resources/views/{layouts/default => vendor}/mail/markdown/table.blade.php (100%) delete mode 100644 tests/Browser/Pages/HomePage.php delete mode 100644 tests/Browser/Pages/Page.php delete mode 100644 tests/Browser/RegistrationTest.php delete mode 100644 tests/Browser/console/.gitignore delete mode 100644 tests/Browser/screenshots/.gitignore delete mode 100644 tests/CreatesApplication.php create mode 100644 tests/RegistrationTest.php diff --git a/app/Database/factories/AirlineFactory.php b/app/Database/factories/AirlineFactory.php new file mode 100644 index 00000000..4dbac59e --- /dev/null +++ b/app/Database/factories/AirlineFactory.php @@ -0,0 +1,17 @@ +define(App\Models\Airline::class, function (Faker $faker) { + return [ + 'id' => $faker->unique()->numberBetween(10, 10000), + 'icao' => function(array $apt) { return substr($apt['id'],0, 4); }, + 'iata' => function (array $apt) { return $apt['id']; }, + 'name' => $faker->sentence(3), + 'country' => $faker->country, + 'active' => 1 + ]; +}); diff --git a/app/Database/factories/UserFactory.php b/app/Database/factories/UserFactory.php index ab310f17..fb704e7e 100644 --- a/app/Database/factories/UserFactory.php +++ b/app/Database/factories/UserFactory.php @@ -12,7 +12,9 @@ $factory->define(App\Models\User::class, function (Faker $faker) 'email' => $faker->safeEmail, 'password' => $password ?: $password = Hash::make('secret'), 'api_key' => $faker->sha1, - 'airline_id' => 1, + 'airline_id' => function () { + return factory(App\Models\Airline::class)->create()->id; + }, 'rank_id' => 1, 'flights' => $faker->numberBetween(0, 1000), 'flight_time' => $faker->numberBetween(0, 10000), diff --git a/app/Http/Controllers/Auth/RegisterController.php b/app/Http/Controllers/Auth/RegisterController.php index c5fd37ec..1b32b873 100755 --- a/app/Http/Controllers/Auth/RegisterController.php +++ b/app/Http/Controllers/Auth/RegisterController.php @@ -2,6 +2,9 @@ namespace App\Http\Controllers\Auth; +use App\Facades\Utils; +use App\Models\User; +use Illuminate\Support\Facades\Hash; use Validator; use App\Models\Airport; use App\Models\Airline; @@ -61,12 +64,12 @@ class RegisterController extends Controller /** * Get a validator for an incoming registration request. * - * @param array $data + * @param array $data * @return \Illuminate\Contracts\Validation\Validator + * @throws \RuntimeException */ protected function create(array $data) { - # First, validate the posted data $this->validate(request(), [ 'name' => 'required', 'email' => 'required|email', @@ -75,13 +78,19 @@ class RegisterController extends Controller 'password' => 'required|confirmed' ]); - # Let's tell the service to create the pilot - if($p = $this->userService->createPilot($data)) - { - //return $this->view('auth.registered'); - return $p; - } + $opts = [ + 'name' => $data['name'], + 'email' => $data['email'], + 'api_key' => Utils::generateApiKey(), + 'airline_id' => $data['airline'], + 'home_airport_id' => $data['home_airport'], + 'curr_airport_id' => $data['home_airport'], + 'password' => Hash::make($data['password']) + ]; - # I'm not sure if we really need to add the error something if createPilot fails? + $user = User::create($opts); + $user = $this->userService->createPilot($user); + + return $user; } } diff --git a/app/Listeners/EmailEventListener.php b/app/Listeners/EmailEventListener.php index 47116a46..c61f2471 100644 --- a/app/Listeners/EmailEventListener.php +++ b/app/Listeners/EmailEventListener.php @@ -1,16 +1,12 @@ user->toArray()); + Log::info('onUserRegister: ' + . $event->user->pilot_id . ' is ' + . PilotState::label($event->user->state) + . ', sending active email'); + + if($event->user->state === PilotState::ACTIVE) { + Mail::to($event->user->email)->send(new \App\Mail\UserRegistered($event->user)); + } else if($event->user->state === PilotState::PENDING) { + Mail::to($event->user->email)->send(new \App\Mail\UserPending($event->user)); + } } } diff --git a/app/Mail/UserPending.php b/app/Mail/UserPending.php new file mode 100644 index 00000000..fc6adf8a --- /dev/null +++ b/app/Mail/UserPending.php @@ -0,0 +1,32 @@ +user = $user; + } + + /** + * Build the message. + * + * @return $this + */ + public function build() + { + return $this->markdown('emails.user.pending') + ->with(['user' => $this->user]); + } +} diff --git a/app/Mail/UserRegistered.php b/app/Mail/UserRegistered.php new file mode 100644 index 00000000..9f0b2474 --- /dev/null +++ b/app/Mail/UserRegistered.php @@ -0,0 +1,34 @@ +user = $user; + } + + /** + * Build the message. + * + * @return $this + */ + public function build() + { + return $this->markdown('emails.user.registered') + ->with([ + 'user' => $this->user, + ]); + } +} diff --git a/app/Models/Enums/PilotState.php b/app/Models/Enums/PilotState.php new file mode 100644 index 00000000..1663e925 --- /dev/null +++ b/app/Models/Enums/PilotState.php @@ -0,0 +1,25 @@ + 'Pending', + PilotState::ACTIVE => 'Active', + PilotState::ON_LEAVE => 'On Leave', + PilotState::SUSPENDED => 'Suspended', + ]; +} diff --git a/app/Models/User.php b/app/Models/User.php index 50212724..c0ccc21a 100755 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -39,7 +39,8 @@ class User extends Authenticatable 'curr_airport_id', 'rank_id', 'timezone', - 'active', + 'state', + 'status', ]; /** diff --git a/app/Services/UserService.php b/app/Services/UserService.php index 36a165d9..5c758b25 100644 --- a/app/Services/UserService.php +++ b/app/Services/UserService.php @@ -4,6 +4,7 @@ namespace App\Services; use App\Events\UserRegistered; use App\Facades\Utils; +use App\Models\Enums\PilotState; use App\Models\User; use App\Models\Rank; use App\Models\Role; @@ -18,29 +19,19 @@ class UserService extends BaseService /** * Register a pilot - * @param array $data + * @param User $user * @return mixed */ - public function createPilot(array $data) + public function createPilot(User $user) { - $opts = [ - 'name' => $data['name'], - 'email' => $data['email'], - 'api_key' => Utils::generateApiKey(), - 'airline_id' => $data['airline'], - 'home_airport_id' => $data['home_airport'], - 'curr_airport_id' => $data['home_airport'], - 'password' => Hash::make($data['password']) - ]; - # Determine if we want to auto accept if(setting('pilot.auto_accept') === true) { - $opts['status'] = config('enums.states.ACTIVE'); + $user->state = PilotState::ACTIVE; } else { - $opts['status'] = config('enums.states.PENDING'); + $user->state = PilotState::PENDING; } - $user = User::create($opts); + $user->save(); # Attach the user roles $role = Role::where('name', 'user')->first(); @@ -49,6 +40,8 @@ class UserService extends BaseService # Let's check their rank $this->calculatePilotRank($user); + $user->refresh(); + event(new UserRegistered($user)); return $user; diff --git a/config/enums.php b/config/enums.php index b76c734f..e1cf8f7d 100644 --- a/config/enums.php +++ b/config/enums.php @@ -9,14 +9,6 @@ return [ 'ENABLED' => 1, ], - # Pilot states - 'states' => [ - 'PENDING' => 0, - 'ACTIVE' => 1, - 'ON_LEAVE' => 2, - 'SUSPENDED' => 3, - ], - 'fuel_types' => [ '100LL' => 0, 'JETA' => 1, diff --git a/config/mail.php b/config/mail.php index 530fa5e5..faf8715f 100755 --- a/config/mail.php +++ b/config/mail.php @@ -57,7 +57,7 @@ return [ 'from' => [ 'name' => env('MAIL_FROM_NAME', 'phpVMS Admin'), - 'address' => env('MAIL_FROM_ADDRESS', ''), + 'address' => env('MAIL_FROM_ADDRESS', 'no-reply@phpvms.net'), ], /* diff --git a/resources/views/emails/user/pending.blade.php b/resources/views/emails/user/pending.blade.php new file mode 100644 index 00000000..4c46d36b --- /dev/null +++ b/resources/views/emails/user/pending.blade.php @@ -0,0 +1,8 @@ +@component('mail::message') +# Thanks for signing up, {!! $user->name !!}! + +You will be notified as soon as your account is approved! + +Thanks,
+{{ config('app.name') }} +@endcomponent diff --git a/resources/views/emails/user/registered.blade.php b/resources/views/emails/user/registered.blade.php new file mode 100644 index 00000000..2e00c8db --- /dev/null +++ b/resources/views/emails/user/registered.blade.php @@ -0,0 +1,12 @@ +@component('mail::message') +# Thanks for signing up, {!! $user->name !!}! + +Your account is ready to use. + +@component('mail::button', ['url' => url('/login')]) +Visit your account now! +@endcomponent + +Thanks,
+{{ config('app.name') }} +@endcomponent diff --git a/resources/views/layouts/default/mail/html/button.blade.php b/resources/views/vendor/mail/html/button.blade.php similarity index 100% rename from resources/views/layouts/default/mail/html/button.blade.php rename to resources/views/vendor/mail/html/button.blade.php diff --git a/resources/views/layouts/default/mail/html/footer.blade.php b/resources/views/vendor/mail/html/footer.blade.php similarity index 100% rename from resources/views/layouts/default/mail/html/footer.blade.php rename to resources/views/vendor/mail/html/footer.blade.php diff --git a/resources/views/layouts/default/mail/html/header.blade.php b/resources/views/vendor/mail/html/header.blade.php similarity index 100% rename from resources/views/layouts/default/mail/html/header.blade.php rename to resources/views/vendor/mail/html/header.blade.php diff --git a/resources/views/layouts/default/mail/html/layout.blade.php b/resources/views/vendor/mail/html/layout.blade.php similarity index 100% rename from resources/views/layouts/default/mail/html/layout.blade.php rename to resources/views/vendor/mail/html/layout.blade.php diff --git a/resources/views/layouts/default/mail/html/message.blade.php b/resources/views/vendor/mail/html/message.blade.php similarity index 100% rename from resources/views/layouts/default/mail/html/message.blade.php rename to resources/views/vendor/mail/html/message.blade.php diff --git a/resources/views/layouts/default/mail/html/panel.blade.php b/resources/views/vendor/mail/html/panel.blade.php similarity index 100% rename from resources/views/layouts/default/mail/html/panel.blade.php rename to resources/views/vendor/mail/html/panel.blade.php diff --git a/resources/views/layouts/default/mail/html/promotion.blade.php b/resources/views/vendor/mail/html/promotion.blade.php similarity index 100% rename from resources/views/layouts/default/mail/html/promotion.blade.php rename to resources/views/vendor/mail/html/promotion.blade.php diff --git a/resources/views/layouts/default/mail/html/promotion/button.blade.php b/resources/views/vendor/mail/html/promotion/button.blade.php similarity index 100% rename from resources/views/layouts/default/mail/html/promotion/button.blade.php rename to resources/views/vendor/mail/html/promotion/button.blade.php diff --git a/resources/views/layouts/default/mail/html/subcopy.blade.php b/resources/views/vendor/mail/html/subcopy.blade.php similarity index 100% rename from resources/views/layouts/default/mail/html/subcopy.blade.php rename to resources/views/vendor/mail/html/subcopy.blade.php diff --git a/resources/views/layouts/default/mail/html/table.blade.php b/resources/views/vendor/mail/html/table.blade.php similarity index 100% rename from resources/views/layouts/default/mail/html/table.blade.php rename to resources/views/vendor/mail/html/table.blade.php diff --git a/resources/views/layouts/default/mail/html/themes/default.css b/resources/views/vendor/mail/html/themes/default.css similarity index 100% rename from resources/views/layouts/default/mail/html/themes/default.css rename to resources/views/vendor/mail/html/themes/default.css diff --git a/resources/views/layouts/default/mail/markdown/button.blade.php b/resources/views/vendor/mail/markdown/button.blade.php similarity index 100% rename from resources/views/layouts/default/mail/markdown/button.blade.php rename to resources/views/vendor/mail/markdown/button.blade.php diff --git a/resources/views/layouts/default/mail/markdown/footer.blade.php b/resources/views/vendor/mail/markdown/footer.blade.php similarity index 100% rename from resources/views/layouts/default/mail/markdown/footer.blade.php rename to resources/views/vendor/mail/markdown/footer.blade.php diff --git a/resources/views/layouts/default/mail/markdown/header.blade.php b/resources/views/vendor/mail/markdown/header.blade.php similarity index 100% rename from resources/views/layouts/default/mail/markdown/header.blade.php rename to resources/views/vendor/mail/markdown/header.blade.php diff --git a/resources/views/layouts/default/mail/markdown/layout.blade.php b/resources/views/vendor/mail/markdown/layout.blade.php similarity index 100% rename from resources/views/layouts/default/mail/markdown/layout.blade.php rename to resources/views/vendor/mail/markdown/layout.blade.php diff --git a/resources/views/layouts/default/mail/markdown/message.blade.php b/resources/views/vendor/mail/markdown/message.blade.php similarity index 100% rename from resources/views/layouts/default/mail/markdown/message.blade.php rename to resources/views/vendor/mail/markdown/message.blade.php diff --git a/resources/views/layouts/default/mail/markdown/panel.blade.php b/resources/views/vendor/mail/markdown/panel.blade.php similarity index 100% rename from resources/views/layouts/default/mail/markdown/panel.blade.php rename to resources/views/vendor/mail/markdown/panel.blade.php diff --git a/resources/views/layouts/default/mail/markdown/promotion.blade.php b/resources/views/vendor/mail/markdown/promotion.blade.php similarity index 100% rename from resources/views/layouts/default/mail/markdown/promotion.blade.php rename to resources/views/vendor/mail/markdown/promotion.blade.php diff --git a/resources/views/layouts/default/mail/markdown/promotion/button.blade.php b/resources/views/vendor/mail/markdown/promotion/button.blade.php similarity index 100% rename from resources/views/layouts/default/mail/markdown/promotion/button.blade.php rename to resources/views/vendor/mail/markdown/promotion/button.blade.php diff --git a/resources/views/layouts/default/mail/markdown/subcopy.blade.php b/resources/views/vendor/mail/markdown/subcopy.blade.php similarity index 100% rename from resources/views/layouts/default/mail/markdown/subcopy.blade.php rename to resources/views/vendor/mail/markdown/subcopy.blade.php diff --git a/resources/views/layouts/default/mail/markdown/table.blade.php b/resources/views/vendor/mail/markdown/table.blade.php similarity index 100% rename from resources/views/layouts/default/mail/markdown/table.blade.php rename to resources/views/vendor/mail/markdown/table.blade.php diff --git a/tests/Browser/Pages/HomePage.php b/tests/Browser/Pages/HomePage.php deleted file mode 100644 index 4f5a87f4..00000000 --- a/tests/Browser/Pages/HomePage.php +++ /dev/null @@ -1,41 +0,0 @@ - '#selector', - ]; - } -} diff --git a/tests/Browser/Pages/Page.php b/tests/Browser/Pages/Page.php deleted file mode 100644 index f8d76222..00000000 --- a/tests/Browser/Pages/Page.php +++ /dev/null @@ -1,20 +0,0 @@ - '#selector', - ]; - } -} diff --git a/tests/Browser/RegistrationTest.php b/tests/Browser/RegistrationTest.php deleted file mode 100644 index c586bc52..00000000 --- a/tests/Browser/RegistrationTest.php +++ /dev/null @@ -1,21 +0,0 @@ -browse(function (Browser $browser) { - $browser->visit('/register') - ->assertSee('Register'); - });*/ - } -} diff --git a/tests/Browser/console/.gitignore b/tests/Browser/console/.gitignore deleted file mode 100644 index d6b7ef32..00000000 --- a/tests/Browser/console/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -* -!.gitignore diff --git a/tests/Browser/screenshots/.gitignore b/tests/Browser/screenshots/.gitignore deleted file mode 100644 index d6b7ef32..00000000 --- a/tests/Browser/screenshots/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -* -!.gitignore diff --git a/tests/CreatesApplication.php b/tests/CreatesApplication.php deleted file mode 100644 index 689c09ab..00000000 --- a/tests/CreatesApplication.php +++ /dev/null @@ -1,22 +0,0 @@ -make(Kernel::class)->bootstrap(); - //Hash::driver('bcrypt')->setRounds(4); - return $app; - } -} diff --git a/tests/RegistrationTest.php b/tests/RegistrationTest.php new file mode 100644 index 00000000..38a79dc6 --- /dev/null +++ b/tests/RegistrationTest.php @@ -0,0 +1,38 @@ +create(); + $user = $userSvc->createPilot($user); + + $this->assertEquals(PilotState::ACTIVE, $user->state); + + Event::assertDispatched(\App\Events\UserRegistered::class, function ($e) use ($user) { + return $e->user->id === $user->id + && $e->user->state === $user->state; + }); + + /*Mail::assertSent(\App\Mail\UserRegistered::class, function ($mail) use ($user) { + return $mail->user->id === $user->id + && $mail->user->state === $user->state; + });*/ + } +} diff --git a/tests/TestCase.php b/tests/TestCase.php index 2bf7f653..d1e8a504 100755 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -39,8 +39,6 @@ abstract class TestCase extends Illuminate\Foundation\Testing\TestCase public function setUp() { parent::setUp(); $this->reset_db(); - - Mail::fake(); } /**