Refactored Survey for more happy endings

Refactored www_submitQuestions to use utility submitQuestions sub (for ease of testing)
Replaced numeric flags, completedCode etc..
Added restart() and exitUrl() to the Expression Engine
Added more Survey instance tests
This commit is contained in:
Patrick Donelan 2009-05-12 12:36:26 +00:00
parent 4332b57ba2
commit 79db642219
6 changed files with 255 additions and 118 deletions

View file

@ -18,7 +18,7 @@ my $session = WebGUI::Test->session;
#----------------------------------------------------------------------------
# Tests
my $tests = 20;
my $tests = 24;
plan tests => $tests + 1;
#----------------------------------------------------------------------------
@ -90,6 +90,46 @@ delete $s->{responseId};
ok($s->canTakeSurvey, '..and also when maxResponsesPerUser set to 0 (unlimited)');
ok($s->responseId, '..(and similarly for responseId)');
# Restart the survey
$s->submitQuestions({
'0-0-0' => 'My chosen answer',
'0-1-0' => 'My chosen answer',
});
cmp_deeply(
$s->responseJSON->responses,
superhashof(
{ '0-1-0' => {
'verbatim' => undef,
'comment' => undef,
'time' => num( time, 5 ),
'value' => ''
},
'0-0-0' => {
'verbatim' => undef,
'comment' => undef,
'time' => num( time, 5 ),
'value' => ''
},
}
),
'submitQuestions does the right thing'
);
# Test out Restart
$s->surveyEnd( { restart => 1 } );
cmp_deeply($s->responseJSON->responses, {}, 'restart removes the in-progress response');
# Test out exitUrl with an explicit
use JSON;
my $surveyEnd = $s->surveyEnd( { exitUrl => 'home' } );
cmp_deeply(from_json($surveyEnd), { type => 'forward', url => '/home' }, 'exitUrl works (it adds a slash for us)');
# Test out exitUrl using survye instance exitURL property
$s->update({ exitURL => 'getting_started'});
$surveyEnd = $s->surveyEnd( { exitUrl => undef } );
cmp_deeply(from_json($surveyEnd), { type => 'forward', url => '/getting_started' }, 'exitUrl works (it adds a slash for us)');
# www_jumpTo
{
# Check a simple www_jumpTo request

View file

@ -22,7 +22,7 @@ my $session = WebGUI::Test->session;
#----------------------------------------------------------------------------
# Tests
my $tests = 52;
my $tests = 55;
plan tests => $tests + 1;
#----------------------------------------------------------------------------
@ -163,6 +163,25 @@ SKIP: {
{ jump => 'target', tags => { a => 'xyz' } },
'..overwritten tag data can be used too'
);
# Try the exitUrl sub
cmp_deeply(
$e->run( $session, q{ exitUrl(blah)} ),
{ exitUrl => 'blah', tags => { } },
'explicit exitUrl works'
);
cmp_deeply(
$e->run( $session, q{ exitUrl()} ),
{ exitUrl => undef, tags => { } },
'..as does unspecified exitUrl'
);
# Try the restart sub
cmp_deeply(
$e->run( $session, q{ restart} ),
{ restart => 1, tags => { } },
'restart works'
);
# Create a test user
$user = WebGUI::User->new( $session, 'new' );

View file

@ -22,7 +22,7 @@ my $session = WebGUI::Test->session;
#----------------------------------------------------------------------------
# Tests
my $tests = 93;
my $tests = 96;
plan tests => $tests + 1;
#----------------------------------------------------------------------------
@ -484,6 +484,11 @@ $rJSON->nextResponse(2); # pretend we just finished s0q2
$rJSON->processExpression(q{jump { (value(s0q2_verbatim))[0] eq 'YesYesYes' && (value(s0q2_verbatim))[1] eq 'NoNoNo' } s2});
is($rJSON->nextResponse, 5, '..and we can get list of verbatims too');
$rJSON->nextResponse(2); # pretend we just finished s0q2
cmp_deeply($rJSON->processExpression(q{restart()}), { restart => 1 }, 'restart works');
cmp_deeply($rJSON->processExpression(q{exitUrl(blah)}), { exitUrl => 'blah' }, 'explicit exitUrl works');
cmp_deeply($rJSON->processExpression(q{exitUrl()}), { exitUrl => undef }, 'unspecified exitUrl works too');
# Clean up after this set of tests
$rJSON->responses({});
$rJSON->questionsAnswered(-1 * $rJSON->questionsAnswered);
@ -499,8 +504,8 @@ $rJSON->lastResponse(4);
my $terminals;
cmp_deeply(
$rJSON->recordResponses({}),
[ 0, undef ],
'recordResponses, if section has no questions, returns terminal info in the section. With no terminal info, returns [0, undef]',
{},
'recordResponses, if section has no questions, returns terminal info in the section. With no terminal info, returns {}',
);
is($rJSON->lastResponse(), 5, 'recordResponses, increments lastResponse if there are no questions in the section');
@ -510,7 +515,7 @@ $rJSON->survey->section([2])->{terminalUrl} = '/terminal';
$rJSON->lastResponse(4);
cmp_deeply(
$rJSON->recordResponses({}),
[ 1, '/terminal' ],
{ terminal => '/terminal' },
'recordResponses, if section has no questions, returns terminal info in the section.',
);
is($rJSON->questionsAnswered, 0, 'questionsAnswered=0, no questions answered');
@ -527,7 +532,7 @@ cmp_deeply(
'1-0-0verbatim' => 'First answer verbatim', # ignored
'1-0-0comment' => 'Section 1, question 0, answer 0 comment',
}),
[ 1, 'question 1-0 terminal' ],
{ terminal => 'question 1-0 terminal' },
'recordResponses: question terminal overrides section terminal',
);
@ -627,7 +632,7 @@ cmp_deeply(
'1-0-0' => "\t\t\t\n\n\n\t\t\t", #SOS in whitespace
'1-0-0comment' => 'Section 1, question 0, answer 0 comment',
}),
[ 1, 'answer 1-0-0 terminal' ],
{ terminal => 'answer 1-0-0 terminal'},
'recordResponses: answer terminal overrides question and section terminals',
);