View source
<?php
class MultifieldTableTest extends DrupalWebTestCase {
public static function getInfo() {
return array(
'name' => 'Multifield table',
'description' => 'Test the Multifield table module.',
'group' => 'Multifield',
);
}
function setUp() {
parent::setUp('multifield', 'multifield_table');
$this->privileged_user = $this
->drupalCreateUser(array(
'bypass node access',
'administer content types',
'administer multifield',
));
$this
->drupalLogin($this->privileged_user);
$type_name = strtolower($this
->randomName(8)) . '_test';
$type = $this
->drupalCreateContentType(array(
'name' => $type_name,
'type' => $type_name,
));
$this->type = $type->type;
$this->hyphen_type = str_replace('_', '-', $this->type);
}
function fieldUIAddNewField($bundle_path, $initial_edit, $field_edit = array(), $instance_edit = array()) {
$initial_edit += array(
'fields[_add_new_field][type]' => 'test_field',
'fields[_add_new_field][widget_type]' => 'test_field_widget',
);
$label = $initial_edit['fields[_add_new_field][label]'];
$field_name = $initial_edit['fields[_add_new_field][field_name]'];
$this
->drupalPost("{$bundle_path}/fields", $initial_edit, t('Save'));
$this
->assertRaw(t('These settings apply to the %label field everywhere it is used.', array(
'%label' => $label,
)), t('Field settings page was displayed.'));
$this
->drupalPost(NULL, $field_edit, t('Save field settings'));
$this
->assertRaw(t('Updated field %label field settings.', array(
'%label' => $label,
)), t('Redirected to instance and widget settings page.'));
$this
->drupalPost(NULL, $instance_edit, t('Save settings'));
$this
->assertRaw(t('Saved %label configuration.', array(
'%label' => $label,
)), t('Redirected to "Manage fields" page.'));
$this
->assertFieldByXPath('//table[@id="field-overview"]//td[1]', $label, t('Field was created and appears in the overview page.'));
}
function checkMultifieldTable($fields) {
$position = 0;
foreach ($fields as $field) {
$position++;
$xpath = $this
->buildXPathQuery('//table/thead/tr/th[:position]', array(
':position' => $position,
));
$this
->assertFieldByXPath($xpath, $field['label']);
$xpath = $this
->buildXPathQuery('//table/thead/tr/th[contains(@class, :field-name)]', array(
':field-name' => drupal_clean_css_identifier($field['name']),
));
$this
->assertFieldByXPath($xpath, $field['label']);
$xpath = $this
->buildXPathQuery('//table/tbody/tr/td[:position]', array(
':position' => $position,
));
$this
->assertFieldByXPath($xpath, $field['value']);
}
}
function shuffleAssoc($list) {
if (!is_array($list)) {
return $list;
}
$keys = array_keys($list);
shuffle($keys);
$random = array();
foreach ($keys as $key) {
$random[$key] = $list[$key];
}
return $random;
}
public function testMultifieldTableBasic() {
$this->multifield = drupal_strtolower($this
->randomName());
$edit = array(
'label' => $this->multifield,
'machine_name' => $this->multifield,
);
$this
->drupalPost('admin/structure/multifield/add', $edit, t('Save'));
$this->multifield_path = 'admin/structure/multifield/manage/' . str_replace('_', '-', $this->multifield);
$fields = array(
'text' => array(
'type' => 'text',
'widget' => 'text_textfield',
'label' => $this
->randomName(),
'name' => drupal_strtolower($this
->randomName()),
'value' => $this
->randomName(),
),
'longtext' => array(
'type' => 'text_long',
'widget' => 'text_textarea',
'label' => $this
->randomName(),
'name' => drupal_strtolower($this
->randomName()),
'value' => $this
->randomName(32),
),
'integer' => array(
'type' => 'number_integer',
'widget' => 'number',
'label' => $this
->randomName(),
'name' => drupal_strtolower($this
->randomName()),
'value' => rand(0, 999),
),
);
foreach ($fields as $field) {
$edit = array(
'fields[_add_new_field][type]' => $field['type'],
'fields[_add_new_field][widget_type]' => $field['widget'],
'fields[_add_new_field][label]' => $field['label'],
'fields[_add_new_field][field_name]' => $field['name'],
);
$this
->fieldUIAddNewField($this->multifield_path, $edit);
}
$bundle_path = 'admin/structure/types/manage/' . $this->hyphen_type;
$edit = array(
'fields[_add_new_field][type]' => $this->multifield,
'fields[_add_new_field][widget_type]' => 'multifield_default',
'fields[_add_new_field][label]' => 'Multifield',
'fields[_add_new_field][field_name]' => 'multifield',
);
$this
->fieldUIAddNewField($bundle_path, $edit);
$manage_display = $bundle_path . '/display';
$edit = array(
'fields[field_multifield][type]' => 'multifield_table',
);
$this
->drupalPost($manage_display, $edit, t('Save'));
$edit = array(
'title' => $this
->randomName(),
);
foreach ($fields as $field) {
$edit["field_multifield[und][0][field_{$field['name']}][und][0][value]"] = $field['value'];
}
$this
->drupalPost('node/add/' . $this->hyphen_type, $edit, t('Save'));
$this->node_url = $this
->getUrl();
$this
->checkMultifieldTable($fields);
$shuffled_fields = $this
->shuffleAssoc($fields);
$position = 0;
$edit = array();
foreach ($shuffled_fields as $field) {
$position++;
$edit["fields[field_{$field['name']}][weight]"] = $position;
}
$this
->drupalPost($this->multifield_path . '/display', $edit, t('Save'));
$this
->drupalGet($this->node_url);
$this
->checkMultifieldTable($shuffled_fields);
}
}