You are here

protected function DataTestCaseUI::randomFields in Data 6

Same name and namespace in other branches
  1. 7 data_ui/tests/data_ui.test \DataTestCaseUI::randomFields()

Generate N random fields. Will create at least 1 field.

1 call to DataTestCaseUI::randomFields()
DataTestCaseUI::createTable in data_ui/tests/data_ui.test
Create a table.

File

data_ui/tests/data_ui.test, line 237

Class

DataTestCaseUI
Test basic Data API functionality.

Code

protected function randomFields($n = 5) {
  $fields = array();
  $excluded_types = array();
  for ($i = 0; $i < $n - 1; $i++) {
    $label = $this
      ->uniqueRandomName();
    $name = data_safe_name($label);

    // Get a random type. Make sure that serial does not occur twice.
    $type = $this
      ->randomValue(data_get_field_types(), $excluded_types);
    if ($type == 'serial') {
      $excluded_types['serial'] = 'serial';
    }
    if (!empty($type)) {
      $fields[$name] = array(
        'name' => $name,
        'label' => $label,
        'type' => $type,
      );
      if (rand(0, 1)) {
        $fields[$name]['unsigned'] = 1;
      }

      // Auto increment fields must be indexed.
      if ($fields[$name]['type'] == 'serial' || rand(0, 1)) {
        $fields[$name]['index'] = 1;
      }
      if (rand(0, 1)) {
        $fields[$name]['primary'] = 1;
      }
    }
  }

  // Make sure we have at least one field that is text, PK and indexed.
  $name = $this
    ->uniqueRandomName();
  $fields[data_safe_name($name)] = array(
    'name' => data_safe_name($name),
    'label' => $name,
    'type' => 'text',
    'index' => 1,
    'primary' => 1,
  );
  return $fields;
}