Skip to content

Commit

Permalink
Testing that richtext conversion logs warnings correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
vidarl committed Jun 20, 2018
1 parent e546df2 commit 926ac73
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 3 deletions.
72 changes: 69 additions & 3 deletions tests/lib/FieldType/Converter/RichTextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@
use eZ\Publish\API\Repository\LocationService;
use eZ\Publish\API\Repository\Values\Content\ContentInfo;
use eZ\Publish\API\Repository\Values\Content\Location;
use Psr\Log\NullLogger;

class RichTextTest extends TestCase
{
/**
* @var []
*/
private $expectedLogWarningWithWildcard = [];

/**
* @param string $xml
* @param bool $isPath
Expand Down Expand Up @@ -54,8 +60,12 @@ public function providerForTestConvert()
foreach (glob(__DIR__ . '/_fixtures/richtext/input/*.xml') as $inputFilePath) {
$basename = basename($inputFilePath, '.xml');
$outputFilePath = __DIR__ . "/_fixtures/richtext/output/{$basename}.xml";
$logFilePath = __DIR__ . "/_fixtures/richtext/log/{$basename}.log";
if (!file_exists($logFilePath)) {
$logFilePath = null;
}

$map[] = [$inputFilePath, $outputFilePath];
$map[] = [$inputFilePath, $outputFilePath, $logFilePath];
}

return $map;
Expand Down Expand Up @@ -123,18 +133,74 @@ private function createApiRepositoryStub()
return $apiRepositoryStub;
}

/**
* Some log messages contains random ids generated by XLST
* For Expected log messages that contains a *, will only match against substring prior to that *
* Example :
* expectedlog : message with generated id=*
* actuallog : message with generated id=123.
*
* Note : Must be public as this is a callback
*
* @param $logMessage
* @return bool
*/
public function validatelogWarningWithWildcard($logMessage)
{
$this->assertGreaterThan(0, count($this->expectedLogWarningWithWildcard), "No more log warnings with wildcard expected, got log message $logMessage");

$nextWildcardLog = array_shift($this->expectedLogWarningWithWildcard);
$expectedLogMessage = substr($nextWildcardLog, 0, strpos($nextWildcardLog, '*'));

$this->assertEquals($expectedLogMessage, substr($logMessage, 0, strlen($expectedLogMessage)), 'Actual log message do not match the expected one');

return true;
}

private function createLoggerStub($logFilePath)
{
$loggerStub = $this->createMock(NullLogger::class);

if ($logFilePath !== null) {
$log = file_get_contents($logFilePath);
$logLines = explode("\n", $log);
$logNo = 0;
foreach ($logLines as $line) {
if ($line === '') {
continue;
}
if (strpos($line, '*') !== false) {
$this->expectedLogWarningWithWildcard[] = $line;
$loggerStub->expects($this->at($logNo++))
->method('warning')
->with($this->callback(array($this, 'validatelogWarningWithWildcard')));
} else {
$loggerStub->expects($this->at($logNo++))
->method('warning')
->with($line);
}
}
} else {
$loggerStub->expects($this->never())
->method('warning');
}

return $loggerStub;
}

/**
* @param string $inputFilePath
* @param string $outputFilePath
*
* @dataProvider providerForTestConvert
*/
public function testConvert($inputFilePath, $outputFilePath)
public function testConvert($inputFilePath, $outputFilePath, $logFilePath)
{
$apiRepositoryStub = $this->createApiRepositoryStub();
$loggerStub = $this->createLoggerStub($logFilePath);

$inputDocument = $this->createDocument($inputFilePath);
$richText = new RichText($apiRepositoryStub);
$richText = new RichText($apiRepositoryStub, $loggerStub);
$richText->setImageContentTypes([27]);

$result = $richText->convert($inputDocument, true, true);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Duplicated id in original ezxmltext for contentobject_attribute.id=[unknown], automatically generated new id : inv5 --> duplicated_id_inv5_*
Duplicated id in original ezxmltext for contentobject_attribute.id=[unknown], automatically generated new id : inv5 --> duplicated_id_inv5_*
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Duplicated id in original ezxmltext for contentobject_attribute.id=[unknown], automatically generated new id : myembed_id --> duplicated_id_myembed_id_idm*
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Duplicated id in original ezxmltext for contentobject_attribute.id=[unknown], automatically generated new id : anchor --> duplicated_id_anchor_idm*
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Replaced non-validating id value in richtext for contentobject_attribute.id=[unknown], changed from : 1name --> rewrite_1name
Replaced non-validating id value in richtext for contentobject_attribute.id=[unknown], changed from : -1name --> rewrite_-1name
Replaced non-validating id value in richtext for contentobject_attribute.id=[unknown], changed from : #aname --> rewrite__aname
Replaced non-validating id value in richtext for contentobject_attribute.id=[unknown], changed from : a@name --> rewrite_a_name
Replaced non-validating id value in richtext for contentobject_attribute.id=[unknown], changed from : an£ame --> rewrite_an__ame
Replaced non-validating id value in richtext for contentobject_attribute.id=[unknown], changed from : aname[ --> rewrite_aname_
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Warning: ezxmltext for contentobject_attribute.id=contains embed or embed-inline tag(s) without node_id or object_id
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Warning: ezxmltext for contentobject_attribute.id=contains embed or embed-inline tag(s) without node_id or object_id

0 comments on commit 926ac73

Please sign in to comment.