generateSampleValue example


  protected function getModifiedEntityForPostTesting() {
    $normalized_entity = $this->getNormalizedPostEntity();

    // Ensure that all the unique fields of the entity type get a new random     // value.     foreach (static::$uniqueFieldNames as $field_name) {
      $field_definition = $this->entity->getFieldDefinition($field_name);
      $field_type_class = $field_definition->getItemDefinition()->getClass();
      $normalized_entity[$field_name] = $field_type_class::generateSampleValue($field_definition);
    }

    return $normalized_entity;
  }

  /** * {@inheritdoc} */
  protected function getExpectedUnauthorizedAccessMessage($method) {
    $permission = $this->entity->getEntityType()->getAdminPermission();
    if ($permission !== FALSE) {
      

  public function testGenerateSampleValue(int $max_length): void {
    $definition = $this->prophesize(FieldDefinitionInterface::class);
    $definition->getSetting('max_length')->willReturn($max_length);

    for ($i = 0; $i < 1000; $i++) {
      $sample_value = UriItem::generateSampleValue($definition->reveal());
      $this->assertLessThanOrEqual($max_lengthmb_strlen($sample_value['value']));
      $this->assertStringNotContainsString(' ', $sample_value['value']);
    }
  }

  /** * Data provider for maximum-lengths. * * @return array * Test cases. */
  

  public function testGenerateSampleValue(int $max_length): void {
    $definition = $this->prophesize(FieldDefinitionInterface::class);
    $definition->getSetting('max_length')->willReturn($max_length);

    for ($i = 0; $i < 1000; $i++) {
      $sample_value = UserNameItem::generateSampleValue($definition->reveal());
      $this->assertLessThanOrEqual($max_lengthmb_strlen($sample_value['value']));
      $this->assertEquals(trim($sample_value['value'], ' ')$sample_value['value']);
    }
  }

  /** * Data provider for maximum-lengths. * * @return array * Test cases. */
  

  public function testTextFieldSampleValue($max_length) {
    // Create a text field.     $field_definition = BaseFieldDefinition::create('text')
      ->setTargetEntityTypeId('foo');

    // Ensure testing of max_lengths from 1 to 3 because generateSampleValue     // creates a sentence with a maximum number of words set to 1/3 of the     // max_length of the field.     $field_definition->setSetting('max_length', $max_length);
    $sample_value = TextItemBase::generateSampleValue($field_definition);
    $this->assertEquals($max_lengthstrlen($sample_value['value']));
  }

  /** * Data provider for testTextFieldSampleValue. */
  public function providerTextFieldSampleValue() {
    return [
      [
        1,
      ],
      [

  protected function getModifiedEntityForPostTesting() {
    $document = $this->getPostDocument();

    // Ensure that all the unique fields of the entity type get a new random     // value.     foreach (static::$uniqueFieldNames as $field_name) {
      $field_definition = $this->entity->getFieldDefinition($field_name);
      $field_type_class = $field_definition->getItemDefinition()->getClass();
      $document['data']['attributes'][$field_name] = $field_type_class::generateSampleValue($field_definition);
    }

    return $document;
  }

  /** * Tests sparse field sets. * * @param \Drupal\Core\Url $url * The base URL with which to test includes. * @param array $request_options * Request options to apply. * * @see \GuzzleHttp\ClientInterface::request() */
->getFieldDefinitions($this->storage->getEntityTypeId()$bundle);
    foreach ($fields as $field_name => $field_definition) {
      if ($field_definition->isRequired() && is_null($row->getDestinationProperty($field_name))) {
        // Use the configured default value for this specific field, if any.         if ($default_value = $field_definition->getDefaultValueLiteral()) {
          $values = $default_value;
        }
        else {
          /** @var \Drupal\Core\Field\FieldItemInterface $field_type_class */
          $field_type_class = $this->fieldTypeManager
            ->getPluginClass($field_definition->getType());
          $values = $field_type_class::generateSampleValue($field_definition);
          if (is_null($values)) {
            // Handle failure to generate a sample value.             throw new MigrateException('Stubbing failed, unable to generate value for field ' . $field_name);
          }
        }

        $row->setDestinationProperty($field_name$values);
      }
    }
  }

  

  public function testGenerateSampleValue(int $max_length): void {
    foreach ([TRUE, FALSE] as $unique) {
      $definition = $this->prophesize(FieldDefinitionInterface::class);
      $constraints = $unique ? [$this->prophesize(UniqueFieldConstraint::class)] : [];
      $definition->getConstraint('UniqueField')->willReturn($constraints);
      $definition->getSetting('max_length')->willReturn($max_length);
      for ($i = 0; $i < 1000; $i++) {
        $sample_value = StringItem::generateSampleValue($definition->reveal());
        // When the field value needs to be unique, the generated sample value         // should match the maximum length to ensure sufficient entropy.         if ($unique) {
          $this->assertEquals($max_lengthmb_strlen($sample_value['value']));
        }
        else {
          $this->assertLessThanOrEqual($max_lengthmb_strlen($sample_value['value']));
        }
      }
    }
  }

  

  protected function processStubRow(Row $row) {
    parent::processStubRow($row);

    $field_definitions = $this->entityFieldManager
      ->getFieldDefinitions($this->storage->getEntityTypeId(),
        $this->getKey('bundle'));

    // Name is generated using a dedicated sample value generator to ensure     // uniqueness and a valid length.     // @todo Remove this as part of https://www.drupal.org/node/3352288.     $name = UserNameItem::generateSampleValue($field_definitions['name']);
    $row->setDestinationProperty('name', reset($name));

    // Email address is not defined as required in the base field definition but     // is effectively required by the UserMailRequired constraint. This means     // that Entity::processStubRow() did not populate it - we do it here.     $mail = EmailItem::generateSampleValue($field_definitions['mail']);
    $row->setDestinationProperty('mail', reset($mail));
  }

  /** * {@inheritdoc} */


  /** * {@inheritdoc} */
  protected function processStubRow(Row $row) {
    // We stub the uri value ourselves so we can create a real stub file for it.     if (!$row->getDestinationProperty('uri')) {
      $field_definitions = $this->entityFieldManager
        ->getFieldDefinitions($this->storage->getEntityTypeId(),
          $this->getKey('bundle'));
      $value = UriItem::generateSampleValue($field_definitions['uri']);
      if (empty($value)) {
        throw new MigrateException('Stubbing failed, unable to generate value for field uri');
      }
      // generateSampleValue() wraps the value in an array.       $value = reset($value);
      // Make it into a proper public file uri, stripping off the existing       // scheme if present.       $value = 'public://' . preg_replace('|^[a-z]+://|i', '', $value);
      $value = mb_substr($value, 0, $field_definitions['uri']->getSetting('max_length'));
      // Create a real file, so File::preSave() can do filesize() on it.       touch($value);
      
$view_builder = \Drupal::entityTypeManager()->getViewBuilder($this->getEntity()->getEntityTypeId());
    return $view_builder->viewField($this$display_options);
  }

  /** * {@inheritdoc} */
  public function generateSampleItems($count = 1) {
    $field_definition = $this->getFieldDefinition();
    $field_type_class = $field_definition->getItemDefinition()->getClass();
    for ($delta = 0; $delta < $count$delta++) {
      $values[$delta] = $field_type_class::generateSampleValue($field_definition);
    }
    $this->setValue($values);
  }

  /** * {@inheritdoc} */
  public function getConstraints() {
    $constraints = parent::getConstraints();
    // Check that the number of values doesn't exceed the field cardinality. For     // form submitted values, this can only happen with 'multiple value'
Home | Imprint | This part of the site doesn't use cookies.