Commit 7ec526e9 authored by alexpott's avatar alexpott

Issue #2050535 by jhedstrom: Expand phpunit tests for \Drupal\Component\Utility\NestedArray.

parent 94cf43e9
......@@ -2,7 +2,7 @@
/**
* @file
* Contains \Drupal\Core\NestedArrayUnitTest.
* Contains \Drupal\Core\NestedArrayTest.
*/
namespace Drupal\Tests\Core;
......@@ -15,7 +15,7 @@
*
* @group System
*/
class NestedArrayUnitTest extends UnitTestCase {
class NestedArrayTest extends UnitTestCase {
/**
* Form array to check.
......@@ -35,7 +35,7 @@ public static function getInfo() {
);
}
function setUp() {
public function setUp() {
parent::setUp();
// Create a form structure with a nested element.
......@@ -50,7 +50,7 @@ function setUp() {
/**
* Tests getting nested array values.
*/
function testGetValue() {
public function testGetValue() {
// Verify getting a value of a nested element.
$value = NestedArray::getValue($this->form, $this->parents);
$this->assertEquals($value['#value'], 'Nested element', 'Nested element value found.');
......@@ -78,7 +78,7 @@ function testGetValue() {
/**
* Tests setting nested array values.
*/
function testSetValue() {
public function testSetValue() {
$new_value = array(
'#value' => 'New value',
'#required' => TRUE,
......@@ -90,10 +90,23 @@ function testSetValue() {
$this->assertSame($this->form['details']['element']['#required'], TRUE, 'New nested element value found.');
}
/**
* Tests force-setting values.
*/
public function testSetValueForce() {
$new_value = array(
'one',
);
$this->form['details']['non-array-parent'] = 'string';
$parents = array('details', 'non-array-parent', 'child');
NestedArray::setValue($this->form, $parents, $new_value, TRUE);
$this->assertEquals($this->form['details']['non-array-parent']['child'], $new_value, 'The nested element was not forced to the new value.');
}
/**
* Tests unsetting nested array values.
*/
function testUnsetValue() {
public function testUnsetValue() {
// Verify unsetting a non-existing nested element throws no errors and the
// non-existing key is properly reported.
$key_existed = NULL;
......@@ -113,7 +126,7 @@ function testUnsetValue() {
/**
* Tests existence of array key.
*/
function testKeyExists() {
public function testKeyExists() {
// Verify that existing key is found.
$this->assertSame(NestedArray::keyExists($this->form, $this->parents), TRUE, 'Nested key found.');
......@@ -126,7 +139,7 @@ function testKeyExists() {
/**
* Tests NestedArray::mergeDeepArray().
*/
function testMergeDeepArray() {
public function testMergeDeepArray() {
$link_options_1 = array(
'fragment' => 'x',
'attributes' => array('title' => 'X', 'class' => array('a', 'b')),
......@@ -144,5 +157,7 @@ function testMergeDeepArray() {
'html' => TRUE,
);
$this->assertSame(NestedArray::mergeDeepArray(array($link_options_1, $link_options_2)), $expected, 'NestedArray::mergeDeepArray() returned a properly merged array.');
// Test wrapper function, NestedArray::mergeDeep().
$this->assertSame(NestedArray::mergeDeep($link_options_1, $link_options_2), $expected, 'NestedArray::mergeDeepArray() returned a properly merged array.');
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment