1 entity_query.test EntityFieldQueryTestCase::setUp()

Sets up a Backdrop site for running functional and integration tests.

Generates a random database prefix and installs Backdrop with the specified installation profile in BackdropWebTestCase::$profile into the prefixed database. Afterwards, installs any additional modules specified by the test.

After installation all caches are flushed and several configuration values are reset to the values of the parent site executing the test, since the default values may be incompatible with the environment in which tests are being executed.

Parameters

...: List of modules to enable for the duration of the test. This can be either a single array or a variable number of string arguments.

Return value

bool: TRUE if set up completes, FALSE if an error occurred.

Overrides BackdropWebTestCase::setUp

See also

BackdropWebTestCase::prepareDatabasePrefix()

BackdropWebTestCase::changeDatabasePrefix()

BackdropWebTestCase::prepareEnvironment()

File

core/modules/entity/tests/entity_query.test, line 22
Unit test file for the entity API.

Class

EntityFieldQueryTestCase
Tests EntityFieldQuery.

Code

function setUp() {
  parent::setUp(array('node', 'field_test', 'entity_query_access_test', 'node_access_test'));

  field_test_create_bundle('bundle1');
  field_test_create_bundle('bundle2');
  field_test_create_bundle('test_bundle');
  field_test_create_bundle('test_entity_bundle');

  $instances = array();
  $this->fields = array();
  $this->field_names[0] = $field_name = backdrop_strtolower($this->randomName() . '_field_name');
  $field = array('field_name' => $field_name, 'type' => 'test_field', 'cardinality' => 4);
  $field = field_create_field($field);
  $this->fields[0] = $field;
  $instance = array(
    'field_name' => $field_name,
    'entity_type' => '',
    'bundle' => '',
    'label' => $this->randomName() . '_label',
    'description' => $this->randomName() . '_description',
    'weight' => mt_rand(0, 127),
    'settings' => array(
      'test_instance_setting' => $this->randomName(),
    ),
    'widget' => array(
      'type' => 'test_field_widget',
      'label' => 'Test Field',
      'settings' => array(
        'test_widget_setting' => $this->randomName(),
      )
    )
  );

  $instances[0] = $instance;

  // Add an instance to that bundle.
  $instances[0]['bundle'] = 'bundle1';
  $instances[0]['entity_type'] = 'test_entity_bundle_key';
  field_create_instance($instances[0]);
  $instances[0]['bundle'] = 'bundle2';
  field_create_instance($instances[0]);
  $instances[0]['bundle'] = $instances[0]['entity_type'] = 'test_entity_bundle';
  field_create_instance($instances[0]);

  $this->field_names[1] = $field_name = backdrop_strtolower($this->randomName() . '_field_name');
  $field = array('field_name' => $field_name, 'type' => 'shape', 'cardinality' => 4);
  $field = field_create_field($field);
  $this->fields[1] = $field;
  $instance = array(
    'field_name' => $field_name,
    'entity_type' => '',
    'bundle' => '',
    'label' => $this->randomName() . '_label',
    'description' => $this->randomName() . '_description',
    'weight' => mt_rand(0, 127),
    'settings' => array(
      'test_instance_setting' => $this->randomName(),
    ),
    'widget' => array(
      'type' => 'test_field_widget',
      'label' => 'Test Field',
      'settings' => array(
        'test_widget_setting' => $this->randomName(),
      )
    )
  );

  $instances[1] = $instance;

  // Add a field instance to the bundles.
  $instances[1]['bundle'] = 'bundle1';
  $instances[1]['entity_type'] = 'test_entity_bundle_key';
  field_create_instance($instances[1]);
  $instances[1]['bundle'] = $instances[1]['entity_type'] = 'test_entity_bundle';
  field_create_instance($instances[1]);

  // Write entity base table if there is one.
  $entities = array();

  // Create entities which have a 'bundle key' defined.
  for ($i = 1; $i < 7; $i++) {
    $entity = entity_create('test_entity', array());
    $entity->ftid = $i;
    $entity->fttype = ($i < 5) ? 'bundle1' : 'bundle2';

    $entity->{$this->field_names[0]}[LANGUAGE_NONE][0]['value'] = $i;
    backdrop_write_record('test_entity_bundle_key', $entity);
    field_attach_insert('test_entity_bundle_key', $entity);
  }

  $entity = entity_create('test_entity', array());
  $entity->ftid = 5;
  $entity->fttype = 'test_entity_bundle';
  $entity->{$this->field_names[1]}[LANGUAGE_NONE][0]['shape'] = 'square';
  $entity->{$this->field_names[1]}[LANGUAGE_NONE][0]['color'] = 'red';
  $entity->{$this->field_names[1]}[LANGUAGE_NONE][1]['shape'] = 'circle';
  $entity->{$this->field_names[1]}[LANGUAGE_NONE][1]['color'] = 'blue';
  backdrop_write_record('test_entity_bundle', $entity);
  field_attach_insert('test_entity_bundle', $entity);

  $instances[2] = $instance;
  $instances[2]['bundle'] = 'test_bundle';
  $instances[2]['field_name'] = $this->field_names[0];
  $instances[2]['entity_type'] = 'test_entity';
  field_create_instance($instances[2]);

  // Create entities with support for revisions.
  for ($i = 1; $i < 5; $i++) {
    $entity = entity_create('test_entity', array());
    $entity->ftid = $i;
    $entity->ftvid = $i;
    $entity->fttype = 'test_bundle';
    $entity->{$this->field_names[0]}[LANGUAGE_NONE][0]['value'] = $i;

    backdrop_write_record('test_entity', $entity);
    field_attach_insert('test_entity', $entity);
    backdrop_write_record('test_entity_revision', $entity);
  }

  // Add two revisions to an entity.
  for ($i = 100; $i < 102; $i++) {
    $entity = entity_create('test_entity', array());
    $entity->ftid = 4;
    $entity->ftvid = $i;
    $entity->fttype = 'test_bundle';
    $entity->{$this->field_names[0]}[LANGUAGE_NONE][0]['value'] = $i;

    backdrop_write_record('test_entity', $entity, 'ftid');
    backdrop_write_record('test_entity_revision', $entity);

    db_update('test_entity')
      ->fields(array('ftvid' => $entity->ftvid))
      ->condition('ftid', $entity->ftid)
      ->execute();

    field_attach_update('test_entity', $entity);
  }
}