_generateTestFieldValues example

$this->createFieldWithStorage('', $entity_type);
    $cardinality = $this->fieldTestData->field_storage->getCardinality();

    // TODO : test empty values filtering and "compression" (store consecutive deltas).     // Preparation: create three revisions and store them in $revision array.     $values = [];
    $entity = $this->container->get('entity_type.manager')
      ->getStorage($entity_type)
      ->create();
    for ($revision_id = 0; $revision_id < 3; $revision_id++) {
      // Note: we try to insert one extra value.       $current_values = $this->_generateTestFieldValues($cardinality + 1);
      $entity->{$this->fieldTestData->field_name}->setValue($current_values);
      $entity->setNewRevision();
      $entity->save();
      $entity_id = $entity->id();
      $current_revision = $entity->getRevisionId();
      $values[$current_revision] = $current_values;
    }

    $storage = $this->container->get('entity_type.manager')->getStorage($entity_type);
    $storage->resetCache();
    $entity = $storage->load($entity_id);
    

  public function testEntityDisplayBuild() {
    $this->createFieldWithStorage('_2');

    $entity_type = 'entity_test';
    $entity_init = $this->container->get('entity_type.manager')
      ->getStorage($entity_type)
      ->create();

    // Populate values to be displayed.     $values = $this->_generateTestFieldValues($this->fieldTestData->field_storage->getCardinality());
    $entity_init->{$this->fieldTestData->field_name}->setValue($values);
    $values_2 = $this->_generateTestFieldValues($this->fieldTestData->field_storage_2->getCardinality());
    $entity_init->{$this->fieldTestData->field_name_2}->setValue($values_2);

    // Simple formatter, label displayed.     $entity = clone($entity_init);
    $display = \Drupal::service('entity_display.repository')
      ->getViewDisplay($entity_type$entity->bundle(), 'full');

    $formatter_setting = $this->randomMachineName();
    $display_options = [
      
// Prepare the field translations.     $entity_type_id = 'entity_test';
    field_test_entity_info_translatable($entity_type_id, TRUE);
    $entity = $this->container->get('entity_type.manager')
      ->getStorage($entity_type_id)
      ->create(['type' => $this->field->getTargetBundle()]);
    $field_translations = [];
    $available_langcodes = array_keys($this->container->get('language_manager')->getLanguages());
    $entity->langcode->value = reset($available_langcodes);
    foreach ($available_langcodes as $langcode) {
      $field_translations[$langcode] = $this->_generateTestFieldValues($this->fieldStorage->getCardinality());
      $translation = $entity->hasTranslation($langcode) ? $entity->getTranslation($langcode) : $entity->addTranslation($langcode);
      $translation->{$this->fieldName}->setValue($field_translations[$langcode]);
    }

    // Save and reload the field translations.     $entity = $this->entitySaveReload($entity);

    // Check if the correct values were saved/loaded.     foreach ($field_translations as $langcode => $items) {
      $result = TRUE;
      foreach ($items as $delta => $item) {
        
// Create a display for the default view mode.     $display_repository->getViewDisplay($field['entity_type']$field['bundle'])
      ->setComponent($this->fieldName, $this->displayOptions['default'])
      ->save();
    // Create a display for the teaser view mode.     EntityViewMode::create(['id' => 'entity_test.teaser', 'targetEntityType' => 'entity_test'])->save();
    $display_repository->getViewDisplay($field['entity_type']$field['bundle'], 'teaser')
      ->setComponent($this->fieldName, $this->displayOptions['teaser'])
      ->save();

    // Create an entity with values.     $this->values = $this->_generateTestFieldValues($this->cardinality);
    $this->entity = EntityTest::create();
    $this->entity->{$this->fieldName}->setValue($this->values);
    $this->entity->save();
  }

  /** * Tests the FieldItemListInterface::view() method. */
  public function testFieldItemListView() {
    $items = $this->entity->get($this->fieldName);

    
$entity_init = $this->container->get('entity_type.manager')
      ->getStorage($entity_type)
      ->create(['type' => $entity_type]);
    $cardinality = $this->fieldTestData->field_storage_2->getCardinality();

    $this->assertFalse($this->fieldTestData->field_storage_2->hasData(), 'There are no entities with field data.');
    $this->assertSame(0, $this->storageRev->countFieldData($this->fieldTestData->field_storage_2), 'There are 0 entities with field data.');

    // Create 1 entity with the field.     $entity = clone($entity_init);
    $values = $this->_generateTestFieldValues($this->fieldTestData->field_storage_2->getCardinality());
    $entity->{$this->fieldTestData->field_name_2} = $values;
    $entity->setNewRevision();
    $entity->save();
    $first_revision = $entity->getRevisionId();

    $this->assertTrue($this->fieldTestData->field_storage_2->hasData(), 'There are entities with field data.');
    $this->assertSame(1, $this->storageRev->countFieldData($this->fieldTestData->field_storage_2), 'There is 1 entity with field data.');

    $entity->{$this->fieldTestData->field_name_2} = [];
    $entity->setNewRevision();
    $entity->save();

    
foreach ($this->fieldStorages as $field_storage) {
        FieldConfig::create([
          'field_storage' => $field_storage,
          'bundle' => $bundle,
        ])->save();
      }
      for ($i = 0; $i < 10; $i++) {
        $entity = $this->container->get('entity_type.manager')
          ->getStorage($this->entityTypeId)
          ->create(['type' => $bundle]);
        foreach ($this->fieldStorages as $field_storage) {
          $entity->{$field_storage->getName()}->setValue($this->_generateTestFieldValues($field_storage->getCardinality()));
        }
        $entity->save();
      }
    }
    $this->entities = $this->container->get('entity_type.manager')
      ->getStorage($this->entityTypeId)->loadMultiple();
    foreach ($this->entities as $entity) {
      // This test relies on the entities having stale field definitions       // so that the deleted field can be accessed on them. Access the field       // now, so that they are always loaded.       $entity->bf_1->value;

      
Home | Imprint | This part of the site doesn't use cookies.