entityValidateAndSave example

$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);
  }

}
// Change the format, this should update the processed properties.     $entity->summary_field->format = 'no_filters';
    $this->assertSame($value(string) $entity->summary_field->processed);
    $this->assertSame($summary(string) $entity->summary_field->summary_processed);

    // Test the generateSampleValue() method.     $entity = $this->container->get('entity_type.manager')
      ->getStorage($entity_type)
      ->create();
    $entity->summary_field->generateSampleItems();
    $this->entityValidateAndSave($entity);
  }

  /** * Creates a text_with_summary field storage and field. * * @param string $entity_type * Entity type for which the field should be created. */
  protected function createField($entity_type) {
    // Create a field .     $this->fieldStorage = FieldStorageConfig::create([
      
    $id = $entity->id();
    $storage = $this->container->get('entity_type.manager')->getStorage('entity_test');
    $storage->resetCache([$id]);
    $entity = $storage->load($id);
    $this->assertInstanceOf(FieldItemListInterface::class$entity->comment);
    $this->assertInstanceOf(CommentItemInterface::class$entity->comment[0]);

    // Test sample item generation.     /** @var \Drupal\entity_test\Entity\EntityTest $entity */
    $entity = EntityTest::create();
    $entity->comment->generateSampleItems();
    $this->entityValidateAndSave($entity);
    $this->assertContains($entity->get('comment')->status, [
      CommentItemInterface::HIDDEN,
      CommentItemInterface::CLOSED,
      CommentItemInterface::OPEN,
    ], 'Comment status value in defined range');

    $mainProperty = $entity->comment[0]->mainPropertyName();
    $this->assertEquals('status', $mainProperty);
  }

  /** * Tests comment author name. */

      ->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);
  }

}

  public function testDateTime() {
    $this->fieldStorage->setSetting('datetime_type', DateTimeItem::DATETIME_TYPE_DATETIME);
    $this->fieldStorage->save();

    // Verify entity creation.     $entity = EntityTest::create();
    $value = '2014-01-01T20:00:00';
    $entity->field_datetime = $value;
    $entity->name->value = $this->randomMachineName();
    $this->entityValidateAndSave($entity);

    // Verify entity has been created properly.     $id = $entity->id();
    $entity = EntityTest::load($id);
    $this->assertInstanceOf(FieldItemListInterface::class$entity->field_datetime);
    $this->assertInstanceOf(FieldItemInterface::class$entity->field_datetime[0]);
    $this->assertEquals($value$entity->field_datetime->value);
    $this->assertEquals($value$entity->field_datetime[0]->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());

    
$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());
    // Test unsaved file entity.


  /** * Tests using entity fields of the datetime field type. */
  public function testDateTime() {
    // Verify entity creation.     $entity = EntityTest::create();
    $value = 1488914208;
    $entity->field_timestamp = $value;
    $entity->name->value = $this->randomMachineName();
    $this->entityValidateAndSave($entity);

    // Verify entity has been created properly.     $id = $entity->id();
    $entity = EntityTest::load($id);
    $this->assertInstanceOf(FieldItemListInterface::class$entity->field_timestamp);
    $this->assertInstanceOf(FieldItemInterface::class$entity->field_timestamp[0]);
    $this->assertEquals($entity->field_timestamp->value, $value);
    $this->assertEquals($entity->field_timestamp[0]->value, $value);

    // Verify changing the date value.     $new_value = 1488914000;
    
$entity->field_integer
      ->getFieldDefinition()
      ->setSetting('min', 99)
      ->setSetting('max', 100);

    $entity->field_decimal->generateSampleItems();
    $entity->field_integer->generateSampleItems();
    $entity->field_float->generateSampleItems();

    // Confirm that the generated sample values are within range.     $this->entityValidateAndSave($entity);
  }

  /** * Tests constraints on numeric item fields. * * @dataProvider dataNumberFieldSettingsProvider * * @param string $type * The field type. * @param int|float $min * The minimum field value. * @param int|float $max * The maximum field value. * @param int|float $value * The test value. * @param bool $expect_constraints * If TRUE this data set will trigger a validation constraint. * @param string $expected_constraint_message * The expected constraint violation message. * * @throws \Drupal\Core\Entity\EntityStorageException */
$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);
  }

}
$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);
  }

}


  /** * 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']);
  }

  /** * Tests a malformed image. */
// Check that setting LinkItem value NULL doesn't generate any error or     // warning.     $entity->field_test[0] = NULL;
    $this->assertNull($entity->field_test[0]->getValue());

    // Test the generateSampleValue() method for generic, external, and internal     // link types.     $entity = EntityTest::create();
    $entity->field_test->generateSampleItems();
    $entity->field_test_external->generateSampleItems();
    $entity->field_test_internal->generateSampleItems();
    $this->entityValidateAndSave($entity);
  }

}
// 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]]);
  }

  
// Create a form display for the default form mode.     \Drupal::service('entity_display.repository')
      ->getFormDisplay('entity_test', 'entity_test')
      ->setComponent($field_name[
        'type' => 'uri',
      ])
      ->save();

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

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