View source
<?php
namespace Drupal\Tests\acquia_contenthub\Kernel;
use Acquia\ContentHubClient\CDF\CDFObject;
use Drupal\acquia_contenthub\AcquiaContentHubEvents;
use Drupal\acquia_contenthub\Event\SerializeCdfEntityFieldEvent;
use Drupal\Core\Field\FieldItemList;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\KernelTests\KernelTestBase;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\node\NodeInterface;
abstract class AcquiaContentHubSerializerTestBase extends KernelTestBase {
protected const BUNDLE = 'article';
protected const ENTITY_TYPE = 'node';
protected $dispatcher;
protected $configFactory;
protected $entity;
protected $entityTypeManager;
protected $clientFactory;
public static $modules = [
'field',
'filter',
'depcalc',
'acquia_contenthub',
'acquia_contenthub_server_test',
'system',
'user',
'acquia_contenthub_server_test',
'node',
];
public function setUp() : void {
parent::setup();
$this
->installEntitySchema('user');
$this
->installSchema('node', 'node_access');
$this
->installEntitySchema('node');
$this->configFactory = $this->container
->get('config.factory');
$this->dispatcher = $this->container
->get('event_dispatcher');
$this->entityTypeManager = $this->container
->get('entity_type.manager');
$this
->createAcquiaContentHubAdminSettings();
$this->clientFactory = $this->container
->get('acquia_contenthub.client.factory');
}
public function createAcquiaContentHubAdminSettings() {
$admin_settings = $this->configFactory
->getEditable('acquia_contenthub.admin_settings');
return $admin_settings
->set('client_name', 'test-client')
->set('origin', '00000000-0000-0001-0000-123456789123')
->set('api_key', '12312321312321')
->set('secret_key', '12312321312321')
->set('hostname', 'https://example.com')
->set('shared_secret', '12312321312321')
->save();
}
public function createContentType(string $field_name = '', string $field_type = '') {
NodeType::create([
'type' => self::BUNDLE,
'name' => self::BUNDLE,
])
->save();
if ($field_name && $field_type) {
FieldStorageConfig::create([
'field_name' => $field_name,
'entity_type' => self::ENTITY_TYPE,
'type' => $field_type,
'cardinality' => 1,
])
->save();
FieldConfig::create([
'entity_type' => self::ENTITY_TYPE,
'field_name' => $field_name,
'bundle' => self::BUNDLE,
'label' => $this
->randomMachineName(),
])
->save();
}
}
public function createNode(array $values = []) : NodeInterface {
$data = [
'title' => $this
->randomMachineName(),
'type' => self::BUNDLE,
'created' => \Drupal::time()
->getRequestTime(),
'changed' => \Drupal::time()
->getRequestTime(),
'uid' => 1,
];
$data = array_merge($data, $values);
$entity = Node::create($data);
$entity
->save();
return $entity;
}
public function dispatchSerializeEvent(string $field_name, FieldItemList $field) : SerializeCdfEntityFieldEvent {
$settings = $this->clientFactory
->getClient()
->getSettings();
$cdf = new CDFObject('drupal8_content_entity', $this->entity
->uuid(), date('c'), date('c'), $settings
->getUuid());
$event = new SerializeCdfEntityFieldEvent($this->entity, $field_name, $field, $cdf);
$this->dispatcher
->dispatch(AcquiaContentHubEvents::SERIALIZE_CONTENT_ENTITY_FIELD, $event);
$this
->assertTrue($event
->isPropagationStopped());
return $event;
}
public function tearDown() : void {
$nodes = $this->entityTypeManager
->getStorage('node')
->loadByProperties([
'type' => self::BUNDLE,
]);
foreach ($nodes as $node) {
$node
->delete();
}
parent::tearDown();
}
}