|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Feature\Scim; |
| 4 | + |
| 5 | +use App\Models\Department; |
| 6 | +use App\Models\User; |
| 7 | +use Laravel\Passport\Passport; |
| 8 | +use Tests\TestCase; |
| 9 | + |
| 10 | +/** |
| 11 | + * Regression for a Rollbar 500 on /scim/v2/Users/{id}: |
| 12 | + * |
| 13 | + * TypeError Illuminate\Database\Grammar::parameterize(): |
| 14 | + * Argument #1 ($values) must be of type array, string given |
| 15 | + * at MappedTable::replace (SnipeSCIMConfig.php:293) |
| 16 | + * |
| 17 | + * MappedTable is a leaf attribute for the User schema's `department` |
| 18 | + * and `location` fields — both scalar-mapped foreign relations. |
| 19 | + * SCIM clients (Entra, Okta in some configurations) send those as |
| 20 | + * complex objects: {"department": {"value": "Engineering"}} or |
| 21 | + * {"department": {"displayName": "Engineering"}}. The parent |
| 22 | + * SnipeRootComplex router also wraps sub-attribute values into an |
| 23 | + * array on the descent. Either way, MappedTable was receiving an |
| 24 | + * array in a code path that unconditionally passed it to |
| 25 | + * firstOrCreate() as a WHERE value — triggering the Grammar TypeError. |
| 26 | + * |
| 27 | + * The fix coerces array inputs to their SCIM-standard scalar leaf |
| 28 | + * (`value` / `displayName`, then any first scalar) before hitting |
| 29 | + * firstOrCreate. |
| 30 | + */ |
| 31 | +class MappedTableComplexValueTest extends TestCase |
| 32 | +{ |
| 33 | + public function test_put_with_department_as_complex_value_object_does_not_crash(): void |
| 34 | + { |
| 35 | + Passport::actingAs(User::factory()->superuser()->create()); |
| 36 | + $target = User::factory()->create(); |
| 37 | + |
| 38 | + $response = $this->putJson('/scim/v2/Users/'.$target->id, [ |
| 39 | + 'schemas' => ['urn:ietf:params:scim:schemas:core:2.0:User'], |
| 40 | + 'userName' => $target->username, |
| 41 | + 'department' => ['value' => 'Engineering'], |
| 42 | + ]); |
| 43 | + |
| 44 | + $response->assertStatus(200); |
| 45 | + $this->assertDatabaseHas('departments', ['name' => 'Engineering']); |
| 46 | + $target->refresh(); |
| 47 | + $this->assertSame('Engineering', $target->department->name); |
| 48 | + } |
| 49 | + |
| 50 | + public function test_put_with_department_as_display_name_object_does_not_crash(): void |
| 51 | + { |
| 52 | + Passport::actingAs(User::factory()->superuser()->create()); |
| 53 | + $target = User::factory()->create(); |
| 54 | + |
| 55 | + $response = $this->putJson('/scim/v2/Users/'.$target->id, [ |
| 56 | + 'schemas' => ['urn:ietf:params:scim:schemas:core:2.0:User'], |
| 57 | + 'userName' => $target->username, |
| 58 | + 'department' => ['displayName' => 'Product'], |
| 59 | + ]); |
| 60 | + |
| 61 | + $response->assertStatus(200); |
| 62 | + $target->refresh(); |
| 63 | + $this->assertSame('Product', $target->department->name); |
| 64 | + } |
| 65 | + |
| 66 | + public function test_put_with_location_as_complex_value_object_does_not_crash(): void |
| 67 | + { |
| 68 | + Passport::actingAs(User::factory()->superuser()->create()); |
| 69 | + $target = User::factory()->create(); |
| 70 | + |
| 71 | + $response = $this->putJson('/scim/v2/Users/'.$target->id, [ |
| 72 | + 'schemas' => ['urn:ietf:params:scim:schemas:core:2.0:User'], |
| 73 | + 'userName' => $target->username, |
| 74 | + 'location' => ['value' => 'HQ'], |
| 75 | + ]); |
| 76 | + |
| 77 | + $response->assertStatus(200); |
| 78 | + $this->assertDatabaseHas('locations', ['name' => 'HQ']); |
| 79 | + $target->refresh(); |
| 80 | + $this->assertSame('HQ', $target->location->name); |
| 81 | + } |
| 82 | + |
| 83 | + public function test_put_with_scalar_department_still_works(): void |
| 84 | + { |
| 85 | + // Non-regression guard: the historical scalar shape must |
| 86 | + // continue to work unchanged. |
| 87 | + Passport::actingAs(User::factory()->superuser()->create()); |
| 88 | + $target = User::factory()->create(); |
| 89 | + |
| 90 | + $response = $this->putJson('/scim/v2/Users/'.$target->id, [ |
| 91 | + 'schemas' => ['urn:ietf:params:scim:schemas:core:2.0:User'], |
| 92 | + 'userName' => $target->username, |
| 93 | + 'department' => 'Support', |
| 94 | + ]); |
| 95 | + |
| 96 | + $response->assertStatus(200); |
| 97 | + $target->refresh(); |
| 98 | + $this->assertSame('Support', $target->department->name); |
| 99 | + } |
| 100 | + |
| 101 | + public function test_patch_replace_with_complex_department_value_does_not_crash(): void |
| 102 | + { |
| 103 | + // PATCH shape from Entra / Okta: op=replace, no top-level |
| 104 | + // path, complex value under the attribute key. |
| 105 | + Passport::actingAs(User::factory()->superuser()->create()); |
| 106 | + $target = User::factory()->create(); |
| 107 | + |
| 108 | + $response = $this->patchJson('/scim/v2/Users/'.$target->id, [ |
| 109 | + 'schemas' => ['urn:ietf:params:scim:api:messages:2.0:PatchOp'], |
| 110 | + 'Operations' => [ |
| 111 | + [ |
| 112 | + 'op' => 'replace', |
| 113 | + 'value' => [ |
| 114 | + 'department' => ['value' => 'Finance'], |
| 115 | + ], |
| 116 | + ], |
| 117 | + ], |
| 118 | + ]); |
| 119 | + |
| 120 | + $response->assertStatus(200); |
| 121 | + $target->refresh(); |
| 122 | + $this->assertSame('Finance', $target->department->name); |
| 123 | + } |
| 124 | + |
| 125 | + public function test_put_with_empty_complex_department_object_nulls_relationship(): void |
| 126 | + { |
| 127 | + // Clients sometimes send {} to clear a relationship. The |
| 128 | + // coercion falls through to null, which the caller uses to |
| 129 | + // null the foreign-key column. No crash, no orphan row. |
| 130 | + Passport::actingAs(User::factory()->superuser()->create()); |
| 131 | + $existingDepartment = Department::factory()->create(); |
| 132 | + $target = User::factory()->create(['department_id' => $existingDepartment->id]); |
| 133 | + |
| 134 | + $response = $this->putJson('/scim/v2/Users/'.$target->id, [ |
| 135 | + 'schemas' => ['urn:ietf:params:scim:schemas:core:2.0:User'], |
| 136 | + 'userName' => $target->username, |
| 137 | + 'department' => new \stdClass, // empty object → [] on decode |
| 138 | + ]); |
| 139 | + |
| 140 | + $response->assertStatus(200); |
| 141 | + $target->refresh(); |
| 142 | + $this->assertNull($target->department_id); |
| 143 | + } |
| 144 | +} |
0 commit comments