示例#1
0
 /**
  * {@inheritdoc}
  *
  * @covers ::__construct
  */
 protected function setUp()
 {
     parent::setUp();
     $this->entityType = $this->getMock('Drupal\\Core\\Config\\Entity\\ConfigEntityTypeInterface');
     $this->entityTypeId = 'test_entity_type';
     $this->entityType->expects($this->any())->method('getKey')->will($this->returnValueMap(array(array('id', 'id'), array('uuid', 'uuid'), array('langcode', 'langcode'))));
     $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('getCurrentLanguage')->willReturn(new Language(array('id' => 'hu')));
     $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->cacheTagsInvalidator = $this->getMock('Drupal\\Core\\Cache\\CacheTagsInvalidatorInterface');
     $this->typedConfigManager = $this->getMock('Drupal\\Core\\Config\\TypedConfigManagerInterface');
     $this->typedConfigManager->expects($this->any())->method('getDefinition')->will($this->returnValue(array('mapping' => array('id' => '', 'uuid' => '', 'dependencies' => ''))));
     $this->configManager = $this->getMock('Drupal\\Core\\Config\\ConfigManagerInterface');
     $this->cacheContextsManager = $this->getMockBuilder('Drupal\\Core\\Cache\\Context\\CacheContextsManager')->disableOriginalConstructor()->getMock();
     $container = new ContainerBuilder();
     $container->set('entity.manager', $this->entityManager);
     $container->set('config.typed', $this->typedConfigManager);
     $container->set('cache_tags.invalidator', $this->cacheTagsInvalidator);
     $container->set('config.manager', $this->configManager);
     $container->set('language_manager', $this->languageManager);
     $container->set('cache_contexts_manager', $this->cacheContextsManager);
     \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' => 'en', '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);
 }
示例#3
0
 /**
  * Creates lookup keys for configuration data.
  *
  * @param \Drupal\Core\Config\Config $config
  *   The configuration object.
  *  @param string $key
  *   The configuration key to look for.
  * @param string $get_method
  *   Which method on the config object to call to get the value. Either 'get'
  *   or 'getOriginal'.
  * @param \Drupal\Core\Config\Entity\ConfigEntityTypeInterface $entity_type
  *   The configuration entity type.
  *
  * @return array
  *   An array of lookup keys concatenated to the configuration values.
  *
  * @throws \Drupal\Core\Config\Entity\Query\InvalidLookupKeyException
  *   The provided $key cannot end with a wildcard. This makes no sense since
  *   you cannot do fast lookups against this.
  */
 protected function getKeys(Config $config, $key, $get_method, ConfigEntityTypeInterface $entity_type)
 {
     if (substr($key, -1) == '*') {
         throw new InvalidLookupKeyException(strtr('%entity_type lookup key %key ends with a wildcard this can not be used as a lookup', ['%entity_type' => $entity_type->id(), '%key' => $key]));
     }
     $parts = explode('.*', $key);
     // Remove leading dots.
     array_walk($parts, function (&$value) {
         $value = trim($value, '.');
     });
     $values = (array) $this->getValues($config, $parts[0], $get_method, $parts);
     $output = array();
     // Flatten the array to a single dimension and add the key to all the
     // values.
     array_walk_recursive($values, function ($current) use(&$output, $key) {
         if (is_scalar($current)) {
             $current = $key . ':' . $current;
         }
         $output[] = $current;
     });
     return $output;
 }
 /**
  * @covers ::toArray
  *
  * @expectedException \Drupal\Core\Config\Schema\SchemaIncompleteException
  */
 public function testToArrayFallback()
 {
     $this->entityType->expects($this->any())->method('getPropertiesToExport')->willReturn([]);
     $this->entity->toArray();
 }
 /**
  * Modifies an entity type definition to include moderation configuration support.
  *
  * That "configuration support" includes a configuration form, a hypermedia
  * link, and a route provider to tie it all together. There's also a
  * moderation handler for per-entity-type variation.
  *
  * @param \Drupal\Core\Config\Entity\ConfigEntityTypeInterface $type
  *   The config entity definition to modify.
  *
  * @return \Drupal\Core\Config\Entity\ConfigEntityTypeInterface
  *   The modified config entity definition.
  */
 protected function addModerationToEntityType(ConfigEntityTypeInterface $type)
 {
     if ($type->hasLinkTemplate('edit-form') && !$type->hasLinkTemplate('moderation-form')) {
         $type->setLinkTemplate('moderation-form', $type->getLinkTemplate('edit-form') . '/moderation');
     }
     if (!$type->getFormClass('moderation')) {
         $type->setFormClass('moderation', BundleModerationConfigurationForm::class);
     }
     // @todo Core forgot to add a direct way to manipulate route_provider, so
     // we have to do it the sloppy way for now.
     $providers = $type->getRouteProviderClasses() ?: [];
     if (empty($providers['moderation'])) {
         $providers['moderation'] = EntityTypeModerationRouteProvider::class;
         $type->setHandlerClass('route_provider', $providers);
     }
     return $type;
 }
示例#6
0
文件: Query.php 项目: 318io/318-io
 /**
  * Loads the config records to examine for the query.
  *
  * @return array
  *   Config records keyed by entity IDs.
  */
 protected function loadRecords()
 {
     $prefix = $this->entityType->getConfigPrefix() . '.';
     $prefix_length = strlen($prefix);
     // Search the conditions for restrictions on configuration object names.
     $names = FALSE;
     $id_condition = NULL;
     $id_key = $this->entityType->getKey('id');
     if ($this->condition->getConjunction() == 'AND') {
         $lookup_keys = $this->entityType->getLookupKeys();
         $conditions = $this->condition->conditions();
         foreach ($conditions as $condition_key => $condition) {
             $operator = $condition['operator'] ?: (is_array($condition['value']) ? 'IN' : '=');
             if (is_string($condition['field']) && ($operator == 'IN' || $operator == '=')) {
                 // Special case ID lookups.
                 if ($condition['field'] == $id_key) {
                     $ids = (array) $condition['value'];
                     $names = array_map(function ($id) use($prefix) {
                         return $prefix . $id;
                     }, $ids);
                 } elseif (in_array($condition['field'], $lookup_keys)) {
                     // If we don't find anything then there are no matches. No point in
                     // listing anything.
                     $names = array();
                     $keys = (array) $condition['value'];
                     $keys = array_map(function ($value) use($condition) {
                         return $condition['field'] . ':' . $value;
                     }, $keys);
                     foreach ($this->getConfigKeyStore()->getMultiple($keys) as $list) {
                         $names = array_merge($names, $list);
                     }
                 }
             } elseif (!$id_condition && $condition['field'] == $id_key) {
                 $id_condition = $condition;
             }
             // We stop at the first restricting condition on name. In the case where
             // there are additional restricting conditions, results will be
             // eliminated when the conditions are checked on the loaded records.
             if ($names !== FALSE) {
                 // If the condition has been responsible for narrowing the list of
                 // configuration to check there is no point in checking it further.
                 unset($conditions[$condition_key]);
                 break;
             }
         }
     }
     // If no restrictions on IDs were found, we need to parse all records.
     if ($names === FALSE) {
         $names = $this->configFactory->listAll($prefix);
     }
     // In case we have an ID condition, try to narrow down the list of config
     // objects to load.
     if ($id_condition && !empty($names)) {
         $value = $id_condition['value'];
         $filter = NULL;
         switch ($id_condition['operator']) {
             case '<>':
                 $filter = function ($name) use($value, $prefix_length) {
                     $id = substr($name, $prefix_length);
                     return $id !== $value;
                 };
                 break;
             case 'STARTS_WITH':
                 $filter = function ($name) use($value, $prefix_length) {
                     $id = substr($name, $prefix_length);
                     return strpos($id, $value) === 0;
                 };
                 break;
             case 'CONTAINS':
                 $filter = function ($name) use($value, $prefix_length) {
                     $id = substr($name, $prefix_length);
                     return strpos($id, $value) !== FALSE;
                 };
                 break;
             case 'ENDS_WITH':
                 $filter = function ($name) use($value, $prefix_length) {
                     $id = substr($name, $prefix_length);
                     return strrpos($id, $value) === strlen($id) - strlen($value);
                 };
                 break;
         }
         if ($filter) {
             $names = array_filter($names, $filter);
         }
     }
     // Load the corresponding records.
     $records = array();
     foreach ($this->configFactory->loadMultiple($names) as $config) {
         $records[substr($config->getName(), $prefix_length)] = $config->get();
     }
     return $records;
 }