Index: modules/field/field.test
===================================================================
RCS file: /cvs/drupal/drupal/modules/field/field.test,v
retrieving revision 1.10
diff -r1.10 field.test
5a6,10
> /**
>  * @file
>  * The unit test file for fields in core.
>  */
> 
16,20c21,23
<     parent::setUp('field_sql_storage', 'field', 'field_test');
< 
<     $this->field_name = drupal_strtolower($this->randomName(). '_field_name');
<     $this->table = _field_sql_storage_tablename($this->field_name);
<     $this->revision_table = _field_sql_storage_revision_tablename($this->field_name);
---
>     parent::setUp(variable_get('field_storage_module', 'field_sql_storage'), 'field', 'field_test');
>     
>     $this->field_name = drupal_strtolower($this->randomName() . '_field_name');
26,27c29,30
<       'label' => $this->randomName(). '_label',
<       'description' => $this->randomName(). '_description',
---
>       'label' => $this->randomName() . '_label',
>       'description' => $this->randomName() . '_description',
43,96d45
<   function testFieldAttachLoad() {
<     $entity_type = 'test_entity';
<     $eid = 0;
< 
<     $etid = _field_sql_storage_etid($entity_type);
<     $columns = array('etid', 'entity_id', 'revision_id', 'delta', $this->field_name . '_value');
< 
<     // Insert data for four revisions to the field revisions table
<     $query = db_insert($this->revision_table)->fields($columns);
<     for ($evid = 0; $evid < 4; ++$evid) {
<       $values[$evid] = array();
<       // Note: we insert one extra value ('<=' instead of '<').
<       for ($delta = 0; $delta <= $this->field['cardinality']; $delta++) {
<         $value = mt_rand(1, 127);
<         $values[$evid][] = $value;
<         $query->values(array($etid, $eid, $evid, $delta, $value));
<       }
<     }
<     $query->execute();
< 
<     // Insert data for the "most current revision" into the field table
<     $query = db_insert($this->table)->fields($columns);
<     foreach ($values[0] as $delta => $value) {
<       $query->values(array($etid, $eid, 0, $delta, $value));
<     }
<     $query->execute();
< 
<     // Load the "most current revision"
<     $entity = field_test_create_stub_entity($eid, 0, $this->instance['bundle']);
<     field_attach_load($entity_type, array($eid => $entity));
<     foreach ($values[0] as $delta => $value) {
<       if ($delta < $this->field['cardinality']) {
<         $this->assertEqual($entity->{$this->field_name}[$delta]['value'], $value, "Value $delta is loaded correctly for current revision");
<       }
<       else {
<         $this->assertFalse(array_key_exists($delta, $entity->{$this->field_name}), "No extraneous value gets loaded for current revision.");
<       }
<     }
< 
<     // Load every revision
<     for ($evid = 0; $evid < 4; ++$evid) {
<       $entity = field_test_create_stub_entity($eid, $evid, $this->instance['bundle']);
<       field_attach_load_revision($entity_type, array($eid => $entity));
<       foreach ($values[$evid] as $delta => $value) {
<         if ($delta < $this->field['cardinality']) {
<           $this->assertEqual($entity->{$this->field_name}[$delta]['value'], $value, "Value $delta for revision $evid is loaded correctly");
<         }
<         else {
<           $this->assertFalse(array_key_exists($delta, $entity->{$this->field_name}), "No extraneous value gets loaded for revision $evid.");
<         }
<       }
<     }
<   }
< 
105,106c54
< 
<   function testFieldAttachInsertAndUpdate() {
---
>   function testFieldAttachLoadAndInsertAndUpdate() {
108,110d55
<     $entity = field_test_create_stub_entity(0, 0, $this->instance['bundle']);
< 
<     // Test insert.
114,123c59,63
<     for ($delta = 0; $delta <= $this->field['cardinality']; $delta++) {
<       $values[$delta]['value'] = mt_rand(1, 127);
<     }
<     $entity->{$this->field_name} = $rev_values[0] = $values;
<     field_attach_insert($entity_type, $entity);
< 
<     $rows = db_select($this->table, 't')->fields('t')->execute()->fetchAllAssoc('delta', PDO::FETCH_ASSOC);
<     foreach ($values as $delta => $value) {
<       if ($delta < $this->field['cardinality']) {
<         $this->assertEqual($rows[$delta][$this->field_name . '_value'], $value['value'], t("Value $delta is inserted correctly"));
---
>     
>     for ($rev_index = 0; $rev_index < 3; $rev_index++) {
>       $rev[$rev_index] = field_test_create_stub_entity(0, $rev_index, $this->instance['bundle']);
>       for ($delta = 0; $delta <= $this->field['cardinality']; $delta++) {
>         $values[$rev_index][$delta]['value'] = mt_rand(1, 127);
125,142c65,69
<       else {
<         $this->assertFalse(array_key_exists($delta, $rows), "No extraneous value gets inserted.");
<       }
<     }
< 
<     // Test update.
<     $entity = field_test_create_stub_entity(0, 1, $this->instance['bundle']);
<     $values = array();
<     // Note: we try to update one extra value ('<=' instead of '<').
<     for ($delta = 0; $delta <= $this->field['cardinality']; $delta++) {
<       $values[$delta]['value'] = mt_rand(1, 127);
<     }
<     $entity->{$this->field_name} = $rev_values[1] = $values;
<     field_attach_update($entity_type, $entity);
<     $rows = db_select($this->table, 't')->fields('t')->execute()->fetchAllAssoc('delta', PDO::FETCH_ASSOC);
<     foreach ($values as $delta => $value) {
<       if ($delta < $this->field['cardinality']) {
<         $this->assertEqual($rows[$delta][$this->field_name . '_value'], $value['value'], t("Value $delta is updated correctly"));
---
>       $current_revision = $rev_index;
>       // Create initial content revision 0 
>       if (!$rev_index) {
>         $rev[$rev_index]->{$this->field_name} = $values[$rev_index];
>         field_attach_insert($entity_type, $rev[$rev_index]);
145c72,75
<         $this->assertFalse(array_key_exists($delta, $rows), "No extraneous value gets updated.");
---
>         // create revisions
>         $rev[$rev_index] = field_test_create_stub_entity(0, $rev_index, $this->instance['bundle']);    
>         $rev[$rev_index]->{$this->field_name} = $values[$rev_index];
>         field_attach_update($entity_type, $rev[$rev_index]);
148,161c78,83
< 
<     // Check that data for both revisions are in the revision table.
<     // We make sure each value is stored correctly, then unset it.
<     // When an entire revision's values are unset (remembering that we
<     // put one extra value in $values per revision), unset the entire
<     // revision. Then, if $rev_values is empty at the end, all
<     // revision data was found.
<     $results = db_select($this->revision_table, 't')->fields('t')->execute();
<     foreach ($results as $row) {
<       $this->assertEqual($row->{$this->field_name . '_value'}, $rev_values[$row->revision_id][$row->delta]['value'], "Value {$row->delta} for revision {$row->revision_id} stored correctly");
<       unset($rev_values[$row->revision_id][$row->delta]);
<       if (count($rev_values[$row->revision_id]) == 1) {
<         unset($rev_values[$row->revision_id]);
<       }
---
>     // Confirm current revision loads
>     $load = field_test_create_stub_entity(0, 0, $this->instance['bundle']);
>     field_attach_load($entity_type, array(0 => $load));
>     $this->assertEqual(count($load->{$this->field_name}), $this->field['cardinality'], "The test object currrent revision has {$this->field['cardinality']} values.");
>     for ($delta = 0; $delta < $this->field['cardinality']; $delta++) {
>       $this->assertEqual($load->{$this->field_name}[$delta]['value'] , $values[$current_revision][$delta]['value'], "current revision  field $delta ok");
163d84
<     $this->assertTrue(empty($rev_values), "All values for all revisions are stored in revision table {$this->revision_table}");
165,172c86,92
<     // Check that update leaves the field data untouched if $object has no
<     // $field_name key.
<     unset($entity->{$this->field_name});
<     field_attach_update($entity_type, $entity);
<     $rows = db_select($this->table, 't')->fields('t')->execute()->fetchAllAssoc('delta', PDO::FETCH_ASSOC);
<     foreach ($values as $delta => $value) {
<       if ($delta < $this->field['cardinality']) {
<         $this->assertEqual($rows[$delta][$this->field_name . '_value'], $value['value'], t("Update with no field_name entry leaves value $delta untouched"));
---
>     // Confirm each revision loads
>     foreach (array_keys($rev) as $vid) {
>       $load = field_test_create_stub_entity(0, $vid, $this->instance['bundle']);
>       field_attach_load_revision($entity_type, array(0 => $load));
>       $this->assertEqual(count($load->{$this->field_name}), $this->field['cardinality'], "The test object revision $vid has {$this->field['cardinality']} values.");
>       for ($delta = 0; $delta < $this->field['cardinality']; $delta++) {
>         $this->assertEqual($load->{$this->field_name}[$delta]['value'], $values[$vid][$delta]['value'], "The test object revision $vid field $delta ok.");
174a95
>   } 
176,184c97,101
<     // Check that update with an empty $object->$field_name empties the field.
<     $entity->{$this->field_name} = NULL;
<     field_attach_update($entity_type, $entity);
<     $rows = db_select($this->table, 't')->fields('t')->execute()->fetchAllAssoc('delta', PDO::FETCH_ASSOC);
<     $this->assertEqual(count($rows), 0, t("Update with an empty field_name entry empties the field."));
<   }
< 
<   // Test insert and update with missing or invalid fields. For the
<   // most part, these tests pass by not crashing or causing exceptions.
---
>   /**
>       * testFieldAttachSaveMissingData tests insert and update with missing or invalid fields. For the
>       * most part, these tests pass by not crashing or causing exceptions.
>       *
>       */
191,193c108,110
<     $count = db_result(db_query("SELECT COUNT(*) FROM {{$this->table}}"));
<     $this->assertEqual($count, 0, 'Missing field results in no inserts');
< 
---
>     field_attach_load($entity_type, array(0 => $entity));
>     $this->assertEqual(count($entity->{$this->field_name}), 0, 'Missing field results in no inserts');
>      
196,198c113,115
<     field_attach_insert($entity_type, $entity);
<     $count = db_result(db_query("SELECT COUNT(*) FROM {{$this->table}}"));
<     $this->assertEqual($count, 0, 'NULL field results in no inserts');
---
>     field_attach_insert($entity_type, $entity);    
>     field_attach_load($entity_type, array(0 => $entity));
>     $this->assertTrue($entity->{$this->field_name} == NULL, 'NULL field results in no inserts');
201c118,119
<     $entity->{$this->field_name} = array(0 => array('value' => 1));
---
>     $value = 5;
>     $entity->{$this->field_name} = array(0 => array('value' => $value));
203,204c121,123
<     $count = db_result(db_query("SELECT COUNT(*) FROM {{$this->table}}"));
<     $this->assertEqual($count, 1, 'Field data saved');
---
>     $entity->{$this->field_name} = NULL;
>     field_attach_load($entity_type, array(0 => $entity));
>     $this->assertTrue($entity->{$this->field_name}[0]['value'] == $value, 'Field data saved');
209,210c128,130
<     $count = db_result(db_query("SELECT COUNT(*) FROM {{$this->table}}"));
<     $this->assertEqual($count, 1, 'Missing field leaves data in table');
---
>     $entity->{$this->field_name} = NULL;
>     field_attach_load($entity_type, array(0 => $entity));
>     $this->assertEqual($entity->{$this->field_name}[0]['value'], $value, 'Field missing data survives');
215,216c135,136
<     $count = db_result(db_query("SELECT COUNT(*) FROM {{$this->table}}"));
<     $this->assertEqual($count, 0, 'NULL field leaves no data in table');
---
>     field_attach_load($entity_type, array(0 => $entity));
>     $this->assertTrue($entity->{$this->field_name} == NULL, 'NULL field update');
218d137
< 
422,424c341
<     $field_name = drupal_strtolower($this->randomName(). '_field_name');
<     $table = _field_sql_storage_tablename($field_name);
<     $revision_table = _field_sql_storage_revision_tablename($field_name);
---
>     $field_name = drupal_strtolower($this->randomName() . '_field_name');
430,431c347,348
<       'label' => $this->randomName(). '_label',
<       'description' => $this->randomName(). '_description',
---
>       'label' => $this->randomName() . '_label',
>       'description' => $this->randomName() . '_description',
729,731c646,648
<     $this->field_single = array('field_name' => drupal_strtolower($this->randomName(). '_field_name'), 'type' => 'test_field');
<     $this->field_multiple = array('field_name' => drupal_strtolower($this->randomName(). '_field_name'), 'type' => 'test_field', 'cardinality' => 4);
<     $this->field_unlimited = array('field_name' => drupal_strtolower($this->randomName(). '_field_name'), 'type' => 'test_field', 'cardinality' => FIELD_CARDINALITY_UNLIMITED);
---
>     $this->field_single = array('field_name' => drupal_strtolower($this->randomName() . '_field_name'), 'type' => 'test_field');
>     $this->field_multiple = array('field_name' => drupal_strtolower($this->randomName() . '_field_name'), 'type' => 'test_field', 'cardinality' => 4);
>     $this->field_unlimited = array('field_name' => drupal_strtolower($this->randomName() . '_field_name'), 'type' => 'test_field', 'cardinality' => FIELD_CARDINALITY_UNLIMITED);
735,736c652,653
<       'label' => $this->randomName(). '_label',
<       'description' => $this->randomName(). '_description',
---
>       'label' => $this->randomName() . '_label',
>       'description' => $this->randomName() . '_description',
895c812
<     $this->assertNoField("$this->field_name[". ($delta + 1) . '][value]', 'No extraneous widget is displayed');
---
>     $this->assertNoField("$this->field_name[" . ($delta + 1) . '][value]', 'No extraneous widget is displayed');
949c866,867
<     } catch (FieldException $e) {
---
>     } 
>     catch (FieldException $e) {
958c876,877
<     } catch (FieldException $e) {
---
>     } 
>     catch (FieldException $e) {
982,984d900
<     // Check that a table has been created for the field.
<     $this->assertTrue(db_table_exists('field_data_' . $field_definition['field_name']), t('A table has been created for the field.'));
< 
989c905,906
<     } catch (FieldException $e) {
---
>     } 
>     catch (FieldException $e) {
998c915,916
<     } catch (FieldException $e) {
---
>     } 
>     catch (FieldException $e) {
Index: modules/field/modules/field_sql_storage/field_sql_storage.test
===================================================================
RCS file: /cvs/drupal/drupal/modules/field/modules/field_sql_storage/field_sql_storage.test,v
retrieving revision 1.2
diff -r1.2 field_sql_storage.test
14a15,25
>     $this->field_name = drupal_strtolower($this->randomName() . '_field_name');
>     $this->field = array('field_name' => $this->field_name, 'type' => 'test_field', 'cardinality' => 4);
>     field_create_field($this->field);
>     $this->instance = array(
>       'field_name' => $this->field_name,
>       'bundle' => 'test_bundle'
>     );
>     field_create_instance($this->instance);
>     $this->table = _field_sql_storage_tablename($this->field_name);
>     $this->revision_table = _field_sql_storage_revision_tablename($this->field_name);
>  
28a40,209
>   
>   function testFieldAttachLoad() {
>     $entity_type = 'test_entity';
>     $eid = 0;
> 
>     $etid = _field_sql_storage_etid($entity_type);
>     $columns = array('etid', 'entity_id', 'revision_id', 'delta', $this->field_name . '_value');
> 
>     // Insert data for four revisions to the field revisions table
>     $query = db_insert($this->revision_table)->fields($columns);
>     for ($evid = 0; $evid < 4; ++$evid) {
>       $values[$evid] = array();
>       // Note: we insert one extra value ('<=' instead of '<').
>       for ($delta = 0; $delta <= $this->field['cardinality']; $delta++) {
>         $value = mt_rand(1, 127);
>         $values[$evid][] = $value;
>         $query->values(array($etid, $eid, $evid, $delta, $value));
>       }
>     }
>     $query->execute();
> 
>     // Insert data for the "most current revision" into the field table
>     $query = db_insert($this->table)->fields($columns);
>     foreach ($values[0] as $delta => $value) {
>       $query->values(array($etid, $eid, 0, $delta, $value));
>     }
>     $query->execute();
> 
>     // Load the "most current revision"
>     $entity = field_test_create_stub_entity($eid, 0, $this->instance['bundle']);
>     field_attach_load($entity_type, array($eid => $entity));
>     foreach ($values[0] as $delta => $value) {
>       if ($delta < $this->field['cardinality']) {
>         $this->assertEqual($entity->{$this->field_name}[$delta]['value'], $value, "Value $delta is loaded correctly for current revision");
>       }
>       else {
>         $this->assertFalse(array_key_exists($delta, $entity->{$this->field_name}), "No extraneous value gets loaded for current revision.");
>       }
>     }
> 
>     // Load every revision
>     for ($evid = 0; $evid < 4; ++$evid) {
>       $entity = field_test_create_stub_entity($eid, $evid, $this->instance['bundle']);
>       field_attach_load_revision($entity_type, array($eid => $entity));
>       foreach ($values[$evid] as $delta => $value) {
>         if ($delta < $this->field['cardinality']) {
>           $this->assertEqual($entity->{$this->field_name}[$delta]['value'], $value, "Value $delta for revision $evid is loaded correctly");
>         }
>         else {
>           $this->assertFalse(array_key_exists($delta, $entity->{$this->field_name}), "No extraneous value gets loaded for revision $evid.");
>         }
>       }
>     }
>   }
>   
>   function testFieldAttachInsertAndUpdate() {
>     $entity_type = 'test_entity';
>     $entity = field_test_create_stub_entity(0, 0, $this->instance['bundle']);
> 
>     // Test insert.
>     $values = array();
>     // Note: we try to insert one extra value ('<=' instead of '<').
>     // TODO : test empty values filtering and "compression" (store consecutive deltas).
>     for ($delta = 0; $delta <= $this->field['cardinality']; $delta++) {
>       $values[$delta]['value'] = mt_rand(1, 127);
>     }
>     $entity->{$this->field_name} = $rev_values[0] = $values;
>     field_attach_insert($entity_type, $entity);
> 
>     $rows = db_select($this->table, 't')->fields('t')->execute()->fetchAllAssoc('delta', PDO::FETCH_ASSOC);
>     foreach ($values as $delta => $value) {
>       if ($delta < $this->field['cardinality']) {
>         $this->assertEqual($rows[$delta][$this->field_name . '_value'], $value['value'], t("Value $delta is inserted correctly"));
>       }
>       else {
>         $this->assertFalse(array_key_exists($delta, $rows), "No extraneous value gets inserted.");
>       }
>     }
> 
>     // Test update.
>     $entity = field_test_create_stub_entity(0, 1, $this->instance['bundle']);
>     $values = array();
>     // Note: we try to update one extra value ('<=' instead of '<').
>     for ($delta = 0; $delta <= $this->field['cardinality']; $delta++) {
>       $values[$delta]['value'] = mt_rand(1, 127);
>     }
>     $entity->{$this->field_name} = $rev_values[1] = $values;
>     field_attach_update($entity_type, $entity);
>     $rows = db_select($this->table, 't')->fields('t')->execute()->fetchAllAssoc('delta', PDO::FETCH_ASSOC);
>     foreach ($values as $delta => $value) {
>       if ($delta < $this->field['cardinality']) {
>         $this->assertEqual($rows[$delta][$this->field_name . '_value'], $value['value'], t("Value $delta is updated correctly"));
>       }
>       else {
>         $this->assertFalse(array_key_exists($delta, $rows), "No extraneous value gets updated.");
>       }
>     }
> 
>     // Check that data for both revisions are in the revision table.
>     // We make sure each value is stored correctly, then unset it.
>     // When an entire revision's values are unset (remembering that we
>     // put one extra value in $values per revision), unset the entire
>     // revision. Then, if $rev_values is empty at the end, all
>     // revision data was found.
>     $results = db_select($this->revision_table, 't')->fields('t')->execute();
>     foreach ($results as $row) {
>       $this->assertEqual($row->{$this->field_name . '_value'}, $rev_values[$row->revision_id][$row->delta]['value'], "Value {$row->delta} for revision {$row->revision_id} stored correctly");
>       unset($rev_values[$row->revision_id][$row->delta]);
>       if (count($rev_values[$row->revision_id]) == 1) {
>         unset($rev_values[$row->revision_id]);
>       }
>     }
>     $this->assertTrue(empty($rev_values), "All values for all revisions are stored in revision table {$this->revision_table}");
> 
>     // Check that update leaves the field data untouched if $object has no
>     // $field_name key.
>     unset($entity->{$this->field_name});
>     field_attach_update($entity_type, $entity);
>     $rows = db_select($this->table, 't')->fields('t')->execute()->fetchAllAssoc('delta', PDO::FETCH_ASSOC);
>     foreach ($values as $delta => $value) {
>       if ($delta < $this->field['cardinality']) {
>         $this->assertEqual($rows[$delta][$this->field_name . '_value'], $value['value'], t("Update with no field_name entry leaves value $delta untouched"));
>       }
>     }
> 
>     // Check that update with an empty $object->$field_name empties the field.
>     $entity->{$this->field_name} = NULL;
>     field_attach_update($entity_type, $entity);
>     $rows = db_select($this->table, 't')->fields('t')->execute()->fetchAllAssoc('delta', PDO::FETCH_ASSOC);
>     $this->assertEqual(count($rows), 0, t("Update with an empty field_name entry empties the field."));
>   }
> 
>   /**
>       * testFieldAttachSaveMissingData tests insert and update with missing or invalid fields. For the
>       * most part, these tests pass by not crashing or causing exceptions.
>       *
>       */
>   function testFieldAttachSaveMissingData() {
>     $entity_type = 'test_entity';
>     $entity = field_test_create_stub_entity(0, 0, $this->instance['bundle']);
> 
>     // Insert: Field is missing
>     field_attach_insert($entity_type, $entity);
>     $count = db_result(db_query("SELECT COUNT(*) FROM {{$this->table}}"));
>     $this->assertEqual($count, 0, 'Missing field results in no inserts');
> 
>     // Insert: Field is NULL
>     $entity->{$this->field_name} = NULL;
>     field_attach_insert($entity_type, $entity);
>     $count = db_result(db_query("SELECT COUNT(*) FROM {{$this->table}}"));
>     $this->assertEqual($count, 0, 'NULL field results in no inserts');
> 
>     // Add some real data
>     $entity->{$this->field_name} = array(0 => array('value' => 1));
>     field_attach_insert($entity_type, $entity);
>     $count = db_result(db_query("SELECT COUNT(*) FROM {{$this->table}}"));
>     $this->assertEqual($count, 1, 'Field data saved');
> 
>     // Update: Field is missing. Data should survive.
>     unset($entity->{$this->field_name});
>     field_attach_update($entity_type, $entity);
>     $count = db_result(db_query("SELECT COUNT(*) FROM {{$this->table}}"));
>     $this->assertEqual($count, 1, 'Missing field leaves data in table');
> 
>     // Update: Field is NULL. Data should be wiped.
>     $entity->{$this->field_name} = NULL;
>     field_attach_update($entity_type, $entity);
>     $count = db_result(db_query("SELECT COUNT(*) FROM {{$this->table}}"));
>     $this->assertEqual($count, 0, 'NULL field leaves no data in table');
>   }
