/** * {@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'])); $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())); $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)); }
/** * {@inheritdoc} */ public function setUp() { $this->id = 1; $values = array('id' => $this->id, 'langcode' => 'en', 'uuid' => '3bb9ee60-bea5-4622-b89b-a63319d10b3a'); $this->entityTypeId = $this->randomName(); $this->bundle = $this->randomName(); $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(); $language = new Language(array('id' => 'en')); $this->languageManager = $this->getMock('\\Drupal\\Core\\Language\\LanguageManagerInterface'); $this->languageManager->expects($this->any())->method('getLanguages')->will($this->returnValue(array('en' => $language))); $this->languageManager->expects($this->any())->method('getLanguage')->with('en')->will($this->returnValue($language)); $this->fieldTypePluginManager = $this->getMockBuilder('\\Drupal\\Core\\Field\\FieldTypePluginManager')->disableOriginalConstructor()->getMock(); $this->fieldTypePluginManager->expects($this->any())->method('getDefaultSettings')->will($this->returnValue(array())); $this->fieldTypePluginManager->expects($this->any())->method('getDefaultInstanceSettings')->will($this->returnValue(array())); $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' => FieldDefinition::create('integer'), 'revision_id' => FieldDefinition::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)); }
/** * @covers ::isNewRevision * @covers ::setNewRevision */ public function testIsNewRevision() { // Set up the entity type so that on the first call there is no revision key // and on the second call there is one. $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()); }
protected function interact(InputInterface $input, OutputInterface $output) { $io = new DrupalStyle($input, $output); // --module option $module = $input->getOption('module'); if (!$module) { // @see Drupal\Console\Command\Shared\ModuleTrait::moduleQuestion $module = $this->moduleQuestion($io); $input->setOption('module', $module); } // --class option $class = $input->getOption('class'); if (!$class) { $class = $io->ask($this->trans('commands.generate.plugin.fieldformatter.questions.class'), 'ExampleFieldFormatter'); $input->setOption('class', $class); } // --plugin label option $label = $input->getOption('label'); if (!$label) { $label = $io->ask($this->trans('commands.generate.plugin.fieldformatter.questions.label'), $this->stringConverter->camelCaseToHuman($class)); $input->setOption('label', $label); } // --name option $plugin_id = $input->getOption('plugin-id'); if (!$plugin_id) { $plugin_id = $io->ask($this->trans('commands.generate.plugin.fieldformatter.questions.plugin-id'), $this->stringConverter->camelCaseToUnderscore($class)); $input->setOption('plugin-id', $plugin_id); } // --field type option $field_type = $input->getOption('field-type'); if (!$field_type) { // Gather valid field types. $field_type_options = []; foreach ($this->fieldTypePluginManager->getGroupedDefinitions($this->fieldTypePluginManager->getUiDefinitions()) as $category => $field_types) { foreach ($field_types as $name => $field_type) { $field_type_options[] = $name; } } $field_type = $io->choice($this->trans('commands.generate.plugin.fieldwidget.questions.field-type'), $field_type_options); $input->setOption('field-type', $field_type); } }