Started refactoring the Survey.pm Wobject class

Refactored www_jumpTo and added tests
Added Params::Validate
Improved docs
Made call to SurveyJSON->createSurveyOrder() unnecessary
Turned ResponseJSON->nextResponse a mutator
This commit is contained in:
Patrick Donelan 2009-02-11 09:25:42 +00:00
parent ba6764065b
commit d882181fd1
5 changed files with 211 additions and 86 deletions

View file

@ -18,7 +18,7 @@ my $session = WebGUI::Test->session;
#----------------------------------------------------------------------------
# Tests
my $tests = 1;
my $tests = 10;
plan tests => $tests + 1;
#----------------------------------------------------------------------------
@ -37,6 +37,61 @@ $import_node = WebGUI::Asset->getImportNode($session);
$survey = $import_node->addChild( { className => 'WebGUI::Asset::Wobject::Survey', } );
isa_ok($survey, 'WebGUI::Asset::Wobject::Survey');
# Load bare-bones survey, containing a single section (S0)
$survey->loadSurveyJSON();
$survey->survey->update([0], { variable => 'S0' });
# Add 2 questions to S0
$survey->survey->newObject([0]); # S0Q0
$survey->survey->update([0,0], { variable => 'S0Q0' });
$survey->survey->newObject([0]); # S0Q1
$survey->survey->update([0,1], { variable => 'S0Q1' });
# Add a new section (S1)
$survey->survey->newObject([]); # S1
$survey->survey->update([1], { variable => 'S1' });
# Add 2 questions to S1
$survey->survey->newObject([1]); # S1Q0
$survey->survey->update([1,0], { variable => 'S1Q0' });
$survey->survey->newObject([1]); # S1Q1
$survey->survey->update([1,1], { variable => 'S1Q1' });
# Persist to db
$survey->saveSurveyJSON();
# Now start a response as admin user
$session->user( { userId =>3 } );
$survey->getResponseId( { noCookie => 1 }); # triggers loadBothJSON()
#for my $address (@{ $survey->response->surveyOrder }) {
# diag (Dumper $address);
#}
# www_jumpTo
{
# Check a simple www_jumpTo request
WebGUI::Test->getPage( $survey, 'www_jumpTo', { formParams => {id => '0'} } );
is( $session->http->getStatus, '201', 'Page request ok' ); # why is "201 - created" status used??
is($survey->response->nextResponse, 0, 'S0 is the first response');
tie my %expectedSurveyOrder, 'Tie::IxHash';
%expectedSurveyOrder = (
'undefined' => 0,
'0' => 0,
'0-0' => 0,
'0-1' => 1,
'1' => 2,
'1-0' => 2,
'1-1' => 3,
);
while (my ($id, $index) = each %expectedSurveyOrder) {
WebGUI::Test->getPage( $survey, 'www_jumpTo', { formParams => {id => $id} } );
$survey->loadSurveyJSON();
is($survey->response->nextResponse, $index, "jumpTo($id) sets nextResponse to $index");
}
}
}
#----------------------------------------------------------------------------

View file

@ -21,7 +21,7 @@ my $session = WebGUI::Test->session;
#----------------------------------------------------------------------------
# Tests
my $tests = 79;
my $tests = 78;
plan tests => $tests + 1;
#----------------------------------------------------------------------------
@ -48,7 +48,6 @@ is($responseJSON->lastResponse(), -1, 'new: default lastResponse is -1');
is($responseJSON->questionsAnswered, 0, 'new: questionsAnswered is 0 by default');
cmp_ok((abs$responseJSON->startTime - $newTime), '<=', 2, 'new: by default startTime set to time');
is_deeply( $responseJSON->responses, {}, 'new: by default, responses is an empty hashref');
is_deeply( $responseJSON->surveyOrder, [], 'new: by default, responses is an empty arrayref');
my $now = time();
my $rJSON = WebGUI::Asset::Wobject::Survey::ResponseJSON->new(buildSurveyJSON($session), qq!{ "startTime": $now }!);
@ -82,13 +81,13 @@ ok( ! $rJSON->hasTimedOut(4*60), 'hasTimedOut, limit check');
####################################################
#
# createSurveyOrder
# initSurveyOrder
#
####################################################
$rJSON = WebGUI::Asset::Wobject::Survey::ResponseJSON->new(buildSurveyJSON($session), q!{}!);
$rJSON->createSurveyOrder();
#$rJSON->initSurveyOrder();
cmp_deeply(
$rJSON->surveyOrder,
[
@ -102,7 +101,7 @@ cmp_deeply(
[ 3, 1, [0, 1, 2, 3, 4, 5, 6] ],
[ 3, 2, [0] ],
],
'createSurveyOrder, enumerated all sections, questions and answers'
'initSurveyOrder, enumerated all sections, questions and answers'
);
####################################################
@ -119,7 +118,7 @@ cmp_deeply(
####################################################
#
# createSurveyOrder, part 2
# initSurveyOrder, part 2
#
####################################################
@ -127,27 +126,27 @@ cmp_deeply(
my $rJSON = WebGUI::Asset::Wobject::Survey::ResponseJSON->new(buildSurveyJSON($session), q!{}!);
$rJSON->survey->section([0])->{randomizeQuestions} = 0;
$rJSON->createSurveyOrder();
$rJSON->initSurveyOrder();
my @question_order = map {$_->[1]} grep {$_->[0] == 0} @{$rJSON->surveyOrder};
cmp_deeply(\@question_order, [0,1,2], 'createSurveyOrder did not shuffle questions');
cmp_deeply(\@question_order, [0,1,2], 'initSurveyOrder did not shuffle questions');
$rJSON->survey->section([0])->{randomizeQuestions} = 1;
srand(42); # Make shuffle predictable
$rJSON->createSurveyOrder();
$rJSON->initSurveyOrder();
@question_order = map {$_->[1]} grep {$_->[0] == 0} @{$rJSON->surveyOrder};
cmp_deeply(\@question_order, [2,0,1], 'createSurveyOrder shuffled questions in first section');
cmp_deeply(\@question_order, [2,0,1], 'initSurveyOrder shuffled questions in first section');
$rJSON->survey->section([0])->{randomizeQuestions} = 0;
$rJSON->survey->question([0,0])->{randomizeAnswers} = 0;
$rJSON->createSurveyOrder();
$rJSON->initSurveyOrder();
my @answer_order = map {@{$_->[2]}} grep {$_->[0] == 3 && $_->[1] == 1} @{$rJSON->surveyOrder};
cmp_deeply(\@answer_order, [0,1,2,3,4,5,6], 'createSurveyOrder did not shuffle answers');
cmp_deeply(\@answer_order, [0,1,2,3,4,5,6], 'initSurveyOrder did not shuffle answers');
$rJSON->survey->question([3,1])->{randomizeAnswers} = 1;
srand(42); # Make shuffle predictable
$rJSON->createSurveyOrder();
$rJSON->initSurveyOrder();
@answer_order = map {@{$_->[2]}} grep {$_->[0] == 3 && $_->[1] == 1} @{$rJSON->surveyOrder};
cmp_deeply(\@answer_order, [1,3,4,5,6,0,2], 'createSurveyOrder shuffled answers');
cmp_deeply(\@answer_order, [1,3,4,5,6,0,2], 'initSurveyOrder shuffled answers');
}
####################################################