View source
<?php
namespace Drupal\Tests\yaml_content\Unit\EntityLoadHelper;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\ContentEntityTypeInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Tests\UnitTestCase;
use Drupal\Tests\yaml_content\Traits\LoadFixturesTrait;
use Drupal\yaml_content\Service\EntityLoadHelper;
class EntityLoadHelperTest extends UnitTestCase {
use LoadFixturesTrait;
protected $loadHelper;
protected function getEntityLoadHelperMock($stubbed_methods = NULL) {
$mock = $this
->getMockBuilder(EntityLoadHelper::class)
->disableOriginalConstructor()
->setMethods($stubbed_methods)
->getMock();
return $mock;
}
public function testEntityTypeManagerIsLazyLoaded() {
$this
->markTestIncomplete('This test has not been implemented yet.');
}
public function testEntityFieldManagerIsLazyLoaded() {
$this
->markTestIncomplete('This test has not been implemented yet.');
}
public function testEntityExistsAttemptsToLoadEntity() {
$this->loadHelper = $this
->getEntityLoadHelperMock([
'loadEntity',
]);
$entity_type = 'text_entity';
$content_data = [
'entity' => 'test_entity',
'type' => 'tester',
'name' => 'Test Entity',
];
$this->loadHelper
->expects($this
->once())
->method('loadEntity')
->with($entity_type, $content_data);
$this->loadHelper
->entityExists($entity_type, $content_data);
}
public function testEntityExistsReturnsTrueWhenAnEntityIsLoaded() {
$this->loadHelper = $this
->getEntityLoadHelperMock([
'loadEntity',
]);
$this->loadHelper
->method('loadEntity')
->willReturn($this
->getEntityMock());
$this
->assertTrue($this->loadHelper
->entityExists('test_entity', []));
}
public function testEntityExistsReturnsFalseWhenAnEntityIsNotLoaded() {
$this->loadHelper = $this
->getEntityLoadHelperMock([
'loadEntity',
]);
$this->loadHelper
->method('loadEntity')
->willReturn(FALSE);
$this
->assertFalse($this->loadHelper
->entityExists('test_entity', []));
}
public function testLoadEntityLoadsUuidFirstIfAvailable() {
$this->loadHelper = $this
->getEntityLoadHelperMock([
'loadByUuid',
'loadByProperties',
]);
$entity_type = 'test_entity';
$content_data = [
'entity' => 'test_entity',
'uuid' => '3c6485e4-69a3-429d-8ab1-3e7df48747bc',
];
$this->loadHelper
->expects($this
->once())
->method('loadByUuid')
->with($entity_type, $content_data['uuid']);
$this->loadHelper
->loadEntity($entity_type, $content_data);
}
public function testLoadEntityDoesntSearchTwiceIfUuidIsProvided() {
$this->loadHelper = $this
->getEntityLoadHelperMock([
'loadByUuid',
'loadByProperties',
]);
$entity_type = 'test_entity';
$content_data = [
'entity' => 'test_entity',
'uuid' => '3c6485e4-69a3-429d-8ab1-3e7df48747bc',
];
$this->loadHelper
->expects($this
->never())
->method('loadByProperties');
$this->loadHelper
->loadEntity($entity_type, $content_data);
}
public function testLoadEntityDoesntSearchTwiceIfNoUuidIsProvided() {
$this->loadHelper = $this
->getEntityLoadHelperMock([
'loadByUuid',
'loadByProperties',
]);
$entity_type = 'test_entity';
$content_data = [
'entity' => 'test_entity',
];
$this->loadHelper
->expects($this
->never())
->method('loadByUuid');
$this->loadHelper
->loadEntity($entity_type, $content_data);
}
public function testLoadEntityLoadsByPropertiesIfUuidIsUnavailable() {
$this->loadHelper = $this
->getEntityLoadHelperMock([
'loadByUuid',
'loadByProperties',
]);
$entity_type = 'test_entity';
$content_data = [
'entity' => 'test_entity',
];
$this->loadHelper
->expects($this
->once())
->method('loadByProperties')
->with($entity_type, $content_data);
$this->loadHelper
->loadEntity($entity_type, $content_data);
}
public function testLoadEntityWithUuidReturnsFalseWithNoMatches() {
$this->loadHelper = $this
->getEntityLoadHelperMock([
'loadByUuid',
'loadByProperties',
]);
$entity_type = 'test_entity';
$content_data = [
'entity' => 'test_entity',
'uuid' => '3c6485e4-69a3-429d-8ab1-3e7df48747bc',
];
$this->loadHelper
->method('loadByUuid')
->willReturn(FALSE);
$actual = $this->loadHelper
->loadEntity($entity_type, $content_data);
$this
->assertFalse($actual);
}
public function testLoadEntityWithUuidReturnsMatchedEntity() {
$this->loadHelper = $this
->getEntityLoadHelperMock([
'loadByUuid',
'loadByProperties',
]);
$entity_type = 'test_entity';
$content_data = [
'entity' => 'test_entity',
'uuid' => '3c6485e4-69a3-429d-8ab1-3e7df48747bc',
];
$matched_entity = $this
->getEntityMock();
$this->loadHelper
->method('loadByUuid')
->willReturn($matched_entity);
$actual = $this->loadHelper
->loadEntity($entity_type, $content_data);
$this
->assertSame($matched_entity, $actual);
}
public function testLoadEntityWithPropertiesReturnsFalseWithNoMatches() {
$this->loadHelper = $this
->getEntityLoadHelperMock([
'loadByUuid',
'loadByProperties',
]);
$entity_type = 'test_entity';
$content_data = [
'entity' => 'test_entity',
];
$this->loadHelper
->method('loadByProperties')
->willReturn(FALSE);
$actual = $this->loadHelper
->loadEntity($entity_type, $content_data);
$this
->assertFalse($actual);
}
public function testLoadEntityWithPropertiesdReturnsMatchedEntity() {
$this->loadHelper = $this
->getEntityLoadHelperMock([
'loadByUuid',
'loadByProperties',
]);
$entity_type = 'test_entity';
$content_data = [
'entity' => 'test_entity',
];
$matched_entity = $this
->getEntityMock();
$this->loadHelper
->method('loadByProperties')
->willReturn($matched_entity);
$actual = $this->loadHelper
->loadEntity($entity_type, $content_data);
$this
->assertSame($matched_entity, $actual);
}
public function testLoadByUuidSearchesByUuidOnly() {
$this
->markTestIncomplete('This test has not been implemented yet.');
}
public function testLoadByUuidReturnsOnlyOneMatch() {
$this
->markTestIncomplete('This test has not been implemented yet.');
}
public function testLoadByUuidReturnsFalseIfNoMatchIsFound() {
$this
->markTestIncomplete('This test has not been implemented yet.');
}
public function testLoadByPropertiesSearchesByPropertiesOnly() {
$this
->markTestIncomplete('This test has not been implemented yet.');
}
public function testLoadByPropertiesReturnsOnlyOneMatch() {
$this
->markTestIncomplete('This test has not been implemented yet.');
}
public function testLoadByPropertiesReturnsFalseIfNoMatchIsFound() {
$this
->markTestIncomplete('This test has not been implemented yet.');
}
public function testExtractContentPropertiesOnlyReturnsProperties() {
$this->loadHelper = $this
->getEntityLoadHelperMock([
'categorizeAttributes',
]);
$entity_type = 'test_entity';
$content_data = [
'entity' => 'test_entity',
'type' => 'test_bundle',
'status' => '1',
'field_title' => 'Test Title',
];
$results = [
'property' => [
'entity' => 'test_entity',
'type' => 'test_bundle',
'status' => '1',
],
'field' => [
'field_title' => 'Test Title',
],
'other' => [],
];
$this->loadHelper
->method('categorizeAttributes')
->willReturn($results);
$actual = $this->loadHelper
->extractContentProperties($entity_type, $content_data);
$this
->assertSame($results['property'], $actual);
}
public function testCategorizeAttributesAlwaysReturnsThreeKeys($entity_type, $content, $expected) {
$this
->setUpCategorizeAttributesTests();
$actual = $this->loadHelper
->categorizeAttributes($entity_type, $content);
$this
->assertSame(array_keys($expected), array_keys($actual), 'categorizeAttributes method did not return the expected keys.');
}
public function testCategorizeAttributesProperlyIdentifiesProperties($entity_type, $content, $expected) {
$this
->setUpCategorizeAttributesTests();
$actual = $this->loadHelper
->categorizeAttributes($entity_type, $content);
$this
->assertArrayEquals($expected['property'], $actual['property']);
}
public function testCategorizeAttributesProperlyIdentifiesFields($entity_type, $content, $expected) {
$this
->setUpCategorizeAttributesTests();
$actual = $this->loadHelper
->categorizeAttributes($entity_type, $content);
$this
->assertArrayEquals($expected['field'], $actual['field']);
}
public function testCategorizeAttributesProperlyIdentifiesOther($entity_type, $content, $expected) {
$this
->setUpCategorizeAttributesTests();
$actual = $this->loadHelper
->categorizeAttributes($entity_type, $content);
$this
->assertArrayEquals($expected['other'], $actual['other']);
}
public function attributeCategorizationProvider() {
$fixture = $this
->loadFixtureContent('attribute_categorization.assertions');
$assertions = [];
foreach ($fixture as $assertion) {
$assertions[] = [
$assertion['entity_type'],
$assertion['content'],
$assertion['expected'],
];
}
return $assertions;
}
public function getEntityDefinition($entity_type) {
$definition = $this
->loadFixtureContent('entity_definitions', [
$entity_type,
]);
$mock = $this
->getMockForAbstractClass(ContentEntityTypeInterface::class);
$mock
->method('hasKey')
->willReturnCallback(function ($key) use ($definition) {
return in_array($key, $definition['entity_keys']);
});
$mock
->method('getKeys')
->willReturn($definition['entity_keys']);
return $mock;
}
public function getEntityFields($entity_type) {
$field_map = $this
->loadFixtureContent('field_list', [
$entity_type,
]);
return $field_map;
}
protected function getEntityStorageMock() {
$mock = $this
->getMockForAbstractClass(EntityStorageInterface::class);
return $mock;
}
protected function getEntityMock() {
$mock = $this
->getMockForAbstractClass(ContentEntityInterface::class);
return $mock;
}
protected function setUpCategorizeAttributesTests() {
$this->loadHelper = $this
->getEntityLoadHelperMock([
'getEntityFields',
'getEntityDefinition',
]);
$this->loadHelper
->method('getEntityFields')
->willReturnCallback([
$this,
'getEntityFields',
]);
$this->loadHelper
->method('getEntityDefinition')
->willReturnCallback([
$this,
'getEntityDefinition',
]);
}
}