# Tests WebGUI::Asset::Wobject::Survey::SurveyJSON # # use strict; use warnings; use FindBin; use lib "$FindBin::Bin/../../../lib"; use Test::More; use Test::Deep; use Test::Exception; use Data::Dumper; use WebGUI::Test; # Must use this before any other WebGUI modules use WebGUI::Session; use JSON; #---------------------------------------------------------------------------- # Init my $session = WebGUI::Test->session; #---------------------------------------------------------------------------- # Tests plan tests => 94; my $tp = use_ok('TAP::Parser'); my $tpa = use_ok('TAP::Parser::Aggregator'); #---------------------------------------------------------------------------- # put your tests here use_ok('WebGUI::Asset::Wobject::Survey::Test'); my $user = WebGUI::User->new( $session, 'new' ); WebGUI::Test->addToCleanup($user); my $import_node = WebGUI::Asset->getImportNode($session); WebGUI::Test->originalConfig('enableSurveyExpressionEngine'); $session->config->set('enableSurveyExpressionEngine', 1); # Create a Survey my $s = $import_node->addChild( { className => 'WebGUI::Asset::Wobject::Survey', } ); isa_ok( $s, 'WebGUI::Asset::Wobject::Survey' ); WebGUI::Test->addToCleanup($s); my $sJSON = $s->surveyJSON; # N.B. Survey starts off with a single empty section (S0) # Add some sections and questions $sJSON->newObject( [] ); # S1 $sJSON->newObject( [] ); # S2 $sJSON->newObject( [] ); # S3 $sJSON->newObject( [] ); # S4 $sJSON->newObject( [] ); # S5 $sJSON->newObject( [] ); # S6 # Name the sections for my $sIndex (0..6) { $sJSON->update( [$sIndex], { variable => "S$sIndex" } ); } # ..and now some questions $sJSON->newObject( [0] ); # S0Q0 $sJSON->newObject( [1] ); # S1Q0 $sJSON->newObject( [2] ); # S2Q0 $sJSON->newObject( [3] ); # S3Q0 $sJSON->newObject( [3] ); # S3Q1 $sJSON->newObject( [3] ); # S3Q2 $sJSON->newObject( [4] ); # S4Q0 $sJSON->newObject( [5] ); # S5Q0 $sJSON->newObject( [5] ); # S5Q1 $sJSON->newObject( [5] ); # S5Q2 # Name the questions $sJSON->update( [ 0, 0 ], { variable => 'S0Q0' } ); $sJSON->update( [ 1, 0 ], { variable => 'S1Q0' } ); $sJSON->update( [ 2, 0 ], { variable => 'S2Q0' } ); $sJSON->update( [ 3, 0 ], { variable => 'S3Q0' } ); $sJSON->update( [ 3, 1 ], { variable => 'S3Q1' } ); $sJSON->update( [ 3, 2 ], { variable => 'S3Q2' } ); $sJSON->update( [ 4, 0 ], { variable => 'S4Q0' } ); $sJSON->update( [ 5, 0 ], { variable => 'S5Q0' } ); $sJSON->update( [ 5, 1 ], { variable => 'S5Q1' } ); $sJSON->update( [ 5, 2 ], { variable => 'S5Q2' } ); # Set additional options.. $sJSON->update( [ 0, 0 ], { questionType => 'Yes/No' } ); # S0Q0 is a Yes/No $sJSON->update( [ 0, 0 ], { gotoExpression => q{ tag('tagged at S0Q0'); } } ); # S0Q0 tagged data $sJSON->update( [ 1, 0 ], { questionType => 'Yes/No' } ); # S1Q0 is a Yes/No $sJSON->update( [ 1, 0, 0 ], { goto => 'S3', recordedAnswer => q{} } ); # S1Q0 answer 0 jumps to S3 (set recordedAnswer to '' to detect subtle bug) $sJSON->update( [ 1, 0, 1 ], { gotoExpression => q{ tag('tagged at S1Q0', 999); }, recordedAnswer => q{} } );# S1Q0 answer 1 tagged numeric data $sJSON->update( [ 3 ], { gotoExpression => q{ jump { score(S3) == 0 } S5; } } ); # jump to S5 if all 3 questions answered as No for my $qIndex (0..2) { $sJSON->update( [ 3, $qIndex ], { questionType => 'Yes/No', required => 1 } ); $sJSON->update( [ 3, $qIndex, 1 ], { value => 0 } ); # Set 'No' score to 0 } $sJSON->update( [ 4, 0 ], { questionType => 'Concern' } ); $sJSON->update( [ 5, 0 ], { questionType => 'Slider', required => 1 } ); $sJSON->update( [ 5, 1 ], { questionType => 'Text', required => 1 } ); $sJSON->update( [ 5, 2 ], { questionType => 'Number', required => 1 } ); $sJSON->update( [ 6 ], { logical => 1, gotoExpression => q{tag('tagged at S6');} } ); # And finally, persist the changes.. $s->persistSurveyJSON; my $rJSON = $s->responseJSON; cmp_deeply( $rJSON->surveyOrder, [ [ 0, 0, [ 0, 1 ] ], # S0Q0 (surveyOrderIndex: 0) [ 1, 0, [ 0, 1 ] ], # S1Q0 (surveyOrderIndex: 1) [ 2, 0, [] ], # S2Q0 (surveyOrderIndex: 2) [ 3, 0, [ 0, 1 ] ], # S3Q0 (surveyOrderIndex: 3) [ 3, 1, [ 0, 1 ] ], # S3Q1 (surveyOrderIndex: 4) [ 3, 2, [ 0, 1 ] ], # S3Q2 (surveyOrderIndex: 5) [ 4, 0, [ 0 .. 10 ] ], # S4Q0 (surveyOrderIndex: 6) [ 5, 0, [0] ], # S5Q0 (surveyOrderIndex: 7) [ 5, 1, [0] ], # S5Q0 (surveyOrderIndex: 8) [ 5, 2, [0] ], # S5Q0 (surveyOrderIndex: 9) [6], # S6 (surveyOrderIndex: 10) ], 'surveyOrder is correct' ); cmp_deeply( $rJSON->surveyOrderIndex, { 'S0' => 0, 'S0Q0' => 0, 'S1' => 1, 'S1Q0' => 1, 'S2' => 2, 'S2Q0' => 2, 'S3' => 3, 'S3Q0' => 3, 'S3Q1' => 4, 'S3Q2' => 5, 'S4' => 6, 'S4Q0' => 6, 'S5' => 7, 'S5Q0' => 7, 'S5Q1' => 8, 'S5Q2' => 9, 'S6' => 10, }, 'surveyOrderIndex correct' ); my $t1 = WebGUI::Asset::Wobject::Survey::Test->create( $session, { assetId => $s->getId } ); WebGUI::Test->addToCleanup(sub {$t1->delete();}); my $spec; # No tests $spec = < < < < < 1 } ); 1..1 not ok 1 - Nothing to do END_TAP # Both answers for S0Q0 jump to the next item, which can be referred to as either S1 or S1Q0 $spec = < < < 1 } ); 1..1 not ok 1 - Checking next on page containing Section S0 Question S0Q0 # Compared next section/question # got : 'S1' (<-- a section) and 'S1Q0' (<-- a question) # expect : 'S2' END_TAP # also fails if we don't answer all required questions $spec = < < 1 } ); 1..1 not ok 1 - Checking next on page containing Section S3 Question S3Q0 # Compared next section/question # got : 'S3' (<-- a section) and 'S3Q0' (<-- a question) # expect : 'S4' END_TAP # now try it on a question that has branching, and doesn't start on the first page $spec = < < < < < < < < < < < < < 1 } ); 1..4 ok 1 - Checking tagged on page containing Section S1 Question S1Q0 not ok 2 - Checking tagged on page containing Section S1 Question S1Q0 # Tag not found: tagged at S0Q0 not ok 3 - Checking tagged on page containing Section S1 Question S1Q0 # Tag not found: tagged at S1Q0 not ok 4 - Checking tagged on page containing Section S1 Question S1Q0 # Tag not found: my data tag END_TAP # Slider, Number & Text question types # And also test the fact that S6 is logical $spec = < < < < < < < < < < 1 } ); 1..1 not ok 1 - S1Q0 answer number 1 property recordedAnswer not defined # got: '' END_TAP sub try_it { my ( $test, $spec, $opts ) = @_; chomp($spec); $test->update( { test => $spec } ); my $result = $t1->run(); ok( $result, 'Tests ran ok' ); if ( my $tap = $opts->{tap} ) { chomp($tap); is( $result->{tap}, $tap, 'TAP matches' ); } my $parser = TAP::Parser->new($result); while ( my $r = $parser->next ) { # we could test extra stuff here, but mainly we just need to make the parser # go all the way through so that we can access ->has_problems } ok( !$parser->has_problems == !$opts->{fail}, ( $opts->{fail} ? "Fails" : "Passes" ) . ' as expected' ); } ################### # get_differences # ################### is(WebGUI::Asset::Wobject::Survey::Test::get_differences('a', 'b'), < 1}, {a => 2}), <