You are here

public function MultifieldUnitTestCase::testMultifieldField in Multifield 7.2

Same name and namespace in other branches
  1. 7 tests/MultifieldUnitTestCase.test \MultifieldUnitTestCase::testMultifieldField()

File

tests/MultifieldUnitTestCase.test, line 13

Class

MultifieldUnitTestCase

Code

public function testMultifieldField() {
  $multifield_field = array(
    'field_name' => 'test',
    'type' => 'multifield',
  );
  $dbschema = Database::getConnection()
    ->schema();
  $this
    ->assertIdentical(multifield_get_fields(), array());
  $this
    ->assertIdentical(multifield_type_has_fields('test'), FALSE);
  $this
    ->assertIdentical(multifield_type_get_fields('test'), array());
  $this
    ->assertIdentical(multifield_get_subfields(), array());
  $this
    ->assertIdentical(multifield_type_has_subfields('test'), FALSE);
  $this
    ->assertIdentical(multifield_type_get_subfields('test'), array());
  $this
    ->assertTrue(field_info_field_types('multifield'));
  $schema = module_invoke('multifield', 'field_schema', $multifield_field);
  $this
    ->assertIdentical($schema, array());
  $this
    ->assertFalse($dbschema
    ->tableExists('field_data_test'));
  $multifield_field = field_create_field($multifield_field);
  $this
    ->assertTrue($multifield_field['id']);
  $multifield = multifield_load('test');
  $this
    ->assertFalse(isset($multifield->mfid));
  $this
    ->assertTrue($multifield->locked);
  $this
    ->assertIdentical(multifield_get_fields(), array(
    'test' => 'test',
  ));
  $this
    ->assertIdentical(multifield_type_has_fields('test'), TRUE);
  $this
    ->assertIdentical(multifield_type_get_fields('test'), array(
    'test',
  ));
  $this
    ->assertIdentical(multifield_get_subfields(), array());
  $this
    ->assertIdentical(multifield_type_has_subfields('test'), FALSE);
  $this
    ->assertIdentical(multifield_type_get_subfields('test'), array());

  //$this->assertFalse(field_info_field_types('test'), NULL);
  $schema = module_invoke('multifield', 'field_schema', $multifield_field);
  $this
    ->assertIdentical($schema, array());
  $this
    ->assertTrue($dbschema
    ->tableExists('field_data_test'));
  $this
    ->assertFalse($dbschema
    ->fieldExists('field_data_test', 'test_field_test_field_1_value'));
  $this
    ->assertFalse($dbschema
    ->indexExists('field_data_test', 'test_field_test_field_1_value'));
  $this
    ->assertFalse($dbschema
    ->fieldExists('field_data_test', 'test_field_test_field_2_value'));
  $this
    ->assertFalse($dbschema
    ->indexExists('field_data_test', 'test_field_test_field_2_value'));
  $field_name1 = 'field_test_field_1';
  $field1 = array(
    'field_name' => $field_name1,
    'type' => 'test_field',
    'cardinality' => 4,
  );
  $field1 = field_create_field($field1);
  $instance1 = array(
    'field_name' => $field_name1,
    'entity_type' => 'multifield',
    'bundle' => 'test',
    'label' => $field_name1 . '_label',
    'description' => $field_name1 . '_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(),
      ),
    ),
  );
  field_create_instance($instance1);
  $this
    ->assertIdentical(multifield_get_fields(), array(
    'test' => 'test',
  ));
  $this
    ->assertIdentical(multifield_type_has_fields('test'), TRUE);
  $this
    ->assertIdentical(multifield_type_get_fields('test'), array(
    'test',
  ));
  $this
    ->assertIdentical(multifield_get_subfields(), array(
    'test' => array(
      $field_name1,
    ),
  ));
  $this
    ->assertIdentical(multifield_type_has_subfields('test'), TRUE);
  $this
    ->assertIdentical(multifield_type_get_subfields('test'), array(
    $field_name1,
  ));

  //$this->assertTrue(field_info_field_types('test'));
  $schema = module_invoke('multifield', 'field_schema', $multifield_field);
  $this
    ->assertIdentical($schema, array(
    'columns' => array(
      $field_name1 . '_value' => array(
        'type' => 'int',
        'size' => 'medium',
        'not null' => FALSE,
      ),
      'id' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
    ),
    'indexes' => array(
      $field_name1 . '_value' => array(
        $field_name1 . '_value',
      ),
      'id' => array(
        'id',
      ),
    ),
  ));
  $this
    ->assertTrue($dbschema
    ->tableExists('field_data_test'));
  $this
    ->assertTrue($dbschema
    ->fieldExists('field_data_test', 'test_field_test_field_1_value'));
  $this
    ->assertTrue($dbschema
    ->indexExists('field_data_test', 'test_field_test_field_1_value'));
  $this
    ->assertFalse($dbschema
    ->fieldExists('field_data_test', 'test_field_test_field_2_value'));
  $this
    ->assertFalse($dbschema
    ->indexExists('field_data_test', 'test_field_test_field_2_value'));
  $field_name2 = 'field_test_field_2';
  $field2 = array(
    'field_name' => $field_name2,
    'type' => 'test_field',
    'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  );
  $field2 = field_create_field($field2);
  $instance2 = array(
    'field_name' => $field_name2,
    'entity_type' => 'multifield',
    'bundle' => 'test',
    'label' => $field_name2 . '_label',
    'description' => $field_name2 . '_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(),
      ),
    ),
  );
  field_create_instance($instance2);
  $this
    ->assertIdentical(multifield_get_fields(), array(
    'test' => 'test',
  ));
  $this
    ->assertIdentical(multifield_type_has_fields('test'), TRUE);
  $this
    ->assertIdentical(multifield_type_get_fields('test'), array(
    'test',
  ));
  $this
    ->assertIdentical(multifield_get_subfields(), array(
    'test' => array(
      $field_name1,
      $field_name2,
    ),
  ));
  $this
    ->assertIdentical(multifield_type_has_subfields('test'), TRUE);
  $this
    ->assertIdentical(multifield_type_get_subfields('test'), array(
    $field_name1,
    $field_name2,
  ));

  //$this->assertTrue(field_info_field_types('test'));
  $schema = module_invoke('multifield', 'field_schema', $multifield_field);
  $this
    ->assertIdentical($schema, array(
    'columns' => array(
      $field_name1 . '_value' => array(
        'type' => 'int',
        'size' => 'medium',
        'not null' => FALSE,
      ),
      $field_name2 . '_value' => array(
        'type' => 'int',
        'size' => 'medium',
        'not null' => FALSE,
      ),
      'id' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
    ),
    'indexes' => array(
      $field_name1 . '_value' => array(
        $field_name1 . '_value',
      ),
      $field_name2 . '_value' => array(
        $field_name2 . '_value',
      ),
      'id' => array(
        'id',
      ),
    ),
  ));
  $this
    ->assertTrue($dbschema
    ->tableExists('field_data_test'));
  $this
    ->assertTrue($dbschema
    ->fieldExists('field_data_test', 'test_field_test_field_1_value'));
  $this
    ->assertTrue($dbschema
    ->indexExists('field_data_test', 'test_field_test_field_1_value'));
  $this
    ->assertTrue($dbschema
    ->fieldExists('field_data_test', 'test_field_test_field_2_value'));
  $this
    ->assertTrue($dbschema
    ->indexExists('field_data_test', 'test_field_test_field_2_value'));

  // Delete the first field from the multifield.
  field_delete_instance($instance1);
  $this
    ->assertIdentical(multifield_get_fields(), array(
    'test' => 'test',
  ));
  $this
    ->assertIdentical(multifield_type_has_fields('test'), TRUE);
  $this
    ->assertIdentical(multifield_type_get_fields('test'), array(
    'test',
  ));
  $this
    ->assertIdentical(multifield_get_subfields(), array(
    'test' => array(
      $field_name2,
    ),
  ));
  $this
    ->assertIdentical(multifield_type_has_subfields('test'), TRUE);
  $this
    ->assertIdentical(multifield_type_get_subfields('test'), array(
    $field_name2,
  ));

  //$this->assertTrue(field_info_field_types('test'));
  $schema = module_invoke('multifield', 'field_schema', $multifield_field);
  $this
    ->assertIdentical($schema, array(
    'columns' => array(
      $field_name2 . '_value' => array(
        'type' => 'int',
        'size' => 'medium',
        'not null' => FALSE,
      ),
      'id' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
    ),
    'indexes' => array(
      $field_name2 . '_value' => array(
        $field_name2 . '_value',
      ),
      'id' => array(
        'id',
      ),
    ),
  ));
  $this
    ->assertTrue($dbschema
    ->tableExists('field_data_test'));
  $this
    ->assertFalse($dbschema
    ->fieldExists('field_data_test', 'test_field_test_field_1_value'));
  $this
    ->assertFalse($dbschema
    ->IndexExists('field_data_test', 'test_field_test_field_1_value'));
  $this
    ->assertTrue($dbschema
    ->fieldExists('field_data_test', 'test_field_test_field_2_value'));
  $this
    ->assertTrue($dbschema
    ->indexExists('field_data_test', 'test_field_test_field_2_value'));

  // Delete the multifield.
  field_delete_field($multifield_field['field_name']);
  $this
    ->assertIdentical(multifield_get_fields(), array());
  $this
    ->assertIdentical(multifield_type_has_fields('test'), FALSE);
  $this
    ->assertIdentical(multifield_type_get_fields('test'), array());
  $this
    ->assertIdentical(multifield_get_subfields(), array());
  $this
    ->assertIdentical(multifield_type_has_subfields('test'), FALSE);
  $this
    ->assertIdentical(multifield_type_get_subfields('test'), array());

  //$this->assertFalse(field_info_field_types('test'));
  $schema = module_invoke('multifield', 'field_schema', $multifield_field);
  $this
    ->assertIdentical($schema, array());
  $this
    ->assertFalse($dbschema
    ->tableExists('field_data_test'));
}