Made first argument to SurveyJSON contructor $session for consistency

with rest of wg.

Also added Params::Validate validation.
This commit is contained in:
Patrick Donelan 2009-02-03 08:31:07 +00:00
parent 76d677ab4f
commit d00c8661f3
3 changed files with 31 additions and 45 deletions

View file

@ -48,6 +48,8 @@ likely operate on the question indexed by:
use strict;
use JSON;
use Params::Validate qw(:all);
Params::Validate::validation_options( on_fail => sub { WebGUI::Error::InvalidParam->throw( error => shift ) } );
# N.B. We're currently using Storable::dclone instead of Clone::clone
# because Colin uncovered some Clone bugs in Perl 5.10
@ -57,30 +59,28 @@ use Storable qw/dclone/;
# The maximum value of questionsPerPage is currently hardcoded here
my $MAX_QUESTIONS_PER_PAGE = 20;
=head2 new ( $json, $log )
=head2 new ( $session, json )
Object constructor.
=head3 $json
=head3 $session
A JSON string used to construct a new Perl object. The JSON string should
contain a hash made up of "survey" and "sections" keys.
WebGUI::Session object
=head3 $log
=head3 $json (optional)
The session logger, from $session->log. The class needs nothing else from the
session object.
A JSON string used to construct a new Perl object. The string should represent
a JSON hash made up of "survey" and "sections" keys.
=cut
sub new {
my $class = shift;
my $json = shift;
my $log = shift;
my ($session, $json) = validate_pos(@_, {isa => 'WebGUI::Session' }, { type => SCALAR, optional => 1});
# Create skeleton object..
my $self = {
log => $log,
session => $session,
sections => [],
survey => {},
};
@ -1146,6 +1146,17 @@ sub section {
return $self->{sections}->[ $address->[0] ];
}
=head2 session
Accessor method for the local WebGUI::Session reference
=cut
sub session {
my $self = shift;
return $self->{session};
}
=head2 questions ($address)
Returns a reference to all the questions from a particular section.
@ -1243,21 +1254,4 @@ sub aIndex {
return $address->[2];
}
=head2 log ($message)
Logs an error message using the session logger.
=head3 $message
The message to log. It will be logged as type "error".
=cut
sub log {
my ( $self, $message ) = @_;
if ( defined $self->{log} ) {
$self->{log}->error($message);
}
}
1;

View file

@ -493,7 +493,7 @@ is($rJSON->questionsAnswered, 0, 'question was all whitespace, not answered');
sub buildSurveyJSON {
my $session = shift;
my $sjson = WebGUI::Asset::Wobject::Survey::SurveyJSON->new(undef, $session->log);
my $sjson = WebGUI::Asset::Wobject::Survey::SurveyJSON->new($session);
##Build 4 sections. Remembering that one is created by default when you make an empty SurveyJSON object
$sjson->newObject([]);
$sjson->newObject([]);

View file

@ -22,7 +22,7 @@ my $session = WebGUI::Test->session;
#----------------------------------------------------------------------------
# Tests
my $tests = 134;
my $tests = 132;
plan tests => $tests + 1 + 3;
#----------------------------------------------------------------------------
@ -126,10 +126,10 @@ skip $tests, "Unable to load SurveyJSON" unless $usedOk;
#
####################################################
$surveyJSON = WebGUI::Asset::Wobject::Survey::SurveyJSON->new('{}', $session->log);
$surveyJSON = WebGUI::Asset::Wobject::Survey::SurveyJSON->new($session, '{}');
isa_ok($surveyJSON, 'WebGUI::Asset::Wobject::Survey::SurveyJSON');
my $sJSON2 = WebGUI::Asset::Wobject::Survey::SurveyJSON->new(undef, $session->log);
my $sJSON2 = WebGUI::Asset::Wobject::Survey::SurveyJSON->new($session);
isa_ok($sJSON2, 'WebGUI::Asset::Wobject::Survey::SurveyJSON', 'even with absolutely no JSON');
undef $sJSON2;
@ -173,9 +173,8 @@ cmp_deeply(
'new: empty JSON in constructor causes 1 new, default section to be created',
);
$surveyJSON = WebGUI::Asset::Wobject::Survey::SurveyJSON->new(
$surveyJSON = WebGUI::Asset::Wobject::Survey::SurveyJSON->new($session,
'{ "sections" : [], "survey" : {} }',
$session->log,
);
cmp_deeply(
@ -188,16 +187,14 @@ cmp_deeply(
lives_ok
{
my $foo = WebGUI::Asset::Wobject::Survey::SurveyJSON->new(
my $foo = WebGUI::Asset::Wobject::Survey::SurveyJSON->new($session,
encode_json({survey => "on 16\x{201d}" }),
$session->log
);
}
'new handles wide characters';
$sJSON2 = WebGUI::Asset::Wobject::Survey::SurveyJSON->new(
$sJSON2 = WebGUI::Asset::Wobject::Survey::SurveyJSON->new($session,
'{ "sections" : [ { "type" : "section" } ], "survey" : {} }',
$session->log,
);
cmp_deeply(
@ -276,7 +273,7 @@ cmp_deeply(
#
####################################################
$surveyJSON = WebGUI::Asset::Wobject::Survey::SurveyJSON->new('{}', $session->log);
$surveyJSON = WebGUI::Asset::Wobject::Survey::SurveyJSON->new($session, '{}');
{
my $section = $surveyJSON->section([0]);
$section->{title} = 'Section 0';
@ -2009,7 +2006,7 @@ cmp_deeply(
#
####################################################
{
my $s = WebGUI::Asset::Wobject::Survey::SurveyJSON->new();
my $s = WebGUI::Asset::Wobject::Survey::SurveyJSON->new($session, '{}');
is($s->totalSections, 1, 'a');
is($s->totalQuestions, 0, 'a');
is($s->totalAnswers, 0, 'a');
@ -2077,12 +2074,7 @@ cmp_deeply(
#
####################################################
WebGUI::Test->interceptLogging;
my $logger = $surveyJSON->log("Everyone in here is innocent");
is ($WebGUI::Test::logger_warns, undef, 'Did not log a warn');
is ($WebGUI::Test::logger_info, undef, 'Did not log an info');
is ($WebGUI::Test::logger_error, "Everyone in here is innocent", 'Logged an error');
isa_ok($surveyJSON->session, 'WebGUI::Session', 'session() accessor works');
}