View source
<?php
namespace Drupal\Tests\acquia_contenthub\Unit;
use Acquia\ContentHubClient\Attribute;
use Drupal\acquia_contenthub\ContentHubEntityDependency;
use Drupal\Tests\UnitTestCase;
class ContentHubEntityDependencyTest extends UnitTestCase {
use ContentHubEntityTrait;
protected $moduleHandler;
protected $container;
private $entity;
protected function setUp() {
parent::setUp();
$this->moduleHandler = $this
->createMock('\\Drupal\\Core\\Extension\\ModuleHandlerInterface');
$this->container = $this
->createMock('Drupal\\Core\\DependencyInjection\\Container');
$this->container
->method('get')
->willReturnCallback(function ($name) {
switch ($name) {
case 'module_handler':
return $this->moduleHandler;
}
return NULL;
});
\Drupal::setContainer($this->container);
}
public function testGetUuidAndType() {
$this->entity = $this
->createContentHubEntity();
$ch_entity_dependency = new ContentHubEntityDependency($this->entity);
$this
->assertEquals($this->entity
->getUuid(), $ch_entity_dependency
->getUuid());
$this
->assertEquals('node', $ch_entity_dependency
->getEntityType());
}
public function testIsEntityDependent() {
$this->entity = $this
->createContentHubEntity();
$ch_entity_dependency = new ContentHubEntityDependency($this->entity);
$this
->assertFalse($ch_entity_dependency
->isEntityDependent());
$values = [
'type' => 'paragraph',
];
$this->entity = $this
->createContentHubEntity($values);
$ch_entity_dependency = new ContentHubEntityDependency($this->entity);
$this
->assertTrue($ch_entity_dependency
->isEntityDependent());
}
public function testGetRelationship() {
$this->entity = $this
->createContentHubEntity();
$ch_entity_dependency = new ContentHubEntityDependency($this->entity);
$this
->assertEquals(ContentHubEntityDependency::RELATIONSHIP_INDEPENDENT, $ch_entity_dependency
->getRelationship());
}
public function testDependencyChain() {
$this->entity = $this
->createContentHubEntity();
$ch_entity_dependency = new ContentHubEntityDependency($this->entity);
$entity1 = $this
->createContentHubEntity([
'uuid' => '00000000-2222-0000-0000-000000000000',
]);
$ch_entity_dependency1 = new ContentHubEntityDependency($entity1);
$ch_entity_dependency
->appendDependencyChain($ch_entity_dependency1);
$expected_dependency_chain = [];
$expected_dependency_chain[] = $entity1
->getUuid();
$this
->assertEquals($expected_dependency_chain, $ch_entity_dependency
->getDependencyChain());
$entity2 = $this
->createContentHubEntity([
'uuid' => '00000000-3333-0000-0000-000000000000',
]);
$ch_entity_dependency2 = new ContentHubEntityDependency($entity2);
$ch_entity_dependency
->appendDependencyChain($ch_entity_dependency2);
$expected_dependency_chain[] = $entity2
->getUuid();
$this
->assertEquals($expected_dependency_chain, $ch_entity_dependency
->getDependencyChain());
$this
->assertTrue($ch_entity_dependency
->isInDependencyChain($ch_entity_dependency1));
$this
->assertTrue($ch_entity_dependency
->isInDependencyChain($ch_entity_dependency2));
$entity3 = $this
->createContentHubEntity([
'uuid' => '00000000-4444-0000-0000-000000000000',
]);
$ch_entity_dependency3 = new ContentHubEntityDependency($entity3);
$this
->assertFalse($ch_entity_dependency
->isInDependencyChain($ch_entity_dependency3));
}
public function testParenthood() {
$this->entity = $this
->createContentHubEntity();
$ch_entity_dependency = new ContentHubEntityDependency($this->entity);
$entity1 = $this
->createContentHubEntity([
'uuid' => '00000000-2222-0000-0000-000000000000',
]);
$ch_entity_dependency1 = new ContentHubEntityDependency($entity1);
$ch_entity_dependency
->setParent($ch_entity_dependency1);
$this
->assertEquals($ch_entity_dependency1, $ch_entity_dependency
->getParent());
}
public function testGetRawEntity() {
$this->entity = $this
->createContentHubEntity();
$ch_entity_dependency = new ContentHubEntityDependency($this->entity);
$this
->assertEquals($this->entity, $ch_entity_dependency
->getRawEntity());
}
public function testSetAuthor() {
$author = '00000000-9999-0000-0000-000000000000';
$this->entity = $this
->createContentHubEntity();
$ch_entity_dependency = new ContentHubEntityDependency($this->entity);
$ch_entity_dependency
->setAuthor($author);
$cdf = $ch_entity_dependency
->getRawEntity();
$expected = [
'type' => Attribute::TYPE_REFERENCE,
'value' => [
'en' => $author,
],
];
$this
->assertEquals($expected, $cdf['attributes']['author']);
}
public function testSetStatus() {
$status = 1;
$this->entity = $this
->createContentHubEntity();
$ch_entity_dependency = new ContentHubEntityDependency($this->entity);
$ch_entity_dependency
->setStatus($status);
$cdf = $ch_entity_dependency
->getRawEntity();
$expected = [
'type' => Attribute::TYPE_INTEGER,
'value' => [
'en' => $status,
],
];
$this
->assertEquals($expected, $cdf['attributes']['status']);
}
public function testGetRemoteDependencies() {
$this->moduleHandler
->expects($this
->at(0))
->method('moduleExists')
->with('entity_embed')
->willReturn(TRUE);
$values['attributes']['field_reference1'] = [
'type' => Attribute::TYPE_REFERENCE,
'value' => [
'en' => '00000000-2222-0000-0000-000000000000',
],
];
$values['attributes']['field_reference2'] = [
'type' => Attribute::TYPE_ARRAY_REFERENCE,
'value' => [
'en' => [
'00000000-3333-0000-0000-000000000000',
'00000000-4444-0000-0000-000000000000',
'00000000-5555-0000-0000-000000000000',
],
],
];
$values['attributes']['author'] = [
'type' => Attribute::TYPE_REFERENCE,
'value' => [
'en' => '00000000-6666-0000-0000-000000000000',
],
];
$body_value = [
'value' => '<drupal-entity data-caption="some_image" data-embed-button="media_browser" data-entity-embed-display="view_mode:media.embedded" data-entity-type="media" data-entity-uuid="00000000-7777-0000-0000-000000000000"></drupal-entity>',
'summary' => '',
'format' => 'rich_text',
];
$values['attributes']['body'] = [
'type' => Attribute::TYPE_ARRAY_STRING,
'value' => [
'en' => [
json_encode($body_value, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT),
],
],
];
$this->entity = $this
->createContentHubEntity($values);
$ch_entity_dependency = new ContentHubEntityDependency($this->entity);
$dependencies = $ch_entity_dependency
->getRemoteDependencies();
$expected = [
'00000000-2222-0000-0000-000000000000',
'00000000-3333-0000-0000-000000000000',
'00000000-4444-0000-0000-000000000000',
'00000000-5555-0000-0000-000000000000',
'00000000-7777-0000-0000-000000000000',
];
$this
->assertEquals($expected, $dependencies);
}
}