From a97e30ed26746ff9e3a5083efed834642227d893 Mon Sep 17 00:00:00 2001 From: Adam Balan Date: Tue, 27 Aug 2024 13:57:01 -0600 Subject: [PATCH] More survey tests --- app/Flare/Models/UserLoginDuration.php | 7 + database/factories/SubmittedSurveyFactory.php | 10 +- .../factories/UserLoginDurationFactory.php | 33 ++++ tests/Console/Survey/StartSurveyTest.php | 159 ++++++++++++++++++ .../Controllers/Api/SurveyControllerTest.php | 77 +++++++++ tests/Traits/CreateSubmittedSurvey.php | 16 ++ tests/Traits/CreateSurvey.php | 2 +- tests/Traits/CreateUserLoginDuration.php | 13 ++ 8 files changed, 311 insertions(+), 6 deletions(-) create mode 100755 database/factories/UserLoginDurationFactory.php create mode 100644 tests/Console/Survey/StartSurveyTest.php create mode 100644 tests/Feature/Game/Survey/Controllers/Api/SurveyControllerTest.php create mode 100755 tests/Traits/CreateSubmittedSurvey.php create mode 100755 tests/Traits/CreateUserLoginDuration.php diff --git a/app/Flare/Models/UserLoginDuration.php b/app/Flare/Models/UserLoginDuration.php index 36e2e408a..730ad9bf3 100755 --- a/app/Flare/Models/UserLoginDuration.php +++ b/app/Flare/Models/UserLoginDuration.php @@ -2,6 +2,7 @@ namespace App\Flare\Models; +use Database\Factories\UserLoginDurationFactory; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; @@ -40,4 +41,10 @@ class UserLoginDuration extends Model public function user(): BelongsTo { return $this->belongsTo(User::class); } + + protected static function newFactory() + { + return UserLoginDurationFactory::new(); + } + } diff --git a/database/factories/SubmittedSurveyFactory.php b/database/factories/SubmittedSurveyFactory.php index ea95a8d39..aab54561c 100644 --- a/database/factories/SubmittedSurveyFactory.php +++ b/database/factories/SubmittedSurveyFactory.php @@ -2,7 +2,7 @@ namespace Database\Factories; -use App\Flare\Models\Survey; +use App\Flare\Models\SubmittedSurvey; use Illuminate\Database\Eloquent\Factories\Factory; class SubmittedSurveyFactory extends Factory @@ -12,7 +12,7 @@ class SubmittedSurveyFactory extends Factory * * @var string */ - protected $model = Survey::class; + protected $model = SubmittedSurvey::class; /** * Define the model's default state. @@ -27,9 +27,9 @@ public function definition() 'survey_id' => null, 'survey_response' => [ [ - 'Some Radio label' => ['value' => 'Option 1', 'type' => 'radio'], - 'Some Checkbox label' => ['value' => 'Option 1', 'type' => 'checkbox'], - 'Some markdown label' => ['value' => 'Some content', 'type' => 'markdown'], + 'Some Radio Label' => ['value' => 'Option 1', 'type' => 'radio'], + 'Some Checkbox Label' => ['value' => ['Option 1'], 'type' => 'checkbox'], + 'Some markdown Label' => ['value' => 'Some content', 'type' => 'markdown'], ] ] ]; diff --git a/database/factories/UserLoginDurationFactory.php b/database/factories/UserLoginDurationFactory.php new file mode 100755 index 000000000..b4f5b26f6 --- /dev/null +++ b/database/factories/UserLoginDurationFactory.php @@ -0,0 +1,33 @@ + null, + 'logged_in_at' => null, + 'logged_out_at' => null, + 'last_activity' => null, + 'duration_in_seconds' => null, + 'last_heart_beat' => null, + ]; + } +} diff --git a/tests/Console/Survey/StartSurveyTest.php b/tests/Console/Survey/StartSurveyTest.php new file mode 100644 index 000000000..592075f20 --- /dev/null +++ b/tests/Console/Survey/StartSurveyTest.php @@ -0,0 +1,159 @@ +characterFactory = (new CharacterFactory())->createBaseCharacter(); + } + + public function tearDown(): void { + parent::tearDown(); + + $this->characterFactory = null; + } + + public function testCannotStartSurveyWhenNoScheduledEventIsRunning() { + + Event::fake(); + + Artisan::call('start:survey'); + + Event::assertNotDispatched(ShowSurvey::class); + } + + public function testCannotStartSurveyWhenFlagIsAlreadyFlippedToShowTheSurvey() { + Event::fake(); + + $character = $this->characterFactory->getCharacter(); + + $this->createScheduledEvent([ + 'event_type' => EventType::FEEDBACK_EVENT, + 'currently_running' => true, + ]); + + $character->user()->update(['is_showing_survey' => true]); + + Artisan::call('start:survey'); + + Event::assertNotDispatched(ShowSurvey::class); + } + + public function testCannotStartSurveyWhenThereIsNoLoggedInDurationRecord() { + Event::fake(); + + + $this->createScheduledEvent([ + 'event_type' => EventType::FEEDBACK_EVENT, + 'currently_running' => true, + ]); + + Artisan::call('start:survey'); + + Event::assertNotDispatched(ShowSurvey::class); + } + + public function testCannotStartSurveyWhenUserLoginDurationIsBelowOneHour() { + Event::fake(); + + + $character = $this->characterFactory->getCharacter(); + + + $this->createScheduledEvent([ + 'event_type' => EventType::FEEDBACK_EVENT, + 'currently_running' => true, + ]); + + $this->createUserLoginDuration([ + 'user_id' => $character->user_id, + 'logged_in_at' => now(), + 'logged_out_at' => now(), + 'last_activity' => now(), + 'duration_in_seconds' => 0, + 'last_heart_beat' => now(), + ]); + + Artisan::call('start:survey'); + + Event::assertNotDispatched(ShowSurvey::class); + } + + public function testCannotStartSurveyWhenYouSubmittedASurvey() { + Event::fake(); + + + $character = $this->characterFactory->getCharacter(); + + + $this->createScheduledEvent([ + 'event_type' => EventType::FEEDBACK_EVENT, + 'currently_running' => true, + ]); + + $this->createUserLoginDuration([ + 'user_id' => $character->user_id, + 'logged_in_at' => now(), + 'logged_out_at' => now()->addHours(3), + 'last_activity' => now()->addHours(3), + 'duration_in_seconds' => now()->addHours(3)->diffInSeconds(now()), + 'last_heart_beat' => now()->addHours(3), + ]); + + $this->createSubmittedSurvey([ + 'character_id' => $character->id, + 'survey_id' => 1000, + ]); + + Artisan::call('start:survey'); + + Event::assertNotDispatched(ShowSurvey::class); + } + + public function testCanStartSurvey() { + + $character = $this->characterFactory->getCharacter(); + + $this->createSurvey(); + + $this->createScheduledEvent([ + 'event_type' => EventType::FEEDBACK_EVENT, + 'currently_running' => true, + ]); + + $this->createUserLoginDuration([ + 'user_id' => $character->user_id, + 'logged_in_at' => now(), + 'logged_out_at' => now()->addHours(3), + 'last_activity' => now()->addHours(3), + 'duration_in_seconds' => now()->addHours(3)->diffInSeconds(now()), + 'last_heart_beat' => now()->addHours(3), + ]); + + Artisan::call('start:survey'); + + $character = $character->refresh(); + + $this->assertTrue($character->user->is_showing_survey); + } +} diff --git a/tests/Feature/Game/Survey/Controllers/Api/SurveyControllerTest.php b/tests/Feature/Game/Survey/Controllers/Api/SurveyControllerTest.php new file mode 100644 index 000000000..bd3b6efaf --- /dev/null +++ b/tests/Feature/Game/Survey/Controllers/Api/SurveyControllerTest.php @@ -0,0 +1,77 @@ +survey = $this->createSurvey(); + + $this->character = (new CharacterFactory)->createBaseCharacter()->getCharacter(); + } + + public function tearDown(): void + { + parent::tearDown(); + + $this->character = null; + $this->survey = null; + } + + public function testFetchSurvey() { + $response = $this->actingAs($this->character->user) + ->call('GET', '/api/survey/'.$this->survey->id,); + + $jsonData = json_decode($response->getContent(), true); + + $this->assertEquals($jsonData['title'], $this->survey->title); + $this->assertEquals($jsonData['description'], $this->survey->description); + $this->assertEquals($jsonData['sections'], $this->survey->sections); + } + + public function testSubmitSurveyResponse() { + + $this->createItem(); + + $response = $this->actingAs($this->character->user) + ->call('POST', '/api/survey/submit/'.$this->survey->id . '/' . $this->character->id, [ + [ + 'Some Radio Label' => ['value' => 'Option 1', 'type' => 'radio'], + 'Some Checkbox Label' => ['value' => ['Option 1'], 'type' => 'checkbox'], + 'Some markdown Label' => ['value' => 'Some content', 'type' => 'markdown'], + ] + ]); + + $jsonData = json_decode($response->getContent(), true); + + $this->assertEquals($jsonData['message'], 'Survey submitted. Thank you and enjoy your new mythical item! You can find it in your character inventory. + Click character sheet and either select inventory management for mobile or see inventory bottom right. This item has been + rewarded regardless of your current inventory amount.'); + + $character = $this->character->refresh(); + + $inventorySlot = $character->inventory->slots->filter(function ($slot) { + return $slot->item->is_mythic; + })->first(); + + $this->assertNotNull($inventorySlot); + } + +} diff --git a/tests/Traits/CreateSubmittedSurvey.php b/tests/Traits/CreateSubmittedSurvey.php new file mode 100755 index 000000000..2b91e76d2 --- /dev/null +++ b/tests/Traits/CreateSubmittedSurvey.php @@ -0,0 +1,16 @@ +create($options); + } + +} diff --git a/tests/Traits/CreateSurvey.php b/tests/Traits/CreateSurvey.php index e3685e2c7..6a7245166 100755 --- a/tests/Traits/CreateSurvey.php +++ b/tests/Traits/CreateSurvey.php @@ -6,7 +6,7 @@ trait CreateSurvey { - public function CreateSurvey(array $options = []): Survey + public function createSurvey(array $options = []): Survey { return Survey::factory()->create($options); diff --git a/tests/Traits/CreateUserLoginDuration.php b/tests/Traits/CreateUserLoginDuration.php new file mode 100755 index 000000000..4ad519b24 --- /dev/null +++ b/tests/Traits/CreateUserLoginDuration.php @@ -0,0 +1,13 @@ +create($options); + } +}