function ContentOptionWidgetTest::testSelect in Content Construction Kit (CCK) 6.3
Same name and namespace in other branches
- 6.2 tests/content.crud.test \ContentOptionWidgetTest::testSelect()
Single select, not required:
- TODO: check there's a 'none' choice in the form.
- Create a node with one value selected.
- Edit the node and unselect the value (selecting '- None -').
Single select, required:
- TODO: check there's no 'none' choice in the form.
Multiple select, not required:
- TODO: check there's a 'none' choice in the form.
- Edit the node and select multiple values.
- Edit the node and unselect one value.
- Edit the node and unselect the values (selecting '- None -').
- Edit the node and unselect the values (selecting nothing).
Multiple select, required:
- TODO: check there's no 'none' choice in the form.
- Check the form doesn't submit when nothing is selected.
File
- tests/
content.crud.test, line 975
Class
Code
function testSelect() {
$type = $this->content_types[0];
$type_url = str_replace('_', '-', $type->type);
// Create the field - start with 'single'.
$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'];
// Create a node with one value selected
$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 the node and unselect the value (selecting '- None -').
$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)');
// Change to a multiple field
$field = $this
->updateField(array(
'multiple' => '1',
'required' => '0',
));
// Edit the node and select multiple values.
$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 the node and unselect one value.
$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 the node and unselect the values (selecting '- None -').
$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 the node and unselect the values (selecting nothing).
// We first need to put values back in (no test needed).
$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)');
// Change the field to 'required'.
$field = $this
->updateField(array(
'required' => '1',
));
// Check the form doesn't submit when nothing is selected.
$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');
}