View source
<?php
namespace Drupal\Tests\typed_data\Kernel;
use Drupal\Core\Entity\TypedData\EntityDataDefinition;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\KernelTests\KernelTestBase;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\TypedData\Exception\MissingDataException;
use Drupal\typed_data\Exception\InvalidArgumentException;
class DataFetcherTest extends KernelTestBase {
protected $typedDataManager;
protected $dataFetcher;
protected $node;
protected $entityTypeManager;
protected static $modules = [
'typed_data',
'system',
'node',
'field',
'text',
'user',
];
protected function setUp() : void {
parent::setUp();
$this
->installEntitySchema('user');
$this
->installEntitySchema('node');
$this
->installSchema('system', [
'sequences',
]);
$this->typedDataManager = $this->container
->get('typed_data_manager');
$this->dataFetcher = $this->container
->get('typed_data.data_fetcher');
$this->entityTypeManager = $this->container
->get('entity_type.manager');
$this->entityTypeManager
->getStorage('node_type')
->create([
'type' => 'page',
])
->save();
FieldStorageConfig::create([
'field_name' => 'field_integer',
'type' => 'integer',
'entity_type' => 'node',
'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
])
->save();
FieldConfig::create([
'field_name' => 'field_integer',
'entity_type' => 'node',
'bundle' => 'page',
])
->save();
$this->node = $this->entityTypeManager
->getStorage('node')
->create([
'title' => 'test',
'type' => 'page',
]);
}
public function testFetchingByBasicPropertyPath() {
$this
->assertEquals($this->node->title->value, $this->dataFetcher
->fetchDataByPropertyPath($this->node
->getTypedData(), 'title.0.value')
->getValue());
}
public function testFetchingByBasicSubPath() {
$this
->assertEquals($this->node->title->value, $this->dataFetcher
->fetchDataBySubPaths($this->node
->getTypedData(), [
'title',
'0',
'value',
])
->getValue());
}
public function testFetchingEntityReference() {
$user = $this->entityTypeManager
->getStorage('user')
->create([
'name' => 'test',
'type' => 'user',
]);
$this->node->uid->entity = $user;
$fetched_user = $this->dataFetcher
->fetchDataByPropertyPath($this->node
->getTypedData(), 'uid.entity')
->getValue();
$this
->assertSame($fetched_user, $user);
}
public function testFetchingAcrossReferences() {
$user = $this->entityTypeManager
->getStorage('user')
->create([
'name' => 'test',
'type' => 'user',
]);
$this->node->uid->entity = $user;
$fetched_value = $this->dataFetcher
->fetchDataByPropertyPath($this->node
->getTypedData(), 'uid.entity.name.value')
->getValue();
$this
->assertSame($fetched_value, 'test');
}
public function testFetchingNonExistingEntityReference() {
$fetched_user = $this->dataFetcher
->fetchDataByPropertyPath($this->node
->getTypedData(), 'uid.0.entity')
->getValue();
$this
->assertNull($fetched_user);
}
public function testFetchingValueAtValidPositions() {
$this->node->field_integer
->setValue([
'0' => 1,
'1' => 2,
]);
$fetched_value = $this->dataFetcher
->fetchDataByPropertyPath($this->node
->getTypedData(), 'field_integer.0.value')
->getValue();
$this
->assertEquals($fetched_value, 1);
$fetched_value = $this->dataFetcher
->fetchDataByPropertyPath($this->node
->getTypedData(), 'field_integer.1.value')
->getValue();
$this
->assertEquals($fetched_value, 2);
}
public function testFetchingValueAtInvalidPosition() {
$this
->expectException(MissingDataException::class);
$this
->expectExceptionMessage("Unable to apply data selector 'field_integer.0.value' at 'field_integer.0'");
$this->node->field_integer
->setValue([]);
$this->dataFetcher
->fetchDataByPropertyPath($this->node
->getTypedData(), 'field_integer.0.value')
->getValue();
}
public function testFetchingInvalidProperty() {
$this
->expectException(InvalidArgumentException::class);
$this
->expectExceptionMessage("Unable to apply data selector 'field_invalid.0.value' at 'field_invalid'");
$this->dataFetcher
->fetchDataByPropertyPath($this->node
->getTypedData(), 'field_invalid.0.value')
->getValue();
}
public function testFetchingEmptyProperty() {
$this->node->field_integer
->setValue([]);
$fetched_value = $this->dataFetcher
->fetchDataByPropertyPath($this->node
->getTypedData(), 'field_integer')
->getValue();
$this
->assertEquals($fetched_value, []);
}
public function testFetchingNotExistingListItem() {
$this
->expectException(MissingDataException::class);
$this->node->field_integer
->setValue([]);
$this->dataFetcher
->fetchDataByPropertyPath($this->node
->getTypedData(), 'field_integer.0')
->getValue();
}
public function testFetchingFromEmptyData() {
$this
->expectException(MissingDataException::class);
$this
->expectExceptionMessage("Unable to apply data selector 'field_integer.0.value' at 'field_integer': Unable to get property field_integer as no entity has been provided.");
$data_empty = $this->typedDataManager
->create(EntityDataDefinition::create('node'));
$this->dataFetcher
->fetchDataByPropertyPath($data_empty, 'field_integer.0.value')
->getValue();
}
public function testBubbleableMetadata() {
$this->node->field_integer
->setValue([]);
$this->node
->save();
$user = $this->entityTypeManager
->getStorage('user')
->create([
'name' => 'test',
'type' => 'user',
]);
$user
->save();
$this->node->uid->entity = $user;
$bubbleable_metadata = new BubbleableMetadata();
$this->dataFetcher
->fetchDataByPropertyPath($this->node
->getTypedData(), 'title.value', $bubbleable_metadata)
->getValue();
$expected = [
'node:' . $this->node
->id(),
];
$this
->assertEquals($expected, $bubbleable_metadata
->getCacheTags());
$this->dataFetcher
->fetchDataByPropertyPath($this->node
->getTypedData(), 'uid.entity.name', $bubbleable_metadata)
->getValue();
$expected = [
'node:' . $this->node
->id(),
'user:' . $user
->id(),
];
$this
->assertEquals($expected, $bubbleable_metadata
->getCacheTags());
}
}