From 24fe2e6dc002d7fecdf70ce6c8868b79c1c77caa Mon Sep 17 00:00:00 2001 From: Colin Kuskie Date: Thu, 4 Dec 2008 20:47:42 +0000 Subject: [PATCH] Add tests for getQuestionEditVars. Refactor out the list of valid question types to make testing easier. --- lib/WebGUI/Asset/Wobject/Survey/SurveyJSON.pm | 37 +++-- t/Asset/Wobject/Survey/SurveyJSON.t | 136 ++++++++++-------- 2 files changed, 102 insertions(+), 71 deletions(-) diff --git a/lib/WebGUI/Asset/Wobject/Survey/SurveyJSON.pm b/lib/WebGUI/Asset/Wobject/Survey/SurveyJSON.pm index 06a741c0a..b13e3ad52 100644 --- a/lib/WebGUI/Asset/Wobject/Survey/SurveyJSON.pm +++ b/lib/WebGUI/Asset/Wobject/Survey/SurveyJSON.pm @@ -288,8 +288,8 @@ sub getSectionEditVars { =head2 getQuestionEditVars ( $address ) Get a safe copy of the variables for this question, to use for editing purposes. Adds -two variables, id, which is the indeces of the answer's position in its parent's question -and section arrays joined by dashes '-', and displayed_id, which is this answer's index +two variables, id, which is the indeces of the question's position in its parent's +section array joined by dashes '-', and displayed_id, which is this question's index in a 1-based array (versus the default, perl style, 0-based array). It removes the answers array ref, and changes questionType from a single element, into @@ -311,17 +311,7 @@ sub getQuestionEditVars { $var{displayed_id} = $address->[1] + 1; delete $var{answers}; delete $var{questionType}; - my @types = ( - 'Agree/Disagree', 'Certainty', 'Concern', 'Confidence', - 'Currency', 'Date', 'Date Range', 'Dual Slider - Range', - 'Education', 'Effectiveness', 'Email', 'File Upload', - 'Gender', 'Hidden', 'Ideology', 'Importance', - 'Likelihood', 'Multi Slider - Allocate', 'Multiple Choice', 'Oppose/Support', - 'Party', 'Phone Number', 'Race', 'Risk', - 'Satisfaction', 'Scale', 'Security', 'Slider', - 'Text', 'Text Date', 'Threat', 'True/False', - 'Yes/No' - ); + my @types = $self->getValidQuestionTypes(); for (@types) { if ( $_ eq $object->{questionType} ) { @@ -334,6 +324,27 @@ sub getQuestionEditVars { return \%var; } ## end sub getQuestionEditVars +=head2 getValidQuestionTypes + +A convenience method. Returns a list of question types. If you add a question +type to the Survey, you must handle it here, and also in updateQuestionAnswers + +=cut + +sub getValidQuestionTypes { + return( + 'Agree/Disagree', 'Certainty', 'Concern', 'Confidence', + 'Currency', 'Date', 'Date Range', 'Dual Slider - Range', + 'Education', 'Effectiveness', 'Email', 'File Upload', + 'Gender', 'Hidden', 'Ideology', 'Importance', + 'Likelihood', 'Multi Slider - Allocate', 'Multiple Choice', 'Oppose/Support', + 'Party', 'Phone Number', 'Race', 'Risk', + 'Satisfaction', 'Scale', 'Security', 'Slider', + 'Text', 'Text Date', 'Threat', 'True/False', + 'Yes/No' + ); +} + =head2 getAnswerEditVars ( $address ) Get a safe copy of the variables for this answer, to use for editing purposes. Adds diff --git a/t/Asset/Wobject/Survey/SurveyJSON.t b/t/Asset/Wobject/Survey/SurveyJSON.t index c9a5714bd..4bedbea43 100644 --- a/t/Asset/Wobject/Survey/SurveyJSON.t +++ b/t/Asset/Wobject/Survey/SurveyJSON.t @@ -21,7 +21,7 @@ my $session = WebGUI::Test->session; #---------------------------------------------------------------------------- # Tests -my $tests = 49; +my $tests = 52; plan tests => $tests + 1 + 3; #---------------------------------------------------------------------------- @@ -802,34 +802,20 @@ cmp_deeply( { title => 'Section 0', questions => [ - { - text => 'Question 0-0', - answers => [], - }, + { text => 'Question 0-0', answers => [], }, { text => 'Question 0-1', answers => [ - { - text => 'Answer 0-1-0', - }, - { - text => 'Answer 0-1-1', - }, + { text => 'Answer 0-1-0', }, + { text => 'Answer 0-1-1', }, ], }, - { - text => 'Question 0-2', - answers => [], - }, + { text => 'Question 0-2', answers => [], }, { text => 'Question 0-1', answers => [ - { - text => 'Answer 0-3-0', - }, - { - text => 'Answer 0-1-1', - }, + { text => 'Answer 0-3-0', }, + { text => 'Answer 0-1-1', }, ], }, ], @@ -837,23 +823,14 @@ cmp_deeply( { title => 'Section 1', questions => [ - { - text => 'Question 1-0', - answers => [], - }, + { text => 'Question 1-0', answers => [], }, ], }, - { - title => 'Section 2', - questions => [], - }, + { title => 'Section 2', questions => [], }, { title => 'Section 3', questions => [ - { - text => 'Question 3-0', - answers => [], - }, + { text => 'Question 3-0', answers => [], }, ], }, ], @@ -871,33 +848,22 @@ cmp_deeply( title => 'Section 0', questions => [ { - text => 'Question 0-0', - answers => [], + text => 'Question 0-0', answers => [], }, { text => 'Question 0-1', answers => [ - { - text => 'Answer 0-1-0', - }, - { - text => 'Answer 0-1-1', - }, + { text => 'Answer 0-1-0', }, + { text => 'Answer 0-1-1', }, ], }, - { - text => 'Question 0-2', - answers => [], + { text => 'Question 0-2', answers => [], }, { text => 'Question 0-3', answers => [ - { - text => 'Answer 0-3-0', - }, - { - text => 'Answer 0-3-1', - }, + { text => 'Answer 0-3-0', }, + { text => 'Answer 0-3-1', }, ], }, ], @@ -905,22 +871,17 @@ cmp_deeply( { title => 'Section 1', questions => [ - { - text => 'Question 1-0', - answers => [], - }, + { text => 'Question 1-0', answers => [], }, ], }, { - title => 'Section 2', - questions => [], + title => 'Section 2', questions => [], }, { title => 'Section 3', questions => [ { - text => 'Question 3-0', - answers => [], + text => 'Question 3-0', answers => [], }, ], }, @@ -1058,8 +1019,67 @@ cmp_deeply( 'getAnswerEditVars: uses a safe copy to build the vars hash' ); +#################################################### +# +# getQuestionEditVars +# +#################################################### + +my @questionTypeVars = map { + { + text => $_, selected => ($_ eq 'Multiple Choice' ? 1 : 0), + } +} $surveyJSON->getValidQuestionTypes(); + +cmp_deeply( + $surveyJSON->getQuestionEditVars([3,0]), + superhashof({ + id => '3-0', + displayed_id => '1', + text => 'Question 3-0', + type => 'question', + questionType => \@questionTypeVars, + }), + 'getQuestionEditVars: retrieved correct question' +); + +my $questionEditVars = $surveyJSON->getQuestionEditVars([3,0]); +$questionEditVars->{commentCols} = 1000; +my (undef, $bareQuestion2, undef) = getBareSkeletons(); +$bareQuestion2->{text} = ignore(); +cmp_deeply( + $surveyJSON->question([3,0]), + $bareQuestion2, + 'getQuestionEditVars: uses a safe copy to build the vars hash' +); + +$surveyJSON->question([3,0])->{questionType} = 'Scale'; + +@questionTypeVars = map { + { + text => $_, selected => ($_ eq 'Scale' ? 1 : 0), + } +} $surveyJSON->getValidQuestionTypes(); + +cmp_deeply( + $surveyJSON->getQuestionEditVars([3,0]), + superhashof({ + questionType => \@questionTypeVars, + }), + 'getQuestionEditVars: does correct detection of questionType' +); + +$surveyJSON->question([3,0])->{questionType} = 'Multiple Choice'; + + } +#################################################### +# +# Utility test routines +# +#################################################### + # Go through a JSON survey type data structure and just grab some unique # elements