From e47dea79362011f14a4b3d0352714639c6969d13 Mon Sep 17 00:00:00 2001 From: formsdev <136701234+formsdev@users.noreply.github.com> Date: Mon, 19 Jun 2023 18:20:31 +0530 Subject: [PATCH] Safer slugs (#135) * Safer slugs * fix test case for slug --- app/Models/Forms/Form.php | 5 ++++- tests/Feature/Forms/FormTest.php | 4 +++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/app/Models/Forms/Form.php b/app/Models/Forms/Form.php index 4e8897f..a45752f 100644 --- a/app/Models/Forms/Form.php +++ b/app/Models/Forms/Form.php @@ -10,6 +10,7 @@ use Database\Factories\FormFactory; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; +use Illuminate\Support\Str; use Spatie\Sluggable\HasSlug; use Spatie\Sluggable\SlugOptions; use Stevebauman\Purify\Facades\Purify; @@ -245,7 +246,9 @@ class Form extends Model { return SlugOptions::create() ->doNotGenerateSlugsOnUpdate() - ->generateSlugsFrom('title') + ->generateSlugsFrom(function (Form $form) { + return $form->title . ' ' . Str::random(6); + }) ->saveSlugsTo('slug'); } diff --git a/tests/Feature/Forms/FormTest.php b/tests/Feature/Forms/FormTest.php index d0c08d0..0471ada 100644 --- a/tests/Feature/Forms/FormTest.php +++ b/tests/Feature/Forms/FormTest.php @@ -86,7 +86,9 @@ it('can regenerate a form url', function () { ->assertSuccessful() ->assertJson(function (AssertableJson $json) use ($newSlug) { return $json->where('type', 'success') - ->where('form.slug', $newSlug) + ->where('form.slug', function ($slug) use ($newSlug) { + return substr($slug, 0, -6) == substr($newSlug, 0, -6); + }) ->etc(); });