Verified Commit 1c017c0e authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3427174 by longwave, quietone, dineshkumarbollu, mondrake, alexpott:...

Issue #3427174 by longwave, quietone, dineshkumarbollu, mondrake, alexpott: Throw exception when calling NestedArray::setValue() when parents reference a non-array value instead of causing a PHP error
parent bcf45f5d
Loading
Loading
Loading
Loading
Loading
+0 −6
Original line number Diff line number Diff line
@@ -2146,12 +2146,6 @@
	'count' => 1,
	'path' => __DIR__ . '/tests/Drupal/Tests/Composer/ComposerTest.php',
];
$ignoreErrors[] = [
	'message' => '#^Call to deprecated method expectError\\(\\) of class PHPUnit\\\\Framework\\\\TestCase\\:
https\\://github\\.com/sebastianbergmann/phpunit/issues/5062$#',
	'count' => 1,
	'path' => __DIR__ . '/tests/Drupal/Tests/Core/Config/ConfigTest.php',
];
$ignoreErrors[] = [
	'message' => '#^Trying to mock an undefined method getRevisionId\\(\\) on class Drupal\\\\Tests\\\\Core\\\\Entity\\\\UrlTestEntity\\.$#',
	'count' => 1,
+5 −2
Original line number Diff line number Diff line
@@ -138,7 +138,7 @@ public static function &getValue(array &$array, array $parents, &$key_exists = N
   * @param bool $force
   *   (optional) If TRUE, the value is forced into the structure even if it
   *   requires the deletion of an already existing non-array parent value. If
   *   FALSE, PHP throws an error if trying to add into a value that is not an
   *   FALSE, throws an exception if trying to add into a value that is not an
   *   array. Defaults to FALSE.
   *
   * @see NestedArray::unsetValue()
@@ -149,7 +149,10 @@ public static function setValue(array &$array, array $parents, $value, $force =
    foreach ($parents as $parent) {
      // PHP auto-creates container arrays and NULL entries without error if $ref
      // is NULL, but throws an error if $ref is set, but not an array.
      if ($force && isset($ref) && !is_array($ref)) {
      if (isset($ref) && !is_array($ref)) {
        if (!$force) {
          throw new \LogicException('Cannot create key "' . $parent . '" on non-array value.');
        }
        $ref = [];
      }
      $ref = &$ref[$parent];
+4 −0
Original line number Diff line number Diff line
@@ -87,6 +87,10 @@ public function testSetValue() {
    NestedArray::setValue($this->form, $this->parents, $new_value);
    $this->assertSame('New value', $this->form['details']['element']['#value'], 'Changed nested element value found.');
    $this->assertTrue($this->form['details']['element']['#required'], 'New nested element value found.');

    $this->expectException(\LogicException::class);
    $this->expectExceptionMessage('Cannot create key "child" on non-array value.');
    NestedArray::setValue($this->form, ['details', 'element', '#value', 'child'], $new_value);
  }

  /**
+2 −1
Original line number Diff line number Diff line
@@ -276,7 +276,8 @@ public function testSetIllegalOffsetValue() {
    $this->config->set('testData', 1);

    // Attempt to treat the single value as a nested item.
    $this->expectError();
    $this->expectException(\LogicException::class);
    $this->expectExceptionMessage('Cannot create key "illegalOffset" on non-array value.');
    $this->config->set('testData.illegalOffset', 1);
  }