View source
<?php
namespace Drupal\Tests\Core\Entity;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Tests\UnitTestCase;
use Drupal\Core\Language\Language;
class ContentEntityBaseUnitTest extends UnitTestCase {
protected $bundle;
protected $entity;
protected $entityUnd;
protected $entityType;
protected $entityManager;
protected $entityTypeId;
protected $typedDataManager;
protected $fieldTypePluginManager;
protected $languageManager;
protected $uuid;
protected $id;
protected $fieldDefinitions;
protected function setUp() {
$this->id = 1;
$values = array(
'id' => $this->id,
'uuid' => '3bb9ee60-bea5-4622-b89b-a63319d10b3a',
'defaultLangcode' => array(
LanguageInterface::LANGCODE_DEFAULT => 'en',
),
);
$this->entityTypeId = $this
->randomMachineName();
$this->bundle = $this
->randomMachineName();
$this->entityType = $this
->getMock('\\Drupal\\Core\\Entity\\EntityTypeInterface');
$this->entityType
->expects($this
->any())
->method('getKeys')
->will($this
->returnValue(array(
'id' => 'id',
'uuid' => 'uuid',
)));
$this->entityManager = $this
->getMock('\\Drupal\\Core\\Entity\\EntityManagerInterface');
$this->entityManager
->expects($this
->any())
->method('getDefinition')
->with($this->entityTypeId)
->will($this
->returnValue($this->entityType));
$this->uuid = $this
->getMock('\\Drupal\\Component\\Uuid\\UuidInterface');
$this->typedDataManager = $this
->getMockBuilder('\\Drupal\\Core\\TypedData\\TypedDataManager')
->disableOriginalConstructor()
->getMock();
$this->typedDataManager
->expects($this
->any())
->method('getDefinition')
->with('entity')
->will($this
->returnValue([
'class' => '\\Drupal\\Core\\Entity\\Plugin\\DataType\\EntityAdapter',
]));
$english = new Language(array(
'id' => 'en',
));
$not_specified = new Language(array(
'id' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
'locked' => TRUE,
));
$this->languageManager = $this
->getMock('\\Drupal\\Core\\Language\\LanguageManagerInterface');
$this->languageManager
->expects($this
->any())
->method('getLanguages')
->will($this
->returnValue(array(
'en' => $english,
LanguageInterface::LANGCODE_NOT_SPECIFIED => $not_specified,
)));
$this->languageManager
->expects($this
->any())
->method('getLanguage')
->with('en')
->will($this
->returnValue($english));
$this->languageManager
->expects($this
->any())
->method('getLanguage')
->with(LanguageInterface::LANGCODE_NOT_SPECIFIED)
->will($this
->returnValue($not_specified));
$this->fieldTypePluginManager = $this
->getMockBuilder('\\Drupal\\Core\\Field\\FieldTypePluginManager')
->disableOriginalConstructor()
->getMock();
$this->fieldTypePluginManager
->expects($this
->any())
->method('getDefaultStorageSettings')
->will($this
->returnValue(array()));
$this->fieldTypePluginManager
->expects($this
->any())
->method('getDefaultFieldSettings')
->will($this
->returnValue(array()));
$this->fieldTypePluginManager
->expects($this
->any())
->method('createFieldItemList')
->will($this
->returnValue($this
->getMock('Drupal\\Core\\Field\\FieldItemListInterface')));
$container = new ContainerBuilder();
$container
->set('entity.manager', $this->entityManager);
$container
->set('uuid', $this->uuid);
$container
->set('typed_data_manager', $this->typedDataManager);
$container
->set('language_manager', $this->languageManager);
$container
->set('plugin.manager.field.field_type', $this->fieldTypePluginManager);
\Drupal::setContainer($container);
$this->fieldDefinitions = array(
'id' => BaseFieldDefinition::create('integer'),
'revision_id' => BaseFieldDefinition::create('integer'),
);
$this->entityManager
->expects($this
->any())
->method('getFieldDefinitions')
->with($this->entityTypeId, $this->bundle)
->will($this
->returnValue($this->fieldDefinitions));
$this->entity = $this
->getMockForAbstractClass('\\Drupal\\Core\\Entity\\ContentEntityBase', array(
$values,
$this->entityTypeId,
$this->bundle,
));
$values['defaultLangcode'] = array(
LanguageInterface::LANGCODE_DEFAULT => LanguageInterface::LANGCODE_NOT_SPECIFIED,
);
$this->entityUnd = $this
->getMockForAbstractClass('\\Drupal\\Core\\Entity\\ContentEntityBase', array(
$values,
$this->entityTypeId,
$this->bundle,
));
}
public function testIsNewRevision() {
$this->entityType
->expects($this
->at(0))
->method('hasKey')
->with('revision')
->will($this
->returnValue(FALSE));
$this->entityType
->expects($this
->at(1))
->method('hasKey')
->with('revision')
->will($this
->returnValue(TRUE));
$this->entityType
->expects($this
->at(2))
->method('hasKey')
->with('revision')
->will($this
->returnValue(TRUE));
$this->entityType
->expects($this
->at(3))
->method('getKey')
->with('revision')
->will($this
->returnValue('revision_id'));
$this->entityType
->expects($this
->at(4))
->method('hasKey')
->with('revision')
->will($this
->returnValue(TRUE));
$this->entityType
->expects($this
->at(5))
->method('getKey')
->with('revision')
->will($this
->returnValue('revision_id'));
$field_item_list = $this
->getMockBuilder('\\Drupal\\Core\\Field\\FieldItemList')
->disableOriginalConstructor()
->getMock();
$field_item = $this
->getMockBuilder('\\Drupal\\Core\\Field\\FieldItemBase')
->disableOriginalConstructor()
->getMockForAbstractClass();
$this->fieldTypePluginManager
->expects($this
->any())
->method('createFieldItemList')
->with($this->entity, 'revision_id', NULL)
->will($this
->returnValue($field_item_list));
$this->fieldDefinitions['revision_id']
->getItemDefinition()
->setClass(get_class($field_item));
$this
->assertFalse($this->entity
->isNewRevision());
$this
->assertTrue($this->entity
->isNewRevision());
$this->entity
->setNewRevision(TRUE);
$this
->assertTRUE($this->entity
->isNewRevision());
}
public function testSetNewRevisionException() {
$this->entityType
->expects($this
->once())
->method('hasKey')
->with('revision')
->will($this
->returnValue(FALSE));
$this
->setExpectedException('LogicException', 'Entity type ' . $this->entityTypeId . ' does not support revisions.');
$this->entity
->setNewRevision();
}
public function testIsDefaultRevision() {
$this
->assertTrue($this->entity
->isDefaultRevision());
$this
->assertTrue($this->entity
->isDefaultRevision(FALSE));
$this
->assertFalse($this->entity
->isDefaultRevision());
}
public function testGetRevisionId() {
$this
->assertNull($this->entity
->getRevisionId());
}
public function testIsTranslatable() {
$this->entityManager
->expects($this
->any())
->method('getBundleInfo')
->with($this->entityTypeId)
->will($this
->returnValue(array(
$this->bundle => array(
'translatable' => TRUE,
),
)));
$this->languageManager
->expects($this
->any())
->method('isMultilingual')
->will($this
->returnValue(TRUE));
$this
->assertTrue($this->entity
->language()
->getId() == 'en');
$this
->assertFalse($this->entity
->language()
->isLocked());
$this
->assertTrue($this->entity
->isTranslatable());
$this
->assertTrue($this->entityUnd
->language()
->getId() == LanguageInterface::LANGCODE_NOT_SPECIFIED);
$this
->assertTrue($this->entityUnd
->language()
->isLocked());
$this
->assertFalse($this->entityUnd
->isTranslatable());
}
public function testIsTranslatableForMonolingual() {
$this->languageManager
->expects($this
->any())
->method('isMultilingual')
->will($this
->returnValue(FALSE));
$this
->assertFalse($this->entity
->isTranslatable());
}
public function testPreSaveRevision() {
$storage = $this
->getMock('\\Drupal\\Core\\Entity\\EntityStorageInterface');
$record = new \stdClass();
$this
->assertNull($this->entity
->preSaveRevision($storage, $record));
}
public function testValidate() {
$validator = $this
->getMock('\\Symfony\\Component\\Validator\\ValidatorInterface');
$empty_violation_list = $this
->getMockBuilder('\\Symfony\\Component\\Validator\\ConstraintViolationList')
->setMethods(NULL)
->getMock();
$non_empty_violation_list = clone $empty_violation_list;
$violation = $this
->getMock('\\Symfony\\Component\\Validator\\ConstraintViolationInterface');
$non_empty_violation_list
->add($violation);
$validator
->expects($this
->at(0))
->method('validate')
->with($this->entity
->getTypedData())
->will($this
->returnValue($empty_violation_list));
$validator
->expects($this
->at(1))
->method('validate')
->with($this->entity
->getTypedData())
->will($this
->returnValue($non_empty_violation_list));
$this->typedDataManager
->expects($this
->exactly(2))
->method('getValidator')
->will($this
->returnValue($validator));
$this
->assertSame(0, count($this->entity
->validate()));
$this
->assertSame(1, count($this->entity
->validate()));
}
public function testRequiredValidation() {
$validator = $this
->getMock('\\Symfony\\Component\\Validator\\ValidatorInterface');
$empty_violation_list = $this
->getMockBuilder('\\Symfony\\Component\\Validator\\ConstraintViolationList')
->setMethods(NULL)
->getMock();
$validator
->expects($this
->at(0))
->method('validate')
->with($this->entity
->getTypedData())
->will($this
->returnValue($empty_violation_list));
$this->typedDataManager
->expects($this
->any())
->method('getValidator')
->will($this
->returnValue($validator));
$storage = $this
->getMock('\\Drupal\\Core\\Entity\\EntityStorageInterface');
$storage
->expects($this
->any())
->method('save')
->willReturnCallback(function (ContentEntityInterface $entity) use ($storage) {
$entity
->preSave($storage);
});
$this->entityManager
->expects($this
->any())
->method('getStorage')
->with($this->entityTypeId)
->will($this
->returnValue($storage));
$this
->assertFalse($this->entity
->isValidationRequired());
$this->entity
->save();
$this->entity
->setValidationRequired(TRUE);
$this
->assertTrue($this->entity
->isValidationRequired());
$this->entity
->validate();
$this->entity
->save();
$this
->assertTrue($this->entity
->isValidationRequired());
$this->entity
->save();
}
public function testBundle() {
$this
->assertSame($this->bundle, $this->entity
->bundle());
}
public function testAccess() {
$access = $this
->getMock('\\Drupal\\Core\\Entity\\EntityAccessControlHandlerInterface');
$operation = $this
->randomMachineName();
$access
->expects($this
->at(0))
->method('access')
->with($this->entity, $operation)
->will($this
->returnValue(TRUE));
$access
->expects($this
->at(1))
->method('access')
->with($this->entity, $operation)
->will($this
->returnValue(AccessResult::allowed()));
$access
->expects($this
->at(2))
->method('createAccess')
->will($this
->returnValue(TRUE));
$access
->expects($this
->at(3))
->method('createAccess')
->will($this
->returnValue(AccessResult::allowed()));
$this->entityManager
->expects($this
->exactly(4))
->method('getAccessControlHandler')
->will($this
->returnValue($access));
$this
->assertTrue($this->entity
->access($operation));
$this
->assertEquals(AccessResult::allowed(), $this->entity
->access($operation, NULL, TRUE));
$this
->assertTrue($this->entity
->access('create'));
$this
->assertEquals(AccessResult::allowed(), $this->entity
->access('create', NULL, TRUE));
}
public function testLabel() {
$callback_label = $this
->randomMachineName();
$callback_container = $this
->getMock(get_class());
$callback_container
->expects($this
->once())
->method(__FUNCTION__)
->will($this
->returnValue($callback_label));
$this->entityType
->expects($this
->once())
->method('getLabelCallback')
->will($this
->returnValue(array(
$callback_container,
__FUNCTION__,
)));
$this
->assertSame($callback_label, $this->entity
->label());
}
public function providerGet() {
return [
[
'result',
'field_name',
'langcode',
[
'field_name' => [
'langcode' => 'result',
],
],
],
[
'getTranslatedField_result',
'field_name',
'langcode',
[
'field_name' => 'no_langcode',
],
],
[
'getTranslatedField_result',
'field_name',
'langcode',
[],
],
];
}
public function testGet($expected, $field_name, $active_langcode, $fields) {
$mock_base = $this
->getMockBuilder('Drupal\\Core\\Entity\\ContentEntityBase')
->disableOriginalConstructor()
->setMethods(array(
'getTranslatedField',
))
->getMockForAbstractClass();
if (isset($fields[$field_name][$active_langcode])) {
$mock_base
->expects($this
->never())
->method('getTranslatedField');
}
else {
$mock_base
->expects($this
->once())
->method('getTranslatedField')
->with($this
->equalTo($field_name), $this
->equalTo($active_langcode))
->willReturn($expected);
}
$ref_langcode = new \ReflectionProperty($mock_base, 'activeLangcode');
$ref_langcode
->setAccessible(TRUE);
$ref_langcode
->setValue($mock_base, $active_langcode);
$ref_fields = new \ReflectionProperty($mock_base, 'fields');
$ref_fields
->setAccessible(TRUE);
$ref_fields
->setValue($mock_base, $fields);
$this
->assertEquals($expected, $mock_base
->get($field_name));
}
public function providerGetFields() {
return [
[
[],
FALSE,
FALSE,
[],
],
[
[
'field' => 'field',
'field2' => 'field2',
],
TRUE,
FALSE,
[
'field',
'field2',
],
],
[
[
'field3' => 'field3',
],
TRUE,
TRUE,
[
'field3',
],
],
[
[],
FALSE,
TRUE,
[
'field4',
],
],
];
}
public function testGetFields($expected, $include_computed, $is_computed, $field_definitions) {
$mock_base = $this
->getMockBuilder('Drupal\\Core\\Entity\\ContentEntityBase')
->disableOriginalConstructor()
->setMethods(array(
'getFieldDefinitions',
'get',
))
->getMockForAbstractClass();
$mocked_field_definitions = array();
foreach ($field_definitions as $name) {
$mock_definition = $this
->getMockBuilder('Drupal\\Core\\Field\\FieldDefinitionInterface')
->setMethods(array(
'isComputed',
))
->getMockForAbstractClass();
$mock_definition
->expects($this
->exactly($include_computed ? 0 : 1))
->method('isComputed')
->willReturn($is_computed);
$mocked_field_definitions[$name] = $mock_definition;
}
$mock_base
->expects($this
->once())
->method('getFieldDefinitions')
->willReturn($mocked_field_definitions);
$get_count = 0;
if ($include_computed) {
$get_count = count($field_definitions);
}
$mock_base
->expects($this
->exactly($get_count))
->method('get')
->willReturnArgument(0);
$this
->assertArrayEquals($expected, $mock_base
->getFields($include_computed));
}
public function testSet() {
$this
->assertSame($this->entity, $this->entity
->set('id', 0));
}
}