Fix tests

This commit is contained in:
philipp lang 2023-12-30 18:46:47 +01:00
parent eb14189bf0
commit b896c37c31
5 changed files with 33 additions and 15 deletions

View File

@ -27,4 +27,13 @@ class Group extends Model
{
return $this->belongsTo(static::class, 'parent_id');
}
public static function booted(): void
{
static::creating(function (self $group) {
if (!$group->getAttribute('inner_name') && $group->getAttribute('name')) {
$group->setAttribute('inner_name', $group->getAttribute('name'));
}
});
}
}

View File

@ -7,9 +7,9 @@ use App\Group;
use App\Maildispatcher\Actions\ResyncAction;
use App\Member\Member;
use App\Member\Membership;
use App\Membership\Actions\MassStoreAction;
use App\Membership\Actions\MembershipDestroyAction;
use App\Membership\Actions\MembershipStoreAction;
use App\Membership\Actions\StoreForGroupAction;
use App\Subactivity;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Support\Facades\Queue;
@ -17,7 +17,7 @@ use Tests\TestCase;
use Throwable;
use Zoomyboy\LaravelNami\Fakes\MembershipFake;
class SyncActionTest extends TestCase
class MassstoreActionTest extends TestCase
{
use DatabaseMigrations;
@ -31,13 +31,13 @@ class SyncActionTest extends TestCase
$subactivity = Subactivity::factory()->create();
$group = Group::factory()->create();
$this->postJson('/api/membership/sync', [
$this->postJson(route('membership.masslist.store'), [
'members' => [$member->id],
'activity_id' => $activity->id,
'subactivity_id' => $subactivity->id,
'group_id' => $group->id,
]);
StoreForGroupAction::assertPushed(fn ($action, $params) => $params[0]->is($group) && $params[1]->is($activity) && $params[2]->is($subactivity) && $params[3][0] === $member->id);
MassStoreAction::assertPushed(fn ($action, $params) => $params[0]->is($group) && $params[1]->is($activity) && $params[2]->is($subactivity) && $params[3][0] === $member->id);
}
public function testItCreatesAMembership(): void
@ -49,7 +49,7 @@ class SyncActionTest extends TestCase
$subactivity = Subactivity::factory()->create();
$group = Group::factory()->create();
StoreForGroupAction::run($group, $activity, $subactivity, [$member->id]);
MassStoreAction::run($group, $activity, $subactivity, [$member->id]);
}
public function testItDeletesAMembership(): void
@ -60,7 +60,7 @@ class SyncActionTest extends TestCase
$member = Member::factory()->defaults()->has(Membership::factory()->inLocal('Leiter*in', 'Rover'))->create();
StoreForGroupAction::run($member->memberships->first()->group, $member->memberships->first()->activity, $member->memberships->first()->subactivity, []);
MassStoreAction::run($member->memberships->first()->group, $member->memberships->first()->activity, $member->memberships->first()->subactivity, []);
}
public function testItRollsbackWhenDeletionFails(): void
@ -78,7 +78,7 @@ class SyncActionTest extends TestCase
->create();
try {
StoreForGroupAction::run($member->memberships->first()->group, $member->memberships->first()->activity, $member->memberships->first()->subactivity, []);
MassStoreAction::run($member->memberships->first()->group, $member->memberships->first()->activity, $member->memberships->first()->subactivity, []);
} catch (Throwable $e) {
}
$this->assertDatabaseCount('memberships', 2);

View File

@ -18,11 +18,11 @@ class IndexTest extends TestCase
$group = Group::factory()->for(Group::first(), 'parent')->create(['name' => 'Afff', 'inner_name' => 'Gruppe', 'level' => Level::REGION]);
$this->get('/group')
->assertInertiaPath('data.data.2.name', 'Afff')
->assertInertiaPath('data.data.2.inner_name', 'Gruppe')
->assertInertiaPath('data.data.2.id', $group->id)
->assertInertiaPath('data.data.2.level', 'Bezirk')
->assertInertiaPath('data.data.2.parent_id', Group::first()->id)
->assertInertiaPath('data.data.1.name', 'Afff')
->assertInertiaPath('data.data.1.inner_name', 'Gruppe')
->assertInertiaPath('data.data.1.id', $group->id)
->assertInertiaPath('data.data.1.level', 'Bezirk')
->assertInertiaPath('data.data.1.parent_id', Group::first()->id)
->assertInertiaPath('data.meta.links.bulkstore', route('group.bulkstore'));
}

View File

@ -53,7 +53,6 @@ class PullMemberActionTest extends TestCase
$member = app(PullMemberAction::class)->handle(1000, 1001);
Group::firstWhere('nami_id', 1000);
$this->assertDatabaseHas('members', [
'firstname' => '::firstname::',
'lastname' => '::lastname::',
@ -76,6 +75,7 @@ class PullMemberActionTest extends TestCase
$this->assertDatabaseHas('groups', [
'name' => 'SG Wald',
'nami_id' => 1000,
'inner_name' => 'SG Wald',
]);
$this->assertEquals(1001, $member->nami_id);
}

View File

@ -46,13 +46,14 @@ class InitializeGroupsTest extends TestCase
$this->assertDatabaseHas('groups', [
'nami_id' => 150,
'name' => 'lorem',
'inner_name' => 'lorem',
'parent_id' => null,
]);
}
public function testItDoesntCreateAGroupTwiceWithTheSameNamiId(): void
{
GroupModel::factory()->create(['nami_id' => 150]);
$existingGroup = GroupModel::factory()->create(['nami_id' => 150, 'inner_name' => 'Def']);
$parentGroup = Group::from(['id' => 150, 'name' => 'lorem', 'parentId' => null]);
$this->api->method('groups')->will($this->returnValueMap([
[null, collect([$parentGroup])],
@ -62,6 +63,12 @@ class InitializeGroupsTest extends TestCase
(new InitializeGroups($this->api))->handle();
$this->assertDatabaseCount('groups', 1);
$this->assertDatabaseHas('groups', [
'id' => $existingGroup->id,
'name' => 'lorem',
'inner_name' => 'Def',
'nami_id' => 150
]);
}
public function testItSynchsSubgroups(): void
@ -101,7 +108,7 @@ class InitializeGroupsTest extends TestCase
public function testItAssignsIdAndParentToAnExistingSubgroup(): void
{
GroupModel::factory()->create(['nami_id' => 200]);
$existingSubgroup = GroupModel::factory()->create(['name' => 'Abc', 'inner_name' => 'Def', 'nami_id' => 200]);
$parentGroup = Group::from(['id' => 150, 'name' => 'root', 'parentId' => null]);
$subgroup = Group::from(['id' => 200, 'name' => 'child', 'parentId' => 150]);
$this->api->method('groups')->will($this->returnValueMap([
@ -114,8 +121,10 @@ class InitializeGroupsTest extends TestCase
$this->assertDatabaseCount('groups', 2);
$this->assertDatabaseHas('groups', [
'id' => $existingSubgroup->id,
'nami_id' => 200,
'name' => 'child',
'inner_name' => 'Def',
'parent_id' => GroupModel::firstWhere('nami_id', 150)->id,
]);
}