Translate the errors

This commit is contained in:
Joas Schilling 2016-08-01 17:56:33 +02:00
parent 66fd216d3a
commit cc719c9bb7
No known key found for this signature in database
GPG Key ID: E166FD8976B3BAC8
12 changed files with 149 additions and 75 deletions

View File

@ -23,6 +23,7 @@ namespace OCA\WorkflowEngine\Check;
use OCP\Files\Storage\IStorage;
use OCP\IL10N;
use OCP\WorkflowEngine\ICheck;
abstract class AbstractStringCheck implements ICheck {
@ -30,6 +31,16 @@ abstract class AbstractStringCheck implements ICheck {
/** @var array[] Nested array: [Pattern => [ActualValue => Regex Result]] */
protected $matches;
/** @var IL10N */
protected $l;
/**
* @param IL10N $l
*/
public function __construct(IL10N $l) {
$this->l = $l;
}
/**
* @param IStorage $storage
* @param string $path
@ -81,12 +92,12 @@ abstract class AbstractStringCheck implements ICheck {
*/
public function validateCheck($operator, $value) {
if (!in_array($operator, ['is', '!is', 'matches', '!matches'])) {
throw new \UnexpectedValueException('Invalid operator', 1);
throw new \UnexpectedValueException($this->l->t('The given operator is invalid'), 1);
}
if (in_array($operator, ['matches', '!matches']) &&
@preg_match($value, null) === false) {
throw new \UnexpectedValueException('Invalid regex', 2);
throw new \UnexpectedValueException($this->l->t('The given regular expression is invalid'), 2);
}
}

View File

@ -23,6 +23,7 @@ namespace OCA\WorkflowEngine\Check;
use OCP\Files\IMimeTypeDetector;
use OCP\IL10N;
use OCP\IRequest;
class FileMimeType extends AbstractStringCheck {
@ -37,10 +38,12 @@ class FileMimeType extends AbstractStringCheck {
protected $mimeTypeDetector;
/**
* @param IL10N $l
* @param IRequest $request
* @param IMimeTypeDetector $mimeTypeDetector
*/
public function __construct(IRequest $request, IMimeTypeDetector $mimeTypeDetector) {
public function __construct(IL10N $l, IRequest $request, IMimeTypeDetector $mimeTypeDetector) {
parent::__construct($l);
$this->request = $request;
$this->mimeTypeDetector = $mimeTypeDetector;
}

View File

@ -23,6 +23,7 @@ namespace OCA\WorkflowEngine\Check;
use OCP\Files\Storage\IStorage;
use OCP\IL10N;
use OCP\IRequest;
use OCP\Util;
use OCP\WorkflowEngine\ICheck;
@ -32,13 +33,18 @@ class FileSize implements ICheck {
/** @var int */
protected $size;
/** @var IL10N */
protected $l;
/** @var IRequest */
protected $request;
/**
* @param IL10N $l
* @param IRequest $request
*/
public function __construct(IRequest $request) {
public function __construct(IL10N $l, IRequest $request) {
$this->l = $l;
$this->request = $request;
}
@ -80,11 +86,11 @@ class FileSize implements ICheck {
*/
public function validateCheck($operator, $value) {
if (!in_array($operator, ['less', '!less', 'greater', '!greater'])) {
throw new \UnexpectedValueException('Invalid operator', 1);
throw new \UnexpectedValueException($this->l->t('The given operator is invalid'), 1);
}
if (!preg_match('/^[0-9]+[ ]?[kmgt]?b$/i', $value)) {
throw new \UnexpectedValueException('Invalid file size', 2);
throw new \UnexpectedValueException($this->l->t('The given file size is invalid'), 2);
}
}

View File

@ -24,6 +24,7 @@ namespace OCA\WorkflowEngine\Check;
use OCP\Files\Cache\ICache;
use OCP\Files\Storage\IStorage;
use OCP\IL10N;
use OCP\SystemTag\ISystemTagManager;
use OCP\SystemTag\ISystemTagObjectMapper;
use OCP\SystemTag\TagNotFoundException;
@ -37,6 +38,9 @@ class FileSystemTags implements ICheck {
/** @var array */
protected $fileSystemTags;
/** @var IL10N */
protected $l;
/** @var ISystemTagManager */
protected $systemTagManager;
@ -50,10 +54,12 @@ class FileSystemTags implements ICheck {
protected $path;
/**
* @param IL10N $l
* @param ISystemTagManager $systemTagManager
* @param ISystemTagObjectMapper $systemTagObjectMapper
*/
public function __construct(ISystemTagManager $systemTagManager, ISystemTagObjectMapper $systemTagObjectMapper) {
public function __construct(IL10N $l, ISystemTagManager $systemTagManager, ISystemTagObjectMapper $systemTagObjectMapper) {
$this->l = $l;
$this->systemTagManager = $systemTagManager;
$this->systemTagObjectMapper = $systemTagObjectMapper;
}
@ -84,15 +90,15 @@ class FileSystemTags implements ICheck {
*/
public function validateCheck($operator, $value) {
if (!in_array($operator, ['is', '!is'])) {
throw new \UnexpectedValueException('Invalid operator', 1);
throw new \UnexpectedValueException($this->l->t('The given operator is invalid'), 1);
}
try {
$this->systemTagManager->getTagsByIds($value);
} catch (TagNotFoundException $e) {
throw new \UnexpectedValueException('Tag does not exist', 2);
throw new \UnexpectedValueException($this->l->t('The given tag id is invalid'), 2);
} catch (\InvalidArgumentException $e) {
throw new \UnexpectedValueException('Tag does not exist', 3);
throw new \UnexpectedValueException($this->l->t('The given tag id is invalid'), 3);
}
}

View File

@ -23,18 +23,24 @@ namespace OCA\WorkflowEngine\Check;
use OCP\Files\Storage\IStorage;
use OCP\IL10N;
use OCP\IRequest;
use OCP\WorkflowEngine\ICheck;
class RequestRemoteAddress implements ICheck {
/** @var IL10N */
protected $l;
/** @var IRequest */
protected $request;
/**
* @param IL10N $l
* @param IRequest $request
*/
public function __construct(IRequest $request) {
public function __construct(IL10N $l, IRequest $request) {
$this->l = $l;
$this->request = $request;
}
@ -73,27 +79,27 @@ class RequestRemoteAddress implements ICheck {
*/
public function validateCheck($operator, $value) {
if (!in_array($operator, ['matchesIPv4', '!matchesIPv4', 'matchesIPv6', '!matchesIPv6'])) {
throw new \UnexpectedValueException('Invalid operator', 1);
throw new \UnexpectedValueException($this->l->t('The given operator is invalid'), 1);
}
$decodedValue = explode('/', $value);
if (sizeof($decodedValue) !== 2) {
throw new \UnexpectedValueException('Invalid IP range', 2);
throw new \UnexpectedValueException($this->l->t('The given IP range is invalid'), 2);
}
if (in_array($operator, ['matchesIPv4', '!matchesIPv4'])) {
if (!filter_var($decodedValue[0], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
throw new \UnexpectedValueException('Invalid IPv4 range', 3);
throw new \UnexpectedValueException($this->l->t('The given IP range is not valid for IPv4'), 3);
}
if ($decodedValue[1] > 32 || $decodedValue[1] <= 0) {
throw new \UnexpectedValueException('Invalid IPv4 range', 4);
throw new \UnexpectedValueException($this->l->t('The given IP range is not valid for IPv4'), 4);
}
} else {
if (!filter_var($decodedValue[0], FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
throw new \UnexpectedValueException('Invalid IPv6 range', 3);
throw new \UnexpectedValueException($this->l->t('The given IP range is not valid for IPv6'), 3);
}
if ($decodedValue[1] > 128 || $decodedValue[1] <= 0) {
throw new \UnexpectedValueException('Invalid IPv6 range', 4);
throw new \UnexpectedValueException($this->l->t('The given IP range is not valid for IPv6'), 4);
}
}
}

View File

@ -24,6 +24,7 @@ namespace OCA\WorkflowEngine\Check;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\Files\Storage\IStorage;
use OCP\IL10N;
use OCP\WorkflowEngine\ICheck;
class RequestTime implements ICheck {
@ -34,13 +35,17 @@ class RequestTime implements ICheck {
/** @var bool[] */
protected $cachedResults;
/** @var IL10N */
protected $l;
/** @var ITimeFactory */
protected $timeFactory;
/**
* @param ITimeFactory $timeFactory
*/
public function __construct(ITimeFactory $timeFactory) {
public function __construct(IL10N $l, ITimeFactory $timeFactory) {
$this->l = $l;
$this->timeFactory = $timeFactory;
}
@ -101,24 +106,24 @@ class RequestTime implements ICheck {
*/
public function validateCheck($operator, $value) {
if (!in_array($operator, ['in', '!in'])) {
throw new \UnexpectedValueException('Invalid operator', 1);
throw new \UnexpectedValueException($this->l->t('The given operator is invalid'), 1);
}
$regexValue = '\"' . self::REGEX_TIME . ' ' . self::REGEX_TIMEZONE . '\"';
$result = preg_match('/^\[' . $regexValue . ',' . $regexValue . '\]$/', $value, $matches);
if (!$result) {
throw new \UnexpectedValueException('Invalid time limits', 2);
throw new \UnexpectedValueException($this->l->t('The given time span is invalid'), 2);
}
$values = json_decode($value, true);
$time1 = \DateTime::createFromFormat('H:i e', $values[0]);
if ($time1 === false) {
throw new \UnexpectedValueException('Invalid start time given', 3);
throw new \UnexpectedValueException($this->l->t('The given start time is invalid'), 3);
}
$time2 = \DateTime::createFromFormat('H:i e', $values[1]);
if ($time2 === false) {
throw new \UnexpectedValueException('Invalid end time given', 3);
throw new \UnexpectedValueException($this->l->t('The given end time is invalid'), 4);
}
}
}

View File

@ -22,6 +22,7 @@
namespace OCA\WorkflowEngine\Check;
use OCP\IL10N;
use OCP\IRequest;
class RequestURL extends AbstractStringCheck {
@ -33,9 +34,11 @@ class RequestURL extends AbstractStringCheck {
protected $request;
/**
* @param IL10N $l
* @param IRequest $request
*/
public function __construct(IRequest $request) {
public function __construct(IL10N $l, IRequest $request) {
parent::__construct($l);
$this->request = $request;
}

View File

@ -22,6 +22,7 @@
namespace OCA\WorkflowEngine\Check;
use OCP\IL10N;
use OCP\IRequest;
class RequestUserAgent extends AbstractStringCheck {
@ -30,9 +31,11 @@ class RequestUserAgent extends AbstractStringCheck {
protected $request;
/**
* @param IL10N $l
* @param IRequest $request
*/
public function __construct(IRequest $request) {
public function __construct(IL10N $l, IRequest $request) {
parent::__construct($l);
$this->request = $request;
}

View File

@ -89,11 +89,11 @@ class UserGroupMembership implements ICheck {
*/
public function validateCheck($operator, $value) {
if (!in_array($operator, ['is', '!is'])) {
throw new \UnexpectedValueException($this->l->t('Operator %s is invalid', $operator), 1);
throw new \UnexpectedValueException($this->l->t('The given operator is invalid'), 1);
}
if (!$this->groupManager->groupExists($value)) {
throw new \UnexpectedValueException($this->l->t('Group %s does not exist', $value), 2);
throw new \UnexpectedValueException($this->l->t('The given group does not exist'), 2);
}
}

View File

@ -24,6 +24,30 @@ namespace OCA\WorkflowEngine\Tests\Check;
class AbstractStringCheckTest extends \Test\TestCase {
protected function getCheckMock() {
$l = $this->getMockBuilder('OCP\IL10N')
->disableOriginalConstructor()
->getMock();
$l->expects($this->any())
->method('t')
->willReturnCallback(function($string, $args) {
return sprintf($string, $args);
});
$check = $this->getMockBuilder('OCA\WorkflowEngine\Check\AbstractStringCheck')
->setConstructorArgs([
$l,
])
->setMethods([
'setPath',
'executeCheck',
'getActualValue',
])
->getMock();
return $check;
}
public function dataExecuteStringCheck() {
return [
['is', 'same', 'same', true],
@ -46,13 +70,7 @@ class AbstractStringCheckTest extends \Test\TestCase {
* @param bool $expected
*/
public function testExecuteStringCheck($operation, $checkValue, $actualValue, $expected) {
$check = $this->getMockBuilder('OCA\WorkflowEngine\Check\AbstractStringCheck')
->setMethods([
'setPath',
'executeCheck',
'getActualValue',
])
->getMock();
$check = $this->getCheckMock();
/** @var \OCA\WorkflowEngine\Check\AbstractStringCheck $check */
$this->assertEquals($expected, $this->invokePrivate($check, 'executeStringCheck', [$operation, $checkValue, $actualValue]));
@ -73,13 +91,7 @@ class AbstractStringCheckTest extends \Test\TestCase {
* @param string $value
*/
public function testValidateCheck($operator, $value) {
$check = $this->getMockBuilder('OCA\WorkflowEngine\Check\AbstractStringCheck')
->setMethods([
'setPath',
'executeCheck',
'getActualValue',
])
->getMock();
$check = $this->getCheckMock();
/** @var \OCA\WorkflowEngine\Check\AbstractStringCheck $check */
$check->validateCheck($operator, $value);
@ -87,10 +99,10 @@ class AbstractStringCheckTest extends \Test\TestCase {
public function dataValidateCheckInvalid() {
return [
['!!is', '', 1, 'Invalid operator'],
['less', '', 1, 'Invalid operator'],
['matches', '/Invalid(Regex/', 2, 'Invalid regex'],
['!matches', '/Invalid(Regex/', 2, 'Invalid regex'],
['!!is', '', 1, 'The given operator is invalid'],
['less', '', 1, 'The given operator is invalid'],
['matches', '/Invalid(Regex/', 2, 'The given regular expression is invalid'],
['!matches', '/Invalid(Regex/', 2, 'The given regular expression is invalid'],
];
}
@ -102,13 +114,7 @@ class AbstractStringCheckTest extends \Test\TestCase {
* @param $exceptionMessage
*/
public function testValidateCheckInvalid($operator, $value, $exceptionCode, $exceptionMessage) {
$check = $this->getMockBuilder('OCA\WorkflowEngine\Check\AbstractStringCheck')
->setMethods([
'setPath',
'executeCheck',
'getActualValue',
])
->getMock();
$check = $this->getCheckMock();
try {
/** @var \OCA\WorkflowEngine\Check\AbstractStringCheck $check */
@ -134,13 +140,7 @@ class AbstractStringCheckTest extends \Test\TestCase {
* @param bool $expected
*/
public function testMatch($pattern, $subject, $matches, $expected) {
$check = $this->getMockBuilder('OCA\WorkflowEngine\Check\AbstractStringCheck')
->setMethods([
'setPath',
'executeCheck',
'getActualValue',
])
->getMock();
$check = $this->getCheckMock();
$this->invokePrivate($check, 'matches', [$matches]);

View File

@ -27,6 +27,21 @@ class RequestRemoteAddressTest extends \Test\TestCase {
/** @var \OCP\IRequest|\PHPUnit_Framework_MockObject_MockObject */
protected $request;
/**
* @return \OCP\IL10N|\PHPUnit_Framework_MockObject_MockObject
*/
protected function getL10NMock() {
$l = $this->getMockBuilder('OCP\IL10N')
->disableOriginalConstructor()
->getMock();
$l->expects($this->any())
->method('t')
->willReturnCallback(function ($string, $args) {
return sprintf($string, $args);
});
return $l;
}
protected function setUp() {
parent::setUp();
@ -52,7 +67,7 @@ class RequestRemoteAddressTest extends \Test\TestCase {
* @param bool $expected
*/
public function testExecuteCheckMatchesIPv4($value, $ip, $expected) {
$check = new \OCA\WorkflowEngine\Check\RequestRemoteAddress($this->request);
$check = new \OCA\WorkflowEngine\Check\RequestRemoteAddress($this->getL10NMock(), $this->request);
$this->request->expects($this->once())
->method('getRemoteAddress')
@ -68,7 +83,7 @@ class RequestRemoteAddressTest extends \Test\TestCase {
* @param bool $expected
*/
public function testExecuteCheckNotMatchesIPv4($value, $ip, $expected) {
$check = new \OCA\WorkflowEngine\Check\RequestRemoteAddress($this->request);
$check = new \OCA\WorkflowEngine\Check\RequestRemoteAddress($this->getL10NMock(), $this->request);
$this->request->expects($this->once())
->method('getRemoteAddress')
@ -96,7 +111,7 @@ class RequestRemoteAddressTest extends \Test\TestCase {
* @param bool $expected
*/
public function testExecuteCheckMatchesIPv6($value, $ip, $expected) {
$check = new \OCA\WorkflowEngine\Check\RequestRemoteAddress($this->request);
$check = new \OCA\WorkflowEngine\Check\RequestRemoteAddress($this->getL10NMock(), $this->request);
$this->request->expects($this->once())
->method('getRemoteAddress')
@ -112,7 +127,7 @@ class RequestRemoteAddressTest extends \Test\TestCase {
* @param bool $expected
*/
public function testExecuteCheckNotMatchesIPv6($value, $ip, $expected) {
$check = new \OCA\WorkflowEngine\Check\RequestRemoteAddress($this->request);
$check = new \OCA\WorkflowEngine\Check\RequestRemoteAddress($this->getL10NMock(), $this->request);
$this->request->expects($this->once())
->method('getRemoteAddress')

View File

@ -27,6 +27,21 @@ class RequestTimeTest extends \Test\TestCase {
/** @var \OCP\AppFramework\Utility\ITimeFactory|\PHPUnit_Framework_MockObject_MockObject */
protected $timeFactory;
/**
* @return \OCP\IL10N|\PHPUnit_Framework_MockObject_MockObject
*/
protected function getL10NMock() {
$l = $this->getMockBuilder('OCP\IL10N')
->disableOriginalConstructor()
->getMock();
$l->expects($this->any())
->method('t')
->willReturnCallback(function ($string, $args) {
return sprintf($string, $args);
});
return $l;
}
protected function setUp() {
parent::setUp();
@ -72,7 +87,7 @@ class RequestTimeTest extends \Test\TestCase {
* @param bool $expected
*/
public function testExecuteCheckIn($value, $timestamp, $expected) {
$check = new \OCA\WorkflowEngine\Check\RequestTime($this->timeFactory);
$check = new \OCA\WorkflowEngine\Check\RequestTime($this->getL10NMock(), $this->timeFactory);
$this->timeFactory->expects($this->once())
->method('getTime')
@ -88,7 +103,7 @@ class RequestTimeTest extends \Test\TestCase {
* @param bool $expected
*/
public function testExecuteCheckNotIn($value, $timestamp, $expected) {
$check = new \OCA\WorkflowEngine\Check\RequestTime($this->timeFactory);
$check = new \OCA\WorkflowEngine\Check\RequestTime($this->getL10NMock(), $this->timeFactory);
$this->timeFactory->expects($this->once())
->method('getTime')
@ -99,9 +114,9 @@ class RequestTimeTest extends \Test\TestCase {
public function dataValidateCheck() {
return [
['in', json_encode(['08:00 Europe/Berlin', '17:00 Europe/Berlin'])],
['!in', json_encode(['08:00 Europe/Berlin', '17:00 America/North_Dakota/Beulah'])],
['in', json_encode(['08:00 America/Port-au-Prince', '17:00 America/Argentina/San_Luis'])],
['in', '["08:00 Europe/Berlin","17:00 Europe/Berlin"]'],
['!in', '["08:00 Europe/Berlin","17:00 America/North_Dakota/Beulah"]'],
['in', '["08:00 America/Port-au-Prince","17:00 America/Argentina/San_Luis"]'],
];
}
@ -111,18 +126,19 @@ class RequestTimeTest extends \Test\TestCase {
* @param string $value
*/
public function testValidateCheck($operator, $value) {
$check = new \OCA\WorkflowEngine\Check\RequestTime($this->timeFactory);
$check = new \OCA\WorkflowEngine\Check\RequestTime($this->getL10NMock(), $this->timeFactory);
$check->validateCheck($operator, $value);
}
public function dataValidateCheckInvalid() {
return [
['!!in', json_encode(['08:00 Europe/Berlin', '17:00 Europe/Berlin']), 1, 'Invalid operator'],
['in', json_encode(['28:00 Europe/Berlin', '17:00 Europe/Berlin']), 2, 'Invalid time limits'],
['in', json_encode(['08:00 Europa/Berlin', '17:00 Europe/Berlin']), 3, 'Invalid timezone1'],
['in', json_encode(['08:00 Europe/Berlin', '17:00 Europa/Berlin']), 3, 'Invalid timezone2'],
['in', json_encode(['08:00 Europe/Bearlin', '17:00 Europe/Berlin']), 3, 'Invalid timezone1'],
['in', json_encode(['08:00 Europe/Berlin', '17:00 Europe/Bearlin']), 3, 'Invalid timezone2'],
['!!in', '["08:00 Europe/Berlin","17:00 Europe/Berlin"]', 1, 'The given operator is invalid'],
['in', '["28:00 Europe/Berlin","17:00 Europe/Berlin"]', 2, 'The given time span is invalid'],
['in', '["08:00 Europe/Berlin","27:00 Europe/Berlin"]', 2, 'The given time span is invalid'],
['in', '["08:00 Europa/Berlin","17:00 Europe/Berlin"]', 3, 'The given start time is invalid'],
['in', '["08:00 Europe/Berlin","17:00 Europa/Berlin"]', 4, 'The given end time is invalid'],
['in', '["08:00 Europe/Bearlin","17:00 Europe/Berlin"]', 3, 'The given start time is invalid'],
['in', '["08:00 Europe/Berlin","17:00 Europe/Bearlin"]', 4, 'The given end time is invalid'],
];
}
@ -134,7 +150,7 @@ class RequestTimeTest extends \Test\TestCase {
* @param string $exceptionMessage
*/
public function testValidateCheckInvalid($operator, $value, $exceptionCode, $exceptionMessage) {
$check = new \OCA\WorkflowEngine\Check\RequestTime($this->timeFactory);
$check = new \OCA\WorkflowEngine\Check\RequestTime($this->getL10NMock(), $this->timeFactory);
try {
$check->validateCheck($operator, $value);