-
Notifications
You must be signed in to change notification settings - Fork 288
fix(imap): use message header date for timestamps #11980
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dhairyajangir
wants to merge
9
commits into
nextcloud:main
Choose a base branch
from
dhairyajangir:fix/display-imap-date-912
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+136
−1
Open
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
bc38f1c
fix(imap): use message header date for timestamps
dhairyajangir 5b29204
Merge branch 'main' into fix/display-imap-date-912
dhairyajangir 49b4b95
Merge branch 'main' into fix/display-imap-date-912
dhairyajangir a7a7781
Merge branch 'main' into fix/display-imap-date-912
dhairyajangir a01527f
Update lib/IMAP/ImapMessageFetcher.php
dhairyajangir 3a7a6e7
Update lib/IMAP/ImapMessageFetcher.php
dhairyajangir 21697ee
fix: Handle invalid date headers in ImapMessageFetcher
dhairyajangir cff90c3
Merge branch 'fix/display-imap-date-912' of https://github.com/dhairy…
dhairyajangir 1681d9d
Merge branch 'main' into fix/display-imap-date-912
dhairyajangir File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace OCA\Mail\Tests\Unit\IMAP; | ||
|
|
||
| use ChristophWurst\Nextcloud\Testing\TestCase; | ||
| use Horde_Imap_Client_Base; | ||
| use Horde_Imap_Client_Data_Fetch; | ||
| use Horde_Imap_Client_DateTime; | ||
| use Horde_Mime_Headers; | ||
| use OCA\Mail\IMAP\Charset\Converter; | ||
| use OCA\Mail\IMAP\ImapMessageFetcher; | ||
| use OCA\Mail\Service\Html; | ||
| use OCA\Mail\Service\PhishingDetection\PhishingDetectionService; | ||
| use OCA\Mail\Service\SmimeService; | ||
| use PHPUnit\Framework\MockObject\MockObject; | ||
| use ReflectionMethod; | ||
|
|
||
| final class ImapMessageFetcherTest extends TestCase { | ||
| private Html|MockObject $htmlService; | ||
| private SmimeService|MockObject $smimeService; | ||
| private Converter|MockObject $converter; | ||
| private PhishingDetectionService|MockObject $phishingDetectionService; | ||
| private Horde_Imap_Client_Base|MockObject $client; | ||
| private ImapMessageFetcher $fetcher; | ||
|
|
||
| protected function setUp(): void { | ||
| parent::setUp(); | ||
|
|
||
| $this->htmlService = $this->createMock(Html::class); | ||
| $this->smimeService = $this->createMock(SmimeService::class); | ||
| $this->converter = $this->createMock(Converter::class); | ||
| $this->phishingDetectionService = $this->createMock(PhishingDetectionService::class); | ||
| $this->client = $this->createMock(Horde_Imap_Client_Base::class); | ||
|
|
||
| $this->fetcher = new ImapMessageFetcher( | ||
| 42, | ||
| 'INBOX', | ||
| $this->client, | ||
| 'user', | ||
| $this->htmlService, | ||
| $this->smimeService, | ||
| $this->converter, | ||
| $this->phishingDetectionService, | ||
| ); | ||
| } | ||
|
|
||
| private function invokeResolveMessageDate(Horde_Imap_Client_Data_Fetch $fetch, Horde_Mime_Headers $headers): Horde_Imap_Client_DateTime { | ||
| $method = new ReflectionMethod(ImapMessageFetcher::class, 'resolveMessageDate'); | ||
| $method->setAccessible(true); | ||
| /** @var Horde_Imap_Client_DateTime $result */ | ||
| $result = $method->invoke($this->fetcher, $fetch, $headers); | ||
| return $result; | ||
| } | ||
|
|
||
| public function testResolveMessageDatePrefersHeader(): void { | ||
| $fetch = $this->createMock(Horde_Imap_Client_Data_Fetch::class); | ||
| $fetch->method('getImapDate') | ||
| ->willReturn(new Horde_Imap_Client_DateTime('2025-10-20 10:00:00 +0000')); | ||
| $headers = Horde_Mime_Headers::parseHeaders("Date: Mon, 01 Jan 2001 12:00:00 +0000\r\n"); | ||
|
|
||
| $result = $this->invokeResolveMessageDate($fetch, $headers); | ||
|
|
||
| self::assertSame('2001-01-01T12:00:00+00:00', $result->format('c')); | ||
| } | ||
|
|
||
| public function testResolveMessageDateFallsBackToInternalWithoutHeader(): void { | ||
| $internal = new Horde_Imap_Client_DateTime('2025-10-20 10:00:00 +0000'); | ||
| $fetch = $this->createMock(Horde_Imap_Client_Data_Fetch::class); | ||
| $fetch->method('getImapDate')->willReturn($internal); | ||
| $headers = Horde_Mime_Headers::parseHeaders(''); | ||
|
|
||
| $result = $this->invokeResolveMessageDate($fetch, $headers); | ||
|
|
||
| self::assertSame($internal->format('c'), $result->format('c')); | ||
| } | ||
|
|
||
| public function testResolveMessageDateFallsBackToInternalOnInvalidHeader(): void { | ||
| $internal = new Horde_Imap_Client_DateTime('2025-10-20 10:00:00 +0000'); | ||
| $fetch = $this->createMock(Horde_Imap_Client_Data_Fetch::class); | ||
| $fetch->method('getImapDate')->willReturn($internal); | ||
| $headers = Horde_Mime_Headers::parseHeaders("Date: not-a-valid-date\r\n"); | ||
|
|
||
| $result = $this->invokeResolveMessageDate($fetch, $headers); | ||
|
|
||
| self::assertSame($internal->format('c'), $result->format('c')); | ||
| } | ||
|
|
||
| public function testResolveMessageDateFallsBackToNowWhenNoDateAvailable(): void { | ||
| $fetch = $this->createMock(Horde_Imap_Client_Data_Fetch::class); | ||
| $fetch->method('getImapDate')->willReturn(null); | ||
| $headers = Horde_Mime_Headers::parseHeaders(''); | ||
|
|
||
| $before = time(); | ||
| $result = $this->invokeResolveMessageDate($fetch, $headers); | ||
| $after = time(); | ||
|
|
||
| self::assertGreaterThanOrEqual($before, $result->getTimestamp()); | ||
| self::assertLessThanOrEqual($after, $result->getTimestamp()); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.