Skip to content

Commit

Permalink
Ensure survey radio button ID uniqueness across surveys (#57)
Browse files Browse the repository at this point in the history
* Ensure survey radio IDs are unique across surveys, to avoid ID clashes with two surveys on the same page.
* Version bump.
  • Loading branch information
jamestait authored Apr 22, 2019
1 parent 63992cb commit c7ebcc4
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 4 deletions.
17 changes: 16 additions & 1 deletion poll/poll.py
Original file line number Diff line number Diff line change
Expand Up @@ -1251,7 +1251,22 @@ def workbench_scenarios():
answers='[["sa", "Strongly Agree"], ["a", "Agree"], ["n", "Neutral"],
["d", "Disagree"], ["sd", "Strongly Disagree"]]'
feedback="### Thank you

for running the tests."/>
""")
"""),
("Survey Multiple",
"""
<vertical_demo>
<survey tally='{"q1": {"sa": 5, "a": 5, "n": 3, "d": 2, "sd": 5}}'
questions='[["q1", {"label": "I feel like this test will pass.", "img": null, "img_alt": null}]]'
answers='[["sa", "Strongly Agree"], ["a", "Agree"], ["n", "Neutral"],
["d", "Disagree"], ["sd", "Strongly Disagree"]]'
feedback="### Thank you&#10;&#10;for running the tests."/>
<survey tally='{"q1": {"sa": 5, "a": 5, "n": 3, "d": 2, "sd": 5}}'
questions='[["q1", {"label": "Most likely to win the World Cup.", "img": null, "img_alt": null}]]'
answers='[["sa", "South Africa"], ["a", "Angola"], ["n", "Netherlands"],
["d", "Deutschland"], ["sd", "Someone different"]]'
feedback="### Thank you&#10;&#10;for running the tests."/>
</vertical_demo>
"""),
]

def get_filename(self):
Expand Down
4 changes: 2 additions & 2 deletions poll/public/html/survey.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ <h3 class="poll-header">{{block_name}}</h3>
{% with answer_count=forloop.counter %}
{% for answer, label in answers %}
{% if forloop.counter == answer_count %}
<label for="{{key}}-{{answer_count}}">
<label for="{{block_id}}-{{key}}-{{answer_count}}">
<input type="radio"
name="{{key}}"
id="{{key}}-{{forloop.counter}}"
id="{{block_id}}-{{key}}-{{forloop.counter}}"
value="{{answer}}"{% if question.choice == answer %} checked{% endif %}
{% if question.img_alt %}
aria-label="{{question.img_alt}} {{label}}"
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def package_data(pkg, roots):

setup(
name='xblock-poll',
version='1.8.4',
version='1.8.5',
description='An XBlock for polling users.',
packages=[
'poll',
Expand Down
11 changes: 11 additions & 0 deletions tests/integration/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,3 +265,14 @@ def test_submit_not_enabled_on_revisit(self):

self.go_to_page('Poll Functions')
self.assertFalse(self.get_submit().is_enabled())

def test_survey_radio_ids_unique(self):
"""
Verify that multiple surveys on the same page with the same question
IDs still produce unique HTML radio button IDs.
"""
self.go_to_page('Survey Multiple')
elements = self.browser.find_elements_by_css_selector('.survey-option input[type=radio]')
all_ids = sorted([element.get_attribute('id') for element in elements])
unique_ids = sorted(list(set(all_ids)))
self.assertSequenceEqual(all_ids, unique_ids)
12 changes: 12 additions & 0 deletions tests/integration/xml/survey_multiple.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<vertical_demo>
<survey tally='{"q1": {"sa": 5, "a": 5, "n": 3, "d": 2, "sd": 5}}'
questions='[["q1", {"label": "I feel like this test will pass.", "img": null, "img_alt": null}]]'
answers='[["sa", "Strongly Agree"], ["a", "Agree"], ["n", "Neutral"],
["d", "Disagree"], ["sd", "Strongly Disagree"]]'
feedback="### Thank you&#10;&#10;for running the tests."/>
<survey tally='{"q1": {"sa": 5, "a": 5, "n": 3, "d": 2, "sd": 5}}'
questions='[["q1", {"label": "Country most likely to win the World Cup.", "img": null, "img_alt": null}]]'
answers='[["sa", "South Africa"], ["a", "Angola"], ["n", "Netherlands"],
["d", "Deutschland"], ["sd", "Someone different"]]'
feedback="### Thank you&#10;&#10;for running the tests."/>
</vertical_demo>

0 comments on commit c7ebcc4

Please sign in to comment.