From 0453f47bc1fa3bfd234dbb3d8ac02c9be46c97cf Mon Sep 17 00:00:00 2001 From: Colin Kuskie Date: Thu, 4 Dec 2008 20:18:49 +0000 Subject: [PATCH] Tests for getDragDrop and getAnswerEditVars. More POD. --- lib/WebGUI/Asset/Wobject/Survey/SurveyJSON.pm | 69 ++++++ t/Asset/Wobject/Survey/SurveyJSON.t | 199 +++++++++++++++++- 2 files changed, 262 insertions(+), 6 deletions(-) diff --git a/lib/WebGUI/Asset/Wobject/Survey/SurveyJSON.pm b/lib/WebGUI/Asset/Wobject/Survey/SurveyJSON.pm index 3bcd2949d..06a741c0a 100644 --- a/lib/WebGUI/Asset/Wobject/Survey/SurveyJSON.pm +++ b/lib/WebGUI/Asset/Wobject/Survey/SurveyJSON.pm @@ -132,6 +132,45 @@ sub newObject { #address is the array of objects currently selected in the edit screen #data is the array of hash items for displaying + +=head2 getDragDropList ( $address ) + +Get a subset of the entire data structure. It will be a list of all sections, along with +one question from a section with all its answers. + +Returns an array reference. Each element of the array will have a subset of section information as +a hashref. This will contain two keys: + + { + type => 'section', + text => the section's title + }, + +The questions for the referenced section will be included, like this: + + { + type => 'question', + text => the question's text + }, + +All answers for the referenced question will also be in the array reference: + + { + type => 'answer', + text => the answer's text + }, + +The sections, question and answer will be in depth-first order: + +section, section, section, question, answer, answer, answer, section, section + +=head3 $address + +An array ref. Sets which question from a section will be listed, along with all +its answers. $address should ALWAYS have two elements. + +=cut + sub getDragDropList { my $self = shift; my $address = shift; @@ -246,6 +285,23 @@ sub getSectionEditVars { return \%var; } ## end 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 +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 +an array of hashrefs, which list the available question types and which one is currently +selected for this question. + +=head3 $address + +An array reference, specifying which answer to fetch variables for. + +=cut + sub getQuestionEditVars { my $self = shift; my $address = shift; @@ -278,6 +334,19 @@ sub getQuestionEditVars { return \%var; } ## end sub getQuestionEditVars +=head2 getAnswerEditVars ( $address ) + +Get a safe copy of the variables for this answer, 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 +in a 1-based array (versus the default, perl style, 0-based array). + +=head3 $address + +An array reference, specifying which answer to fetch variables for. + +=cut + sub getAnswerEditVars { my $self = shift; my $address = shift; diff --git a/t/Asset/Wobject/Survey/SurveyJSON.t b/t/Asset/Wobject/Survey/SurveyJSON.t index 581e79ecd..c9a5714bd 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 = 43; +my $tests = 49; plan tests => $tests + 1 + 3; #---------------------------------------------------------------------------- @@ -860,16 +860,203 @@ cmp_deeply( 'copy: safe copy of a section' ); +##Finish renaming copied sections for sane downstream testing + +$surveyJSON->question([0, 3])->{text} = 'Question 0-3'; +$surveyJSON->answer([0, 3, 1])->{text} = 'Answer 0-3-1'; +cmp_deeply( + summarizeSectionSkeleton($surveyJSON), + [ + { + title => 'Section 0', + questions => [ + { + text => 'Question 0-0', + answers => [], + }, + { + text => 'Question 0-1', + answers => [ + { + text => 'Answer 0-1-0', + }, + { + text => 'Answer 0-1-1', + }, + ], + }, + { + text => 'Question 0-2', + answers => [], + }, + { + text => 'Question 0-3', + answers => [ + { + text => 'Answer 0-3-0', + }, + { + text => 'Answer 0-3-1', + }, + ], + }, + ], + }, + { + title => 'Section 1', + questions => [ + { + text => 'Question 1-0', + answers => [], + }, + ], + }, + { + title => 'Section 2', + questions => [], + }, + { + title => 'Section 3', + questions => [ + { + text => 'Question 3-0', + answers => [], + }, + ], + }, + ], + 'sanity check' +); + #################################################### # -# TODO +# getDragDropList # #################################################### -# To try to bust the data object -# Create a section, put questions in it. -# Copy the section, then alter one question in it. It should -# alter the original since it is a reference. +cmp_deeply( + $surveyJSON->getDragDropList([0, 1]), + [ + { + type => 'section', + text => 'Section 0', + }, + { + type => 'question', + text => 'Question 0-0', + }, + { + type => 'question', + text => 'Question 0-1', + }, + { + type => 'answer', + text => 'Answer 0-1-0', + }, + { + type => 'answer', + text => 'Answer 0-1-1', + }, + { + type => 'question', + text => 'Question 0-2', + }, + { + type => 'question', + text => 'Question 0-3', + }, + { + type => 'section', + text => 'Section 1', + }, + { + type => 'section', + text => 'Section 2', + }, + { + type => 'section', + text => 'Section 3', + }, + ], + 'getDragDropList: list of sections, questions and answers is correct' +); + +cmp_deeply( + $surveyJSON->getDragDropList([1, 0]), + [ + { + type => 'section', + text => 'Section 0', + }, + { + type => 'section', + text => 'Section 1', + }, + { + type => 'question', + text => 'Question 1-0', + }, + { + type => 'section', + text => 'Section 2', + }, + { + type => 'section', + text => 'Section 3', + }, + ], + 'getDragDropList: list of sections, and question with no answer' +); + +cmp_deeply( + $surveyJSON->getDragDropList([2, 0]), + [ + { + type => 'section', + text => 'Section 0', + }, + { + type => 'section', + text => 'Section 1', + }, + { + type => 'section', + text => 'Section 2', + }, + { + type => 'section', + text => 'Section 3', + }, + ], + 'getDragDropList: list of sections, no questions' +); + +#################################################### +# +# getAnswerEditVars +# +#################################################### + +cmp_deeply( + $surveyJSON->getAnswerEditVars([0,1,0]), + superhashof({ + id => '0-1-0', + displayed_id => '1', + text => 'Answer 0-1-0', + type => 'answer', + }), + 'getAnswerEditVars: retrieved correct answer' +); + +my $answerEditVars = $surveyJSON->getAnswerEditVars([0,1,0]); +$answerEditVars->{textRows} = 1000; +my (undef, undef, $bareAnswer2) = getBareSkeletons(); +$bareAnswer2->{text} = ignore(); +cmp_deeply( + $surveyJSON->answer([0,1,0]), + $bareAnswer2, + 'getAnswerEditVars: uses a safe copy to build the vars hash' +); }