Skip to content

Commit

Permalink
Merge pull request facebookarchive#328 from manishbisht/invalid-rule-…
Browse files Browse the repository at this point in the history
…class-fix

Invalid rule class checker in Transformer
  • Loading branch information
everton-rosario committed Apr 2, 2018
2 parents 6ad858b + bebd831 commit db20430
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/Facebook/InstantArticles/Transformer/Transformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -455,14 +455,22 @@ public function loadRules($json_file)
$class = $configuration_rule['class'];
try {
$factory_method = new \ReflectionMethod($class, 'createFrom');
$this->addRule($factory_method->invoke(null, $configuration_rule));
} catch (\ReflectionException $e) {
$factory_method =
new \ReflectionMethod(
'Facebook\\InstantArticles\\Transformer\\Rules\\'.$class,
try {
$factory_method = new \ReflectionMethod(
'Facebook\\InstantArticles\\Transformer\\Rules\\' . $class,
'createFrom'
);
$this->addRule($factory_method->invoke(null, $configuration_rule));
} catch (\ReflectionException $e) {
$this->addWarning("$class was not found");
$this->addLog(
TransformerLog::ERROR,
"$class was not found"
);
}
}
$this->addRule($factory_method->invoke(null, $configuration_rule));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,15 @@ public function testTransformerUnexpectedControlCharacterRules()

$this->assertEquals($expected_error1 == $result || $expected_error2 == $result, true);
}

public function testTransformerInvalidRuleClassName()
{
$expected_error = new TransformerLog(TransformerLog::ERROR, 'TextNodeRul was not found');
$json_file = file_get_contents(__DIR__ . '/invalid-rule-class.json');
$transformer = new Transformer();
$transformer->loadRules($json_file);
TransformerLog::setLevel(TransformerLog::DEBUG);
$result = $transformer->getLogs()[1];
$this->assertEquals($expected_error, $result);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"style_name": "mydefaultstyle",
"ads": {
"audience_network_placement_id": "1234"
},
"analytics": {
"fb_pixel_id": "4321",
"raw_html": "<script>alert('hello world!');</script><div class='block'></div>"
},
"rules": [
{
"class": "TextNodeRul"
}
]
}

0 comments on commit db20430

Please sign in to comment.