Skip to content
Snippets Groups Projects
Verified Commit 4e91afb6 authored by Ken Mortimer's avatar Ken Mortimer Committed by Jibran Ijaz
Browse files

Issue #3383916 by mortim07, jibran, larowlan: Ensure typed data prototypes are...

Issue #3383916 by mortim07, jibran, larowlan: Ensure typed data prototypes are cached according to pipeline
parent 249c6102
No related branches found
Tags 8.x-1.11
No related merge requests found
Pipeline #65051 passed with warnings
......@@ -47,7 +47,7 @@ class DatasetPipelineBase extends PluginBase implements DatasetPipelineInterface
*
* @var \Drupal\Core\TypedData\MapDataDefinition
*/
protected $dataDefintion;
protected $dataDefinition;
/**
* Constructs a new DatasetPipelineBase.
......@@ -93,15 +93,15 @@ class DatasetPipelineBase extends PluginBase implements DatasetPipelineInterface
if (!$this->hasValidation()) {
return new ConstraintViolationList();
}
if (!isset($this->dataDefintion)) {
$this->dataDefintion = MapDataDefinition::create('data_pipelines_data');
if (!isset($this->dataDefinition)) {
$this->dataDefinition = MapDataDefinition::create('data_pipelines_data');
foreach ($this->pluginDefinition['validations']['field'] as $field_name => $definitions) {
// @todo do we want to support other than string data-types?
$this->dataDefintion->setPropertyDefinition($field_name, DataDefinition::create('string')->setConstraints($definitions));
$this->dataDefinition->setPropertyDefinition($field_name, DataDefinition::create('string')->setConstraints($definitions));
}
$this->dataDefintion->setConstraints($this->pluginDefinition['validations']['record']);
$this->dataDefinition->setConstraints($this->pluginDefinition['validations']['record']);
}
$typed_data = $this->typedDataManager->create($this->dataDefintion, $data);
$typed_data = $this->typedDataManager->create($this->dataDefinition, $data, $this->getPluginId());
$violations = $typed_data->validate();
return $violations;
}
......
......@@ -4,7 +4,15 @@ declare(strict_types=1);
namespace Drupal\Tests\data_pipelines\Kernel;
use Drupal\Core\TypedData\MapDataDefinition;
use Drupal\Core\TypedData\Plugin\DataType\StringData;
use Drupal\Core\TypedData\TypedDataManager;
use Drupal\data_pipelines\DatasetData;
use Drupal\data_pipelines\DatasetPipelineBase;
use Drupal\data_pipelines\Entity\Dataset;
use Prophecy\Argument;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\ConstraintViolationList;
/**
* Defines a test for DatasetPipelineBase.
......@@ -137,4 +145,20 @@ class DatasetPipelineBaseTest extends DatasetKernelTestBase {
$this->assertEquals('Validation error in row 3: All records should have 3 values', $violation);
}
/**
* A test to ensure that the typed data created during validation is cached according to the data pipeline.
*/
public function testTypedDataCaching(): void {
$dataset_data = new DatasetData([
'foo' => 'bar',
]);
$string_data = $this->prophesize(StringData::class);
$string_data->validate()->willReturn(new ConstraintViolationList());
$typed_data_manager = $this->prophesize(TypedDataManager::class);
$typed_data_manager->create(Argument::type(MapDataDefinition::class), $dataset_data, 'test_pipeline_1')->shouldBeCalled()->willReturn($string_data->reveal());
$this->container->set('typed_data_manager', $typed_data_manager->reveal());
$data_pipeline = DatasetPipelineBase::create($this->container, [], 'test_pipeline_1', ['validations' => ['field' => ['foo' => [new NotBlank()]], 'record' => []]]);
$data_pipeline->validate($dataset_data);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment