diff --git a/resources/js/components/slugs/Slug.js b/resources/js/components/slugs/Slug.js index b40167e5a41..d1cf5b9a86d 100644 --- a/resources/js/components/slugs/Slug.js +++ b/resources/js/components/slugs/Slug.js @@ -113,7 +113,7 @@ export default class Slug { this.#controller = new AbortController; let aborted = false; - return axios.post(cp_url('slug'), payload, { signal: this.#controller.signal }) + return axios.post(cp_url('slug'), payload, { signal: this.#controller.signal, transformResponse: [(data) => data] }) .then(response => response.data) .catch(e => { if (axios.isCancel(e)) { diff --git a/resources/js/components/slugs/Slugify.vue b/resources/js/components/slugs/Slugify.vue index 4c32be80d61..31c2aa12e45 100644 --- a/resources/js/components/slugs/Slugify.vue +++ b/resources/js/components/slugs/Slugify.vue @@ -42,7 +42,7 @@ export default { handler() { if (!this.shouldSlugify) { this.slug = this.to; - } else if (!this.from) { + } else if (this.from === null || this.from === undefined || this.from === '') { this.slug = ''; } else { this.slugify(); diff --git a/src/Fieldtypes/Text.php b/src/Fieldtypes/Text.php index d2e8dbfc32b..dfc580e74c4 100644 --- a/src/Fieldtypes/Text.php +++ b/src/Fieldtypes/Text.php @@ -154,8 +154,10 @@ public function process($data) public function preProcessIndex($value) { - if ($value) { - return $this->config('prepend').$value.$this->config('append'); + if (is_null($value)) { + return null; } + + return $this->config('prepend').$value.$this->config('append'); } } diff --git a/tests/Feature/SlugTest.php b/tests/Feature/SlugTest.php index d9a017e628e..c78798f98a5 100644 --- a/tests/Feature/SlugTest.php +++ b/tests/Feature/SlugTest.php @@ -40,6 +40,9 @@ public static function slugProvider() 'german characters' => ['Björn Müller', '-', 'de', 'bjoern-mueller'], 'arabic characters' => ['صباح الخير', '-', 'ar', 'sbah-alkhyr'], 'alternate separator' => ['one two three', '_', 'en', 'one_two_three'], + 'null string' => ['null', '-', 'en', 'null'], + 'zero string' => ['0', '-', 'en', '0'], + 'false string' => ['false', '-', 'en', 'false'], ]; } } diff --git a/tests/Fieldtypes/TextTest.php b/tests/Fieldtypes/TextTest.php index b679a4652f6..9f483409e52 100644 --- a/tests/Fieldtypes/TextTest.php +++ b/tests/Fieldtypes/TextTest.php @@ -33,4 +33,29 @@ public static function processValuesProvider() 'number' => ['number', [0, 3, 3, 3.14, null]], ]; } + + #[Test] + #[DataProvider('preProcessIndexProvider')] + public function it_pre_processes_index_values($config, $value, $expected) + { + $field = (new Text)->setField(new Field('test', array_merge([ + 'type' => 'text', + ], $config))); + + $this->assertSame($expected, $field->preProcessIndex($value)); + } + + public static function preProcessIndexProvider() + { + return [ + 'string value' => [[], 'hello', 'hello'], + 'null value' => [[], null, null], + 'zero integer' => [[], 0, '0'], + 'zero string' => [[], '0', '0'], + 'zero with prepend' => [['prepend' => '$'], 0, '$0'], + 'zero with append' => [['append' => '%'], 0, '0%'], + 'zero with prepend and append' => [['prepend' => '$', 'append' => '%'], 0, '$0%'], + 'string with prepend' => [['prepend' => '$'], 'hello', '$hello'], + ]; + } }