View source
<?php
class ContentCrudTestCase extends DrupalTestCase {
var $enabled_schema = FALSE;
var $content_types = array();
var $nodes = array();
var $last_field = NULL;
var $next_field_n = 1;
function assertSchemaMatchesTables($tables) {
$groups = array(
'per_field' => 'content_',
'per_type' => 'content_type_',
);
foreach ($groups as $group => $table_prefix) {
if (isset($tables[$group])) {
foreach ($tables[$group] as $entity => $columns) {
if (isset($columns)) {
$db_columns = array(
'nid',
'vid',
);
foreach ($columns as $prefix => $items) {
if (is_array($items)) {
foreach ($items as $item) {
$db_columns[] = $prefix . '_' . $item;
}
}
else {
$db_columns[] = $items;
}
}
$this
->_assertSchemaMatches($table_prefix . $entity, $db_columns);
}
else {
$this
->_assertTableNotExists($table_prefix . $entity);
}
}
}
}
}
function _assertTableNotExists($table) {
$this
->assertFalse(db_table_exists($table), t('Table !table is absent', array(
'!table' => $table,
)));
}
function _assertSchemaMatches($table, $columns) {
$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, ', '),
)));
if (!$this->enabled_schema) {
$this->enabled_schema = $this
->drupalModuleEnable('schema');
}
if ($this->enabled_schema) {
$table = db_prefix_tables('{' . $table . '}');
$inspect = schema_invoke('inspect', $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'], ', '),
)));
}
function _compareArrayForChanges($fields, $data, $message, $prefix = '') {
foreach ($fields as $key => $value) {
$newprefix = $prefix == '' ? $key : $prefix . '][' . $key;
if (is_array($value)) {
$compare_to = isset($data[$key]) ? $data[$key] : array();
$this
->_compareArrayForChanges($value, $compare_to, $message, $newprefix);
}
else {
$this
->assertEqual($value, $data[$key], t($message, array(
'!key' => $newprefix,
)));
}
}
}
function assertNodeSaveValues($node, $values) {
if (is_numeric($node) && isset($this->nodes[$node])) {
$node = $this->nodes[$node];
}
$node = $values + (array) $node;
$node = (object) $node;
node_save($node);
$this
->assertNodeValues($node, $values);
return $values;
}
function assertNodeValues($node, $values) {
if (is_numeric($node) && isset($this->nodes[$node])) {
$node = $this->nodes[$node];
}
$node = node_load($node->nid, NULL, TRUE);
$this
->_compareArrayForChanges($values, (array) $node, 'Node data [!key] is correct');
}
function assertNodeMissingFields($node, $fields) {
if (is_numeric($node) && isset($this->nodes[$node])) {
$node = $this->nodes[$node];
}
$node = (array) node_load($node->nid, NULL, TRUE);
foreach ($fields as $field) {
$this
->assertFalse(isset($node[$field]), t('Node should be lacking field !key', array(
'!key' => $field,
)));
}
}
function createRandomTextFieldData() {
return array(
'value' => '!SimpleTest! test value' . $this
->randomName(60),
'format' => 2,
);
}
function loginWithPermissions($permissions = NULL) {
if (!isset($permissions)) {
$permissions = array(
'access content',
'administer content types',
'administer nodes',
'administer filters',
);
}
$user = $this
->drupalCreateUserRolePerm($permissions);
$this
->drupalLoginUser($user);
}
function acquireContentTypes($count) {
$this->content_types = array();
for ($i = 0; $i < $count; $i++) {
$name = 'simpletest_t' . ($i + 1);
$this->content_types[$i] = $this
->drupalCreateContentType(array(
'name' => $name,
'type' => $name,
));
}
content_clear_type_cache();
}
function acquireNodes($count = 1) {
$this->nodes = array();
foreach ($this->content_types as $content_type) {
for ($i = 0; $i < $count; $i++) {
$this->nodes[] = $this
->drupalCreateNode(array(
'type' => $content_type->type,
));
}
}
}
function createField($settings, $content_type = 0) {
if (is_numeric($content_type) && isset($this->content_types[$content_type])) {
$content_type = $this->content_types[$content_type];
}
$defaults = array(
'field_name' => 'simpletest_f' . $this->next_field_n++,
'type_name' => $content_type->type,
);
$settings = $settings + $defaults;
$this->last_field = content_field_instance_create($settings);
return $this->last_field;
}
function createFieldText($settings, $content_type = 0) {
$this
->drupalModuleEnable('text');
$defaults = array(
'type' => 'text',
'widget_type' => 'text_textfield',
);
$settings = $settings + $defaults;
return $this
->createField($settings, $content_type);
}
function updateField($settings, $field = NULL) {
if (!isset($field)) {
$field = $this->last_field;
}
$defaults = array(
'field_name' => $field['field_name'],
'type_name' => $field['type_name'],
);
$settings = $settings + $defaults;
$this->last_field = content_field_instance_update($settings);
return $this->last_field;
}
function shareField($new_content_type, $field = NULL) {
if (!isset($field)) {
$field = $this->last_field;
}
if (is_numeric($new_content_type) && isset($this->content_types[$new_content_type])) {
$new_content_type = $this->content_types[$new_content_type];
}
$field['type_name'] = $new_content_type->type;
$this->last_field = content_field_instance_create($field);
return $this->last_field;
}
function deleteField($content_type, $field = NULL) {
if (!isset($field)) {
$field = $this->last_field;
}
if (is_numeric($content_type) && isset($this->content_types[$content_type])) {
$content_type = $this->content_types[$content_type];
}
content_field_instance_delete($field['field_name'], $content_type->type);
}
}
class ContentCrudSingleToMultipleTest extends ContentCrudTestCase {
function get_info() {
return array(
'name' => t('CCK CRUD API - Single to multiple'),
'desc' => t('Tests the CRUD (create, read, update, delete) API for content types by creating a single value field and chaning it to a multivalue field, sharing it between several content types.'),
'group' => t('CCK'),
);
}
function testSingleToMultiple() {
$this
->loginWithPermissions();
$this
->acquireContentTypes(3);
$this
->acquireNodes();
$this
->createFieldText(array(
'text_processing' => 1,
));
$this
->assertSchemaMatchesTables(array(
'per_type' => array(
'simpletest_t1' => array(
'simpletest_f1' => array(
'value',
'format',
),
),
),
));
$node0values = $this
->assertNodeSaveValues(0, array(
'simpletest_f1' => array(
0 => $this
->createRandomTextFieldData(),
),
));
$this
->updateField(array(
'multiple' => 1,
));
$this
->assertSchemaMatchesTables(array(
'per_type' => array(
'simpletest_t1' => array(),
),
'per_field' => array(
'simpletest_f1' => array(
'delta',
'simpletest_f1' => array(
'value',
'format',
),
),
),
));
$this
->assertNodeValues(0, $node0values);
for ($share_with_content_type = 1; $share_with_content_type <= 2; $share_with_content_type++) {
$this
->shareField($share_with_content_type);
$this
->assertSchemaMatchesTables(array(
'per_type' => array(
'simpletest_t' . ($share_with_content_type + 1) => array(),
),
));
$this
->assertNodeSaveValues($share_with_content_type, array(
'simpletest_f1' => array(
0 => $this
->createRandomTextFieldData(),
),
));
}
for ($delete_from_content_type = 2; $delete_from_content_type >= 0; $delete_from_content_type--) {
$this
->deleteField($delete_from_content_type);
$this
->assertNodeMissingFields($this->nodes[$delete_from_content_type], array(
'simpletest_f1',
));
}
$this
->assertSchemaMatchesTables(array(
'per_type' => array(
'simpletest_t1' => NULL,
'simpletest_t2' => NULL,
'simpletest_t3' => NULL,
),
));
}
}
class ContentCrudMultipleToSingleTest extends ContentCrudTestCase {
function get_info() {
return array(
'name' => t('CCK CRUD API - Multiple to single'),
'desc' => t('Tests the CRUD (create, read, update, delete) API for content types by creating a multivalue field and chaning it to a single value field, sharing it between several content types.'),
'group' => t('CCK'),
);
}
function testMultipleToSingle() {
$this
->loginWithPermissions();
$this
->acquireContentTypes(3);
$this
->acquireNodes();
$this
->createFieldText(array(
'text_processing' => 1,
'multiple' => 1,
));
$this
->assertSchemaMatchesTables(array(
'per_type' => array(
'simpletest_t1' => array(),
),
'per_field' => array(
'simpletest_f1' => array(
'delta',
'simpletest_f1' => array(
'value',
'format',
),
),
),
));
$this
->assertNodeSaveValues(0, array(
'simpletest_f1' => array(
0 => $this
->createRandomTextFieldData(),
1 => $this
->createRandomTextFieldData(),
2 => $this
->createRandomTextFieldData(),
),
));
$this
->updateField(array(
'multiple' => 0,
));
$this
->assertSchemaMatchesTables(array(
'per_type' => array(
'simpletest_t1' => array(
'simpletest_f1' => array(
'value',
'format',
),
),
),
'per_field' => array(
'simpletest_f1' => NULL,
),
));
$node0values = $this
->assertNodeSaveValues(0, array(
'simpletest_f1' => array(
0 => $this
->createRandomTextFieldData(),
),
));
$this
->shareField(1);
$this
->assertSchemaMatchesTables(array(
'per_type' => array(
'simpletest_t1' => array(),
'simpletest_t2' => array(),
),
'per_field' => array(
'simpletest_f1' => array(
'simpletest_f1' => array(
'value',
'format',
),
),
),
));
$node1values = $this
->assertNodeSaveValues(1, array(
'simpletest_f1' => array(
0 => $this
->createRandomTextFieldData(),
),
));
$this
->assertNodeValues(0, $node0values);
$this
->shareField(2);
$this
->assertSchemaMatchesTables(array(
'per_type' => array(
'simpletest_t3' => array(),
),
));
$this
->assertNodeSaveValues(2, array(
'simpletest_f1' => array(
0 => $this
->createRandomTextFieldData(),
),
));
$this
->assertNodeValues(1, $node1values);
$this
->assertNodeValues(0, $node0values);
$this
->deleteField(2);
$this
->assertSchemaMatchesTables(array(
'per_type' => array(
'simpletest_t3' => NULL,
),
));
$this
->assertNodeMissingFields($this->nodes[2], array(
'simpletest_f1',
));
$this
->deleteField(1);
$this
->assertSchemaMatchesTables(array(
'per_type' => array(
'simpletest_t1' => array(
'simpletest_f1' => array(
'value',
'format',
),
),
'simpletest_t2' => NULL,
),
'per_field' => array(
'simpletest_f1' => NULL,
),
));
$this
->assertNodeMissingFields(1, array(
'simpletest_f1',
));
$this
->assertNodeValues(0, $node0values);
$this
->deleteField(0);
$this
->assertSchemaMatchesTables(array(
'per_type' => array(
'simpletest_t1' => NULL,
),
));
$this
->assertNodeMissingFields(0, array(
'simpletest_f1',
));
}
}