View source
<?php
namespace Drupal\Tests\feeds_para_mapper\Unit\Helpers;
use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Field\FieldItemList;
use Drupal\entity_reference_revisions\EntityReferenceRevisionsFieldItemList;
use Drupal\node\Entity\Node;
use Drupal\paragraphs\Entity\Paragraph;
use Prophecy\Argument;
use Prophecy\Prophecy\ObjectProphecy;
use Prophecy\Prophet;
class EntityHelper {
public $node;
public $paragraphs;
protected $prophet;
protected $fieldHelper;
public $values;
public $host_fields_values;
public function __construct(FieldHelper $fieldHelper) {
$this->prophet = new Prophet();
$this->node = $this
->getEntity('node', $fieldHelper->node_bundle, 100);
$this->paragraphs = array();
$this->host_fields_values = array();
$last = $this->node;
foreach ($fieldHelper->fieldsConfig as $config) {
$st = $config->settings['handler_settings'];
if (isset($st['target_bundles'])) {
$this->values[$config->name] = array();
foreach ($st['target_bundles'] as $target_bundle) {
foreach ($config->paragraph_ids as $paragraph_id) {
if (isset($config->host_field)) {
$this->host_fields_values[$config->name] = array(
array(
'value' => $config->host_field,
),
);
}
$this->paragraphs[$paragraph_id] = $this
->getEntity('paragraph', $target_bundle, $paragraph_id, $config->host_field, $last
->reveal());
$this->values[$config->name][] = array(
'target_id' => $paragraph_id,
);
$last = $this->paragraphs[$paragraph_id];
}
}
}
}
$this->fieldHelper = $fieldHelper;
}
private function getEntity($type, $bundle, $id, $host_field = null, $host = null) {
$class = Node::class;
if ($type === 'paragraph') {
$class = Paragraph::class;
}
$entity = $this->prophet
->prophesize($class);
$entity
->isNew()
->willReturn(true);
$that = $this;
$entity
->hasField(Argument::type('string'))
->will(function ($args) use ($that, $type, $bundle) {
if ($type === 'node') {
$field = $that->fieldHelper->fields[0]
->reveal()
->getName();
if ($field === $args[0]) {
return true;
}
return false;
}
else {
$fields = $that->fieldHelper
->getBundleFields($bundle);
foreach ($fields as $field) {
if ($field
->reveal()
->getName() === $args[0]) {
return true;
}
}
return false;
}
});
$entity
->getEntityTypeId()
->willReturn($type);
$entity
->bundle()
->willReturn($bundle);
$entity
->id()
->willReturn($id);
$that = $this;
if (isset($host_field)) {
$entity
->getParentEntity()
->willReturn($host);
}
$entity
->get(Argument::type('string'))
->will(function ($args) use ($entity, $host_field, $bundle, $that) {
if ($args[0] === 'parent_field_name') {
return $that
->getFieldItemListMock($host_field, 'text');
}
$fields = $that->fieldHelper
->getBundleFields($bundle);
$found = array_filter($fields, function ($field) use ($args) {
$name = $field
->reveal()
->getName();
return $name === $args[0];
})[0];
return $that
->getFieldItemListMock($args[0], 'reference', $found
->reveal());
});
$entity
->getType()
->willReturn($bundle);
$entity
->getFieldDefinitions()
->will(function ($args) use ($that, $type, $bundle) {
return $that->fieldHelper
->getFieldDefinitions($type, $bundle);
});
$entity
->save()
->willReturn(TRUE);
return $entity;
}
public function getFieldItemListMock($field, $type = "reference", $instance = null) {
$class = EntityReferenceRevisionsFieldItemList::class;
$values =& $this->values;
if ($type !== 'reference') {
$class = FieldItemList::class;
$values =& $this->host_fields_values;
}
$fieldItem = $this->prophet
->prophesize($class);
$fieldItem
->getValue()
->will(function ($args) use ($field, $values) {
$result = array();
if (isset($values[$field])) {
$result = $values[$field];
}
return $result;
});
$fieldItem
->appendItem(Argument::any())
->will(function ($args) use ($field, &$values) {
$v = array();
if (isset($values[$field])) {
$v = $values[$field];
}
$v[] = array(
'entity' => $args[0],
);
$values[$field] = $v;
return $this
->reveal();
});
$fieldItem
->set(Argument::type('int'), Argument::any())
->will(function ($args) use ($field, &$values) {
$v = array();
if (isset($values[$field])) {
$v = $values[$field];
}
$v[$args[0]] = $args[1];
$values[$field] = $v;
return $this
->reveal();
});
$fieldItem
->removeItem(Argument::type('int'))
->will(function ($args) use ($field, &$values) {
$v = array();
if (isset($values[$field])) {
$v = $values[$field];
}
unset($v[$args[0]]);
$values[$field] = $v;
return $this
->reveal();
});
$fieldItem
->getFieldDefinition()
->willReturn($instance);
return $fieldItem
->reveal();
}
public function setValue($field, $value) {
$this->values[$field] = $value;
}
public function getEntityTypeManagerMock() {
$manager = $this->prophet
->prophesize('Drupal\\Core\\Entity\\EntityTypeManagerInterface');
$storage = $this
->getStorageMock()
->reveal();
try {
$manager
->getStorage(Argument::type('string'))
->willReturn($storage);
} catch (InvalidPluginDefinitionException $e) {
} catch (PluginNotFoundException $e) {
}
return $manager;
}
protected function getStorageMock() {
$storage = $this->prophet
->prophesize(EntityStorageInterface::class);
$that = $this;
$storage
->create(Argument::type('array'))
->will(function ($args) use ($that) {
$bundle = $args[0]['type'];
$id = random_int(10, 10);
return $that
->getEntity('paragraph', $bundle, $id)
->reveal();
});
$storage
->load(Argument::any())
->will(function ($args) use ($that) {
$id = $args[0];
return $that->paragraphs[$id];
});
return $storage;
}
}