function TypedDataTest::testTypedDataListsFilter
Same name in other branches
- 9 core/tests/Drupal/KernelTests/Core/TypedData/TypedDataTest.php \Drupal\KernelTests\Core\TypedData\TypedDataTest::testTypedDataListsFilter()
- 8.9.x core/tests/Drupal/KernelTests/Core/TypedData/TypedDataTest.php \Drupal\KernelTests\Core\TypedData\TypedDataTest::testTypedDataListsFilter()
- 10 core/tests/Drupal/KernelTests/Core/TypedData/TypedDataTest.php \Drupal\KernelTests\Core\TypedData\TypedDataTest::testTypedDataListsFilter()
Tests the filter() method on typed data lists.
File
-
core/
tests/ Drupal/ KernelTests/ Core/ TypedData/ TypedDataTest.php, line 466
Class
- TypedDataTest
- Tests the functionality of all core data types.
Namespace
Drupal\KernelTests\Core\TypedDataCode
public function testTypedDataListsFilter() : void {
// Check that an all-pass filter leaves the list untouched.
$value = [
'zero',
'one',
];
$typed_data = $this->createTypedData(ListDataDefinition::create('string'), $value);
$typed_data->filter(function (TypedDataInterface $item) {
return TRUE;
});
$this->assertEquals(2, $typed_data->count());
$this->assertEquals('zero', $typed_data[0]->getValue());
$this->assertEquals(0, $typed_data[0]->getName());
$this->assertEquals('one', $typed_data[1]->getValue());
$this->assertEquals(1, $typed_data[1]->getName());
// Check that a none-pass filter empties the list.
$value = [
'zero',
'one',
];
$typed_data = $this->createTypedData(ListDataDefinition::create('string'), $value);
$typed_data->filter(function (TypedDataInterface $item) {
return FALSE;
});
$this->assertEquals(0, $typed_data->count());
// Check that filtering correctly renumbers elements.
$value = [
'zero',
'one',
'two',
];
$typed_data = $this->createTypedData(ListDataDefinition::create('string'), $value);
$typed_data->filter(function (TypedDataInterface $item) {
return $item->getValue() !== 'one';
});
$this->assertEquals(2, $typed_data->count());
$this->assertEquals('zero', $typed_data[0]->getValue());
$this->assertEquals(0, $typed_data[0]->getName());
$this->assertEquals('two', $typed_data[1]->getValue());
$this->assertEquals(1, $typed_data[1]->getName());
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.