loadEntityByName example

protected function doTestFormCRUD($entity_type) {
    $name1 = $this->randomMachineName(8);
    $name2 = $this->randomMachineName(10);

    $edit = [
      'name[0][value]' => $name1,
      'field_test_text[0][value]' => $this->randomMachineName(16),
    ];

    $this->drupalGet($entity_type . '/add');
    $this->submitForm($edit, 'Save');
    $entity = $this->loadEntityByName($entity_type$name1);
    $this->assertNotNull($entitynew FormattableMarkup('%entity_type: Entity found in the database.', ['%entity_type' => $entity_type]));

    $edit['name[0][value]'] = $name2;
    $this->drupalGet($entity_type . '/manage/' . $entity->id() . '/edit');
    $this->submitForm($edit, 'Save');
    $entity = $this->loadEntityByName($entity_type$name1);
    $this->assertNull($entitynew FormattableMarkup('%entity_type: The entity has been modified.', ['%entity_type' => $entity_type]));
    $entity = $this->loadEntityByName($entity_type$name2);
    $this->assertNotNull($entitynew FormattableMarkup('%entity_type: Modified entity found in the database.', ['%entity_type' => $entity_type]));
    $this->assertNotEquals($name1$entity->name->value, new FormattableMarkup('%entity_type: The entity name has been modified.', ['%entity_type' => $entity_type]));

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