From b5ed5ba2b794b753449f5d2dfdd8c445fefdfb09 Mon Sep 17 00:00:00 2001 From: Philipp Daun Date: Wed, 20 Nov 2024 09:00:32 +0100 Subject: [PATCH] Prevent creation of duplicate terms on slug change (#384) * Fetch term by old slug if present Signed-off-by: Philipp Daun * Add a test --------- Signed-off-by: Philipp Daun Co-authored-by: Ryan Mitchell --- src/Taxonomies/Term.php | 3 ++- tests/Terms/TermTest.php | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 tests/Terms/TermTest.php diff --git a/src/Taxonomies/Term.php b/src/Taxonomies/Term.php index e6b9f620..87e4b37b 100644 --- a/src/Taxonomies/Term.php +++ b/src/Taxonomies/Term.php @@ -74,10 +74,11 @@ public static function makeModelFromContract(Contract $source) } return $class::firstOrNew([ - 'slug' => $source->slug(), + 'slug' => $source->getOriginal('slug', $source->slug()), 'taxonomy' => $source->taxonomy(), 'site' => $source->locale(), ])->fill([ + 'slug' => $source->slug(), 'uri' => $source->uri(), 'data' => $data, 'updated_at' => $source->lastModified(), diff --git a/tests/Terms/TermTest.php b/tests/Terms/TermTest.php new file mode 100644 index 00000000..2d9d8dc1 --- /dev/null +++ b/tests/Terms/TermTest.php @@ -0,0 +1,32 @@ +title('test')->save(); + + $term = tap(TermFacade::make('test-term')->taxonomy('test')->data([]))->save(); + + $this->assertCount(1, TermModel::all()); + $this->assertSame('test-term', TermModel::first()->slug); + + $term->slug('new-slug'); + $term->save(); + + $this->assertCount(1, TermModel::all()); + $this->assertSame('new-slug', TermModel::first()->slug); + } +}