/** * {@inheritdoc} */ public function setUp() { $this->entityTypeId = $this->randomMachineName(); $this->provider = $this->randomMachineName(); $this->entityType = $this->getMock('\\Drupal\\Core\\Entity\\EntityTypeInterface'); $this->entityType->expects($this->any())->method('getProvider')->will($this->returnValue($this->provider)); $this->entityManager = $this->getMock('\\Drupal\\Core\\Entity\\EntityManagerInterface'); $this->entityManager->expects($this->any())->method('getDefinition')->with($this->entityTypeId)->will($this->returnValue($this->entityType)); }
/** * @covers ::createInstance * @covers ::__construct */ function testCreateInstance() { $this->entityType->expects($this->any())->method('id')->willReturn('currency'); $entity_manager = $this->getMock(EntityManagerInterface::class); $entity_manager->expects($this->once())->method('getStorage')->with('currency')->willReturn($this->entityStorage); $container = $this->getMock(ContainerInterface::class); $map = array(array('entity.manager', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, $entity_manager), array('module_handler', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, $this->moduleHandler), array('string_translation', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, $this->stringTranslation)); $container->expects($this->any())->method('get')->willReturnMap($map); $sut = CurrencyLocaleListBuilder::createInstance($container, $this->entityType); $this->assertInstanceOf(CurrencyLocaleListBuilder::class, $sut); }
/** * {@inheritdoc} */ protected function setUp() { $this->entityType = $this->getMock('\\Drupal\\Core\\Entity\\EntityTypeInterface'); $this->entityType->expects($this->any())->method('getProvider')->will($this->returnValue('responsive_image')); $this->entityManager = $this->getMock('\\Drupal\\Core\\Entity\\EntityManagerInterface'); $this->entityManager->expects($this->any())->method('getDefinition')->with('responsive_image_style')->will($this->returnValue($this->entityType)); $this->breakpointManager = $this->getMock('\\Drupal\\breakpoint\\BreakpointManagerInterface'); $container = new ContainerBuilder(); $container->set('entity.manager', $this->entityManager); $container->set('breakpoint.manager', $this->breakpointManager); \Drupal::setContainer($container); }
/** * {@inheritdoc} */ protected function setUp() { $this->entityType = $this->randomMachineName(); $this->entityInfo = $this->getMock('\\Drupal\\Core\\Entity\\EntityTypeInterface'); $this->entityInfo->expects($this->any())->method('getProvider')->will($this->returnValue('entity')); $this->entityManager = $this->getMock('\\Drupal\\Core\\Entity\\EntityManagerInterface'); $this->uuid = $this->getMock('\\Drupal\\Component\\Uuid\\UuidInterface'); $container = new ContainerBuilder(); $container->set('entity.manager', $this->entityManager); $container->set('uuid', $this->uuid); \Drupal::setContainer($container); }
/** * {@inheritdoc} */ public function setUp() { $this->entityTypeId = $this->randomName(); $this->entityType = $this->getMock('\\Drupal\\Core\\Entity\\EntityTypeInterface'); $this->entityType->expects($this->any())->method('getProvider')->will($this->returnValue('breakpoint')); $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'); $container = new ContainerBuilder(); $container->set('entity.manager', $this->entityManager); $container->set('uuid', $this->uuid); \Drupal::setContainer($container); }
/** * {@inheritdoc} * * @covers ::__construct() */ protected function setUp() { parent::setUp(); $this->entityType = $this->getMock('Drupal\\Core\\Entity\\EntityTypeInterface'); $this->entityTypeId = 'test_entity_type'; $this->entityType->expects($this->any())->method('getKey')->will($this->returnValueMap(array(array('id', 'id'), array('uuid', 'uuid')))); $this->entityType->expects($this->any())->method('id')->will($this->returnValue($this->entityTypeId)); $this->entityType->expects($this->any())->method('getConfigPrefix')->will($this->returnValue('the_config_prefix')); $this->entityType->expects($this->any())->method('getClass')->will($this->returnValue(get_class($this->getMockEntity()))); $this->entityType->expects($this->any())->method('getListCacheTags')->willReturn(array('test_entity_type_list')); $this->moduleHandler = $this->getMock('Drupal\\Core\\Extension\\ModuleHandlerInterface'); $this->uuidService = $this->getMock('Drupal\\Component\\Uuid\\UuidInterface'); $this->languageManager = $this->getMock('Drupal\\Core\\Language\\LanguageManagerInterface'); $this->languageManager->expects($this->any())->method('getDefaultLanguage')->will($this->returnValue(new Language(array('langcode' => 'en')))); $this->configFactory = $this->getMock('Drupal\\Core\\Config\\ConfigFactoryInterface'); $this->entityQuery = $this->getMock('Drupal\\Core\\Entity\\Query\\QueryInterface'); $this->entityStorage = $this->getMockBuilder('Drupal\\Core\\Config\\Entity\\ConfigEntityStorage')->setConstructorArgs(array($this->entityType, $this->configFactory, $this->uuidService, $this->languageManager))->setMethods(array('getQuery'))->getMock(); $this->entityStorage->expects($this->any())->method('getQuery')->will($this->returnValue($this->entityQuery)); $this->entityStorage->setModuleHandler($this->moduleHandler); $this->entityManager = $this->getMock('\\Drupal\\Core\\Entity\\EntityManagerInterface'); $this->entityManager->expects($this->any())->method('getDefinition')->with('test_entity_type')->will($this->returnValue($this->entityType)); $this->cacheBackend = $this->getMock('Drupal\\Core\\Cache\\CacheBackendInterface'); $this->typedConfigManager = $this->getMock('Drupal\\Core\\Config\\TypedConfigManagerInterface'); $this->typedConfigManager->expects($this->any())->method('getDefinition')->will($this->returnValue(array('mapping' => array('id' => '', 'uuid' => '', 'dependencies' => '')))); $container = new ContainerBuilder(); $container->set('entity.manager', $this->entityManager); $container->set('config.typed', $this->typedConfigManager); $container->set('cache.test', $this->cacheBackend); $container->setParameter('cache_bins', array('cache.test' => 'test')); \Drupal::setContainer($container); }
/** * {@inheritdoc} */ protected function setUp() { $this->editorId = $this->randomMachineName(); $this->entityTypeId = $this->randomMachineName(); $this->entityType = $this->getMock('\\Drupal\\Core\\Entity\\EntityTypeInterface'); $this->entityType->expects($this->any())->method('getProvider')->will($this->returnValue('editor')); $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->editorPluginManager = $this->getMockBuilder('Drupal\\editor\\Plugin\\EditorManager')->disableOriginalConstructor()->getMock(); $container = new ContainerBuilder(); $container->set('entity.manager', $this->entityManager); $container->set('uuid', $this->uuid); $container->set('plugin.manager.editor', $this->editorPluginManager); \Drupal::setContainer($container); }
/** * {@inheritdoc} */ public function setUp() { $this->entityTypeId = $this->randomName(); $this->provider = $this->randomName(); $this->entityType = $this->getMock('\\Drupal\\Core\\Entity\\EntityTypeInterface'); $this->entityType->expects($this->any())->method('getProvider')->will($this->returnValue($this->provider)); $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->breakpointGroupId = $this->randomName(9); $this->breakpointGroup = $this->getMock('Drupal\\breakpoint\\Entity\\BreakpointGroup', array(), array(array('name' => 'test', 'id' => $this->breakpointGroupId))); $this->breakpointGroupStorage = $this->getMock('\\Drupal\\Core\\Config\\Entity\\ConfigEntityStorageInterface'); $this->breakpointGroupStorage->expects($this->any())->method('load')->with($this->breakpointGroupId)->will($this->returnValue($this->breakpointGroup)); $this->entityManager->expects($this->any())->method('getStorage')->will($this->returnValue($this->breakpointGroupStorage)); $container = new ContainerBuilder(); $container->set('entity.manager', $this->entityManager); $container->set('uuid', $this->uuid); \Drupal::setContainer($container); }
/** * @covers ::toArray() */ public function testToArray() { $field = new FieldConfig(array('field_name' => $this->fieldStorage->getName(), 'entity_type' => 'test_entity_type', 'bundle' => 'test_bundle', 'field_type' => 'test_field'), $this->entityTypeId); $expected = array('id' => 'test_entity_type.test_bundle.field_test', 'uuid' => NULL, 'status' => TRUE, 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED, 'field_name' => 'field_test', 'entity_type' => 'test_entity_type', 'bundle' => 'test_bundle', 'label' => '', 'description' => '', 'required' => FALSE, 'default_value' => array(), 'default_value_callback' => '', 'settings' => array(), 'dependencies' => array(), 'field_type' => 'test_field'); $this->entityManager->expects($this->any())->method('getDefinition')->with($this->entityTypeId)->will($this->returnValue($this->entityType)); $this->entityType->expects($this->once())->method('getKey')->with('id')->will($this->returnValue('id')); $this->typedConfigManager->expects($this->once())->method('getDefinition')->will($this->returnValue(array('mapping' => array_fill_keys(array_keys($expected), '')))); $export = $field->toArray(); $this->assertEquals($expected, $export); }
/** * @covers ::label */ public function testLabel() { // Make a mock with one method that we use as the entity's label callback. // We check that it is called, and that the entity's label is the callback's // return value. $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()); }
/** * {@inheritdoc} */ public function setUp() { $this->editorId = $this->randomMachineName(); $this->entityTypeId = $this->randomMachineName(); $this->entityType = $this->getMock('\\Drupal\\Core\\Entity\\EntityTypeInterface'); $this->entityType->expects($this->any())->method('getProvider')->will($this->returnValue('editor')); $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->editorPluginManager = $this->getMockBuilder('Drupal\\editor\\Plugin\\EditorManager')->disableOriginalConstructor()->getMock(); $this->moduleHandler = $this->getMock('\\Drupal\\Core\\Extension\\ModuleHandlerInterface'); $this->moduleHandler->expects($this->once())->method('invokeAll')->with('editor_default_settings', array($this->editorId))->will($this->returnValue(array())); $this->moduleHandler->expects($this->once())->method('alter')->with('editor_default_settings', array(), $this->editorId)->will($this->returnValue(array())); $container = new ContainerBuilder(); $container->set('entity.manager', $this->entityManager); $container->set('uuid', $this->uuid); $container->set('plugin.manager.editor', $this->editorPluginManager); $container->set('module_handler', $this->moduleHandler); \Drupal::setContainer($container); }
/** * @covers ::render * * @depends testBuildHeader */ public function testRender() { $query = $this->getMock(QueryInterface::class); $query->expects($this->atLeastOnce())->method('pager')->willReturnSelf(); $query->expects($this->atLeastOnce())->method('sort')->willReturnSelf(); $this->entityStorage->expects($this->atLeastOnce())->method('getQuery')->willReturn($query); $this->entityType->expects($this->any())->method('getClass')->willReturn(ConfigEntityBase::class); $this->entityStorage->expects($this->once())->method('loadMultipleOverrideFree')->willReturn([]); $build = $this->sut->render(); unset($build['table']['#attached']); unset($build['table']['#header']); $expected_build = array('#type' => 'table', '#title' => NULL, '#rows' => [], '#attributes' => array('class' => array('payment-method-configuration-list')), '#cache' => ['contexts' => NULL, 'tags' => NULL]); $this->assertInstanceOf(TranslatableMarkup::class, $build['table']['#empty']); unset($build['table']['#empty']); $this->assertEquals($expected_build, $build['table']); }
/** * @covers ::createDuplicate */ public function testCreateDuplicate() { $this->entityType->expects($this->at(0))->method('getKey')->with('id')->will($this->returnValue('id')); $this->entityType->expects($this->at(1))->method('hasKey')->with('uuid')->will($this->returnValue(TRUE)); $this->entityType->expects($this->at(2))->method('getKey')->with('uuid')->will($this->returnValue('uuid')); $new_uuid = '8607ef21-42bc-4913-978f-8c06207b0395'; $this->uuid->expects($this->once())->method('generate')->will($this->returnValue($new_uuid)); $duplicate = $this->entity->createDuplicate(); $this->assertInstanceOf('\\Drupal\\Core\\Entity\\Entity', $duplicate); $this->assertNotSame($this->entity, $duplicate); $this->assertFalse($this->entity->isNew()); $this->assertTrue($duplicate->isNew()); $this->assertNull($duplicate->id()); $this->assertNull($duplicate->getOriginalId()); $this->assertNotEquals($this->entity->uuid(), $duplicate->uuid()); $this->assertSame($new_uuid, $duplicate->uuid()); }
/** * @covers ::delete * @covers ::doDelete */ public function testDelete() { $entities['foo'] = $this->getMockEntity('Drupal\\Core\\Entity\\Entity', array(array('id' => 'foo'))); $entities['bar'] = $this->getMockEntity('Drupal\\Core\\Entity\\Entity', array(array('id' => 'bar'))); $this->entityType->expects($this->once())->method('getClass')->will($this->returnValue(get_class(reset($entities)))); $this->setUpKeyValueEntityStorage(); $this->moduleHandler->expects($this->at(0))->method('invokeAll')->with('test_entity_type_predelete'); $this->moduleHandler->expects($this->at(1))->method('invokeAll')->with('entity_predelete'); $this->moduleHandler->expects($this->at(2))->method('invokeAll')->with('test_entity_type_predelete'); $this->moduleHandler->expects($this->at(3))->method('invokeAll')->with('entity_predelete'); $this->moduleHandler->expects($this->at(4))->method('invokeAll')->with('test_entity_type_delete'); $this->moduleHandler->expects($this->at(5))->method('invokeAll')->with('entity_delete'); $this->moduleHandler->expects($this->at(6))->method('invokeAll')->with('test_entity_type_delete'); $this->moduleHandler->expects($this->at(7))->method('invokeAll')->with('entity_delete'); $this->keyValueStore->expects($this->once())->method('deleteMultiple')->with(array('foo', 'bar')); $this->entityStorage->delete($entities); }
/** * @covers ::label */ public function testLabel() { // Make a mock with one method that we use as the entity's uri_callback. We // check that it is called, and that the entity's label is the callback's // return value. $callback_label = $this->randomMachineName(); $property_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->at(0))->method('getLabelCallback')->will($this->returnValue(array($callback_container, __FUNCTION__))); $this->entityType->expects($this->at(1))->method('getLabelCallback')->will($this->returnValue(NULL)); $this->entityType->expects($this->at(2))->method('getKey')->with('label')->will($this->returnValue('label')); // Set a dummy property on the entity under test to test that the label can // be returned form a property if there is no callback. $this->entityManager->expects($this->at(1))->method('getDefinition')->with($this->entityTypeId)->will($this->returnValue(array('entity_keys' => array('label' => 'label')))); $this->entity->label = $property_label; $this->assertSame($callback_label, $this->entity->label()); $this->assertSame($property_label, $this->entity->label()); }
/** * Setup for the tests of the ::load() method. */ function setupTestLoad() { // Use an entity type object which has the methods enabled which are being // called by the protected method Entity::getEntityTypeFromStaticClass(). $methods = get_class_methods('Drupal\\Core\\Entity\\EntityType'); unset($methods[array_search('getClass', $methods)]); unset($methods[array_search('setClass', $methods)]); $this->entityType = $this->getMockBuilder('\\Drupal\\Core\\Entity\\EntityType')->disableOriginalConstructor()->setMethods($methods)->getMock(); // Base our mocked entity on a real entity class so we can test if calling // Entity::load() on the base class will bubble up to an actual entity. $this->entityTypeId = 'entity_test_mul'; $methods = get_class_methods('Drupal\\entity_test\\Entity\\EntityTestMul'); unset($methods[array_search('load', $methods)]); unset($methods[array_search('loadMultiple', $methods)]); unset($methods[array_search('create', $methods)]); $this->entity = $this->getMockBuilder('Drupal\\entity_test\\Entity\\EntityTestMul')->disableOriginalConstructor()->setMethods($methods)->getMock(); $this->entityType->setClass(get_class($this->entity)); $this->entityManager->expects($this->once())->method('getDefinitions')->will($this->returnValue(array($this->entityTypeId => $this->entityType))); $this->entityType->expects($this->any())->method('id')->will($this->returnValue($this->entityTypeId)); }
/** * {@inheritdoc} */ 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'])); $this->typedDataManager->expects($this->any())->method('getDefaultConstraints')->willReturn([]); $validation_constraint_manager = $this->getMockBuilder('\\Drupal\\Core\\Validation\\ConstraintManager')->disableOriginalConstructor()->getMock(); $validation_constraint_manager->expects($this->any())->method('create')->willReturn([]); $this->typedDataManager->expects($this->any())->method('getValidationConstraintManager')->willReturn($validation_constraint_manager); $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(LanguageInterface::LANGCODE_NOT_SPECIFIED => $not_specified))); $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->fieldItemList = $this->getMock('\\Drupal\\Core\\Field\\FieldItemListInterface'); $this->fieldTypePluginManager->expects($this->any())->method('createFieldItemList')->willReturn($this->fieldItemList); $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)); $this->entityAdapter = EntityAdapter::createFromEntity($this->entity); }
/** * Prepares an entity that defines a field definition. * * @param bool $custom_invoke_all * (optional) Whether the test will set up its own * ModuleHandlerInterface::invokeAll() implementation. Defaults to FALSE. * @param string $field_definition_id * (optional) The ID to use for the field definition. Defaults to 'id'. * @param array $entity_keys * (optional) An array of entity keys for the mocked entity type. Defaults * to an empty array. * * @return \Drupal\Core\Field\BaseFieldDefinition|\PHPUnit_Framework_MockObject_MockObject * A field definition object. */ protected function setUpEntityWithFieldDefinition($custom_invoke_all = FALSE, $field_definition_id = 'id', $entity_keys = array()) { $this->entityType = $this->getMock('Drupal\\Core\\Entity\\EntityTypeInterface'); $this->entity = $this->getMockBuilder('Drupal\\Tests\\Core\\Entity\\EntityManagerTestEntity')->disableOriginalConstructor()->getMockForAbstractClass(); $entity_class = get_class($this->entity); $this->entityType->expects($this->any())->method('getClass')->will($this->returnValue($entity_class)); $this->entityType->expects($this->any())->method('getKeys')->will($this->returnValue($entity_keys + array('default_langcode' => 'default_langcode'))); $this->entityType->expects($this->any())->method('isSubclassOf')->with($this->equalTo('\\Drupal\\Core\\Entity\\FieldableEntityInterface'))->will($this->returnValue(TRUE)); $field_definition = $this->getMockBuilder('Drupal\\Core\\Field\\BaseFieldDefinition')->disableOriginalConstructor()->getMock(); $entity_class::$baseFieldDefinitions = array($field_definition_id => $field_definition); $entity_class::$bundleFieldDefinitions = array(); $this->moduleHandler = $this->getMock('Drupal\\Core\\Extension\\ModuleHandlerInterface'); $this->moduleHandler->expects($this->any())->method('alter'); if (!$custom_invoke_all) { $this->moduleHandler->expects($this->any())->method('getImplementations')->will($this->returnValue(array())); } // Mock the base field definition override. $override_entity_type = $this->getMock('Drupal\\Core\\Entity\\EntityTypeInterface'); $override_entity_type->expects($this->any())->method('getClass')->will($this->returnValue(get_class($this->entity))); $override_entity_type->expects($this->any())->method('getHandlerClass')->with('storage')->will($this->returnValue('\\Drupal\\Tests\\Core\\Entity\\TestConfigEntityStorage')); $this->setUpEntityManager(array('test_entity_type' => $this->entityType, 'base_field_override' => $override_entity_type)); return $field_definition; }
/** * @covers ::language */ public function testLanguage() { $this->entityType->expects($this->any())->method('getKey')->will($this->returnValueMap(array(array('langcode', 'langcode')))); $this->assertSame('en', $this->entity->language()->getId()); }