You are here

function PollTestCase::assertPollChoiceOrder in Drupal 7

Assert correct poll choice order in the node form after submission.

Verifies both the order in the DOM and in the 'weight' form elements.

Parameters

$choices: An array containing poll choices, as generated by PollTestCase::_generateChoices().

$index: (optional) The amount/number of already submitted poll choices. Defaults to 0.

$preview: (optional) Whether to also check the poll preview.

See also

PollTestCase::_pollGenerateEdit()

1 call to PollTestCase::assertPollChoiceOrder()
PollTestCase::pollCreate in modules/poll/poll.test
Creates a poll.

File

modules/poll/poll.test, line 132
Tests for poll.module.

Class

PollTestCase
@file Tests for poll.module.

Code

function assertPollChoiceOrder(array $choices, $index = 0, $preview = FALSE) {
  $expected = array();
  $weight = 0;
  foreach ($choices as $id => $label) {
    if ($id < $index) {

      // The expected weight of each choice is higher than the previous one.
      $weight++;

      // Directly assert the weight form element value for this choice.
      $this
        ->assertFieldByName('choice[chid:' . $id . '][weight]', $weight, format_string('Found choice @id with weight @weight.', array(
        '@id' => $id,
        '@weight' => $weight,
      )));

      // Append to our (to be reversed) stack of labels.
      $expected[$weight] = $label;
    }
  }
  ksort($expected);

  // Verify DOM order of poll choices (i.e., #weight of form elements).
  $elements = $this
    ->xpath('//input[starts-with(@name, :prefix) and contains(@name, :suffix)]', array(
    ':prefix' => 'choice[chid:',
    ':suffix' => '][chtext]',
  ));
  $expected_order = $expected;
  foreach ($elements as $element) {
    $next_label = array_shift($expected_order);
    $this
      ->assertEqual((string) $element['value'], $next_label);
  }

  // If requested, also verify DOM order in preview.
  if ($preview) {
    $elements = $this
      ->xpath('//div[contains(@class, :teaser)]/descendant::div[@class=:text]', array(
      ':teaser' => 'node-teaser',
      ':text' => 'text',
    ));
    $expected_order = $expected;
    foreach ($elements as $element) {
      $next_label = array_shift($expected_order);
      $this
        ->assertEqual((string) $element, $next_label, format_string('Found choice @label in preview.', array(
        '@label' => $next_label,
      )));
    }
  }
}