FIX: Polls can be quoted and loaded (#10456)

FIX: Polls can be quoted and loaded (#10456)

Variable ‘post’ was used to hold the current post and the post where the poll appeared, which can be different when polls are quoted.

diff --git a/plugins/poll/assets/javascripts/initializers/extend-for-poll.js.es6 b/plugins/poll/assets/javascripts/initializers/extend-for-poll.js.es6
index 897d37e..25bfb5f 100644
--- a/plugins/poll/assets/javascripts/initializers/extend-for-poll.js.es6
+++ b/plugins/poll/assets/javascripts/initializers/extend-for-poll.js.es6
@@ -60,7 +60,7 @@ function initializePolls(api) {
       return;
     }
 
-    let post = helper.getModel();
+    const post = helper.getModel();
     api.preventCloak(post.id);
     post.pollsChanged();
 
@@ -73,25 +73,24 @@ function initializePolls(api) {
       const $poll = $(pollElem);
       const pollName = $poll.data("poll-name");
       let poll = polls[pollName];
+      let pollPost = post;
       let vote = votes[pollName] || [];
 
       const quotedId = $poll.parent(".expanded-quote").data("post-id");
-      if (quotedId) {
-        const quotedPost = post.quoted[quotedId];
-        if (quotedPost) {
-          post = EmberObject.create(quotedPost);
-          poll = EmberObject.create(
-            quotedPost.polls.find(p => p.name === pollName)
-          );
-          vote = quotedPost.polls_votes || {};
-          vote = vote[pollName] || [];
-        }
+      if (quotedId && post.quoted[quotedId]) {
+        pollPost = post.quoted[quotedId];
+        pollPost = EmberObject.create(pollPost);
+        poll = EmberObject.create(
+          pollPost.polls.find(p => p.name === pollName)
+        );
+        vote = pollPost.polls_votes || {};
+        vote = vote[pollName] || [];
       }
 
       if (poll) {
         const attrs = {
-          id: `${pollName}-${post.id}`,
-          post,
+          id: `${pollName}-${pollPost.id}`,
+          post: pollPost,
           poll,
           vote,
           groupableUserFields: (
diff --git a/plugins/poll/test/javascripts/acceptance/poll-quote-test.js.es6 b/plugins/poll/test/javascripts/acceptance/poll-quote-test.js.es6
new file mode 100644
index 0000000..465b593
--- /dev/null
+++ b/plugins/poll/test/javascripts/acceptance/poll-quote-test.js.es6
@@ -0,0 +1,699 @@
+import { acceptance } from "helpers/qunit-helpers";
+import { clearPopupMenuOptionsCallback } from "discourse/controllers/composer";
+import { Promise } from "rsvp";
+
+acceptance("Poll quote", {
+  loggedIn: true,
+  settings: { poll_enabled: true },
+  beforeEach() {
+    clearPopupMenuOptionsCallback();
+  },
+  pretend(server, helper) {
+    server.get("/t/topic_with_two_polls.json", () => {
+      return new Promise(resolve => {
+        resolve(
+          helper.response({
+            post_stream: {
+              posts: [
+                {
+                  id: 133,
+                  name: null,
+                  username: "bianca",
+                  avatar_template:
+                    "/letter_avatar_proxy/v4/letter/b/3be4f8/{size}.png",
+                  created_at: "2020-08-17T12:05:24.577Z",
+                  cooked:
+                    '<div class="poll" data-poll-status="open" data-poll-name="poll1" data-poll-results="always" data-poll-charttype="bar" data-poll-type="regular">\n<div>\n<div class="poll-container">\n<ul>\n<li data-poll-option-id="bf48cd4958a17180e2a298e246988f94">Alpha</li>\n<li data-poll-option-id="c19aa835729ab0413a84a2c9850c4005">Beta</li>\n</ul>\n</div>\n<div class="poll-info">\n<p>\n<span class="info-number">0</span>\n<span class="info-label">voters</span>\n</p>\n</div>\n</div>\n</div>\n<div class="poll" data-poll-status="open" data-poll-name="poll2" data-poll-results="always" data-poll-charttype="bar" data-poll-type="regular">\n<div>\n<div class="poll-container">\n<ul>\n<li data-poll-option-id="def034c6770c6fd3754c054ef9ec4721">First</li>\n<li data-poll-option-id="e0f55d1a981683789bec2a0b05eb70ef">Second</li>\n</ul>\n</div>\n<div class="poll-info">\n<p>\n<span class="info-number">0</span>\n<span class="info-label">voters</span>\n</p>\n</div>\n</div>\n</div>',
+                  post_number: 1,
+                  post_type: 1,
+                  updated_at: "2020-08-17T12:05:24.577Z",
+                  reply_count: 0,
+                  reply_to_post_number: null,
+                  quote_count: 0,
+                  incoming_link_count: 0,
+                  reads: 1,
+                  readers_count: 0,
+                  score: 0,
+                  yours: true,
+                  topic_id: 130,
+                  topic_slug: "topic-with-two-polls",
+                  display_username: null,
+                  primary_group_name: null,
+                  primary_group_flair_url: null,
+                  primary_group_flair_bg_color: null,
+                  primary_group_flair_color: null,
+                  version: 1,
+                  can_edit: true,
+                  can_delete: false,
+                  can_recover: false,
+                  can_wiki: true,
+                  link_counts: [
+                    {
+                      url:
+                        "http://forum.example.com/t/topic-with-two-quoted-polls/129",
+                      internal: true,
+                      reflection: true,
+                      title: "Topic with two quoted polls",
+                      clicks: 0
+                    }
+                  ],
+                  read: true,
+                  user_title: "Tester",
+                  title_is_group: false,
+                  bookmarked: false,
+                  actions_summary: [
+                    {
+                      id: 3,
+                      can_act: true
+                    },
+                    {
+                      id: 4,
+                      can_act: true
+                    },
+                    {
+                      id: 8,
+                      can_act: true
+                    },
+                    {
+                      id: 7,
+                      can_act: true
+                    }
+                  ],
+                  moderator: false,
+                  admin: true,
+                  staff: true,
+                  user_id: 1,
+                  hidden: false,
+                  trust_level: 0,
+                  deleted_at: null,
+                  user_deleted: false,
+                  edit_reason: null,
+                  can_view_edit_history: true,
+                  wiki: false,
+                  reviewable_id: 0,
+                  reviewable_score_count: 0,
+                  reviewable_score_pending_count: 0,
+                  calendar_details: [],
+                  polls: [
+                    {
+                      name: "poll1",
+                      type: "regular",
+                      status: "open",
+                      results: "always",
+                      options: [
+                        {
+                          id: "bf48cd4958a17180e2a298e246988f94",
+                          html: "Alpha",
+                          votes: 0
+                        },
+                        {
+                          id: "c19aa835729ab0413a84a2c9850c4005",
+                          html: "Beta",
+                          votes: 0
+                        }
+                      ],
+                      voters: 0,
+                      chart_type: "bar"
+                    },
+                    {
+                      name: "poll2",
+                      type: "regular",
+                      status: "open",
+                      results: "always",
+                      options: [
+                        {
+                          id: "def034c6770c6fd3754c054ef9ec4721",
+                          html: "First",
+                          votes: 0
+                        },
+                        {
+                          id: "e0f55d1a981683789bec2a0b05eb70ef",
+                          html: "Second",
+                          votes: 0
+                        }
+                      ],
+                      voters: 0,
+                      chart_type: "bar"
+                    }

[... diff too long, it was truncated ...]

GitHub sha: a27b6770

This commit appears in #10456 which was approved by ZogStriP. It was merged by nbianca.