Skip to content

Commit

Permalink
Merge pull request #1487 from SemanticMediaWiki/regex
Browse files Browse the repository at this point in the history
Suppress error on regex compilation, refs #1417
  • Loading branch information
mwjames committed Mar 31, 2016
2 parents 352362f + 3b75187 commit 5a35ba0
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,12 @@ private function doPregMatch( $pattern, $dataValue, $reference ) {
$pattern = $pattern . '/';
}

if ( !preg_match( $pattern, $dataValue->getDataItem()->getSortKey() ) ) {
// Convert escaping
$pattern = str_replace( "/\\", "\\", $pattern );

// @to suppress any errors caused by an invalid regex, the user should
// test the expression before making it available
if ( !@preg_match( $pattern, $dataValue->getDataItem()->getSortKey() ) ) {
$dataValue->addErrorMsg(
array(
'smw-datavalue-allows-pattern-mismatch',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,16 @@ public function testCanConstruct() {
);
}

public function testValidateUsingAMockedQueryEngine() {
/**
* @dataProvider allowedPatternProvider
*/
public function testPatternUsingMockedDataValue( $allowedPattern, $testString, $expectedConstraintViolation ) {

$property = $this->dataItemFactory->newDIProperty( 'Has allowed pattern' );

$this->mediaWikiNsContentReader->expects( $this->once() )
->method( 'read' )
->will( $this->returnValue( " \nFoo|^(Bar|Foo bar)$/e\n" ) );
->will( $this->returnValue( $allowedPattern ) );

$this->propertySpecificationLookup->expects( $this->any() )
->method( 'getAllowedPatternFor' )
Expand All @@ -79,7 +82,7 @@ public function testValidateUsingAMockedQueryEngine() {

$dataValue->expects( $this->any() )
->method( 'getDataItem' )
->will( $this->returnValue( $this->dataItemFactory->newDIBlob( 'Foo bar' ) ) );
->will( $this->returnValue( $this->dataItemFactory->newDIBlob( $testString ) ) );

$instance = new PatternConstraintValueValidator();

Expand All @@ -89,9 +92,42 @@ public function testValidateUsingAMockedQueryEngine() {

$instance->validate( $dataValue );

$this->assertFalse(
$this->assertEquals(
$expectedConstraintViolation,
$instance->hasConstraintViolation()
);
}

public function allowedPatternProvider() {

$provider[] = array(
" \nFoo|^(Bar|Foo bar)$/e\n",
'Foo bar',
false
);

#1 valid
$provider[] = array(
" \nFoo|(ev\d{7}\d{4})|((tt|nm|ch|co|ev)\d{7})\n",
'tt0042876',
false
);

#2 uses '/\'
$provider[] = array(
" \nFoo|(ev\d{7}/\d{4})|((tt|nm|ch|co|ev)\d{7})\n",
'tt0042876',
false
);

#3 "Compilation failed: missing )", suppress error
$provider[] = array(
" \nFoo|(ev\d{7}\d{4})|((tt|nm|ch|co|ev)\d{7}\n",
'Foo',
true
);

return $provider;
}

}

0 comments on commit 5a35ba0

Please sign in to comment.