function FormTest::testFieldFormJSAddMore in Zircon Profile 8        
                          
                  
                        Same name and namespace in other branches
- 8.0 core/modules/field/src/Tests/FormTest.php \Drupal\field\Tests\FormTest::testFieldFormJSAddMore()
File
 
   - core/modules/field/src/Tests/FormTest.php, line 404
- Contains \Drupal\field\Tests\FormTest.
Class
  
  - FormTest 
- Tests field form handling.
Namespace
  Drupal\field\Tests
Code
function testFieldFormJSAddMore() {
  $field_storage = $this->fieldStorageUnlimited;
  $field_name = $field_storage['field_name'];
  $this->field['field_name'] = $field_name;
  entity_create('field_storage_config', $field_storage)
    ->save();
  entity_create('field_config', $this->field)
    ->save();
  entity_get_form_display($this->field['entity_type'], $this->field['bundle'], 'default')
    ->setComponent($field_name)
    ->save();
  
  $this
    ->drupalGet('entity_test/add');
  
  $this
    ->drupalPostForm(NULL, array(), t('Add another item'));
  $this
    ->drupalPostForm(NULL, array(), t('Add another item'));
  
  $count = 3;
  $delta_range = $count - 1;
  $values = $weights = $pattern = $expected_values = $edit = array();
  for ($delta = 0; $delta <= $delta_range; $delta++) {
    
    do {
      $value = mt_rand(1, 127);
    } while (in_array($value, $values));
    do {
      $weight = mt_rand(-$delta_range, $delta_range);
    } while (in_array($weight, $weights));
    $edit["{$field_name}[{$delta}][value]"] = $value;
    $edit["{$field_name}[{$delta}][_weight]"] = $weight;
    
    $values[$delta] = $value;
    $weights[$delta] = $weight;
    $field_values[$weight]['value'] = (string) $value;
    $pattern[$weight] = "<input [^>]*value=\"{$value}\" [^>]*";
  }
  
  $commands = $this
    ->drupalPostAjaxForm(NULL, $edit, $field_name . '_add_more');
  $this
    ->setRawContent($commands[2]['data']);
  for ($delta = 0; $delta <= $delta_range; $delta++) {
    $this
      ->assertFieldByName("{$field_name}[{$delta}][value]", $values[$delta], "Widget {$delta} is displayed and has the right value");
    $this
      ->assertFieldByName("{$field_name}[{$delta}][_weight]", $weights[$delta], "Widget {$delta} has the right weight");
  }
  ksort($pattern);
  $pattern = implode('.*', array_values($pattern));
  $this
    ->assertPattern("|{$pattern}|s", 'Widgets are displayed in the correct order');
  $this
    ->assertFieldByName("{$field_name}[{$delta}][value]", '', "New widget is displayed");
  $this
    ->assertFieldByName("{$field_name}[{$delta}][_weight]", $delta, "New widget has the right weight");
  $this
    ->assertNoField("{$field_name}[" . ($delta + 1) . '][value]', 'No extraneous widget is displayed');
}