Signed-off-by: Daniel Kesselberg <mail@danielkesselberg.de>pull/53834/head
parent
e0a21e5927
commit
336c6d2957
@ -0,0 +1,37 @@ |
||||
<?php |
||||
|
||||
declare(strict_types=1); |
||||
|
||||
/** |
||||
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors |
||||
* SPDX-License-Identifier: AGPL-3.0-or-later |
||||
*/ |
||||
|
||||
namespace OC\Mail; |
||||
|
||||
use Egulias\EmailValidator\EmailValidator as EquliasEmailValidator; |
||||
use Egulias\EmailValidator\Validation\NoRFCWarningsValidation; |
||||
use Egulias\EmailValidator\Validation\RFCValidation; |
||||
use OCP\IAppConfig; |
||||
use OCP\Mail\IEmailValidator; |
||||
|
||||
class EmailValidator implements IEmailValidator { |
||||
public function __construct( |
||||
private IAppConfig $appConfig, |
||||
) { |
||||
} |
||||
|
||||
public function isValid(string $email): bool { |
||||
if ($email === '') { |
||||
// Shortcut: empty addresses are never valid |
||||
return false; |
||||
} |
||||
|
||||
$strictMailCheck = $this->appConfig->getValueString('core', 'enforce_strict_email_check', 'yes') === 'yes'; |
||||
|
||||
$validator = new EquliasEmailValidator(); |
||||
$validation = $strictMailCheck ? new NoRFCWarningsValidation() : new RFCValidation(); |
||||
|
||||
return $validator->isValid($email, $validation); |
||||
} |
||||
} |
||||
@ -0,0 +1,24 @@ |
||||
<?php |
||||
|
||||
declare(strict_types=1); |
||||
|
||||
/** |
||||
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors |
||||
* SPDX-License-Identifier: AGPL-3.0-or-later |
||||
*/ |
||||
|
||||
namespace OCP\Mail; |
||||
|
||||
/** |
||||
* Validator for email addresses |
||||
* |
||||
* @since 32.0.0 |
||||
*/ |
||||
interface IEmailValidator { |
||||
/** |
||||
* @param string $email Email address to be validated |
||||
* @return bool True if the mail address is valid, false otherwise |
||||
* @since 32.0.0 |
||||
*/ |
||||
public function isValid(string $email): bool; |
||||
} |
||||
@ -0,0 +1,53 @@ |
||||
<?php |
||||
|
||||
declare(strict_types=1); |
||||
|
||||
/** |
||||
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors |
||||
* SPDX-License-Identifier: AGPL-3.0-or-later |
||||
*/ |
||||
|
||||
namespace Test\Mail; |
||||
|
||||
use OC\Mail\EmailValidator; |
||||
use OCP\IAppConfig; |
||||
use PHPUnit\Framework\Attributes\DataProvider; |
||||
use PHPUnit\Framework\MockObject\MockObject; |
||||
use Test\TestCase; |
||||
|
||||
class EmailValidatorTest extends TestCase { |
||||
private IAppConfig&MockObject $appConfig; |
||||
private EmailValidator $emailValidator; |
||||
|
||||
|
||||
protected function setUp(): void { |
||||
parent::setUp(); |
||||
|
||||
$this->appConfig = $this->createMock(IAppConfig::class); |
||||
$this->emailValidator = new EmailValidator($this->appConfig); |
||||
} |
||||
|
||||
public static function mailAddressProvider(): array { |
||||
return [ |
||||
['lukas@nextcloud.com', true, false], |
||||
['lukas@localhost', true, false], |
||||
['lukas@192.168.1.1', true, false], |
||||
['lukas@éxämplè.com', true, false], |
||||
['asdf', false, false], |
||||
['', false, false], |
||||
['lukas@nextcloud.org@nextcloud.com', false, false], |
||||
['test@localhost', true, false], |
||||
['test@localhost', false, true], |
||||
]; |
||||
} |
||||
|
||||
#[DataProvider('mailAddressProvider')] |
||||
public function testIsValid($email, $expected, $strict): void { |
||||
$this->appConfig |
||||
->expects($this->atMost(1)) |
||||
->method('getValueString') |
||||
->with('core', 'enforce_strict_email_check', 'yes') |
||||
->willReturn($strict ? 'yes' : 'no'); |
||||
$this->assertSame($expected, $this->emailValidator->isValid($email)); |
||||
} |
||||
} |
||||
@ -0,0 +1,30 @@ |
||||
<?php |
||||
|
||||
declare(strict_types=1); |
||||
|
||||
/** |
||||
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors |
||||
* SPDX-License-Identifier: AGPL-3.0-or-later |
||||
*/ |
||||
|
||||
namespace Test\Traits; |
||||
|
||||
use OC\Mail\EmailValidator; |
||||
use OCP\IAppConfig; |
||||
use OCP\Mail\IEmailValidator; |
||||
use PHPUnit\Framework\TestCase; |
||||
|
||||
trait EmailValidatorTrait { |
||||
protected function getEmailValidatorWithStrictEmailCheck(): IEmailValidator { |
||||
if (!($this instanceof TestCase)) { |
||||
throw new \RuntimeException('This trait can only be used in a test case'); |
||||
} |
||||
|
||||
$appConfig = $this->createMock(IAppConfig::class); |
||||
$appConfig->method('getValueString') |
||||
->with('core', 'enforce_strict_email_check', 'yes') |
||||
->willReturn('yes'); |
||||
|
||||
return new EmailValidator($appConfig); |
||||
} |
||||
} |
||||
Loading…
Reference in new issue