Skip to content

Commit

Permalink
Added limits in embed structures in discord notifications messages
Browse files Browse the repository at this point in the history
  • Loading branch information
mho22 committed Jan 3, 2025
1 parent 34a0669 commit 4a55bd7
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Notifications/Channels/Discord/DiscordMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public function fields(array $fields, bool $inline = true): self
foreach ($fields as $label => $value) {
$this->fields[] = [
'name' => Str::limit($label, 250),
'value' => Str::limi($value, 1000),
'value' => Str::limit($value, 1000),
'inline' => $inline,
];
}
Expand Down
78 changes: 78 additions & 0 deletions tests/Notifications/Channels/Discord/DiscordMessageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

use Spatie\Backup\Notifications\Channels\Discord\DiscordMessage;

beforeEach(function () {
$this->message = new DiscordMessage;
});

it('resizes embed title if character count is greater than 256', function () {
$string = fake()->regexify('[A-Za-z0-9]{300}');
$count = strlen($string);

expect($count)->toBeGreaterThan(256);

$this->message->title($string);
$title = $this->message->toArray()['embeds'][0]['title'];
$count = strlen($title);

expect($count)->toBeLessThan(256);
});

it('resizes embed description if character count is greater than 4096', function () {
$string = fake()->regexify('[A-Za-z0-9]{5000}');
$count = strlen($string);

expect($count)->toBeGreaterThan(4096);

$this->message->title($string);
$description = $this->message->toArray()['embeds'][0]['description'];
$count = strlen($description);

expect($count)->toBeLessThan(4096);
});

it('resizes fields if fields count is greater than 25', function () {
$array = array_map(fn() => fake()->word(), range(1, 50));
$count = count($array);

expect($count)->toBeGreaterThan(25);

$this->message->fields($array);
$fields = $this->message->toArray()['embeds'][0]['fields'];
$count = count($fields);

expect($count)->toEqual(25);
});

it('resizes field name if character count is greater than 256 and value if character count is greater than 1024', function () {
$name = fake()->regexify('[A-Za-z0-9]{300}');
$value = fake()->regexify('[A-Za-z0-9]{2000}');
$array = [$name => $value];
$nameCount = strlen($name);
$valueCount = strlen($value);

expect($nameCount)->toBeGreaterThan(256);
expect($valueCount)->toBeGreaterThan(1024);

$this->message->fields($array);
$fields = $this->message->toArray()['embeds'][0]['fields'][0];
$nameCount = strlen($fields['name']);
$valueCount = strlen($fields['value']);

expect($nameCount)->toBeLessThan(256);
expect($valueCount)->toBeLessThan(1024);
});

it('resizes footer text if character count is greater than 2048', function () {
$string = fake()->regexify('[A-Za-z0-9]{3000}');
$count = strlen($string);

expect($count)->toBeGreaterThan(2048);

$this->message->footer($string);
$footer = $this->message->toArray()['embeds'][0]['footer']['text'];
$count = strlen($footer);

expect($count)->toBeLessThan(2048);
});

0 comments on commit 4a55bd7

Please sign in to comment.