View source
<?php
declare (strict_types=1);
namespace Drupal\Tests\Core\Form;
use Drupal\Core\Form\FormStateValuesTrait;
use Drupal\Tests\UnitTestCase;
class FormStateValuesTraitTest extends UnitTestCase {
public function testSetValueForElement() {
$element = [
'#parents' => [
'foo',
'bar',
],
];
$value = $this
->randomMachineName();
$form_state = new FormStateValuesTraitStub();
$form_state
->setValueForElement($element, $value);
$expected = [
'foo' => [
'bar' => $value,
],
];
$this
->assertSame($expected, $form_state
->getValues());
}
public function testGetValue($key, $expected, $default = NULL) {
$form_state = (new FormStateValuesTraitStub())
->setValues([
'foo' => 'one',
'bar' => [
'baz' => 'two',
],
]);
$this
->assertSame($expected, $form_state
->getValue($key, $default));
}
public static function providerGetValue() {
$data = [];
$data[] = [
'foo',
'one',
];
$data[] = [
[
'bar',
'baz',
],
'two',
];
$data[] = [
[
'foo',
'bar',
'baz',
],
NULL,
];
$data[] = [
'baz',
'baz',
'baz',
];
$data[] = [
NULL,
[
'foo' => 'one',
'bar' => [
'baz' => 'two',
],
],
];
return $data;
}
public function testGetValueModifyReturn() {
$initial_values = $values = [
'foo' => 'one',
'bar' => [
'baz' => 'two',
],
];
$form_state = (new FormStateValuesTraitStub())
->setValues($values);
$value =& $form_state
->getValue(NULL);
$this
->assertSame($initial_values, $value);
$value = [
'bing' => 'bang',
];
$this
->assertSame([
'bing' => 'bang',
], $form_state
->getValues());
$this
->assertSame('bang', $form_state
->getValue('bing'));
$this
->assertSame([
'bing' => 'bang',
], $form_state
->getValue(NULL));
}
public function testSetValue($key, $value, $expected) {
$form_state = (new FormStateValuesTraitStub())
->setValues([
'bar' => 'wrong',
]);
$form_state
->setValue($key, $value);
$this
->assertSame($expected, $form_state
->getValues());
}
public static function providerSetValue() {
$data = [];
$data[] = [
'foo',
'one',
[
'bar' => 'wrong',
'foo' => 'one',
],
];
$data[] = [
[
'bar',
'baz',
],
'two',
[
'bar' => [
'baz' => 'two',
],
],
];
$data[] = [
[
'foo',
'bar',
'baz',
],
NULL,
[
'bar' => 'wrong',
'foo' => [
'bar' => [
'baz' => NULL,
],
],
],
];
return $data;
}
public function testHasValue($key, $expected) {
$form_state = (new FormStateValuesTraitStub())
->setValues([
'foo' => 'one',
'bar' => [
'baz' => 'two',
],
'true' => TRUE,
'false' => FALSE,
'null' => NULL,
]);
$this
->assertSame($expected, $form_state
->hasValue($key));
}
public static function providerHasValue() {
$data = [];
$data[] = [
'foo',
TRUE,
];
$data[] = [
[
'bar',
'baz',
],
TRUE,
];
$data[] = [
[
'foo',
'bar',
'baz',
],
FALSE,
];
$data[] = [
'true',
TRUE,
];
$data[] = [
'false',
TRUE,
];
$data[] = [
'null',
FALSE,
];
return $data;
}
public function testIsValueEmpty($key, $expected) {
$form_state = (new FormStateValuesTraitStub())
->setValues([
'foo' => 'one',
'bar' => [
'baz' => 'two',
],
'true' => TRUE,
'false' => FALSE,
'null' => NULL,
]);
$this
->assertSame($expected, $form_state
->isValueEmpty($key));
}
public static function providerIsValueEmpty() {
$data = [];
$data[] = [
'foo',
FALSE,
];
$data[] = [
[
'bar',
'baz',
],
FALSE,
];
$data[] = [
[
'foo',
'bar',
'baz',
],
TRUE,
];
$data[] = [
'true',
FALSE,
];
$data[] = [
'false',
TRUE,
];
$data[] = [
'null',
TRUE,
];
return $data;
}
}
class FormStateValuesTraitStub {
use FormStateValuesTrait;
protected $values = [];
public function &getValues() {
return $this->values;
}
}