View source
<?php
class ContentCrudTestCase extends DrupalWebTestCase {
var $enabled_schema = FALSE;
var $content_types = array();
var $nodes = array();
var $last_field = NULL;
var $next_field_n = 1;
function setUp() {
$args = func_get_args();
$modules = array_merge(array(
'content_test',
'content',
'schema',
'text',
), $args);
call_user_func_array(array(
'parent',
'setUp',
), $modules);
module_load_include('inc', 'content', 'includes/content.crud');
}
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 = module_exists('schema');
}
if ($this->enabled_schema) {
$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'], ', '),
)));
}
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
->drupalCreateUser($permissions);
$this
->drupalLogin($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) {
$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 ContentCrudBasicTest extends ContentCrudTestCase {
public static function getInfo() {
return array(
'name' => t('CRUD - Basic API tests'),
'description' => t('Tests the field CRUD (create, read, update, delete) API. <strong>Requires <a href="@schema_link">Schema module</a>.</strong>', array(
'@schema_link' => 'http://www.drupal.org/project/schema',
)),
'group' => t('CCK'),
);
}
function setUp() {
parent::setUp();
$this
->acquireContentTypes(1);
}
function testBasic() {
$field = $this
->createFieldText(array(
'widget_type' => 'text_textarea',
'text_processing' => 1,
'rows' => 5,
), 0);
$fields = content_field_instance_read(array(
'field_name' => $field['field_name'],
'type_name' => $this->content_types[0]->type,
));
$field1 = array_pop($fields);
$field2 = content_field_instance_collapse($field1);
$field3 = content_field_instance_expand($field2);
$field4 = content_field_instance_collapse($field3);
$this
->assertIdentical($field1, $field3, 'collapse then expand is identity');
$this
->assertIdentical($field2, $field4, 'expand then collapse is identity');
$fields = content_field_instance_read(array(
'field_name' => $field['field_name'],
'type_name' => $this->content_types[0]->type,
));
$field1 = array_pop($fields);
$field2 = content_field_instance_collapse($field1);
$field3 = content_field_instance_collapse($field2);
$this
->assertIdentical($field2, $field3, 'collapse is final');
$field2 = content_field_instance_expand($field1);
$field3 = content_field_instance_expand($field2);
$this
->assertIdentical($field2, $field3, 'expand is final');
$fields = content_field_instance_read(array(
'field_name' => $field['field_name'],
'type_name' => $this->content_types[0]->type,
));
$field1 = array_pop($fields);
$field2 = content_field_instance_update($field1);
$fields = content_field_instance_read(array(
'field_name' => $field['field_name'],
'type_name' => $this->content_types[0]->type,
));
$field3 = array_pop($fields);
$this
->assertIdentical($field1, $field3, 'read, update, read is identity');
}
}
class ContentCrudSingleToMultipleTest extends ContentCrudTestCase {
public static function getInfo() {
return array(
'name' => t('CRUD - Single to multiple'),
'description' => t('Tests the field CRUD (create, read, update, delete) API by creating a single value field and changing it to a multivalue field, sharing it between several content types. <strong>Requires <a href="@schema_link">Schema module</a>.</strong>', array(
'@schema_link' => 'http://www.drupal.org/project/schema',
)),
'group' => t('CCK'),
);
}
function setUp() {
parent::setUp();
$this
->loginWithPermissions();
$this
->acquireContentTypes(3);
$this
->acquireNodes();
}
function testSingleToMultiple() {
$this
->createFieldText(array(
'text_processing' => 1,
));
$target_schema = array(
'per_type' => array(
'simpletest_t1' => array(
'simpletest_f1' => array(
'value',
'format',
),
),
),
'per_field' => array(),
);
$this
->assertSchemaMatchesTables($target_schema);
$node0values = $this
->assertNodeSaveValues(0, array(
'simpletest_f1' => array(
0 => $this
->createRandomTextFieldData(),
),
));
$this
->updateField(array(
'multiple' => 1,
));
$target_schema = array(
'per_type' => array(
'simpletest_t1' => array(),
),
'per_field' => array(
'simpletest_f1' => array(
'delta',
'simpletest_f1' => array(
'value',
'format',
),
),
),
);
$this
->assertSchemaMatchesTables($target_schema);
$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);
$target_schema['per_type']['simpletest_t' . ($share_with_content_type + 1)] = array();
$this
->assertSchemaMatchesTables($target_schema);
$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);
$target_schema['per_type']['simpletest_t' . ($delete_from_content_type + 1)] = NULL;
if ($delete_from_content_type == 0) {
$target_schema['per_field']['simpletest_f1'] = NULL;
}
$this
->assertSchemaMatchesTables($target_schema);
$this
->assertNodeMissingFields($this->nodes[$delete_from_content_type], array(
'simpletest_f1',
));
}
}
}
class ContentCrudMultipleToSingleTest extends ContentCrudTestCase {
public static function getInfo() {
return array(
'name' => t('CRUD - Multiple to single'),
'description' => t('Tests the field CRUD (create, read, update, delete) API by creating a multivalue field and changing it to a single value field, sharing it between several content types. <strong>Requires <a href="@schema_link">Schema module</a>.</strong>', array(
'@schema_link' => 'http://www.drupal.org/project/schema',
)),
'group' => t('CCK'),
);
}
function setUp() {
parent::setUp();
$this
->loginWithPermissions();
$this
->acquireContentTypes(3);
$this
->acquireNodes();
}
function testMultipleToSingle() {
$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_t1' => array(),
'simpletest_t2' => array(),
'simpletest_t3' => array(),
),
'per_field' => array(
'simpletest_f1' => array(
'simpletest_f1' => array(
'value',
'format',
),
),
),
));
$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_t1' => array(),
'simpletest_t2' => array(),
'simpletest_t3' => NULL,
),
'per_field' => array(
'simpletest_f1' => array(
'simpletest_f1' => array(
'value',
'format',
),
),
),
));
$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,
'simpletest_t3' => 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,
'simpletest_t2' => NULL,
'simpletest_t3' => NULL,
),
'per_field' => array(
'simpletest_f1' => NULL,
),
));
$this
->assertNodeMissingFields(0, array(
'simpletest_f1',
));
}
}
class ContentUICrud extends ContentCrudTestCase {
public static function getInfo() {
return array(
'name' => t('Admin UI'),
'description' => t('Tests the CRUD (create, read, update, delete) operations for content fields via the UI. <strong>Requires <a href="@schema_link">Schema module</a>.</strong>', array(
'@schema_link' => 'http://www.drupal.org/project/schema',
)),
'group' => t('CCK'),
);
}
function setUp() {
parent::setUp('fieldgroup');
$this
->loginWithPermissions();
}
function testAddFieldUI() {
$type1 = 'simpletest' . mt_rand();
$type1_name = $this
->randomName(10);
$edit = array(
'type' => $type1,
'name' => $type1_name,
);
$this
->drupalPost('admin/content/types/add', $edit, 'Save content type');
$admin_type1_url = 'admin/content/node-type/' . $type1;
$field_name = strtolower($this
->randomName(10));
$field_label = $this
->randomName(10);
$edit = array(
'_add_new_field[label]' => $field_label,
'_add_new_field[field_name]' => $field_name,
'_add_new_field[type]' => 'text',
'_add_new_field[widget_type]' => 'text_textfield',
);
$this
->drupalPost($admin_type1_url . '/fields', $edit, 'Save');
$this
->assertRaw('These settings apply only to the <em>' . $field_label . '</em> field', 'Field settings page displayed');
$this
->assertRaw('Size of textfield', 'Field and widget types correct.');
$this
->assertNoRaw('Change basic information', 'No basic information displayed');
$field_name = 'field_' . $field_name;
$edit = array();
$this
->drupalPost(NULL, $edit, 'Save field settings');
$this
->assertRaw('Added field <em>' . $field_label . '</em>.', 'Field settings saved');
$field_type1_url = $admin_type1_url . '/fields/' . $field_name;
$this
->assertRaw($field_type1_url, 'Field displayed on overview.');
$this
->assertSchemaMatchesTables(array(
'per_type' => array(
$type1 => array(
$field_name => array(
'value',
),
),
),
));
$type2 = 'simpletest' . mt_rand();
$type2_name = $this
->randomName(10);
$edit = array(
'type' => $type2,
'name' => $type2_name,
);
$this
->drupalPost('admin/content/types/add', $edit, 'Save content type');
$admin_type2_url = 'admin/content/node-type/' . $type2;
$edit = array(
'_add_existing_field[label]' => $field_label,
'_add_existing_field[field_name]' => $field_name,
'_add_existing_field[widget_type]' => 'text_textarea',
);
$this
->drupalPost($admin_type2_url . '/fields', $edit, 'Save');
$this
->assertRaw('These settings apply only to the <em>' . $field_label . '</em> field', 'Field settings page displayed');
$this
->assertRaw('Rows', 'Field and widget types correct.');
$this
->assertNoRaw('Change basic information', 'No basic information displayed');
$edit = array();
$this
->drupalPost(NULL, $edit, 'Save field settings');
$this
->assertRaw('Added field <em>' . $field_label . '</em>.', 'Field settings saved');
$field_type2_url = $admin_type2_url . '/fields/' . $field_name;
$this
->assertRaw($field_type2_url, 'Field displayed on overview.');
$this
->assertSchemaMatchesTables(array(
'per_field' => array(
$field_name => array(
$field_name => array(
'value',
),
),
),
'per_type' => array(
$type1 => array(),
$type2 => array(),
),
));
$edit = array();
$this
->drupalPost($field_type2_url, $edit, 'Change basic information');
$this
->assertRaw('Edit basic information', 'Basic information form displayed');
$field_label2 = $this
->randomName(10);
$edit = array(
'label' => $field_label2,
'widget_type' => 'text_textfield',
);
$this
->drupalPost(NULL, $edit, 'Continue');
$this
->assertRaw('These settings apply only to the <em>' . $field_label2 . '</em> field', 'Label changed');
$this
->assertRaw('Size of textfield', 'Widget changed');
$edit = array();
$this
->drupalPost(NULL, $edit, 'Save field settings');
$this
->assertRaw('Saved field <em>' . $field_label2 . '</em>.', 'Field settings saved');
$group1_name = strtolower($this
->randomName(10));
$group1_label = $this
->randomName(10);
$edit = array(
'_add_new_group[label]' => $group1_label,
'_add_new_group[group_name]' => $group1_name,
);
$this
->drupalPost($admin_type2_url . '/fields', $edit, 'Save');
$group1_name = 'group_' . $group1_name;
$this
->assertRaw($admin_type2_url . '/groups/' . $group1_name, 'Group created');
$edit = array();
$this
->drupalPost($field_type2_url . '/remove', $edit, 'Remove');
$this
->assertRaw('Removed field <em>' . $field_label2 . '</em> from <em>' . $type2_name . '</em>', 'Field removed');
$this
->assertNoRaw($field_type2_url, 'Field not displayed on overview.');
$this
->assertSchemaMatchesTables(array(
'per_type' => array(
$type1 => array(
$field_name => array(
'value',
),
),
),
));
$field2_label = $this
->randomName(10);
$field2_name = strtolower($this
->randomName(10));
$group2_label = $this
->randomName(10);
$group2_name = strtolower($this
->randomName(10));
$edit = array(
'_add_new_field[label]' => $field2_label,
'_add_new_field[field_name]' => $field2_name,
'_add_new_field[type]' => 'text',
'_add_new_field[widget_type]' => 'text_textfield',
'_add_new_field[parent]' => $group1_name,
'_add_existing_field[label]' => $field_label,
'_add_existing_field[field_name]' => $field_name,
'_add_existing_field[widget_type]' => 'text_textarea',
'_add_existing_field[parent]' => '_add_new_group',
'_add_new_group[label]' => $group2_label,
'_add_new_group[group_name]' => $group2_name,
);
$this
->drupalPost($admin_type2_url . '/fields', $edit, 'Save');
$this
->assertRaw('These settings apply only to the <em>' . $field2_label . '</em> field', 'Field settings page for new field displayed');
$edit = array();
$this
->drupalPost(NULL, $edit, 'Save field settings');
$this
->assertRaw('Added field <em>' . $field2_label . '</em>.', 'Field settings for new field saved');
$this
->assertRaw('These settings apply only to the <em>' . $field_label . '</em> field', 'Field settings page for existing field displayed');
$edit = array();
$this
->drupalPost(NULL, $edit, 'Save field settings');
$this
->assertRaw('Added field <em>' . $field_label . '</em>.', 'Field settings for existing field saved');
$field2_name = 'field_' . $field2_name;
$field2_type2_url = $admin_type2_url . '/fields/' . $field2_name;
$this
->assertRaw($field2_type2_url, 'New field displayed in overview');
$this
->assertRaw($field_type2_url, 'Existing field displayed in overview');
$group2_name = 'group_' . $group2_name;
$this
->assertRaw($admin_type2_url . '/groups/' . $group2_name, 'New group displayed in overview');
$groups = fieldgroup_groups($type2, FALSE, TRUE);
$this
->assertTrue(isset($groups[$group1_name]['fields'][$field2_name]), 'New field in correct group');
$this
->assertTrue(isset($groups[$group2_name]['fields'][$field_name]), 'Existing field in correct group');
$this
->assertFieldByXPath('//select[@id="edit-' . strtr($field2_name, '_', '-') . '-parent"]//option[@selected]', $group1_name, 'Parenting for new field correct in overview');
$this
->assertFieldByXPath('//select[@id="edit-' . strtr($field_name, '_', '-') . '-parent"]//option[@selected]', $group2_name, 'Parenting for existing field correct in overview');
$this
->assertSchemaMatchesTables(array(
'per_field' => array(
$field_name => array(
$field_name => array(
'value',
),
),
),
'per_type' => array(
$type1 => array(),
$type2 => array(
$field2_name => array(
'value',
),
),
),
));
}
function testFieldContentUI() {
$type1 = 'simpletest' . mt_rand();
$type1_obj = $this
->drupalCreateContentType(array(
'type' => $type1,
));
$admin_type1_url = 'admin/content/node-type/' . $type1;
$field_name = strtolower($this
->randomName(10));
$field_url = 'field_' . $field_name;
$field = $this
->createFieldText(array(
'text_processing' => 1,
'multiple' => 0,
'field_name' => $field_url,
), $type1_obj);
$edit = array();
$edit['title'] = $this
->randomName(20);
$edit['body'] = $this
->randomName(20);
$value = $this
->randomName(20);
$edit[$field_url . '[0][value]'] = $value;
$this
->drupalPost('node/add/' . $type1, $edit, 'Save');
$node = node_load(array(
'title' => $edit['title'],
));
$this
->drupalGet('node/' . $node->nid);
$this
->assertText($value, 'Textfield value saved and displayed');
$edit = array();
$edit['multiple'] = '1';
$this
->drupalPost($admin_type1_url . '/fields/' . $field_url, $edit, 'Save field settings');
$edit = array();
$edit['title'] = $this
->randomName(20);
$edit['body'] = $this
->randomName(20);
$this
->drupalPost('node/add/' . $type1, $edit, "Add another item");
$this
->drupalPost(NULL, $edit, "Add another item");
$value1 = $this
->randomName(20);
$value2 = $this
->randomName(20);
$value3 = $this
->randomName(20);
$edit[$field_url . '[0][value]'] = $value1;
$edit[$field_url . '[1][value]'] = $value2;
$edit[$field_url . '[2][value]'] = $value3;
$this
->drupalPost(NULL, $edit, 'Save');
$node = node_load(array(
'title' => $edit['title'],
));
$this
->drupalGet('node/' . $node->nid);
$this
->assertText($value3, '3rd textfield value saved and displayed');
}
}
class ContentOptionWidgetTest extends ContentCrudTestCase {
public static function getInfo() {
return array(
'name' => t('Option widgets'),
'description' => t('Tests the optionwidgets.'),
'group' => t('CCK'),
);
}
function setUp() {
parent::setUp('optionwidgets');
$this
->loginWithPermissions();
$this
->acquireContentTypes(1);
}
function testOnOffCheckbox() {
$type = $this->content_types[0];
$type_url = str_replace('_', '-', $type->type);
$on_text = $this
->randomName(5);
$on_value = $this
->randomName(5);
$off_text = $on_text . '_off';
$off_value = $on_value . '_off';
$settings = array(
'type' => 'text',
'widget_type' => 'optionwidgets_onoff',
'allowed_values' => "{$off_value}|{$off_text}\r\n{$on_value}|{$on_text}",
);
$field = $this
->createField($settings, 0);
$field_name = $field['field_name'];
$edit = array(
'title' => $this
->randomName(20),
'body' => $this
->randomName(20),
$field_name . '[value]' => $on_value,
);
$this
->drupalPost('node/add/' . $type_url, $edit, 'Save');
$node = node_load(array(
'title' => $edit['title'],
));
$this
->assertEqual($node->{$field_name}[0]['value'], $on_value, 'Checkbox: checked (saved)');
$this
->drupalGet('node/' . $node->nid);
$this
->assertText($on_text, 'Checkbox: checked (displayed)');
$edit = array(
$field_name . '[value]' => FALSE,
);
$this
->drupalPost('node/' . $node->nid . '/edit', $edit, 'Save');
$node = node_load($node->nid, NULL, TRUE);
$this
->assertEqual($node->{$field_name}[0]['value'], $off_value, 'Checkbox: unchecked (saved)');
$this
->drupalGet('node/' . $node->nid);
$this
->assertText($off_text, 'Checkbox: unchecked (displayed)');
}
function testSelect() {
$type = $this->content_types[0];
$type_url = str_replace('_', '-', $type->type);
$value1 = $this
->randomName(5);
$value1_alias = $value1 . '_alias';
$value2 = $this
->randomName(5);
$value2_alias = $value2 . '_alias';
$settings = array(
'type' => 'text',
'widget_type' => 'optionwidgets_select',
'allowed_values' => "{$value1}|{$value1_alias}\r\n{$value2}|{$value2_alias}",
);
$field = $this
->createField($settings, 0);
$field_name = $field['field_name'];
$edit = array(
'title' => $this
->randomName(20),
'body' => $this
->randomName(20),
);
$edit[$field_name . '[value]'] = $value1;
$this
->drupalPost('node/add/' . $type_url, $edit, 'Save');
$node = node_load(array(
'title' => $edit['title'],
));
$this
->assertEqual($node->{$field_name}[0]['value'], $value1, 'Select: selected (saved)');
$this
->drupalGet('node/' . $node->nid);
$this
->assertText($value1_alias, 'Select: selected (displayed)');
$edit = array(
$field_name . '[value]' => '',
);
$this
->drupalPost('node/' . $node->nid . '/edit', $edit, 'Save');
$node = node_load($node->nid, NULL, TRUE);
$this
->assertIdentical($node->{$field_name}[0]['value'], NULL, 'Select: unselected (saved)');
$this
->drupalGet('node/' . $node->nid);
$this
->assertNoText($value1_alias, 'Select: unselected (displayed)');
$field = $this
->updateField(array(
'multiple' => '1',
'required' => '0',
));
$edit = array(
$field_name . '[value][]' => array(
$value1 => $value1,
$value2 => $value2,
),
);
$this
->drupalPost('node/' . $node->nid . '/edit', $edit, 'Save');
$node = node_load($node->nid, NULL, TRUE);
$this
->assertEqual($node->{$field_name}[0]['value'], $value1, 'Multiple Select: selected 1 (saved)');
$this
->assertEqual($node->{$field_name}[1]['value'], $value2, 'Multiple Select: selected 2 (saved)');
$this
->drupalGet('node/' . $node->nid);
$this
->assertText($value1_alias, 'Multiple Select: selected 1 (displayed)');
$this
->assertText($value2_alias, 'Multiple Select: selected 2 (displayed)');
$edit = array(
$field_name . '[value][]' => array(
$value1 => $value1,
),
);
$this
->drupalPost('node/' . $node->nid . '/edit', $edit, 'Save');
$node = node_load($node->nid, NULL, TRUE);
$this
->assertEqual($node->{$field_name}[0]['value'], $value1, 'Multiple Select: selected 1 (saved)');
$this
->assertTrue(!isset($node->{$field_name}[1]), 'Multiple Select: unselected 2 (saved)');
$this
->drupalGet('node/' . $node->nid);
$this
->assertText($value1_alias, 'Multiple Select: selected 1 (displayed)');
$this
->assertNoText($value2_alias, 'Multiple Select: unselected 2 (displayed)');
$edit = array(
$field_name . '[value][]' => array(
'' => '',
),
);
$this
->drupalPost('node/' . $node->nid . '/edit', $edit, 'Save');
$node = node_load($node->nid, NULL, TRUE);
$this
->assertIdentical($node->{$field_name}[0]['value'], NULL, 'Multiple Select: unselected 1 ("-none-" selected) (saved)');
$this
->assertTrue(!isset($node->{$field_name}[1]), 'Multiple Select: unselected 2 ("-none-" selected) (saved)');
$this
->drupalGet('node/' . $node->nid);
$this
->assertNoText($value1_alias, 'Multiple Select: unselected 1 ("-none-" selected) (displayed)');
$this
->assertNoText($value2_alias, 'Multiple Select: unselected 2 ("-none-" selected) (displayed)');
$edit = array();
$edit[$field_name . '[value][]'] = array(
$value1 => FALSE,
$value2 => FALSE,
);
$this
->drupalPost('node/' . $node->nid . '/edit', $edit, 'Save');
$edit = array();
$this
->drupalPost('node/' . $node->nid . '/edit', $edit, 'Save');
$node = node_load($node->nid, NULL, TRUE);
$this
->assertIdentical($node->{$field_name}[0]['value'], NULL, 'Multiple Select: unselected 1 (no selection) (saved)');
$this
->assertTrue(!isset($node->{$field_name}[1]), 'Multiple Select: unselected 2 (no selection) (saved)');
$this
->drupalGet('node/' . $node->nid);
$this
->assertNoText($value1_alias, 'Multiple Select: unselected 1 (no selection) (displayed)');
$this
->assertNoText($value2_alias, 'Multiple Select: unselected 2 (no selection) (displayed)');
$field = $this
->updateField(array(
'required' => '1',
));
$edit = array();
$this
->drupalPost('node/' . $node->nid . '/edit', $edit, 'Save');
$this
->assertRaw(t('!name field is required.', array(
'!name' => t($field['widget']['label']),
)), 'Multiple Select: "required" property is respected');
$edit = array(
'title' => $this
->randomName(20),
'body' => $this
->randomName(20),
);
$this
->drupalPost('node/add/' . $type_url, $edit, 'Save');
$this
->assertRaw(t('!name field is required.', array(
'!name' => t($field['widget']['label']),
)), 'Multiple Select: "required" property is respected');
}
function testRadios() {
$type = $this->content_types[0];
$type_url = str_replace('_', '-', $type->type);
$value1 = $this
->randomName(5);
$value1_alias = $value1 . '_alias';
$value2 = $this
->randomName(5);
$value2_alias = $value2 . '_alias';
$settings = array(
'type' => 'text',
'widget_type' => 'optionwidgets_buttons',
'allowed_values' => "{$value1}|{$value1_alias}\r\n{$value2}|{$value2_alias}",
);
$field = $this
->createField($settings, 0);
$field_name = $field['field_name'];
$edit = array();
$edit['title'] = $this
->randomName(20);
$edit['body'] = $this
->randomName(20);
$edit[$field_name . '[value]'] = $value1;
$this
->drupalPost('node/add/' . $type_url, $edit, 'Save');
$node = node_load(array(
'title' => $edit['title'],
));
$this
->assertEqual($node->{$field_name}[0]['value'], $value1, 'Radios: checked (saved)');
$this
->drupalGet('node/' . $node->nid);
$this
->assertText($value1_alias, 'Radios: checked (displayed)');
$edit = array();
$edit[$field_name . '[value]'] = '';
$this
->drupalPost('node/' . $node->nid . '/edit', $edit, 'Save');
$node = node_load($node->nid, NULL, TRUE);
$this
->assertIdentical($node->{$field_name}[0]['value'], NULL, 'Radios: unchecked (saved)');
$this
->drupalGet('node/' . $node->nid);
$this
->assertNoText($value1_alias, 'Radios: unchecked (displayed)');
$field = $this
->updateField(array(
'required' => '1',
));
$edit = array();
$edit['title'] = $this
->randomName(20);
$edit['body'] = $this
->randomName(20);
$this
->drupalPost('node/add/' . $type_url, $edit, 'Save');
$this
->assertRaw(t('!name field is required.', array(
'!name' => t($field['widget']['label']),
)), 'Radios: "required" property is respected');
}
function testChecboxes() {
$type = $this->content_types[0];
$type_url = str_replace('_', '-', $type->type);
$value1 = $this
->randomName(5);
$value1_alias = $value1 . '_alias';
$value2 = $this
->randomName(5);
$value2_alias = $value2 . '_alias';
$settings = array(
'type' => 'text',
'multiple' => '1',
'widget_type' => 'optionwidgets_buttons',
'allowed_values' => "{$value1}|{$value1_alias}\r\n{$value2}|{$value2_alias}",
);
$field = $this
->createField($settings, 0);
$field_name = $field['field_name'];
$edit = array(
'title' => $this
->randomName(20),
'body' => $this
->randomName(20),
$field_name . '[value][' . $value1 . ']' => $value1,
$field_name . '[value][' . $value2 . ']' => $value2,
);
$this
->drupalPost('node/add/' . $type_url, $edit, 'Save');
$node = node_load(array(
'title' => $edit['title'],
));
$this
->assertEqual($node->{$field_name}[0]['value'], $value1, 'Checkboxes: selected 1 (saved)');
$this
->assertEqual($node->{$field_name}[1]['value'], $value2, 'Checkboxes: selected 2 (saved)');
$this
->drupalGet('node/' . $node->nid);
$this
->assertText($value1_alias, 'Checkboxes: selected 1 (displayed)');
$this
->assertText($value2_alias, 'Checkboxes: selected 2 (displayed)');
$edit = array(
$field_name . '[value][' . $value1 . ']' => $value1,
$field_name . '[value][' . $value2 . ']' => FALSE,
);
$this
->drupalPost('node/' . $node->nid . '/edit', $edit, 'Save');
$node = node_load($node->nid, NULL, TRUE);
$this
->assertEqual($node->{$field_name}[0]['value'], $value1, 'Checkboxes: selected 1 (saved)');
$this
->assertTrue(!isset($node->{$field_name}[1]), 'Checkboxes: unselected 2 (saved)');
$this
->drupalGet('node/' . $node->nid);
$this
->assertText($value1_alias, 'Checkboxes: selected 1 (displayed)');
$this
->assertNoText($value2_alias, 'Checkboxes: unselected 2 (displayed)');
$edit = array(
$field_name . '[value][' . $value1 . ']' => FALSE,
$field_name . '[value][' . $value2 . ']' => FALSE,
);
$this
->drupalPost('node/' . $node->nid . '/edit', $edit, 'Save');
$node = node_load($node->nid, NULL, TRUE);
$this
->assertIdentical($node->{$field_name}[0]['value'], NULL, 'Checkboxes: unselected 1 (no selection) (saved)');
$this
->assertTrue(!isset($node->{$field_name}[1]), 'Checkboxes: unselected 2 (no selection) (saved)');
$this
->drupalGet('node/' . $node->nid);
$this
->assertNoText($value1_alias, 'Checkboxes: unselected 1 (no selection) (displayed)');
$this
->assertNoText($value2_alias, 'Checkboxes: unselected 2 (no selection) (displayed)');
$field = $this
->updateField(array(
'required' => '1',
));
$edit = array(
$field_name . '[value][' . $value1 . ']' => FALSE,
$field_name . '[value][' . $value2 . ']' => FALSE,
);
$this
->drupalPost('node/' . $node->nid . '/edit', $edit, 'Save');
$this
->assertRaw(t('!name field is required.', array(
'!name' => t($field['widget']['label']),
)), 'Checkboxes: "required" property is respected');
$edit = array();
$edit['title'] = $this
->randomName(20);
$edit['body'] = $this
->randomName(20);
$this
->drupalPost('node/add/' . $type_url, $edit, 'Save');
$this
->assertRaw(t('!name field is required.', array(
'!name' => t($field['widget']['label']),
)), 'Checkboxes: "required" property is respected');
}
}
class ContentEmptyDeltaTest extends ContentCrudTestCase {
public static function getInfo() {
return array(
'name' => t('Empty deltas'),
'description' => t('Test leaving empty values on a multivalue field and then removing them.'),
'group' => t('CCK'),
);
}
function setUp() {
parent::setUp();
$this
->loginWithPermissions();
$this
->acquireContentTypes(1);
}
function testEmptyTextField() {
$type = $this->content_types[0];
$type_url = str_replace('_', '-', $type->type);
$value1 = $this
->randomName(5);
$value2 = $this
->randomName(5);
$value3 = $this
->randomName(5);
$field = $this
->createFieldText(array(
'text_processing' => 0,
'multiple' => 1,
));
$field_name = $field['field_name'];
$edit = array(
'title' => $this
->randomName(20),
'body' => $this
->randomName(20),
'type' => $type->name,
);
$edit[$field_name][0]['value'] = $value1;
$edit[$field_name][1]['value'] = $value2;
$edit[$field_name][2]['value'] = $value3;
$node = $this
->drupalCreateNode($edit);
$max_delta = max(array_keys($node->{$field_name}));
$this
->assertEqual($max_delta, 2, 'Three values saved, highest delta is 2');
$this
->drupalGet('node/' . $node->nid);
$this
->assertText($value1, 'First value displayed');
$this
->assertText($value2, 'Second value displayed');
$this
->assertText($value3, 'Third value displayed');
$node->{$field_name}[1]['value'] = '';
node_save($node);
$node = node_load($node->nid, NULL, TRUE);
$this
->assertIdentical($node->{$field_name}[1]['value'], NULL, 'Second value is empty');
$max_delta = max(array_keys($node->{$field_name}));
$this
->assertEqual($max_delta, 2, 'Three values saved, highest delta is 2');
$this
->drupalGet('node/' . $node->nid);
$this
->assertText($value1, 'First value displayed');
$this
->assertNoText($value2, 'Second value not displayed');
$this
->assertText($value3, 'Third value displayed');
$node->{$field_name}[1]['_remove'] = 1;
node_save($node);
$node = node_load($node->nid, NULL, TRUE);
$this
->assertEqual($node->{$field_name}[1]['value'], $value3, 'Third value has moved to delta 1');
$max_delta = max(array_keys($node->{$field_name}));
$this
->assertEqual($max_delta, 1, 'Two values saved, highest delta is 1');
$this
->drupalGet('node/' . $node->nid);
$this
->assertText($value1, 'First value displayed');
$this
->assertNoText($value2, 'Second value not displayed');
$this
->assertText($value3, 'Third value displayed');
}
}