Fixed inconsistencies between answer 'comment' and 'verbatim'. Updated tests. Made minor changes to reporting methods but they still need some love.
200 lines
6.2 KiB
Perl
200 lines
6.2 KiB
Perl
# Tests WebGUI::Asset::Wobject::Survey
|
|
#
|
|
#
|
|
|
|
use strict;
|
|
use warnings;
|
|
use FindBin;
|
|
use lib "$FindBin::Bin/../../lib";
|
|
use Test::More;
|
|
use Test::Deep;
|
|
use Data::Dumper;
|
|
use WebGUI::Test; # Must use this before any other WebGUI modules
|
|
use WebGUI::Session;
|
|
|
|
#----------------------------------------------------------------------------
|
|
# Init
|
|
my $session = WebGUI::Test->session;
|
|
|
|
#----------------------------------------------------------------------------
|
|
# Tests
|
|
my $tests = 29;
|
|
plan tests => $tests + 1;
|
|
|
|
#----------------------------------------------------------------------------
|
|
# put your tests here
|
|
|
|
my $usedOk = use_ok('WebGUI::Asset::Wobject::Survey');
|
|
my ($survey);
|
|
|
|
SKIP: {
|
|
|
|
skip $tests, "Unable to load Survey" unless $usedOk;
|
|
my $user = WebGUI::User->new( $session, 'new' );
|
|
WebGUI::Test->usersToDelete($user);
|
|
my $import_node = WebGUI::Asset->getImportNode($session);
|
|
|
|
# Create a Survey
|
|
$survey = $import_node->addChild( { className => 'WebGUI::Asset::Wobject::Survey', } );
|
|
isa_ok($survey, 'WebGUI::Asset::Wobject::Survey');
|
|
|
|
my $sJSON = $survey->surveyJSON;
|
|
|
|
# Load bare-bones survey, containing a single section (S0)
|
|
$sJSON->update([0], { variable => 'S0' });
|
|
|
|
# Add 2 questions to S0
|
|
$sJSON->newObject([0]); # S0Q0
|
|
$sJSON->update([0,0], { variable => 'S0Q0', questionType => 'Yes/No' });
|
|
$sJSON->newObject([0]); # S0Q1
|
|
$sJSON->update([0,1], { variable => 'S0Q1', questionType => 'Yes/No' });
|
|
|
|
# Add a new section (S1)
|
|
$sJSON->newObject([]); # S1
|
|
$sJSON->update([1], { variable => 'S1' });
|
|
|
|
# Add 2 questions to S1
|
|
$sJSON->newObject([1]); # S1Q0
|
|
$sJSON->update([1,0], { variable => 'S1Q0' });
|
|
$sJSON->newObject([1]); # S1Q1
|
|
$sJSON->update([1,1], { variable => 'S1Q1' });
|
|
|
|
$survey->persistSurveyJSON;
|
|
|
|
# Now start a response as the test user
|
|
$session->user( { user => $user } );
|
|
|
|
my $responseId = $survey->responseId;
|
|
my $s = WebGUI::Asset::Wobject::Survey->newByResponseId($session, $responseId);
|
|
is($s->getId, $survey->getId, 'newByResponseId returns same Survey');
|
|
is($s->get('maxResponsesPerUser'), 1, 'maxResponsesPerUser defaults to 1');
|
|
ok($s->canTakeSurvey, '..which means user can take survey');
|
|
|
|
# Complete Survey
|
|
$s->surveyEnd();
|
|
|
|
# Uncache canTake
|
|
delete $s->{canTake};
|
|
delete $s->{responseId};
|
|
ok(!$s->canTakeSurvey, 'Cannot take survey a second time (maxResponsesPerUser=1)');
|
|
cmp_deeply($s->responseId, undef, '..and similarly cannot get responseId');
|
|
|
|
# Change maxResponsesPerUser to 2
|
|
$s->update({maxResponsesPerUser => 2});
|
|
delete $s->{canTake};
|
|
ok($s->canTakeSurvey, '..but can take when maxResponsesPerUser increased to 2');
|
|
ok($s->responseId, '..and similarly can get responseId');
|
|
|
|
# Change maxResponsesPerUser to 0
|
|
$s->update({maxResponsesPerUser => 0});
|
|
delete $s->{canTake};
|
|
delete $s->{responseId};
|
|
ok($s->canTakeSurvey, '..and also when maxResponsesPerUser set to 0 (unlimited)');
|
|
ok($s->responseId, '..(and similarly for responseId)');
|
|
|
|
# Start a new response as another user
|
|
$s->update({maxResponsesPerUser => 1});
|
|
is($s->takenCount( { userId => 1 } ), 0, 'Visitor has no responses');
|
|
my $u = WebGUI::User->new( $session, 'new' );
|
|
WebGUI::Test->usersToDelete($u);
|
|
is($s->takenCount( { userId => $u->userId } ), 0, 'New user has no responses');
|
|
delete $s->{canTake};
|
|
delete $s->{responseId};
|
|
$session->user( { userId => $u->userId } );
|
|
ok($s->canTakeSurvey, 'Separate counts for separate users');
|
|
ok($s->responseId, '..(and similarly for responseId)');
|
|
# Put things back to normal..
|
|
delete $s->{canTake};
|
|
delete $s->{responseId};
|
|
$session->user( { user => $user } );
|
|
|
|
# Restart the survey
|
|
$s->update({maxResponsesPerUser => 0});
|
|
$s->submitQuestions({
|
|
'0-0-0' => 'this text ignored',
|
|
'0-1-0' => 'this text ignored',
|
|
});
|
|
|
|
cmp_deeply(
|
|
$s->responseJSON->responses,
|
|
superhashof(
|
|
{ '0-1-0' => {
|
|
'time' => num( time, 5 ),
|
|
'value' => 1
|
|
},
|
|
'0-0-0' => {
|
|
'time' => num( time, 5 ),
|
|
'value' => 1
|
|
},
|
|
}
|
|
),
|
|
'submitQuestions does the right thing'
|
|
);
|
|
|
|
# Test Restart
|
|
$s->surveyEnd( { restart => 1 } );
|
|
cmp_deeply($s->responseJSON->responses, {}, 'restart removes the in-progress response');
|
|
ok($responseId ne $s->responseId, '..and uses a new responseId');
|
|
|
|
# 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 survey 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
|
|
$session->user( { userId => 3 } );
|
|
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->responseJSON->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} } );
|
|
is($survey->responseJSON->nextResponse, $index, "jumpTo($id) sets nextResponse to $index");
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
|
|
eval 'use GraphViz';
|
|
|
|
SKIP: {
|
|
|
|
skip "Unable to load GraphViz", 1 if $@;
|
|
|
|
$survey->surveyJSON->remove([1]);
|
|
my ($storage, $filename) = $survey->graph( { format => 'plain', layout => 'dot' } );
|
|
like($storage->getFileContentsAsScalar($filename), qr{
|
|
^graph .* # starts with graph
|
|
(node .*){3} # ..then 3 nodes
|
|
(edge .*){3} # ..then 3 edges
|
|
stop$ # ..and end with stop
|
|
}xs, 'Generated graph looks roughly okay');
|
|
|
|
}
|
|
|
|
#----------------------------------------------------------------------------
|
|
# Cleanup
|
|
END {
|
|
$survey->purge() if $survey;
|
|
|
|
my $versionTag = WebGUI::VersionTag->getWorking( $session, 1 );
|
|
$versionTag->rollback() if $versionTag;
|
|
}
|