function ContentCrudTestCase::_assertSchemaMatches in Content Construction Kit (CCK) 6.2
Same name and namespace in other branches
- 6.3 tests/content.crud.test \ContentCrudTestCase::_assertSchemaMatches()
- 6 tests/content.crud.test \ContentCrudTestCase::_assertSchemaMatches()
Helper function for assertSchemaMatchesTables Checks that the database and schema for the given table contain only the expected fields.
Parameters
$table Name of the table to check:
$columns Array of column names:
1 call to ContentCrudTestCase::_assertSchemaMatches()
- ContentCrudTestCase::assertSchemaMatchesTables in tests/
content.crud.test - Checks that the database itself and the reported database schema match the expected columns for the given tables.
File
- tests/
content.crud.test, line 102
Class
- ContentCrudTestCase
- Base class for CCK CRUD tests. Defines many helper functions useful for writing CCK CRUD tests.
Code
function _assertSchemaMatches($table, $columns) {
// First test: check the expected structure matches the stored schema.
$schema = drupal_get_schema($table, TRUE);
$mismatches = array();
if ($schema === FALSE) {
$mismatches[] = t('table does not exist');
}
else {
$fields = $schema['fields'];
foreach ($columns as $field) {
if (!isset($fields[$field])) {
$mismatches[] = t('field !field is missing from table', array(
'!field' => $field,
));
}
}
$columns_reverse = array_flip($columns);
foreach ($fields as $name => $info) {
if (!isset($columns_reverse[$name])) {
$mismatches[] = t('table contains unexpected field !field', array(
'!field' => $name,
));
}
}
}
$this
->assertEqual(count($mismatches), 0, t('Table !table matches schema: !details', array(
'!table' => $table,
'!details' => implode($mismatches, ', '),
)));
// Second test: check the schema matches the actual db structure.
// This is the part that relies on schema.module.
if (!$this->enabled_schema) {
$this->enabled_schema = module_exists('schema');
}
if ($this->enabled_schema) {
// Clunky workaround for http://drupal.org/node/215198
$prefixed_table = db_prefix_tables('{' . $table . '}');
$inspect = schema_invoke('inspect', $prefixed_table);
$inspect = isset($inspect[$table]) ? $inspect[$table] : NULL;
$compare = schema_compare_table($schema, $inspect);
if ($compare['status'] == 'missing') {
$compare['reasons'] = array(
t('table does not exist'),
);
}
}
else {
$compare = array(
'status' => 'unknown',
'reasons' => array(
t('cannot enable schema module'),
),
);
}
$this
->assertEqual($compare['status'], 'same', t('Table schema for !table matches database: !details', array(
'!table' => $table,
'!details' => implode($compare['reasons'], ', '),
)));
}