From 2255a1a9fb64258cf7caac49aeb3038adbb56b2c Mon Sep 17 00:00:00 2001 From: Crypta Electrica Date: Sat, 25 Jan 2025 00:34:14 +1030 Subject: [PATCH] tests: add tests for faction squad filter --- tests/Squads/FactionRuleTest.php | 198 +++++++++++++++++++++++++++++++ 1 file changed, 198 insertions(+) create mode 100644 tests/Squads/FactionRuleTest.php diff --git a/tests/Squads/FactionRuleTest.php b/tests/Squads/FactionRuleTest.php new file mode 100644 index 000000000..7aebb0910 --- /dev/null +++ b/tests/Squads/FactionRuleTest.php @@ -0,0 +1,198 @@ +set('database.default', 'testbench'); + $app['config']->set('database.connections.testbench', [ + 'driver' => 'sqlite', + 'database' => ':memory:', + 'prefix' => '', + ]); + $app['config']->set('database.redis.client', 'mock'); + } + + /** + * @param \Illuminate\Foundation\Application $app + * @return array|string[] + */ + protected function getPackageProviders($app) + { + return [ + RedisMockServiceProvider::class, + WebServiceProvider::class, + ]; + } + + protected function setUp(): void + { + parent::setUp(); + + $this->loadMigrationsFrom(realpath(__DIR__ . '/../database/migrations')); + + Event::fake(); + + CharacterInfo::factory(50) + ->create() + ->each(function($character) { + $character->affiliation()->save(CharacterAffiliation::factory()->make()); + }); + + User::factory(10) + ->create() + ->each(function ($user) { + CharacterInfo::whereDoesntHave('refresh_token')->get() + ->random(rand(1, 5))->each(function ($character) use ($user) { + RefreshToken::factory()->create([ + 'character_id' => $character->character_id, + 'user_id' => $user->id, + ]); + }); + }); + } + + public function testUserHasNoCharacterInFaction() + { + // spawn test squad + $squad = new Squad([ + 'name' => 'Testing Squad', + 'description' => 'Some description', + 'type' => 'auto', + 'filters' => json_encode([ + 'and' => [ + [ + 'name' => 'faction', + 'path' => 'affiliation', + 'field' => 'faction_id', + 'operator' => '=', + 'criteria' => 500001, + 'text' => 'Random Faction', + ], + ], + ]), + ]); + + // pickup users + $users = User::all(); + + // ensure no users are eligible + foreach ($users as $user) { + $this->assertFalse($squad->isUserEligible($user)); + } + } + + public function testUserHasCharacterInFaction() + { + // spawn test squad + $squad = new Squad([ + 'name' => 'Testing Squad', + 'description' => 'Some description', + 'type' => 'auto', + 'filters' => json_encode([ + 'and' => [ + [ + 'name' => 'faction', + 'path' => 'affiliation', + 'field' => 'faction_id', + 'operator' => '=', + 'criteria' => 500001, + 'text' => 'Random Faction', + ], + ], + ]), + ]); + + // update an user to match criteria + $reference_user = User::first(); + $reference_user->characters->first()->affiliation->update(['faction_id' => 500001]); + + $users = User::all(); + + foreach ($users as $user) { + $user->id == $reference_user->id ? + $this->assertTrue($squad->isUserEligible($user)) : + $this->assertFalse($squad->isUserEligible($user)); + } + } + + /** + * This test checks whether a character from a corp outside an alliance is eligible for a squad with a alliance is not filter. + * In SeAT 4, this was not working properly + */ + public function testCharacterHasNoFactionWithFactionIsNotFilter(){ + $squad = new Squad([ + 'name' => 'Testing Squad', + 'description' => 'Some description', + 'type' => 'auto', + 'filters' => json_encode([ + 'and' => [ + [ + 'name' => 'faction', + 'path' => 'affiliation', + 'field' => 'faction_id', + 'operator' => '<>', + 'criteria' => 500001, + 'text' => 'Random Faction', + ], + ], + ]), + ]); + + $user = User::first(); + + $user->characters->each(function ($character){ + $character->affiliation->update([ + 'faction_id' => 500001, + ]); + }); + $this->assertFalse($squad->isUserEligible($user)); + + $user->characters->each(function ($character){ + $character->affiliation->update([ + 'faction_id' => null, + ]); + }); + $this->assertTrue($squad->isUserEligible($user)); + } +}