Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only allow deletion of related models in nested mutations #2454

Draft
wants to merge 16 commits into
base: master
Choose a base branch
from
Draft
55 changes: 55 additions & 0 deletions tests/Integration/Execution/MutationExecutor/BelongsToManyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,61 @@ public function testDeleteWithBelongsToMany(string $action): void
$this->assertNotNull(User::find(2));
}

/**
* @dataProvider existingModelMutations
*/
public function testDoNotDeleteWithoutRelationWithBelongsToMany(string $action): void
{
$users = [factory(User::class)->create(), factory(User::class)->create()];
factory(Role::class)
->createMany([[], []])
->each(function (Role $role, int $index) use ($users): void {
$role->users()->attach($users[$index]);
});

$this->graphQL(/** @lang GraphQL */ <<<GRAPHQL
mutation {
{$action}Role(input: {
id: 1
name: "is_user"
users: {
delete: [2]
spawnia marked this conversation as resolved.
Show resolved Hide resolved
}
}) {
id
name
users {
id
}
}
}
GRAPHQL
)->assertJson([
'data' => [
"{$action}Role" => [
'id' => '1',
'name' => 'is_user',
'users' => [
[
'id' => '1',
],
],
],
],
]);

/** @var Role $role */
$role = Role::find(1);
lucaslenz marked this conversation as resolved.
Show resolved Hide resolved
$this->assertCount(1, $role->users()->get());
$this->assertSame('is_user', $role->name);

/** @var Role $role */
$role = Role::find(2);
$this->assertCount(1, $role->users()->get());
$this->assertNotNull(User::find(1));
$this->assertNotNull(User::find(2));
}

/**
* @dataProvider existingModelMutations
*/
Expand Down
53 changes: 53 additions & 0 deletions tests/Integration/Execution/MutationExecutor/HasManyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,59 @@ public function testDeleteHasMany(string $action): void
]);
}

/**
* @dataProvider existingModelMutations
*/
public function testDeleteHasManyWithoutRelation(string $action): void
{
$tasks = [factory(Task::class)->create(), factory(Task::class)->create()];
factory(User::class)
->createMany([[], []])
->each(function (User $user, int $index) use ($tasks): void {
$user->tasks()->save($tasks[$index]);
});

$this->graphQL(/** @lang GraphQL */ <<<GRAPHQL
mutation {
${$action}User(input: {
id: 1
name: "foo"
tasks: {
delete: [{$tasks[1]->id}]
}
}) {
id
name
tasks {
id
}
}
}
GRAPHQL
)->assertJson([
'data' => [
"${$action}User" => [
'id' => '1',
'name' => 'foo',
'tasks' => [
[
'id' => (string) $tasks[0]->id,
],
],
],
],
]);

/** @var User $user */
$user = User::find(1);
$this->assertCount(1, $user->tasks()->get());
lucaslenz marked this conversation as resolved.
Show resolved Hide resolved

/** @var User $user */
$user = User::find(2);
$this->assertCount(1, $user->tasks()->get());
$this->assertNotNull(Task::find($tasks[1]->id));
}

/**
* @dataProvider existingModelMutations
*/
Expand Down
44 changes: 44 additions & 0 deletions tests/Integration/Execution/MutationExecutor/MorphManyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,50 @@ public function testUpdateAndDeleteMorphMany(string $action): void
]);
}

/**
* @dataProvider existingModelMutations
*/
public function testShouldNotDeleteWithoutRelationUpdateAndDeleteMorphMany(string $action): void
{
$images = [factory(Image::class)->create(), factory(Image::class)->create()];
factory(Task::class)
->createMany([[], []])
->each(function (Task $task, int $index) use ($images): void {
$task->images()->save($images[$index]);
});

$this->graphQL(/** @lang GraphQL */ "
mutation {
{$action}Task(input: {
id: 1
name: \"foo\"
lucaslenz marked this conversation as resolved.
Show resolved Hide resolved
images: {
delete: [1]
spawnia marked this conversation as resolved.
Show resolved Hide resolved
}
}) {
id
}
}
")->assertJson([
'data' => [
"{$action}Task" => [
'id' => '1',
'name' => 'foo',
'images' => [
[
'id' => $images[0]->id,
]
],
],
],
]);

/** @var Task $task */
$task = Task::find(2);
$this->assertCount(1, $task->images()->get());
$this->assertNotNull(Image::find(2));
}

/**
* @dataProvider existingModelMutations
*/
Expand Down