Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions lib/Push.php
Original file line number Diff line number Diff line change
Expand Up @@ -927,6 +927,17 @@ protected function encodeNotif(int $id, INotification $notification, int $maxLen
// Max length of encryption is ~240, so we need to make sure the subject is shorter.
// Also, subtract two for encapsulating quotes will be added.
$maxDataLength = $maxLength - strlen((string)json_encode($data)) - 2;

if ($maxDataLength < 0 && isset($data['link'])) {
// Remove the link to try saving data bytes
unset($data['link']);
$maxDataLength = $maxLength - strlen((string)json_encode($data)) - 2;
}

if ($maxDataLength <= 0) {
return $data;
}

$data['subject'] = Util::shortenMultibyteString($notification->getParsedSubject(), $maxDataLength);
if ($notification->getParsedSubject() !== $data['subject']) {
$data['subject'] .= '…';
Expand Down
21 changes: 21 additions & 0 deletions tests/Unit/PushTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1722,4 +1722,25 @@ public function testValidateTokenAndGetAgeCached(): void {
$push = $this->getPush();
$this->assertSame(1234567890, self::invokePrivate($push, 'validateTokenAndGetAge', [42]));
}

public static function dataEncodeNotifOverlongFields(): array {
return [
'link dropped' => [str_repeat('a', 60), 'https://cert.europa.eu/' . str_repeat('x', 100), 'Subject'],
'object type and id too long' => [str_repeat('/', 64), '', ''],
];
}

#[DataProvider(methodName: 'dataEncodeNotifOverlongFields')]
public function testEncodeNotifOverlongFields(string $objectId, string $link, string $expectedSubject): void {
$notification = $this->createStub(INotification::class);
$notification->method('getApp')->willReturn('announcementcenter');
$notification->method('getObjectType')->willReturn($objectId);
$notification->method('getObjectId')->willReturn($objectId);
$notification->method('getLink')->willReturn($link);
$notification->method('getParsedSubject')->willReturn('Subject');

$data = self::invokePrivate($this->getPush(), 'encodeNotif', [42, $notification, 200]);
$this->assertSame($expectedSubject, $data['subject']);
$this->assertArrayNotHasKey('link', $data);
}
}
Loading