Skip to content

Commit

Permalink
Ensure find_string & replace_string size are equal
Browse files Browse the repository at this point in the history
That validation is performed in `parseLines` so the size of them can't mismatch when merging configuration for example.
A warning log is raised in case of mismatch.
  • Loading branch information
j0k3r committed Jan 7, 2022
1 parent 9d46a68 commit 474bbe1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/SiteConfig/ConfigBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -332,11 +332,6 @@ public function mergeConfig(SiteConfig $currentConfig, SiteConfig $newConfig)
// replace_string: <img
// To fix that issue, we combine find & replace as key & value in one array, we merge them and then rebuild find & replace string in the current config

// in case of bad configuration
if (\count($currentConfig->find_string) !== \count($currentConfig->replace_string)) {
return $currentConfig;
}

$findReplaceCurrentConfig = array_combine($currentConfig->find_string, $currentConfig->replace_string);
$findReplaceNewConfig = array_combine($newConfig->find_string, $newConfig->replace_string);
$findReplaceMerged = array_merge((array) $findReplaceCurrentConfig, (array) $findReplaceNewConfig);
Expand Down Expand Up @@ -412,6 +407,14 @@ public function parseLines(array $lines)
}
}

// in case of bad configuration
if (\count($config->find_string) !== \count($config->replace_string)) {
$this->logger->warning('find_string & replace_string size mismatch, check the site config to fix it', ['find_string' => $config->find_string, 'replace_string' => $config->replace_string]);

$config->find_string = [];
$config->replace_string = [];
}

return $config;
}

Expand Down
22 changes: 22 additions & 0 deletions tests/SiteConfig/ConfigBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,4 +274,26 @@ public function testMergeConfigMultipleTimes(): void
$this->assertCount(2, $config4->find_string);
$this->assertCount(2, $config4->replace_string);
}

public function testCleanupFindReplaceString(): void
{
$logger = new Logger('foo');
$handler = new TestHandler();
$logger->pushHandler($handler);

$configBuilder = new ConfigBuilder(['site_config' => [__DIR__]]);
$configBuilder->setLogger($logger);

$configActual = $configBuilder->parseLines([
'find_string: src="/assets/img/highlight_ph.png"',
]);

$this->assertCount(0, $configActual->find_string);
$this->assertCount(0, $configActual->replace_string);

$records = $handler->getRecords();

$this->assertSame('find_string & replace_string size mismatch, check the site config to fix it', $records[0]['message']);
$this->assertCount(2, $records[0]['context']);
}
}

0 comments on commit 474bbe1

Please sign in to comment.