Skip to content

Commit

Permalink
Fix error data too long for column 'title' (#961)
Browse files Browse the repository at this point in the history
  • Loading branch information
realodix committed Apr 25, 2024
1 parent 5a53adb commit 5f2e2cf
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
16 changes: 16 additions & 0 deletions app/Models/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class Url extends Model

const GUEST_NAME = 'Guest';

const TITLE_LENGTH = 255;

/**
* The attributes that are mass assignable.
*
Expand Down Expand Up @@ -114,6 +116,20 @@ protected function destination(): Attribute
);
}

protected function title(): Attribute
{
return Attribute::make(
set: function ($value) {
if (mb_strlen($value) > self::TITLE_LENGTH) {
// $limit minus 3 because Str::limit() adds 3 extra characters.
return str($value)->limit(self::TITLE_LENGTH - 3, '...');
}

return $value;
},
);
}

protected function clicks(): Attribute
{
return Attribute::make(
Expand Down
15 changes: 15 additions & 0 deletions tests/Unit/Models/UrlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,21 @@ public function getShortUrlAttribute(): void
$this->assertSame($expected, $actual);
}

#[Group('u-model')]
public function testSetTitleLength(): void
{
$lengthLimit = Url::TITLE_LENGTH;

$url = Url::factory()->create(['title' => str_repeat('a', $lengthLimit)]);
$this->assertEquals($lengthLimit, strlen($url->title));

$url = Url::factory()->create(['title' => str_repeat('a', $lengthLimit - 10)]);
$this->assertLessThan($lengthLimit, strlen($url->title));

$url = Url::factory()->create(['title' => str_repeat('a', $lengthLimit + 10)]);
$this->assertEquals($lengthLimit, strlen($url->title));
}

#[Test]
#[Group('u-model')]
public function setMetaTitleAttributeWhenWebTitleSetToFalse(): void
Expand Down

0 comments on commit 5f2e2cf

Please sign in to comment.