feat: add sieve utils
Signed-off-by: Daniel Kesselberg <mail@danielkesselberg.de>
This commit is contained in:
Родитель
3906f272cb
Коммит
9486987428
|
@ -0,0 +1,38 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
|
||||
* SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
*/
|
||||
|
||||
namespace OCA\Mail\Sieve;
|
||||
|
||||
class SieveUtils {
|
||||
/**
|
||||
* Escape a string for use in a Sieve script
|
||||
*
|
||||
* @see https://www.rfc-editor.org/rfc/rfc5228#section-2.4.2
|
||||
*/
|
||||
public static function escapeString(string $subject): string {
|
||||
$subject = preg_replace(
|
||||
['/\\\\/', '/"/'],
|
||||
['\\\\\\\\', '\\"'],
|
||||
$subject
|
||||
);
|
||||
|
||||
return (string)$subject;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a string list for use in a Sieve script
|
||||
*
|
||||
* @see https://www.rfc-editor.org/rfc/rfc5228#section-2.4.2.1
|
||||
*/
|
||||
public static function stringList(array $values): string {
|
||||
$values = array_map([__CLASS__, 'escapeString'], $values);
|
||||
|
||||
return '["' . implode('", "', $values) . '"]';
|
||||
}
|
||||
}
|
|
@ -0,0 +1,51 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
|
||||
* SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
*/
|
||||
|
||||
namespace Unit\Sieve;
|
||||
|
||||
use ChristophWurst\Nextcloud\Testing\TestCase;
|
||||
use OCA\Mail\Sieve\SieveUtils;
|
||||
|
||||
class SieveUtilsTest extends TestCase {
|
||||
/**
|
||||
* @dataProvider providerEscapeString
|
||||
*/
|
||||
public function testEscapeString(string $subject, string $expected): void {
|
||||
$actual = SieveUtils::escapeString($subject);
|
||||
$this->assertSame($expected, $actual);
|
||||
}
|
||||
|
||||
public static function providerEscapeString(): array {
|
||||
return [
|
||||
['foo"bar', 'foo\"bar'],
|
||||
['foo\\bar', 'foo\\\\bar'],
|
||||
['foo"\\bar', 'foo\"\\\\bar'],
|
||||
['foobar', 'foobar'],
|
||||
['', ''],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerStringList
|
||||
*/
|
||||
public function testStringList(array $values, string $expected): void {
|
||||
$actual = SieveUtils::stringList($values);
|
||||
$this->assertSame($expected, $actual);
|
||||
}
|
||||
|
||||
public static function providerStringList(): array {
|
||||
return [
|
||||
[['Hello', 'World'], '["Hello", "World"]'],
|
||||
[['foo"bar', 'foo\\bar'], '["foo\"bar", "foo\\\\bar"]'],
|
||||
[['foo"bar', 'foo\\bar', 'foo"\\bar'], '["foo\"bar", "foo\\\\bar", "foo\"\\\\bar"]'],
|
||||
[['foobar'], '["foobar"]'],
|
||||
[[], '[""]'],
|
||||
];
|
||||
}
|
||||
}
|
Загрузка…
Ссылка в новой задаче