public function TypedDataTest::testTypedDataListsFilter in Drupal 9
Same name and namespace in other branches
- 8 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 438
Class
- TypedDataTest
- Tests the functionality of all core data types.
Namespace
Drupal\KernelTests\Core\TypedDataCode
public function testTypedDataListsFilter() {
// 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());
}