View source
<?php
namespace Drupal\Tests\Core\Entity;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Cache\Cache;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\EntityTypeRepositoryInterface;
use Drupal\Core\Language\Language;
use Drupal\entity_test\Entity\EntityTestMul;
use Drupal\Tests\UnitTestCase;
class EntityUnitTest extends UnitTestCase {
protected $entity;
protected $entityType;
protected $entityTypeManager;
protected $entityTypeId;
protected $routeProvider;
protected $uuid;
protected $languageManager;
protected $cacheTagsInvalidator;
protected $values;
protected function setUp() : void {
$this->values = [
'id' => 1,
'langcode' => 'en',
'uuid' => '3bb9ee60-bea5-4622-b89b-a63319d10b3a',
];
$this->entityTypeId = $this
->randomMachineName();
$this->entityType = $this
->createMock('\\Drupal\\Core\\Entity\\EntityTypeInterface');
$this->entityType
->expects($this
->any())
->method('getListCacheTags')
->willReturn([
$this->entityTypeId . '_list',
]);
$this->entityTypeManager = $this
->getMockForAbstractClass(EntityTypeManagerInterface::class);
$this->entityTypeManager
->expects($this
->any())
->method('getDefinition')
->with($this->entityTypeId)
->will($this
->returnValue($this->entityType));
$this->uuid = $this
->createMock('\\Drupal\\Component\\Uuid\\UuidInterface');
$this->languageManager = $this
->createMock('\\Drupal\\Core\\Language\\LanguageManagerInterface');
$this->languageManager
->expects($this
->any())
->method('getLanguage')
->with('en')
->will($this
->returnValue(new Language([
'id' => 'en',
])));
$this->cacheTagsInvalidator = $this
->createMock('Drupal\\Core\\Cache\\CacheTagsInvalidator');
$container = new ContainerBuilder();
$container
->set('entity_type.manager', $this->entityTypeManager);
$container
->set('uuid', $this->uuid);
$container
->set('language_manager', $this->languageManager);
$container
->set('cache_tags.invalidator', $this->cacheTagsInvalidator);
\Drupal::setContainer($container);
$this->entity = $this
->getMockForAbstractClass('\\Drupal\\Core\\Entity\\EntityBase', [
$this->values,
$this->entityTypeId,
]);
}
public function testId() {
$this
->assertSame($this->values['id'], $this->entity
->id());
}
public function testUuid() {
$this
->assertSame($this->values['uuid'], $this->entity
->uuid());
}
public function testIsNew() {
$this
->assertFalse($this->entity
->isNew());
$this
->assertSame($this->entity, $this->entity
->enforceIsNew());
$this
->assertTrue($this->entity
->isNew());
}
public function testGetEntityType() {
$this
->assertSame($this->entityType, $this->entity
->getEntityType());
}
public function testBundle() {
$this
->assertSame($this->entityTypeId, $this->entity
->bundle());
}
public function testLabel() {
$property_label = $this
->randomMachineName();
$this->entityType
->expects($this
->atLeastOnce())
->method('getKey')
->with('label')
->will($this
->returnValue('label'));
$this->entityTypeManager
->expects($this
->atLeastOnce())
->method('getDefinition')
->with($this->entityTypeId)
->will($this
->returnValue([
'entity_keys' => [
'label' => 'label',
],
]));
$this->entity->label = $property_label;
$this
->assertSame($property_label, $this->entity
->label());
}
public function testAccess() {
$access = $this
->createMock('\\Drupal\\Core\\Entity\\EntityAccessControlHandlerInterface');
$operation = $this
->randomMachineName();
$access
->expects($this
->once())
->method('access')
->with($this->entity, $operation)
->will($this
->returnValue(AccessResult::allowed()));
$access
->expects($this
->once())
->method('createAccess')
->will($this
->returnValue(AccessResult::allowed()));
$this->entityTypeManager
->expects($this
->exactly(2))
->method('getAccessControlHandler')
->will($this
->returnValue($access));
$this
->assertEquals(AccessResult::allowed(), $this->entity
->access($operation));
$this
->assertEquals(AccessResult::allowed(), $this->entity
->access('create'));
}
public function testLanguage() {
$this->entityType
->expects($this
->any())
->method('getKey')
->willReturnMap([
[
'langcode',
'langcode',
],
]);
$this
->assertSame('en', $this->entity
->language()
->getId());
}
public function setupTestLoad() {
$this->entityTypeId = 'entity_test_mul';
$methods = get_class_methods(EntityTestMul::class);
unset($methods[array_search('load', $methods)]);
unset($methods[array_search('loadMultiple', $methods)]);
unset($methods[array_search('create', $methods)]);
$this->entity = $this
->getMockBuilder(EntityTestMul::class)
->disableOriginalConstructor()
->setMethods($methods)
->getMock();
}
public function testLoad() {
$this
->setupTestLoad();
$class_name = get_class($this->entity);
$entity_type_repository = $this
->getMockForAbstractClass(EntityTypeRepositoryInterface::class);
$entity_type_repository
->expects($this
->once())
->method('getEntityTypeFromClass')
->with($class_name)
->willReturn($this->entityTypeId);
$storage = $this
->createMock(EntityStorageInterface::class);
$storage
->expects($this
->once())
->method('load')
->with(1)
->will($this
->returnValue($this->entity));
$this->entityTypeManager
->expects($this
->once())
->method('getStorage')
->with($this->entityTypeId)
->will($this
->returnValue($storage));
\Drupal::getContainer()
->set('entity_type.repository', $entity_type_repository);
$this
->assertSame($this->entity, $class_name::load(1));
}
public function testLoadMultiple() {
$this
->setupTestLoad();
$class_name = get_class($this->entity);
$entity_type_repository = $this
->getMockForAbstractClass(EntityTypeRepositoryInterface::class);
$entity_type_repository
->expects($this
->once())
->method('getEntityTypeFromClass')
->with($class_name)
->willReturn($this->entityTypeId);
$storage = $this
->createMock(EntityStorageInterface::class);
$storage
->expects($this
->once())
->method('loadMultiple')
->with([
1,
])
->will($this
->returnValue([
1 => $this->entity,
]));
$this->entityTypeManager
->expects($this
->once())
->method('getStorage')
->with($this->entityTypeId)
->will($this
->returnValue($storage));
\Drupal::getContainer()
->set('entity_type.repository', $entity_type_repository);
$this
->assertSame([
1 => $this->entity,
], $class_name::loadMultiple([
1,
]));
}
public function testCreate() {
$this
->setupTestLoad();
$class_name = get_class($this->entity);
$entity_type_repository = $this
->getMockForAbstractClass(EntityTypeRepositoryInterface::class);
$entity_type_repository
->expects($this
->once())
->method('getEntityTypeFromClass')
->with($class_name)
->willReturn($this->entityTypeId);
$storage = $this
->createMock(EntityStorageInterface::class);
$storage
->expects($this
->once())
->method('create')
->with([])
->will($this
->returnValue($this->entity));
$this->entityTypeManager
->expects($this
->once())
->method('getStorage')
->with($this->entityTypeId)
->will($this
->returnValue($storage));
\Drupal::getContainer()
->set('entity_type.repository', $entity_type_repository);
$this
->assertSame($this->entity, $class_name::create([]));
}
public function testSave() {
$storage = $this
->createMock('\\Drupal\\Core\\Entity\\EntityStorageInterface');
$storage
->expects($this
->once())
->method('save')
->with($this->entity);
$this->entityTypeManager
->expects($this
->once())
->method('getStorage')
->with($this->entityTypeId)
->will($this
->returnValue($storage));
$this->entity
->save();
}
public function testDelete() {
$this->entity->id = $this
->randomMachineName();
$storage = $this
->createMock('\\Drupal\\Core\\Entity\\EntityStorageInterface');
$storage
->expects($this
->once())
->method('delete');
$this->entityTypeManager
->expects($this
->once())
->method('getStorage')
->with($this->entityTypeId)
->will($this
->returnValue($storage));
$this->entity
->delete();
}
public function testGetEntityTypeId() {
$this
->assertSame($this->entityTypeId, $this->entity
->getEntityTypeId());
}
public function testPreSave() {
$storage = $this
->createMock('\\Drupal\\Core\\Entity\\EntityStorageInterface');
$this
->assertNull($this->entity
->preSave($storage));
}
public function testPostSave() {
$this->cacheTagsInvalidator
->expects($this
->exactly(2))
->method('invalidateTags')
->withConsecutive([
[
$this->entityTypeId . '_list',
],
], [
[
$this->entityTypeId . '_list',
$this->entityTypeId . ':' . $this->values['id'],
],
]);
$storage = $this
->createMock('\\Drupal\\Core\\Entity\\EntityStorageInterface');
$this->entity
->postSave($storage, FALSE);
$this->entity
->postSave($storage, TRUE);
}
public function testPostSaveBundle() {
$this->cacheTagsInvalidator
->expects($this
->exactly(2))
->method('invalidateTags')
->withConsecutive([
[
$this->entityTypeId . '_list',
$this->entityTypeId . '_list:' . $this->entity
->bundle(),
],
], [
[
$this->entityTypeId . '_list',
$this->entityTypeId . '_list:' . $this->entity
->bundle(),
$this->entityTypeId . ':' . $this->values['id'],
],
]);
$this->entityType
->expects($this
->atLeastOnce())
->method('hasKey')
->with('bundle')
->willReturn(TRUE);
$storage = $this
->createMock('\\Drupal\\Core\\Entity\\EntityStorageInterface');
$this->entity
->postSave($storage, FALSE);
$this->entity
->postSave($storage, TRUE);
}
public function testPreCreate() {
$storage = $this
->createMock('\\Drupal\\Core\\Entity\\EntityStorageInterface');
$values = [];
$this
->assertNull($this->entity
->preCreate($storage, $values));
}
public function testPostCreate() {
$storage = $this
->createMock('\\Drupal\\Core\\Entity\\EntityStorageInterface');
$this
->assertNull($this->entity
->postCreate($storage));
}
public function testPreDelete() {
$storage = $this
->createMock('\\Drupal\\Core\\Entity\\EntityStorageInterface');
$this
->assertNull($this->entity
->preDelete($storage, [
$this->entity,
]));
}
public function testPostDelete() {
$this->cacheTagsInvalidator
->expects($this
->once())
->method('invalidateTags')
->with([
$this->entityTypeId . '_list',
$this->entityTypeId . ':' . $this->values['id'],
]);
$storage = $this
->createMock('\\Drupal\\Core\\Entity\\EntityStorageInterface');
$storage
->expects($this
->once())
->method('getEntityType')
->willReturn($this->entityType);
$entities = [
$this->values['id'] => $this->entity,
];
$this->entity
->postDelete($storage, $entities);
}
public function testPostDeleteBundle() {
$this->cacheTagsInvalidator
->expects($this
->once())
->method('invalidateTags')
->willReturnCallback(function (array $tags) {
self::assertEqualsCanonicalizing([
$this->entityTypeId . '_list',
$this->entityTypeId . ':' . $this->values['id'],
$this->entityTypeId . '_list:' . $this->entity
->bundle(),
], $tags);
return NULL;
});
$this->entityType
->expects($this
->atLeastOnce())
->method('hasKey')
->with('bundle')
->willReturn(TRUE);
$storage = $this
->createMock('\\Drupal\\Core\\Entity\\EntityStorageInterface');
$storage
->expects($this
->once())
->method('getEntityType')
->willReturn($this->entityType);
$entities = [
$this->values['id'] => $this->entity,
];
$this->entity
->postDelete($storage, $entities);
}
public function testPostLoad() {
$storage = $this
->createMock('\\Drupal\\Core\\Entity\\EntityStorageInterface');
$entities = [
$this->entity,
];
$this
->assertNull($this->entity
->postLoad($storage, $entities));
}
public function testReferencedEntities() {
$this
->assertSame([], $this->entity
->referencedEntities());
}
public function testCacheTags() {
$this
->assertEqualsCanonicalizing([
$this->entityTypeId . ':' . 1,
], $this->entity
->getCacheTags());
$this
->assertEqualsCanonicalizing([
$this->entityTypeId . ':' . 1,
], $this->entity
->getCacheTagsToInvalidate());
$this->entity
->addCacheTags([
'additional_cache_tag',
]);
$tags = [
$this->entityTypeId . ':' . 1,
'additional_cache_tag',
];
$this
->assertEqualsCanonicalizing($tags, $this->entity
->getCacheTags());
$this
->assertEqualsCanonicalizing([
$this->entityTypeId . ':' . 1,
], $this->entity
->getCacheTagsToInvalidate());
}
public function testCacheContexts() {
$cache_contexts_manager = $this
->getMockBuilder('Drupal\\Core\\Cache\\Context\\CacheContextsManager')
->disableOriginalConstructor()
->getMock();
$cache_contexts_manager
->method('assertValidTokens')
->willReturn(TRUE);
$container = new ContainerBuilder();
$container
->set('cache_contexts_manager', $cache_contexts_manager);
\Drupal::setContainer($container);
$this
->assertEqualsCanonicalizing([], $this->entity
->getCacheContexts());
$this->entity
->addCacheContexts([
'user',
]);
$this
->assertEqualsCanonicalizing([
'user',
], $this->entity
->getCacheContexts());
}
public function testCacheMaxAge() {
$this
->assertEquals(Cache::PERMANENT, $this->entity
->getCacheMaxAge());
$this->entity
->mergeCacheMaxAge(600);
$this->entity
->mergeCacheMaxAge(1800);
$this
->assertEquals(600, $this->entity
->getCacheMaxAge());
}
}