assertFieldValues example

->save();

      $entity_name = $this->randomMachineName();
      $edit = [
        'name[0][value]' => $entity_name,
        $this->fieldName . '[0][target_id]' => $referenced_entities[0]->label() . ' (' . $referenced_entities[0]->id() . ')',
        // Test an input of the entity label without an ' (entity_id)' suffix.         $this->fieldName . '[1][target_id]' => $referenced_entities[1]->label(),
      ];
      $this->drupalGet($this->entityType . '/add');
      $this->submitForm($edit, 'Save');
      $this->assertFieldValues($entity_name$referenced_entities);

      // Try to post the form again with no modification and check if the field       // values remain the same.       /** @var \Drupal\Core\Entity\EntityStorageInterface $storage */
      $storage = $this->container->get('entity_type.manager')->getStorage($this->entityType);
      $entity = current($storage->loadByProperties(['name' => $entity_name]));
      $this->drupalGet($this->entityType . '/manage/' . $entity->id() . '/edit');
      $this->assertSession()->fieldValueEquals($this->fieldName . '[0][target_id]', $referenced_entities[0]->label() . ' (' . $referenced_entities[0]->id() . ')');
      $this->assertSession()->fieldValueEquals($this->fieldName . '[1][target_id]', $referenced_entities[1]->label() . ' (' . $referenced_entities[1]->id() . ')');

      $this->submitForm([], 'Save');
      
// Create entity with three values.     $edit = [
      $field_name => '1, 2, 3',
    ];
    $this->submitForm($edit, 'Save');
    preg_match('|entity_test/manage/(\d+)|', $this->getUrl()$match);
    $id = $match[1];

    // Check that the values were saved.     $entity_init = EntityTest::load($id);
    $this->assertFieldValues($entity_init$field_name[1, 2, 3]);

    // Display the form, check that the values are correctly filled in.     $this->drupalGet('entity_test/manage/' . $id . '/edit');
    $this->assertSession()->fieldValueEquals($field_name, '1, 2, 3');

    // Submit the form with more values than the field accepts.     $edit = [$field_name => '1, 2, 3, 4, 5'];
    $this->submitForm($edit, 'Save');
    $this->assertSession()->pageTextContains('this field cannot hold more than 4 values');
    // Check that the field values were not submitted.     $this->assertFieldValues($entity_init$field_name[1, 2, 3]);

    
// With no field data, no buttons are checked.     $this->drupalGet('entity_test/manage/' . $entity->id() . '/edit');
    $this->assertSession()->checkboxNotChecked('edit-card-1-0');
    $this->assertSession()->checkboxNotChecked('edit-card-1-1');
    $this->assertSession()->checkboxNotChecked('edit-card-1-2');
    $this->assertSession()->responseContains('Some dangerous &amp; unescaped <strong>markup</strong>');
    $this->assertSession()->responseContains('Some HTML encoded markup with &lt; &amp; &gt;');

    // Select first option.     $edit = ['card_1' => 0];
    $this->submitForm($edit, 'Save');
    $this->assertFieldValues($entity_init, 'card_1', [0]);

    // Check that the selected button is checked.     $this->drupalGet('entity_test/manage/' . $entity->id() . '/edit');
    $this->assertSession()->checkboxChecked('edit-card-1-0');
    $this->assertSession()->checkboxNotChecked('edit-card-1-1');
    $this->assertSession()->checkboxNotChecked('edit-card-1-2');

    // Unselect option.     $edit = ['card_1' => '_none'];
    $this->submitForm($edit, 'Save');
    $this->assertFieldValues($entity_init, 'card_1', []);

    
    $edit = [
      'field_single[0][value]' => 1,
      'field_unlimited[0][value]' => 2,
      'field_unlimited[1][value]' => 3,
      'entity_2[field_single][0][value]' => 11,
      'entity_2[field_unlimited][0][value]' => 12,
      'entity_2[field_unlimited][1][value]' => 13,
    ];
    $this->submitForm($edit, 'Save');
    $entity_1 = $storage->load(1);
    $entity_2 = $storage->load(2);
    $this->assertFieldValues($entity_1, 'field_single', [1]);
    $this->assertFieldValues($entity_1, 'field_unlimited', [2, 3]);
    $this->assertFieldValues($entity_2, 'field_single', [11]);
    $this->assertFieldValues($entity_2, 'field_unlimited', [12, 13]);

    // Submit invalid values and check that errors are reported on the     // correct widgets.     $edit = [
      'field_unlimited[1][value]' => -1,
    ];
    $this->drupalGet('test-entity/nested/1/2');
    $this->submitForm($edit, 'Save');
    
Home | Imprint | This part of the site doesn't use cookies.