| 1 |
<?php |
<?php |
| 2 |
// $Id: pbs.test,v 1.6 2009/04/22 02:03:03 bjaspan Exp $ |
// $Id: pbs.test,v 1.7 2009/06/01 00:12:32 bjaspan Exp $ |
| 3 |
|
|
| 4 |
class PbsTestCase extends DrupalWebTestCase { |
class PbsTestCase extends DrupalWebTestCase { |
| 5 |
function getInfo() { |
function getInfo() { |
| 59 |
for ($delta = 0; $delta < $max; ++$delta) { |
for ($delta = 0; $delta < $max; ++$delta) { |
| 60 |
$values[$delta]['value'] = mt_rand(0, 127); |
$values[$delta]['value'] = mt_rand(0, 127); |
| 61 |
} |
} |
| 62 |
$entity->$field_name = $values; |
$entity->{$field_name}[FIELD_LANGUAGE_NONE] = $values; |
| 63 |
} |
} |
| 64 |
field_attach_insert($this->entity_type, $entity); |
field_attach_insert($this->entity_type, $entity); |
| 65 |
$entities[$id++] = $entity; |
$entities[$id++] = $entity; |
| 79 |
|
|
| 80 |
foreach ($revisions as $vid => $items) { |
foreach ($revisions as $vid => $items) { |
| 81 |
$entity = field_test_create_stub_entity(0, $vid, 'test_bundle'); |
$entity = field_test_create_stub_entity(0, $vid, 'test_bundle'); |
| 82 |
$entity->card_1 = $items; |
$entity->card_1[FIELD_LANGUAGE_NONE] = $items; |
| 83 |
field_attach_insert($this->entity_type, $entity); |
field_attach_update($this->entity_type, $entity); |
| 84 |
} |
} |
| 85 |
|
|
| 86 |
return $revisions; |
return $revisions; |
| 91 |
$entities = $this->createEntities(5); |
$entities = $this->createEntities(5); |
| 92 |
|
|
| 93 |
// Verify that all the rows in the bundle table were created correctly. |
// Verify that all the rows in the bundle table were created correctly. |
| 94 |
$res = db_query('SELECT * FROM {'.pbs_tablename('test_bundle').'}'); |
$res = db_select(pbs_tablename('test_bundle'), 't', array('fetch' => PDO::FETCH_ASSOC))->fields('t')->execute(); |
| 95 |
while ($row = db_fetch_array($res)) { |
foreach ($res as $row) { |
| 96 |
$entity = $entities[$row['entity_id']]; |
$entity = $entities[$row['entity_id']]; |
| 97 |
foreach ($this->fields as $field_name => $field) { |
foreach ($this->fields as $field_name => $field) { |
| 98 |
if ($field['cardinality'] != FIELD_CARDINALITY_UNLIMITED) { |
if ($field['cardinality'] != FIELD_CARDINALITY_UNLIMITED) { |
| 99 |
for ($delta = 0; $delta < $field['cardinality']; ++$delta) { |
for ($delta = 0; $delta < $field['cardinality']; ++$delta) { |
| 100 |
$this->assertEqual($entity->{$field_name}[$delta]['value'], $row["{$field_name}_value_$delta"], "Entity {$entity->ftid} field {$field_name} delta $delta is correct"); |
$this->assertEqual($entity->{$field_name}[FIELD_LANGUAGE_NONE][$delta]['value'], $row["{$field_name}_value_$delta"], "Entity {$entity->ftid} field {$field_name} delta $delta is correct"); |
| 101 |
} |
} |
| 102 |
} |
} |
| 103 |
} |
} |
| 106 |
// Verify that all the rows in the field tables were created |
// Verify that all the rows in the field tables were created |
| 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 (!PBS_PRIMARY_STORAGE || $field['cardinality'] == FIELD_CARDINALITY_UNLIMITED) { |
| 110 |
$res = db_query('SELECT * FROM {'._field_sql_storage_tablename($field).'}'); |
$res = db_select(_field_sql_storage_tablename($field), 't', array('fetch' => PDO::FETCH_ASSOC))->fields('t')->execute(); |
| 111 |
while ($row = db_fetch_array($res)) { |
foreach ($res as $row) { |
| 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}[FIELD_LANGUAGE_NONE][$row['delta']]['value'], $row["{$field_name}_value"], "Entity {$entity->ftid} field {$field_name} delta {$row['delta']} is correct"); |
| 114 |
} |
} |
| 115 |
} |
} |
| 116 |
else { |
else { |
| 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}[FIELD_LANGUAGE_NONE], $locals[$id]->{$field_name}[FIELD_LANGUAGE_NONE], "Entity $id field {$field_name} loaded correctly."); |
| 135 |
} |
} |
| 136 |
} |
} |
| 137 |
|
|
| 141 |
pbs_field_attach_pre_load($this->entity_type, array($ent->ftid => $ent), FIELD_LOAD_CURRENT, $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['id']]), "Field $field_name gets set in skip_fields"); |
| 145 |
} |
} |
| 146 |
else { |
else { |
| 147 |
$this->assertTrue(!isset($skip[$field_name]), "Field $field_name does not get set in skip_fields"); |
$this->assertTrue(!isset($skip[$field['id']]), "Field $field_name does not get set in skip_fields"); |
| 148 |
} |
} |
| 149 |
} |
} |
| 150 |
|
|
| 151 |
// Verify that skip_fields is honored |
// Verify that skip_fields is honored |
| 152 |
$skip = array('card_1' => 1); |
$skip = array($this->fields['card_1']['id'] => 1); |
| 153 |
$ent = reset($entities); |
$ent = reset($entities); |
| 154 |
$stub = field_test_create_stub_entity($ent->ftid, $ent->ftvid, 'test_bundle'); |
$stub = field_test_create_stub_entity($ent->ftid, $ent->ftvid, 'test_bundle'); |
| 155 |
pbs_field_attach_pre_load($this->entity_type, array($stub->ftid => $stub), FIELD_LOAD_CURRENT, $skip); |
pbs_field_attach_pre_load($this->entity_type, array($stub->ftid => $stub), FIELD_LOAD_CURRENT, $skip); |
| 200 |
field_attach_update($this->entity_type, $ent0); |
field_attach_update($this->entity_type, $ent0); |
| 201 |
$stub0 = field_test_create_stub_entity($ent0->ftid, $ent0->ftvid, $ent0->fttype); |
$stub0 = field_test_create_stub_entity($ent0->ftid, $ent0->ftvid, $ent0->fttype); |
| 202 |
field_attach_load($this->entity_type, array($stub0->ftid => $stub0)); |
field_attach_load($this->entity_type, array($stub0->ftid => $stub0)); |
| 203 |
$this->assertEqual($entities[$ent0->ftid]->card_4, $stub0->card_4, 'Missing field properties leave existing data in place.'); |
$this->assertEqual($entities[$ent0->ftid]->card_4[FIELD_LANGUAGE_NONE], $stub0->card_4[FIELD_LANGUAGE_NONE], 'Missing field properties leave existing data in place.'); |
| 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 |
| 241 |
); |
); |
| 242 |
|
|
| 243 |
$ent0 = clone(reset($entities)); |
$ent0 = clone(reset($entities)); |
| 244 |
$ent0->card_4 = $partial; |
$ent0->card_4[FIELD_LANGUAGE_NONE] = $partial; |
| 245 |
field_attach_update($this->entity_type, $ent0); |
field_attach_update($this->entity_type, $ent0); |
| 246 |
$stub0 = field_test_create_stub_entity($ent0->ftid, $ent0->ftvid, $ent0->fttype); |
$stub0 = field_test_create_stub_entity($ent0->ftid, $ent0->ftvid, $ent0->fttype); |
| 247 |
field_attach_load($this->entity_type, array($stub0->ftid => $stub0)); |
field_attach_load($this->entity_type, array($stub0->ftid => $stub0)); |
| 248 |
$this->assertEqual(array_merge($partial), $stub0->card_4, 'Saving partial field values removes all others and loads starting at delta zero.'); |
$this->assertEqual(array_merge($partial), $stub0->card_4[FIELD_LANGUAGE_NONE], 'Saving partial field values removes all others and loads starting at delta zero.'); |
| 249 |
} |
} |
| 250 |
|
|
| 251 |
function testRevisions() { |
function testRevisions() { |
| 255 |
$last_vid = end(array_keys($revisions)); |
$last_vid = end(array_keys($revisions)); |
| 256 |
$entity = field_test_create_stub_entity(0, $last_vid, 'test_bundle'); |
$entity = field_test_create_stub_entity(0, $last_vid, 'test_bundle'); |
| 257 |
field_attach_load($this->entity_type, array(0 => $entity)); |
field_attach_load($this->entity_type, array(0 => $entity)); |
| 258 |
$this->assertEqual($entity->card_1, $revisions[$entity->ftvid], "Current revision loaded correctly."); |
$this->assertEqual($entity->card_1[FIELD_LANGUAGE_NONE], $revisions[$entity->ftvid], "Current revision loaded correctly."); |
| 259 |
|
|
| 260 |
// Verify every revision |
// Verify every revision |
| 261 |
foreach ($revisions as $vid => $items) { |
foreach ($revisions as $vid => $items) { |
| 262 |
$entity = field_test_create_stub_entity(0, $vid, 'test_bundle'); |
$entity = field_test_create_stub_entity(0, $vid, 'test_bundle'); |
| 263 |
field_attach_load_revision($this->entity_type, array(0 => $entity)); |
field_attach_load_revision($this->entity_type, array(0 => $entity)); |
| 264 |
$this->assertEqual($entity->card_1, $items, "Entity revision $vid loaded correctly."); |
$this->assertEqual($entity->card_1[FIELD_LANGUAGE_NONE], $items, "Entity revision $vid loaded correctly."); |
| 265 |
} |
} |
| 266 |
} |
} |
| 267 |
|
|
| 273 |
$entity = field_test_create_stub_entity(0, $delete_vid, 'test_bundle'); |
$entity = field_test_create_stub_entity(0, $delete_vid, 'test_bundle'); |
| 274 |
field_attach_delete_revision($this->entity_type, $entity); |
field_attach_delete_revision($this->entity_type, $entity); |
| 275 |
|
|
| 276 |
|
// TODO: WTF is this for? |
| 277 |
field_attach_load_revision($this->entity_type, array(0 => $entity)); |
field_attach_load_revision($this->entity_type, array(0 => $entity)); |
| 278 |
|
|
| 279 |
// Verify every revision |
// Verify every revision |
| 284 |
$this->assertEqual($entity->card_1, array(), "Entity revision $delete_vid is deleted."); |
$this->assertEqual($entity->card_1, array(), "Entity revision $delete_vid is deleted."); |
| 285 |
} |
} |
| 286 |
else { |
else { |
| 287 |
$this->assertEqual($entity->card_1, $items, "Entity revision $vid loaded correctly."); |
$this->assertEqual($entity->card_1[FIELD_LANGUAGE_NONE], $items, "Entity revision $vid loaded correctly."); |
| 288 |
} |
} |
| 289 |
} |
} |
| 290 |
|
|