You are here

function ContentOptionWidgetTest::testRadios in Content Construction Kit (CCK) 6.3

Same name and namespace in other branches
  1. 6.2 tests/content.crud.test \ContentOptionWidgetTest::testRadios()

Single (radios), 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 (radios), 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 1095

Class

ContentOptionWidgetTest

Code

function testRadios() {
  $type = $this->content_types[0];
  $type_url = str_replace('_', '-', $type->type);

  // Create the field - 'single' (radios).
  $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'];

  // Create a node with one value selected
  $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 the node and unselect the value (selecting '- None -').
  $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)');

  // Change field to required.
  $field = $this
    ->updateField(array(
    'required' => '1',
  ));

  // Check the form doesn't submit when nothing is selected.
  // Doing this on the pre-filled node doesn't take, so we test that on a new node.
  $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');
}