summaryrefslogtreecommitdiff
path: root/modules/poll/poll.test
diff options
context:
space:
mode:
authorAngie Byron <webchick@24967.no-reply.drupal.org>2010-10-25 15:51:21 +0000
committerAngie Byron <webchick@24967.no-reply.drupal.org>2010-10-25 15:51:21 +0000
commit2f76c696c013f5cbaec91ac724c39297611539b3 (patch)
treead61188e6599f6e353052c46e8e77916446736b4 /modules/poll/poll.test
parente027699979be3dd671390a259ef60d6c7410890e (diff)
downloadbrdo-2f76c696c013f5cbaec91ac724c39297611539b3.tar.gz
brdo-2f76c696c013f5cbaec91ac724c39297611539b3.tar.bz2
#445736 by sun: Fixed Poll does not respect weight in Preview or More button.
Diffstat (limited to 'modules/poll/poll.test')
-rw-r--r--modules/poll/poll.test137
1 files changed, 112 insertions, 25 deletions
diff --git a/modules/poll/poll.test b/modules/poll/poll.test
index 23af858c2..ba1a53553 100644
--- a/modules/poll/poll.test
+++ b/modules/poll/poll.test
@@ -11,36 +11,41 @@ class PollTestCase extends DrupalWebTestCase {
/**
* Creates a poll.
*
- * @param string $title The title of the poll.
- * @param array $choices Choices.
- * @param boolean $test_preview Whether to test if the preview is working or not.
- * @return integer The nid of the created poll, or FALSE on error.
+ * @param string $title
+ * The title of the poll.
+ * @param array $choices
+ * A list of choice labels.
+ * @param boolean $preview
+ * (optional) Whether to test if the preview is working or not. Defaults to
+ * TRUE.
+ *
+ * @return
+ * The node id of the created poll, or FALSE on error.
*/
- function pollCreate($title, $choices, $test_preview = TRUE) {
+ function pollCreate($title, $choices, $preview = TRUE) {
$this->assertTrue(TRUE, 'Create a poll');
$web_user = $this->drupalCreateUser(array('create poll content', 'access content', 'edit own poll content'));
$this->drupalLogin($web_user);
- // Get the form first to initialize the state of the internal browser
+ // Get the form first to initialize the state of the internal browser.
$this->drupalGet('node/add/poll');
- // Prepare a form with two choices
+ // Prepare a form with two choices.
list($edit, $index) = $this->_pollGenerateEdit($title, $choices);
+ // Re-submit the form until all choices are filled in.
if (count($choices) > 2) {
- // Re-submit the form while the choices are all in
while ($index < count($choices)) {
$this->drupalPost(NULL, $edit, t('More choices'));
+ $this->assertPollChoiceOrder($choices, $index);
list($edit, $index) = $this->_pollGenerateEdit($title, $choices, $index);
}
}
- if ($test_preview) {
+ if ($preview) {
$this->drupalPost(NULL, $edit, t('Preview'));
- foreach ($choices as $k => $choice_text) {
- $this->assertRaw($choice_text, t('Choice @choice found was in preview.', array('@choice' => $k)));
- }
+ $this->assertPollChoiceOrder($choices, $index, TRUE);
list($edit, $index) = $this->_pollGenerateEdit($title, $choices, $index);
}
@@ -52,20 +57,42 @@ class PollTestCase extends DrupalWebTestCase {
return isset($node->nid) ? $node->nid : FALSE;
}
- function _pollGenerateEdit($title, $choices, $index = 0) {
- $max_new_choices = $index == 0 ? 2 : 5;
+ /**
+ * Generates POST values for the poll node form, specifically poll choices.
+ *
+ * @param $title
+ * The title for the poll node.
+ * @param $choices
+ * An array containing poll choices, as generated by
+ * PollTestCase::_generateChoices().
+ * @param $index
+ * (optional) The amount/number of already submitted poll choices. Defaults
+ * to 0.
+ *
+ * @return
+ * An indexed array containing:
+ * - The generated POST values, suitable for
+ * DrupalWebTestCase::drupalPost().
+ * - The number of poll choices contained in 'edit', for potential re-usage
+ * in subsequent invocations of this function.
+ */
+ function _pollGenerateEdit($title, array $choices, $index = 0) {
+ $max_new_choices = ($index == 0 ? 2 : 5);
$already_submitted_choices = array_slice($choices, 0, $index);
$new_choices = array_values(array_slice($choices, $index, $max_new_choices));
- $langcode = LANGUAGE_NONE;
$edit = array(
- "title" => $title
+ 'title' => $title,
);
foreach ($already_submitted_choices as $k => $text) {
$edit['choice[chid:' . $k . '][chtext]'] = $text;
}
foreach ($new_choices as $k => $text) {
$edit['choice[new:' . $k . '][chtext]'] = $text;
+ // To test poll choice weights, every new choice is sorted in front of
+ // existing choices. Existing/already submitted choices should keep their
+ // weight.
+ $edit['choice[new:' . $k . '][weight]'] = (- $index - $k);
}
return array($edit, count($already_submitted_choices) + count($new_choices));
}
@@ -78,6 +105,67 @@ class PollTestCase extends DrupalWebTestCase {
return $choices;
}
+ /**
+ * Assert correct poll choice order in the node form after submission.
+ *
+ * Verifies both the order in the DOM and in the 'weight' form elements.
+ *
+ * @param $choices
+ * An array containing poll choices, as generated by
+ * PollTestCase::_generateChoices().
+ * @param $index
+ * (optional) The amount/number of already submitted poll choices. Defaults
+ * to 0.
+ * @param $preview
+ * (optional) Whether to also check the poll preview.
+ *
+ * @see PollTestCase::_pollGenerateEdit()
+ */
+ function assertPollChoiceOrder(array $choices, $index = 0, $preview = FALSE) {
+ $expected = array();
+ foreach ($choices as $id => $label) {
+ if ($id < $index) {
+ // The expected weight of each choice is exactly the negated id.
+ // @see PollTestCase::_pollGenerateEdit()
+ $weight = -$id;
+ // Directly assert the weight form element value for this choice.
+ $this->assertFieldByName('choice[chid:' . $id . '][weight]', $weight, t('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, t('Found choice @label in preview.', array(
+ '@label' => $next_label,
+ )));
+ }
+ }
+ }
+
function pollUpdate($nid, $title, $edit) {
// Edit the poll node.
$this->drupalPost('node/' . $nid . '/edit', $edit, t('Save'));
@@ -332,7 +420,6 @@ class PollJSAddChoice extends DrupalWebTestCase {
$web_user = $this->drupalCreateUser(array('create poll content', 'access content'));
$this->drupalLogin($web_user);
$this->drupalGet('node/add/poll');
- $langcode = LANGUAGE_NONE;
$edit = array(
"title" => $this->randomName(),
'choice[new:0][chtext]' => $this->randomName(),
@@ -597,33 +684,33 @@ class PollExpirationTestCase extends PollTestCase {
$title = $this->randomName();
$choices = $this->_generateChoices(2);
$poll_nid = $this->pollCreate($title, $choices, FALSE);
- $this->assertTrue($poll_nid, t('Poll for auto-expire test created.'), t('Poll'));
+ $this->assertTrue($poll_nid, t('Poll for auto-expire test created.'));
// Visit the poll edit page and verify that by default, expiration
// is set to unlimited.
$this->drupalGet("node/$poll_nid/edit");
- $this->assertField('runtime', t('Poll expiration setting found.'), t('Poll'));
+ $this->assertField('runtime', t('Poll expiration setting found.'));
$elements = $this->xpath('//select[@id="edit-runtime"]/option[@selected="selected"]');
- $this->assertTrue(isset($elements[0]['value']) && $elements[0]['value'] == 0, t('Poll expiration set to unlimited.'), t('Poll'));
+ $this->assertTrue(isset($elements[0]['value']) && $elements[0]['value'] == 0, t('Poll expiration set to unlimited.'));
// Set the expiration to one week.
$edit = array();
$poll_expiration = 604800; // One week.
$edit['runtime'] = $poll_expiration;
$this->drupalPost(NULL, $edit, t('Save'));
- $this->assertRaw(t('Poll %title has been updated.', array('%title' => $title)), t('Poll expiration settings saved.'), t('Poll'));
+ $this->assertRaw(t('Poll %title has been updated.', array('%title' => $title)), t('Poll expiration settings saved.'));
// Make sure that the changed expiration settings is kept.
$this->drupalGet("node/$poll_nid/edit");
$elements = $this->xpath('//select[@id="edit-runtime"]/option[@selected="selected"]');
- $this->assertTrue(isset($elements[0]['value']) && $elements[0]['value'] == $poll_expiration, t('Poll expiration set to unlimited.'), t('Poll'));
+ $this->assertTrue(isset($elements[0]['value']) && $elements[0]['value'] == $poll_expiration, t('Poll expiration set to unlimited.'));
// Force a cron run. Since the expiration date has not yet been reached,
// the poll should remain active.
drupal_cron_run();
$this->drupalGet("node/$poll_nid/edit");
$elements = $this->xpath('//input[@id="edit-active-1"]');
- $this->assertTrue(isset($elements[0]) && !empty($elements[0]['checked']), t('Poll is still active.'), t('Poll'));
+ $this->assertTrue(isset($elements[0]) && !empty($elements[0]['checked']), t('Poll is still active.'));
// Test expiration. Since REQUEST_TIME is a constant and we don't
// want to keep SimpleTest waiting until the moment of expiration arrives,
@@ -638,6 +725,6 @@ class PollExpirationTestCase extends PollTestCase {
drupal_cron_run();
$this->drupalGet("node/$poll_nid/edit");
$elements = $this->xpath('//input[@id="edit-active-0"]');
- $this->assertTrue(isset($elements[0]) && !empty($elements[0]['checked']), t('Poll has expired.'), t('Poll'));
+ $this->assertTrue(isset($elements[0]) && !empty($elements[0]['checked']), t('Poll has expired.'));
}
-} \ No newline at end of file
+}