|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | +/** |
| 5 | + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 6 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 7 | + */ |
| 8 | + |
| 9 | +namespace OCA\Activity\Tests; |
| 10 | + |
| 11 | +use OCA\Activity\Data; |
| 12 | +use OCA\Activity\DigestSender; |
| 13 | +use OCA\Activity\GroupHelper; |
| 14 | +use OCA\Activity\UserSettings; |
| 15 | +use OCP\Activity\IEvent; |
| 16 | +use OCP\Activity\IManager; |
| 17 | +use OCP\Defaults; |
| 18 | +use OCP\IConfig; |
| 19 | +use OCP\IDateTimeFormatter; |
| 20 | +use OCP\IURLGenerator; |
| 21 | +use OCP\IUserManager; |
| 22 | +use OCP\L10N\IFactory; |
| 23 | +use OCP\Mail\IMailer; |
| 24 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 25 | +use PHPUnit\Framework\MockObject\MockObject; |
| 26 | +use Psr\Log\LoggerInterface; |
| 27 | + |
| 28 | +class DigestSenderTest extends TestCase { |
| 29 | + private DigestSender $digestSender; |
| 30 | + |
| 31 | + protected function setUp(): void { |
| 32 | + parent::setUp(); |
| 33 | + |
| 34 | + $this->digestSender = new DigestSender( |
| 35 | + $this->createMock(IConfig::class), |
| 36 | + $this->createMock(Data::class), |
| 37 | + $this->createMock(UserSettings::class), |
| 38 | + $this->createMock(GroupHelper::class), |
| 39 | + $this->createMock(IMailer::class), |
| 40 | + $this->createMock(IManager::class), |
| 41 | + $this->createMock(IUserManager::class), |
| 42 | + $this->createMock(IURLGenerator::class), |
| 43 | + $this->createMock(Defaults::class), |
| 44 | + $this->createMock(IFactory::class), |
| 45 | + $this->createMock(IDateTimeFormatter::class), |
| 46 | + $this->createMock(LoggerInterface::class), |
| 47 | + ); |
| 48 | + } |
| 49 | + |
| 50 | + public static function provideGetHTMLSubjectData(): array { |
| 51 | + return [ |
| 52 | + 'no rich subject escapes parsed subject' => [ |
| 53 | + '', 'Hello <World>', [], |
| 54 | + 'Hello <World>', |
| 55 | + ], |
| 56 | + 'linked parameter renders anchor' => [ |
| 57 | + 'File {file} was shared', '', |
| 58 | + ['file' => ['type' => 'file', 'path' => 'photo.jpg', 'name' => 'photo.jpg', 'link' => 'https://cloud.example.com/f/123']], |
| 59 | + 'File <a href="https://cloud.example.com/f/123">photo.jpg</a> was shared', |
| 60 | + ], |
| 61 | + 'double-quote in link is escaped' => [ |
| 62 | + 'File {file} was shared', '', |
| 63 | + ['file' => ['type' => 'file', 'path' => 'photo.jpg', 'name' => 'photo.jpg', 'link' => 'https://cloud.example.com/f/123"onmouseover="alert(1)']], |
| 64 | + 'File <a href="https://cloud.example.com/f/123"onmouseover="alert(1)">photo.jpg</a> was shared', |
| 65 | + ], |
| 66 | + 'parameter without link uses strong tag' => [ |
| 67 | + 'File {file} was shared', '', |
| 68 | + ['file' => ['type' => 'file', 'path' => 'photo.jpg', 'name' => 'photo.jpg']], |
| 69 | + 'File <strong>photo.jpg</strong> was shared', |
| 70 | + ], |
| 71 | + ]; |
| 72 | + } |
| 73 | + |
| 74 | + #[DataProvider('provideGetHTMLSubjectData')] |
| 75 | + public function testGetHTMLSubject(string $richSubject, string $parsedSubject, array $richParams, string $expected): void { |
| 76 | + $event = $this->createMock(IEvent::class); |
| 77 | + $event->method('getRichSubject')->willReturn($richSubject); |
| 78 | + $event->method('getParsedSubject')->willReturn($parsedSubject); |
| 79 | + $event->method('getRichSubjectParameters')->willReturn($richParams); |
| 80 | + |
| 81 | + $result = self::invokePrivate($this->digestSender, 'getHTMLSubject', [$event]); |
| 82 | + $this->assertSame($expected, $result); |
| 83 | + } |
| 84 | +} |
0 commit comments