From 82d39d625ca7c6c117821d48f877bf719f8542a7 Mon Sep 17 00:00:00 2001
From: catch <catch@35733.no-reply.drupal.org>
Date: Sat, 7 Jan 2012 17:39:37 +0900
Subject: [PATCH] Issue #939880 by amateescu, marcingy: Fixed Poll Module
 throws PDOException when creating poll content.

---
 core/modules/poll/poll.module |  5 ++---
 core/modules/poll/poll.test   | 15 ++++++++++++++-
 2 files changed, 16 insertions(+), 4 deletions(-)

diff --git a/core/modules/poll/poll.module b/core/modules/poll/poll.module
index 982981afb9b4..9b6b28850bd9 100644
--- a/core/modules/poll/poll.module
+++ b/core/modules/poll/poll.module
@@ -404,6 +404,7 @@ function _poll_choice_form($key, $chid = NULL, $value = '', $votes = 0, $weight
     '#maxlength' => 7,
     '#parents' => array('choice', $key, 'chvotes'),
     '#access' => user_access('administer nodes'),
+    '#element_validate' => array('element_validate_integer'),
   );
 
   $form['weight'] = array(
@@ -447,10 +448,8 @@ function poll_node_form_submit(&$form, &$form_state) {
  */
 function poll_validate($node, $form) {
   if (isset($node->title)) {
-    // Check for at least two options and validate amount of votes:
+    // Check for at least two options and validate amount of votes.
     $realchoices = 0;
-    // Renumber fields
-    $node->choice = array_values($node->choice);
     foreach ($node->choice as $i => $choice) {
       if ($choice['chtext'] != '') {
         $realchoices++;
diff --git a/core/modules/poll/poll.test b/core/modules/poll/poll.test
index 8c159ea70511..557dcec3e341 100644
--- a/core/modules/poll/poll.test
+++ b/core/modules/poll/poll.test
@@ -24,8 +24,9 @@ class PollTestCase extends DrupalWebTestCase {
   function pollCreate($title, $choices, $preview = TRUE) {
     $this->assertTrue(TRUE, 'Create a poll');
 
+    $admin_user = $this->drupalCreateUser(array('create poll content', 'administer nodes'));
     $web_user = $this->drupalCreateUser(array('create poll content', 'access content', 'edit own poll content'));
-    $this->drupalLogin($web_user);
+    $this->drupalLogin($admin_user);
 
     // Get the form first to initialize the state of the internal browser.
     $this->drupalGet('node/add/poll');
@@ -33,6 +34,18 @@ class PollTestCase extends DrupalWebTestCase {
     // Prepare a form with two choices.
     list($edit, $index) = $this->_pollGenerateEdit($title, $choices);
 
+    // Verify that the vote count element only allows non-negative integers.
+    $edit['choice[new:1][chvotes]'] = -1;
+    $edit['choice[new:0][chvotes]'] = $this->randomString(7);
+    $this->drupalPost(NULL, $edit, t('Save'));
+    $this->assertText(t('Negative values are not allowed.'));
+    $this->assertText(t('Vote count for new choice must be an integer.'));
+
+    // Repeat steps for initializing the state of the internal browser.
+    $this->drupalLogin($web_user);
+    $this->drupalGet('node/add/poll');
+    list($edit, $index) = $this->_pollGenerateEdit($title, $choices);
+
     // Re-submit the form until all choices are filled in.
     if (count($choices) > 2) {
       while ($index < count($choices)) {
-- 
GitLab