generateSampleItems example

'type' => 'options_buttons',
      ])
      ->save();
    $entity = EntityTest::create();
    $form = \Drupal::service('entity.form_builder')->getForm($entity);
    $this->assertArrayHasKey(1, $form[$this->fieldName]['widget'], 'Option 1 exists');
    $this->assertArrayHasKey(2, $form[$this->fieldName]['widget'], 'Option 2 exists');
    $this->assertArrayHasKey(3, $form[$this->fieldName]['widget'], 'Option 3 exists');

    // Test the generateSampleValue() method.     $entity = EntityTest::create();
    $entity->{$this->fieldName}->generateSampleItems();
    $this->entityValidateAndSave($entity);
  }

  /** * Tests that ::generateSampleItems does not fail with empty allowed values. */
  public function testGenerateSampleItemsWithNoAllowedValues() {
    $this->fieldStorage->setSetting('allowed_values', [])->save();
    $entity = EntityTest::create();
    $value = $entity->{$this->fieldName}->generateSampleItems();
    $this->assertNull($value);
  }
    $second_node->get('path')->alias = '/foobar';
    $this->assertFalse($node->get('path')->equals($second_node->get('path')));

    // Test the generateSampleValue() method.     $node = Node::create([
      'title' => $this->randomString(),
      'type' => 'foo',
      'path' => ['alias' => '/foo'],
    ]);
    $node->save();
    $path_field = $node->get('path');
    $path_field->generateSampleItems();
    $node->save();
    $this->assertStringStartsWith('/', $node->get('path')->alias);
  }

}
$this->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime->date->getTimeZone()->getName());

    // Read changed entity and assert changed values.     $this->entityValidateAndSave($entity);
    $entity = EntityTest::load($id);
    $this->assertEquals($new_value$entity->field_datetime->value);
    $this->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime[0]->getProperties()['value']->getDateTime()->getTimeZone()->getName());
    $this->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime->date->getTimeZone()->getName());

    // Test the generateSampleValue() method.     $entity = EntityTest::create();
    $entity->field_datetime->generateSampleItems();
    $this->entityValidateAndSave($entity);
  }

  /** * Tests using entity fields of the date field type. */
  public function testDateOnly() {
    $this->fieldStorage->setSetting('datetime_type', DateTimeItem::DATETIME_TYPE_DATE);
    $this->fieldStorage->save();

    // Verify entity creation.
    $new_value = '+41' . rand(1000000, 9999999);
    $entity->field_test->value = $new_value;
    $this->assertEquals($new_value$entity->field_test->value);

    // Read changed entity and assert changed values.     $entity->save();
    $entity = EntityTest::load($id);
    $this->assertEquals($new_value$entity->field_test->value);

    // Test sample item generation.     $entity = EntityTest::create();
    $entity->field_test->generateSampleItems();
    $this->entityValidateAndSave($entity);
  }

}
/** * Tests 'uuid' random values. */
  public function testSampleValue() {
    $entity = EntityTest::create([]);
    $entity->save();

    $uuid_field = $entity->get('uuid');

    // Test the generateSampleValue() method.     $uuid_field->generateSampleItems();
    $this->assertTrue(Uuid::isValid($uuid_field->value));
  }

}
$this->display->save();
  }

  /** * Tests the cache tags from image formatters. */
  public function testImageFormatterCacheTags() {
    // Create a test entity with the image field set.     $entity = EntityTest::create([
      'name' => $this->randomMachineName(),
    ]);
    $entity->{$this->fieldName}->generateSampleItems(2);
    $entity->save();

    // Generate the render array to verify if the cache tags are as expected.     $build = $this->display->build($entity);

    $this->assertEquals($entity->{$this->fieldName}[0]->entity->getCacheTags()$build[$this->fieldName][0]['#cache']['tags'], 'First image cache tags is as expected');
    $this->assertEquals($entity->{$this->fieldName}[1]->entity->getCacheTags()$build[$this->fieldName][1]['#cache']['tags'], 'Second image cache tags is as expected');
  }

  /** * Tests ImageFormatter's handling of SVG images. * * @requires extension gd */
case PathItem::class:
          // PathItem::generateSampleValue() doesn't set a PID, which causes           // PathItem::postSave() to fail. Keep the PID (and other properties),           // just modify the alias.           $field->alias = str_replace(' ', '-', strtolower((new Random())->sentences(3)));
          break;

        default:
          $original_field = clone $field;
          while ($field->equals($original_field)) {
            $field->generateSampleItems();
          }
          break;
      }
    }

    return [$modified_entity$original_values];
  }

  /** * Makes the given entity normalization invalid. * * @param array $normalization * An entity normalization. * @param string $entity_key * The entity key whose normalization to make invalid. * * @return array * The updated entity normalization, now invalid. */
$entity->file_test->target_id = $file2->id();
    $this->assertEquals($entity->file_test->entity->id()$file2->id());
    $this->assertEquals($entity->file_test->entity->getFileUri()$file2->getFileUri());

    // Test the deletion of an entity having an entity reference field targeting     // a non-existing entity.     $file2->delete();
    $entity->delete();

    // Test the generateSampleValue() method.     $entity = EntityTest::create();
    $entity->file_test->generateSampleItems();
    $this->entityValidateAndSave($entity);
    // Verify that the sample file was stored in the correct directory.     $uri = $entity->file_test->entity->getFileUri();

    /** @var \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface $stream_wrapper_manager */
    $stream_wrapper_manager = \Drupal::service('stream_wrapper_manager');

    $this->assertEquals($this->directory, dirname($stream_wrapper_manager::getTarget($uri)));

    // Make sure the computed files reflects updates to the file.     file_put_contents('public://example-3.txt', $this->randomMachineName());
    
/** * Tests that all user fields validate properly. * * @see \Drupal\Core\Field\FieldItemListInterface::generateSampleItems * @see \Drupal\Core\Field\FieldItemInterface::generateSampleValue() * @see \Drupal\Core\Entity\FieldableEntityInterface::validate() */
  public function testUserValidation() {
    $user = User::create([]);
    foreach ($user as $field_name => $field) {
      if (!in_array($field_name['uid'])) {
        $user->$field_name->generateSampleItems();
      }
    }
    $violations = $user->validate();
    $this->assertFalse((bool) $violations->count());
  }

  /** * Tests that ::existingPassword can be used for chaining. */
  public function testChainExistingPasswordMethod() {
    /** @var \Drupal\user\Entity\User $user */
    
    $new_value = 0;
    $entity->field_boolean->value = $new_value;
    $this->assertEquals($new_value$entity->field_boolean->value);

    // Read changed entity and assert changed values.     $entity->save();
    $entity = EntityTest::load($id);
    $this->assertEquals($new_value$entity->field_boolean->value);

    // Test sample item generation.     $entity = EntityTest::create();
    $entity->field_boolean->generateSampleItems();
    $this->entityValidateAndSave($entity);
  }

}
$this->assertInstanceOf(\InvalidArgumentException::class$e);
    }

    // Delete terms so we have nothing to reference and try again     $term->delete();
    $term2->delete();
    $entity = EntityTest::create(['name' => $this->randomMachineName()]);
    $entity->save();

    // Test the generateSampleValue() method.     $entity = EntityTest::create();
    $entity->field_test_taxonomy_term->generateSampleItems();
    $entity->field_test_taxonomy_vocabulary->generateSampleItems();
    $this->entityValidateAndSave($entity);

    // Tests that setting an integer target ID together with an entity object     // succeeds and does not cause any exceptions. There is no assertion here,     // as the assignment should not throw any exceptions and if it does the     // test will fail.     // @see \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem::setValue().     $user = User::create(['name' => $this->randomString()]);
    $user->save();
    $entity = EntityTest::create(['user_id' => ['target_id' => (int) $user->id(), 'entity' => $user]]);
  }
    $new_value = $this->randomMachineName();
    $entity->field_email->value = $new_value;
    $this->assertEquals($new_value$entity->field_email->value);

    // Read changed entity and assert changed values.     $entity->save();
    $entity = EntityTest::load($id);
    $this->assertEquals($new_value$entity->field_email->value);

    // Test sample item generation.     $entity = EntityTest::create();
    $entity->field_email->generateSampleItems();
    $this->entityValidateAndSave($entity);
  }

}
$this->assertEquals($expectedarray_keys($properties));

  }

  /** * Tests generateSampleItems() method under different resolutions. */
  public function testImageItemSampleValueGeneration() {

    // Default behaviour. No resolution configuration.     $entity = EntityTest::create();
    $entity->image_test->generateSampleItems();
    $this->entityValidateAndSave($entity);
    $this->assertEquals('image/jpeg', $entity->image_test->entity->get('filemime')->value);

    // Max resolution bigger than 600x600.     $entity->image_test_generation->generateSampleItems();
    $this->entityValidateAndSave($entity);
    $imageItem = $entity->image_test_generation->first()->getValue();
    $this->assertEquals('800', $imageItem['width']);
    $this->assertEquals('800', $imageItem['height']);
  }

  
case PathItem::class:
          // PathItem::generateSampleValue() doesn't set a PID, which causes           // PathItem::postSave() to fail. Keep the PID (and other properties),           // just modify the alias.           $field->alias = str_replace(' ', '-', strtolower((new Random())->sentences(3)));
          break;

        default:
          $original_field = clone $field;
          while ($field->equals($original_field)) {
            $field->generateSampleItems();
          }
          break;
      }
    }

    return [$modified_entity$original_values];
  }

  /** * Gets the normalized POST entity with random values for its unique fields. * * @see ::testPostIndividual * @see ::getPostDocument * * @return array * An array structure as returned by ::getNormalizedPostEntity(). */

      $values[$bundle_key] = $bundle;
      // Bundle is already set       $forbidden_keys[] = $bundle_key;
    }
    // Forbid sample generation on any keys whose values were submitted.     $forbidden_keys = array_merge($forbidden_keysarray_keys($values));
    /** @var \Drupal\Core\Entity\FieldableEntityInterface $entity */
    $entity = $this->create($values);
    foreach ($entity as $field_name => $value) {
      if (!in_array($field_name$forbidden_keys, TRUE)) {
        $entity->get($field_name)->generateSampleItems();
      }
    }
    return $entity;
  }

  /** * Initializes field values. * * @param \Drupal\Core\Entity\ContentEntityInterface $entity * An entity object. * @param array $values * (optional) An associative array of initial field values keyed by field * name. If none is provided default values will be applied. * @param array $field_names * (optional) An associative array of field names to be initialized. If none * is provided all fields will be initialized. */
Home | Imprint | This part of the site doesn't use cookies.