Skip to content

Commit

Permalink
Merge cfd463b into 4f3b4bb
Browse files Browse the repository at this point in the history
  • Loading branch information
christianlupus authored May 30, 2022
2 parents 4f3b4bb + cfd463b commit 0450ec9
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 8 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
[#1014](https://github.com/nextcloud/cookbook/pull/1014) @christianlupus
- Correct transifex translations
[#1024](https://github.com/nextcloud/cookbook/pull/1024) @christianlupus
- Correct singular/plural translations
[#1026](https://github.com/nextcloud/cookbook/pull/1026) @christianlupus
- Update eslint-plugin-vue

### Documentation
Expand Down
8 changes: 4 additions & 4 deletions lib/Helper/HtmlToDomParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,20 +195,20 @@ private function logAllErrors(array $groupedErrors, string $url): void {
}

private function logWarning(int $code, array $group, string $url): void {
$msg = $this->l->t('Warning %u occurred %u times while parsing %s.', [$code, $group['count'], $url]);
$this->logger->notice($this->formatError($msg, $group['first']));
$msg = $this->l->n('Warning %u occurred while parsing %s.', 'Warning %u occurred %n times while parsing %s.', $group['count'], [$code, $url]);
$this->logger->info($this->formatError($msg, $group['first']));
$this->state = max($this->state, self::PARSING_WARNING);
}


private function logError(int $code, array $group, string $url): void {
$msg = $this->l->t('Error %u occurred %u times while parsing %s.', [$code, $group['count'], $url]);
$msg = $this->l->n('Error %u occurred while parsing %s.', 'Error %u occurred %n times while parsing %s.', $group['count'], [$code, $url]);
$this->logger->warning($this->formatError($msg, $group['first']));
$this->state = max($this->state, self::PARSING_ERROR);
}

private function logFatalError(int $code, array $group, string $url): void {
$msg = $this->l->t('Fatal error %u occurred %u times while parsing %s.', [$code, $group['count'], $url]);
$msg = $this->l->n('Fatal error %u occurred while parsing %s.', 'Fatal error %u occurred %n times while parsing %s.', $group['count'], [$code, $url]);
$this->logger->error($this->formatError($msg, $group['first']));
$this->state = max($this->state, self::PARSING_FATAL_ERROR);
}
Expand Down
129 changes: 125 additions & 4 deletions tests/Unit/Helper/HtmlToDomParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public function testParsing($successDomParsing, $stateAtEnd, $errors, $numErrors
->method('clearErrors');

$this->logger->expects($this->exactly($numErrors[2]))
->method('notice');
->method('info');
$this->logger->expects($this->exactly($numErrors[1]))
->method('warning');
$this->logger->expects($this->exactly($numErrors[0]))
Expand Down Expand Up @@ -274,13 +274,15 @@ public function testLogging(): void {

$url = 'http://example.com/recipe';

$this->l->method('n')->willReturnArgument(1);

$this->logger->expects($this->exactly(3))
->method('notice');
->method('info');
$this->logger->expects($this->exactly(2))
->method('warning')
->withConsecutive(
["libxml: Error %u occurred %u times while parsing %s. First time it occurred in line %u and column %u: The message"],
["libxml: Error %u occurred %u times while parsing %s. First time it occurred in line %u and column %u: The message"]
["libxml: Error %u occurred %n times while parsing %s. First time it occurred in line %u and column %u: The message"],
["libxml: Error %u occurred %n times while parsing %s. First time it occurred in line %u and column %u: The message"]
);
$this->logger->expects($this->exactly(0))
->method('error');
Expand All @@ -292,6 +294,125 @@ public function testLogging(): void {
$this->sut->loadHtmlString($dom, $url, $html);
}

public function dpSingleLogging() {
return [
[LIBXML_ERR_WARNING, true, false, false],
[LIBXML_ERR_ERROR, false, true, false],
[LIBXML_ERR_FATAL, false, false, true],
];
}

/**
* @dataProvider dpSingleLogging
*/
public function testSingleLogging($errorLevel, $logWarn, $logErr, $logCrit) {
/**
* @var MockObject|DOMDocument $dom
*/
$dom = $this->createMock(DOMDocument::class);
$dom->method('loadHtml')->willReturn(true);

$this->xmlMock->method('getErrors')->willReturn([
$this->getXMLError(2, $errorLevel, '/file', 1, 2, 'The message'),
]);

$url = 'http://example.com/recipe';

$this->l->method('n')->willReturnArgument(0);

if ($logWarn) {
$this->logger->expects($this->once())->method('info')
->with(
'libxml: Warning %u occurred while parsing %s. '.
'First time it occurred in line %u and column %u: The message',
[]
);
$this->logger->expects($this->never())->method('warning');
$this->logger->expects($this->never())->method('error');
}
if ($logErr) {
$this->logger->expects($this->never())->method('info');
$this->logger->expects($this->once())->method('warning')
->with(
'libxml: Error %u occurred while parsing %s. '.
'First time it occurred in line %u and column %u: The message',
[]
);
$this->logger->expects($this->never())->method('error');
}
if ($logCrit) {
$this->logger->expects($this->never())->method('info');
$this->logger->expects($this->never())->method('warning');
$this->logger->expects($this->once())->method('error')
->with(
'libxml: Fatal error %u occurred while parsing %s. '.
'First time it occurred in line %u and column %u: The message',
[]
);
}

$html = 'Foo Bar Baz';

$this->sut->loadHtmlString($dom, $url, $html);
$this->assertEquals($errorLevel, $this->sut->getState());
}

/**
* @dataProvider dpSingleLogging
*/
public function testMultipleLogging($errorLevel, $logWarn, $logErr, $logCrit) {
/**
* @var MockObject|DOMDocument $dom
*/
$dom = $this->createMock(DOMDocument::class);
$dom->method('loadHtml')->willReturn(true);

$this->xmlMock->method('getErrors')->willReturn([
$this->getXMLError(2, $errorLevel, '/file', 1, 2, 'The message'),
$this->getXMLError(2, $errorLevel, '/file', 10, 20, 'The new message'),
]);

$url = 'http://example.com/recipe';

$this->l->method('n')->willReturnArgument(1);

if ($logWarn) {
$this->logger->expects($this->once())->method('info')
->with(
'libxml: Warning %u occurred %n times while parsing %s. '.
'First time it occurred in line %u and column %u: The message',
[]
);
$this->logger->expects($this->never())->method('warning');
$this->logger->expects($this->never())->method('error');
}
if ($logErr) {
$this->logger->expects($this->never())->method('info');
$this->logger->expects($this->once())->method('warning')
->with(
'libxml: Error %u occurred %n times while parsing %s. '.
'First time it occurred in line %u and column %u: The message',
[]
);
$this->logger->expects($this->never())->method('error');
}
if ($logCrit) {
$this->logger->expects($this->never())->method('info');
$this->logger->expects($this->never())->method('warning');
$this->logger->expects($this->once())->method('error')
->with(
'libxml: Fatal error %u occurred %n times while parsing %s. '.
'First time it occurred in line %u and column %u: The message',
[]
);
}

$html = 'Foo Bar Baz';

$this->sut->loadHtmlString($dom, $url, $html);
$this->assertEquals($errorLevel, $this->sut->getState());
}

public function triggerXmlInternalErrors(bool $param): bool {
return $this->xmlMock->useInternalErrors($param);
}
Expand Down

0 comments on commit 0450ec9

Please sign in to comment.