|
| 1 | +<?php |
| 2 | + |
| 3 | +use Eclipse\Catalogue\Models\Product; |
| 4 | +use Eclipse\Catalogue\Models\Property; |
| 5 | +use Eclipse\Catalogue\Models\PropertyValue; |
| 6 | + |
| 7 | +it('merges values by moving product references and deleting source', function () { |
| 8 | + $property = Property::factory()->create(); |
| 9 | + $source = PropertyValue::factory()->create(['property_id' => $property->id, 'value' => 'Old']); |
| 10 | + $target = PropertyValue::factory()->create(['property_id' => $property->id, 'value' => 'New']); |
| 11 | + |
| 12 | + // Create products linked to source (and one already linked to target) |
| 13 | + $productA = Product::factory()->create(); |
| 14 | + $productB = Product::factory()->create(); |
| 15 | + $productC = Product::factory()->create(); |
| 16 | + |
| 17 | + $productA->propertyValues()->attach($source->id); |
| 18 | + $productB->propertyValues()->attach($source->id); |
| 19 | + $productC->propertyValues()->attach($target->id); // should remain |
| 20 | + |
| 21 | + // Also add duplicate A to target to ensure duplicate cleanup works |
| 22 | + $productA->propertyValues()->attach($target->id); |
| 23 | + |
| 24 | + $result = $source->mergeInto($target->id); |
| 25 | + |
| 26 | + expect($result['deleted'])->toBe(1) |
| 27 | + ->and($result['relinked'])->toBeGreaterThanOrEqual(2) |
| 28 | + ->and(PropertyValue::query()->whereKey($source->id)->doesntExist())->toBeTrue(); |
| 29 | + |
| 30 | + // All products should now reference only the target |
| 31 | + expect($productA->propertyValues()->pluck('property_value_id')->all()) |
| 32 | + ->toEqual([$target->id]); |
| 33 | + expect($productB->propertyValues()->pluck('property_value_id')->all()) |
| 34 | + ->toEqual([$target->id]); |
| 35 | + expect($productC->propertyValues()->pluck('property_value_id')->all()) |
| 36 | + ->toEqual([$target->id]); |
| 37 | +}); |
| 38 | + |
| 39 | +it('does not leave duplicate pivot rows after merge', function () { |
| 40 | + $property = Property::factory()->create(); |
| 41 | + $source = PropertyValue::factory()->create(['property_id' => $property->id]); |
| 42 | + $target = PropertyValue::factory()->create(['property_id' => $property->id]); |
| 43 | + $product = Product::factory()->create(); |
| 44 | + |
| 45 | + // Link product to both source and target |
| 46 | + $product->propertyValues()->attach($source->id); |
| 47 | + $product->propertyValues()->attach($target->id); |
| 48 | + |
| 49 | + $source->mergeInto($target->id); |
| 50 | + |
| 51 | + $count = DB::table('catalogue_product_has_property_value') |
| 52 | + ->where('product_id', $product->id) |
| 53 | + ->where('property_value_id', $target->id) |
| 54 | + ->count(); |
| 55 | + |
| 56 | + expect($count)->toBe(1); |
| 57 | +}); |
| 58 | + |
| 59 | +it('rolls back merge when values belong to different properties', function () { |
| 60 | + $prop1 = Property::factory()->create(); |
| 61 | + $prop2 = Property::factory()->create(); |
| 62 | + $source = PropertyValue::factory()->create(['property_id' => $prop1->id]); |
| 63 | + $target = PropertyValue::factory()->create(['property_id' => $prop2->id]); |
| 64 | + $product = Product::factory()->create(); |
| 65 | + $product->propertyValues()->attach($source->id); |
| 66 | + |
| 67 | + try { |
| 68 | + $source->mergeInto($target->id); |
| 69 | + test()->fail('Expected exception not thrown'); |
| 70 | + } catch (Throwable $e) { |
| 71 | + // ok |
| 72 | + } |
| 73 | + |
| 74 | + // Ensure source still exists and link remains |
| 75 | + expect(PropertyValue::query()->whereKey($source->id)->exists())->toBeTrue(); |
| 76 | + $links = DB::table('catalogue_product_has_property_value') |
| 77 | + ->where('product_id', $product->id) |
| 78 | + ->where('property_value_id', $source->id) |
| 79 | + ->count(); |
| 80 | + expect($links)->toBe(1); |
| 81 | +}); |
0 commit comments