Remove \Drupal\Tests\cdn\Unit\File\FileUrlGeneratorTest::getConfigFactoryStub() once the CDN module can require Drupal 8.5
>>> [!note] Migrated issue
<!-- Drupal.org comment -->
<!-- Migrated from issue #2934643. -->
Reported by: [wim leers](https://www.drupal.org/user/99777)
>>>
<p>Quoting myself from <span class="drupalorg-gitlab-issue-link project-issue-status-info project-issue-status-7"><a href="https://www.drupal.org/project/drupal/issues/2862248" title="Status: Closed (fixed)">#2862248: UnitTestCase::getConfigFactoryStub() doesn't accept dotted keys</a></span>:</p>
<blockquote><p>
Hah! While scanning the RTBC queue, this one caught my eye.</p>
<p>I also ran into this in #2708787, in the CDN contrib module that I maintain. I added a work-around in <span class="drupalorg-gitlab-issue-link drupalorg-gitlab-link-wrapper"><a href="https://git.drupalcode.org/project/cdn/-/work_items/2708787" class="drupalorg-gitlab-link">https://git.drupalcode.org/project/cdn/-/work_items/2708787</a></span>, but then forgot to report it back to the Drupal core issue queue.</p>
<p>I overrode <code>UnitTestCase::getConfigFactoryStub()</code> in my unit test, with the following interdiff:</p>
<pre>diff --git a/core/tests/Drupal/Tests/UnitTestCase.php b/core/tests/Drupal/Tests/UnitTestCase.php<br>index 58173e5..030b5e4 100644<br>--- a/core/tests/Drupal/Tests/UnitTestCase.php<br>+++ b/core/tests/Drupal/Tests/UnitTestCase.php<br>@@ -111,20 +111,24 @@ public function getConfigFactoryStub(array $configs = []) {<br> $config_editable_map = [];<br> // Construct the desired configuration object stubs, each with its own<br> // desired return map.<br>- foreach ($configs as $config_name => $config_values) {<br>- $map = [];<br>- foreach ($config_values as $key => $value) {<br>- $map[] = [$key, $value];<br>- }<br>- // Also allow to pass in no argument.<br>- $map[] = ['', $config_values];<br>+ foreach ($configs as $config_name => $map) {<br>+ $get = function ($key) use ($map) {<br>+ $parts = explode('.', $key);<br>+ if (count($parts) == 1) {<br>+ return isset($map[$key]) ? $map[$key] : NULL;<br>+ }<br>+ else {<br>+ $value = NestedArray::getValue($map, $parts, $key_exists);<br>+ return $key_exists ? $value : NULL;<br>+ }<br>+ };<br> <br> $immutable_config_object = $this->getMockBuilder('Drupal\Core\Config\ImmutableConfig')<br> ->disableOriginalConstructor()<br> ->getMock();<br> $immutable_config_object->expects($this->any())<br> ->method('get')<br>- ->will($this->returnValueMap($map));<br>+ ->willReturnCallback($get);<br> $config_get_map[] = [$config_name, $immutable_config_object];<br> <br> $mutable_config_object = $this->getMockBuilder('Drupal\Core\Config\Config')<br>@@ -132,7 +136,7 @@ public function getConfigFactoryStub(array $configs = []) {<br> ->getMock();<br> $mutable_config_object->expects($this->any())<br> ->method('get')<br>- ->will($this->returnValueMap($map));<br>+ ->willReturnCallback($get);<br> $config_editable_map[] = [$config_name, $mutable_config_object];<br> }<br> // Construct a config factory with the array of configuration object stubs</pre><p>And had this docblock for my override:</p>
<pre> /**<br> * {@inheritdoc}<br> *<br> * Overridden, because the way ImmutableConfig::get() is mocked, does not<br> * match the actual implementation, which then causes tests to fail.<br> */</pre><p>Glad to see this being solved in Drupal core! 🎉
</p></blockquote>
> Related issue: [Issue #2708787](https://www.drupal.org/node/2708787)
> Related issue: [Issue #2862248](https://www.drupal.org/node/2862248)
issue