| 1 |
<?php |
<?php |
| 2 |
// $Id: pbs.test,v 1.5 2009/02/08 23:55:27 bjaspan Exp $ |
// $Id: pbs.test,v 1.6 2009/04/22 02:03:03 bjaspan Exp $ |
| 3 |
|
|
| 4 |
class PbsTestCase extends DrupalWebTestCase { |
class PbsTestCase extends DrupalWebTestCase { |
| 5 |
function getInfo() { |
function getInfo() { |
| 24 |
|
|
| 25 |
function createFieldAndInstance($name, $cardinality, $bundle = 'test_bundle') { |
function createFieldAndInstance($name, $cardinality, $bundle = 'test_bundle') { |
| 26 |
$field = array('field_name' => $name, 'type' => 'test_field', 'cardinality' => $cardinality); |
$field = array('field_name' => $name, 'type' => 'test_field', 'cardinality' => $cardinality); |
| 27 |
field_create_field($field); |
$this->fields[$name] = field_create_field($field); |
| 28 |
$instance = array( |
$instance = array( |
| 29 |
'field_name' => $name, |
'field_name' => $name, |
| 30 |
'bundle' => $bundle, |
'bundle' => $bundle, |
| 42 |
) |
) |
| 43 |
) |
) |
| 44 |
); |
); |
| 45 |
field_create_instance($instance); |
$this->instances[$name] = field_create_instance($instance); |
|
|
|
|
$this->fields[$name] = $field; |
|
|
$this->instances[$name] = $instance; |
|
| 46 |
} |
} |
| 47 |
|
|
| 48 |
function createEntities($num, $bundle = 'test_bundle') { |
function createEntities($num, $bundle = 'test_bundle') { |
| 107 |
// correctly. |
// correctly. |
| 108 |
foreach ($this->fields as $field_name => $field) { |
foreach ($this->fields as $field_name => $field) { |
| 109 |
if ($field['cardinality'] == FIELD_CARDINALITY_UNLIMITED) { |
if ($field['cardinality'] == FIELD_CARDINALITY_UNLIMITED) { |
| 110 |
$res = db_query('SELECT * FROM {'._field_sql_storage_tablename($field_name).'}'); |
$res = db_query('SELECT * FROM {'._field_sql_storage_tablename($field).'}'); |
| 111 |
while ($row = db_fetch_array($res)) { |
while ($row = db_fetch_array($res)) { |
| 112 |
$entity = $entities[$row['entity_id']]; |
$entity = $entities[$row['entity_id']]; |
| 113 |
$this->assertEqual($entity->{$field_name}[$row['delta']]['value'], $row["{$field_name}_value"], "Entity {$entity->ftid} field {$field_name} delta {$row['delta']} is correct"); |
$this->assertEqual($entity->{$field_name}[$row['delta']]['value'], $row["{$field_name}_value"], "Entity {$entity->ftid} field {$field_name} delta {$row['delta']} is correct"); |
| 114 |
} |
} |
| 115 |
} |
} |
| 116 |
else { |
else { |
| 117 |
$count = db_result(db_query('SELECT COUNT(*) FROM {'._field_sql_storage_tablename($field_name).'}')); |
$count = db_result(db_query('SELECT COUNT(*) FROM {'._field_sql_storage_tablename($field).'}')); |
| 118 |
$this->assertEqual($count, 0, "No data written to per-field table for limited-cardinality field {$field_name}"); |
$this->assertEqual($count, 0, "No data written to per-field table for limited-cardinality field {$field_name}"); |
| 119 |
} |
} |
| 120 |
} |
} |
| 128 |
foreach ($entities as $id => $saved_entity) { |
foreach ($entities as $id => $saved_entity) { |
| 129 |
$locals[$id] = field_test_create_stub_entity($id, $id, 'test_bundle'); |
$locals[$id] = field_test_create_stub_entity($id, $id, 'test_bundle'); |
| 130 |
} |
} |
|
|
|
| 131 |
field_attach_load($this->entity_type, $locals); |
field_attach_load($this->entity_type, $locals); |
|
|
|
| 132 |
foreach ($entities as $id => $saved_entity) { |
foreach ($entities as $id => $saved_entity) { |
| 133 |
foreach ($this->fields as $field_name => $field) { |
foreach ($this->fields as $field_name => $field) { |
| 134 |
$this->assertEqual($saved_entity->{$field_name}, $locals[$id]->{$field_name}, "Entity $id field {$field_name} loaded correctly."); |
$this->assertEqual($saved_entity->{$field_name}, $locals[$id]->{$field_name}, "Entity $id field {$field_name} loaded correctly."); |
| 136 |
} |
} |
| 137 |
|
|
| 138 |
// Verify that skip_fields is maintained |
// Verify that skip_fields is maintained |
|
$additions = array(); |
|
| 139 |
$skip = array(); |
$skip = array(); |
| 140 |
$ent = reset($entities); |
$ent = reset($entities); |
| 141 |
pbs_field_attach_pre_load($this->entity_type, array($ent->ftid => $ent), FIELD_LOAD_CURRENT, $additions, $skip); |
pbs_field_attach_pre_load($this->entity_type, array($ent->ftid => $ent), FIELD_LOAD_CURRENT, $skip); |
| 142 |
foreach ($this->fields as $field_name => $field) { |
foreach ($this->fields as $field_name => $field) { |
| 143 |
if ($field['cardinality'] != FIELD_CARDINALITY_UNLIMITED) { |
if ($field['cardinality'] != FIELD_CARDINALITY_UNLIMITED) { |
| 144 |
$this->assertTrue(isset($skip[$field_name]), "Field $field_name gets set in skip_fields"); |
$this->assertTrue(isset($skip[$field_name]), "Field $field_name gets set in skip_fields"); |
| 149 |
} |
} |
| 150 |
|
|
| 151 |
// Verify that skip_fields is honored |
// Verify that skip_fields is honored |
|
$additions = array(); |
|
| 152 |
$skip = array('card_1' => 1); |
$skip = array('card_1' => 1); |
| 153 |
$ent = reset($entities); |
$ent = reset($entities); |
| 154 |
pbs_field_attach_pre_load($this->entity_type, array($ent->ftid => $ent), FIELD_LOAD_CURRENT, $additions, $skip); |
$stub = field_test_create_stub_entity($ent->ftid, $ent->ftvid, 'test_bundle'); |
| 155 |
$this->assertTrue(empty($addtions[$ent->ftid]['card_1']), 'Field in skip_fields is not loaded'); |
pbs_field_attach_pre_load($this->entity_type, array($stub->ftid => $stub), FIELD_LOAD_CURRENT, $skip); |
| 156 |
$this->assertTrue(!empty($additions[$ent->ftid]['card_4']), 'Field not in skip_fields is loaded'); |
$this->assertTrue(!isset($stub->card_1), 'Field in skip_fields is not loaded'); |
| 157 |
|
$this->assertTrue(!empty($stub->card_4), 'Field not in skip_fields is loaded'); |
| 158 |
} |
} |
| 159 |
|
|
| 160 |
/** |
/** |
| 180 |
$entity2 = $this->createEntities(1, 'shared_test'); |
$entity2 = $this->createEntities(1, 'shared_test'); |
| 181 |
|
|
| 182 |
// Verify that data from both entities is in the shared table |
// Verify that data from both entities is in the shared table |
| 183 |
$count = db_result(db_query('SELECT COUNT(*) FROM {'._field_sql_storage_tablename('card_4').'}')); |
$count = db_result(db_query('SELECT COUNT(*) FROM {'._field_sql_storage_tablename($this->fields['card_4']).'}')); |
| 184 |
$this->assertEqual($count, 8, 'All values written to per-field table.'); |
$this->assertEqual($count, 8, 'All values written to per-field table.'); |
| 185 |
|
|
| 186 |
// Verify that data from both entities is in the appropriate |
// Verify that data from both entities is in the appropriate |
| 204 |
} |
} |
| 205 |
|
|
| 206 |
// Test that a NULL field property removes the data (and with no |
// Test that a NULL field property removes the data (and with no |
| 207 |
// data we should be back an empty array on load). |
// data we should get back an empty array on load). |
| 208 |
function testNullField() { |
function testNullField() { |
| 209 |
$entities = $this->createEntities(1); |
$entities = $this->createEntities(1); |
| 210 |
|
|