You are here

function ContentOptionWidgetTest::testChecboxes in Content Construction Kit (CCK) 6.2

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

Multiple (checkboxes), not required:

  • TODO: check there's no 'none' choice in the form.
  • Create a node with two values.
  • Edit the node and select only one value.
  • Edit the node and unselect the values (selecting nothing).

Multiple (checkboxes), 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 1155

Class

ContentOptionWidgetTest

Code

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

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

  // Create a node with two values selected
  $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 the node and unselect the values (selecting nothing -
  // there is no 'none' choice for checkboxes).
  $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 the node and unselect the values (selecting nothing -
  // there is no 'none' choice for checkboxes).
  $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)');

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

  // Check the form doesn't submit when nothing is selected.
  $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');
}