コード例 #1
0
 /**
  * {@inheritdoc}
  */
 public function setUp()
 {
     parent::setup();
     require_once $this->root . '/core/includes/entity.inc';
     $this->namespaces['Drupal\\Core\\Entity'] = $this->root . '/core/lib/Drupal/Core/Entity';
     $language = $this->prophesize(LanguageInterface::class);
     $language->getId()->willReturn('en');
     $this->languageManager = $this->prophesize(LanguageManagerInterface::class);
     $this->languageManager->getCurrentLanguage()->willReturn($language->reveal());
     $this->languageManager->getLanguages()->willReturn([$language->reveal()]);
     // We need to support multiple entity types, including a test type:
     $type_info = ['test' => ['id' => 'test', 'label' => 'Test', 'entity_keys' => ['bundle' => 'bundle']], 'user' => ['id' => 'user', 'label' => 'Test User', 'entity_keys' => ['bundle' => 'user']], 'node' => ['id' => 'node', 'label' => 'Test Node', 'entity_keys' => ['bundle' => 'dummy']]];
     $type_array = [];
     foreach ($type_info as $type => $info) {
         $entity_type = new ContentEntityType($info);
         $type_array[$type] = $entity_type;
         $this->entityTypeManager->getDefinition($type)->willReturn($entity_type);
         $this->entityManager->getDefinition($type)->willReturn($entity_type);
     }
     // We need a user_role mock as well.
     $role_entity_info = ['id' => 'user_role', 'label' => 'Test Role'];
     $role_type = new ConfigEntityType($role_entity_info);
     $type_array['user_role'] = $role_type;
     $this->entityTypeManager->getDefinitions()->willReturn($type_array);
     $this->entityManager->getDefinitions()->willReturn($type_array);
     $this->entityAccess = $this->prophesize(EntityAccessControlHandlerInterface::class);
     $this->entityTypeManager->getAccessControlHandler(Argument::any())->willReturn($this->entityAccess->reveal());
     // The base field definitions for our test entity aren't used, and would
     // require additional mocking. It doesn't appear that any of our tests rely
     // on this for any other entity type that we are mocking.
     $this->entityFieldManager->getBaseFieldDefinitions(Argument::any())->willReturn([]);
     $this->entityManager->getBaseFieldDefinitions(Argument::any())->willReturn([]);
     // Return some dummy bundle information for now, so that the entity manager
     // does not call out to the config entity system to get bundle information.
     $this->entityTypeBundleInfo->getBundleInfo(Argument::any())->willReturn(['test' => ['label' => 'Test']]);
     $this->entityManager->getBundleInfo(Argument::any())->willReturn(['test' => ['label' => 'Test']]);
     $this->moduleHandler->getImplementations('entity_type_build')->willReturn([]);
     $this->fieldTypeManager = new FieldTypePluginManager($this->namespaces, $this->cacheBackend, $this->moduleHandler->reveal(), $this->typedDataManager);
     $this->container->set('plugin.manager.field.field_type', $this->fieldTypeManager);
 }
コード例 #2
0
 /**
  * {@inheritdoc}
  */
 public function setUp()
 {
     parent::setup();
     require_once $this->root . '/core/includes/entity.inc';
     $this->namespaces['Drupal\\Core\\Entity'] = $this->root . '/core/lib/Drupal/Core/Entity';
     $language = $this->prophesize(LanguageInterface::class);
     $language->getId()->willReturn('en');
     $this->languageManager = $this->prophesize(LanguageManagerInterface::class);
     $this->languageManager->getCurrentLanguage()->willReturn($language->reveal());
     $this->languageManager->getLanguages()->willReturn([$language->reveal()]);
     $entityType = new ContentEntityType(['id' => 'test', 'label' => 'Test', 'entity_keys' => ['bundle' => 'bundle']]);
     $this->entityManager->getDefinitions()->willReturn(['test' => $entityType]);
     $this->entityAccess = $this->prophesize(EntityAccessControlHandlerInterface::class);
     $this->entityManager->getAccessControlHandler(Argument::any())->willReturn($this->entityAccess->reveal());
     // The base field definitions for our test entity aren't used, and would
     // require additional mocking.
     $this->entityManager->getBaseFieldDefinitions('test')->willReturn([]);
     // Return some dummy bundle information for now, so that the entity manager
     // does not call out to the config entity system to get bundle information.
     $this->entityManager->getBundleInfo(Argument::any())->willReturn(['test' => ['label' => 'Test']]);
     $this->moduleHandler->getImplementations('entity_type_build')->willReturn([]);
 }
コード例 #3
0
 /**
  * Tests the getTranslationFromContext() method.
  *
  * @covers ::getTranslationFromContext
  */
 public function testGetTranslationFromContext()
 {
     $language = new Language(['id' => 'en']);
     $this->languageManager->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)->willReturn($language)->shouldBeCalledTimes(1);
     $this->languageManager->getFallbackCandidates(Argument::type('array'))->will(function ($args) {
         $context = $args[0];
         $candidates = array();
         if (!empty($context['langcode'])) {
             $candidates[$context['langcode']] = $context['langcode'];
         }
         return $candidates;
     })->shouldBeCalledTimes(1);
     $translated_entity = $this->prophesize(ContentEntityInterface::class);
     $entity = $this->prophesize(ContentEntityInterface::class);
     $entity->getUntranslated()->willReturn($entity);
     $entity->language()->willReturn($language);
     $entity->hasTranslation(LanguageInterface::LANGCODE_DEFAULT)->willReturn(FALSE);
     $entity->hasTranslation('custom_langcode')->willReturn(TRUE);
     $entity->getTranslation('custom_langcode')->willReturn($translated_entity->reveal());
     $entity->getTranslationLanguages()->willReturn([new Language(['id' => 'en']), new Language(['id' => 'custom_langcode'])]);
     $entity->addCacheContexts(['languages:language_content'])->shouldBeCalled();
     $this->assertSame($entity->reveal(), $this->entityRepository->getTranslationFromContext($entity->reveal()));
     $this->assertSame($translated_entity->reveal(), $this->entityRepository->getTranslationFromContext($entity->reveal(), 'custom_langcode'));
 }
コード例 #4
0
 /**
  * @covers ::getExtraFields
  */
 function testGetExtraFields()
 {
     $this->setUpEntityTypeDefinitions();
     $entity_type_id = $this->randomMachineName();
     $bundle = $this->randomMachineName();
     $language_code = 'en';
     $hook_bundle_extra_fields = [$entity_type_id => [$bundle => ['form' => ['foo_extra_field' => ['label' => 'Foo']]]]];
     $processed_hook_bundle_extra_fields = $hook_bundle_extra_fields;
     $processed_hook_bundle_extra_fields[$entity_type_id][$bundle] += ['display' => []];
     $cache_id = 'entity_bundle_extra_fields:' . $entity_type_id . ':' . $bundle . ':' . $language_code;
     $language = new Language(['id' => $language_code]);
     $this->languageManager->getCurrentLanguage()->willReturn($language)->shouldBeCalledTimes(1);
     $this->cacheBackend->get($cache_id)->shouldBeCalled();
     $this->moduleHandler->invokeAll('entity_extra_field_info')->willReturn($hook_bundle_extra_fields);
     $this->moduleHandler->alter('entity_extra_field_info', $hook_bundle_extra_fields)->shouldBeCalled();
     $this->cacheBackend->set($cache_id, $processed_hook_bundle_extra_fields[$entity_type_id][$bundle], Cache::PERMANENT, ['entity_field_info'])->shouldBeCalled();
     $this->assertSame($processed_hook_bundle_extra_fields[$entity_type_id][$bundle], $this->entityFieldManager->getExtraFields($entity_type_id, $bundle));
 }