Skip to content
Snippets Groups Projects
Commit 042d4807 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3447327 by alexpott: The shorttext value must be between 1 and 100 characters long

parent e0069a76
No related branches found
Tags 2.0.0-beta12
1 merge request!48Fix very long titles
Pipeline #173325 passed
......@@ -3,6 +3,7 @@
namespace Drupal\vgwort\Api;
use Drupal\Component\Render\PlainTextOutput;
use Drupal\Component\Utility\Unicode;
/**
* The message text.
......@@ -52,8 +53,10 @@ class MessageText implements \JsonSerializable {
* FALSE.
*/
public function __construct(string $shorttext, string $text, string $text_type = 'plainText', private readonly bool $lyric = FALSE) {
// @todo Is there any maximum length for this value?
$this->shorttext = PlainTextOutput::renderFromHtml($shorttext);
// The maximum length for the shorttext field is 100 and the minimum length
// is 1. As the title field is required we do not need to check the minimum
// length.
$this->shorttext = Unicode::truncate(PlainTextOutput::renderFromHtml($shorttext), 100, TRUE, FALSE, 50);
$this->addText($text, $text_type);
}
......
......@@ -53,4 +53,21 @@ JSON;
new MessageText('The title', 'The text', 'nope');
}
public function testVeryLongTitles(): void {
$long_title = str_repeat('A long title ', 100);
$text = new MessageText($long_title, '<strong>The text</strong>');
$data = json_decode($this->jsonEncode($text), TRUE);
$this->assertGreaterThan(100, strlen($long_title));
$this->assertSame(97, strlen($data['shorttext']));
// Ensure the truncation does not break a word,
$this->assertMatchesRegularExpression('/ long$/', $data['shorttext']);
// Ensure that long strings are truncated even if there is no work break.
$long_title = str_repeat('enormous', 100);
$text = new MessageText($long_title, '<strong>The text</strong>');
$data = json_decode($this->jsonEncode($text), TRUE);
$this->assertGreaterThan(100, strlen($long_title));
$this->assertSame(100, strlen($data['shorttext']));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment