Skip to content

Commit

Permalink
Item12952: unit test fixes and knock-on changes for probems so reveal…
Browse files Browse the repository at this point in the history
…ed. SaveTests still fail, though this is non-critical as the code is not currently used
  • Loading branch information
crawford committed Oct 14, 2014
1 parent 13841b7 commit b3932c8
Show file tree
Hide file tree
Showing 12 changed files with 245 additions and 550 deletions.
53 changes: 18 additions & 35 deletions ConfigurePlugin/lib/Foswiki/Plugins/ConfigurePlugin.pm
Expand Up @@ -79,7 +79,7 @@ sub initPlugin {

# Register each of the RPC methods with JsonRpcContrib
foreach my $method (
qw(getcfg getspec search check_current_value changecfg deletecfg purgecfg wizard)
qw(getcfg getspec search check_current_value changecfg deletecfg wizard)
)
{
Foswiki::Contrib::JsonRpcContrib::registerMethod( 'configure', $method,
Expand Down Expand Up @@ -356,32 +356,14 @@ sub getspec {
@matches = ($root);
}

if ( defined $depth ) {

# Children to a fixed depth only; prune
foreach my $m (@matches) {
_prune( $m, $depth );
}
foreach my $m (@matches) {
$m->unparent();
$m->prune($depth) if defined $depth;
}

return \@matches;
}

# 0 will prune children
# 1 will prune children-of-children
sub _prune {
my ( $node, $level ) = @_;

if ( $level == 0 ) {
delete $node->{children};
}
elsif ( $node->{children} ) {
foreach my $c ( @{ $node->{children} } ) {
_prune( $c, $level - 1 );
}
}
}

# Recursive locate references to other keys in the values of keys
# Returns a =forward= hash mapping keys to a list of the keys that depend
# on their value, and a =reverse= hash mapping keys to a list of keys
Expand Down Expand Up @@ -460,7 +442,6 @@ each being a hash with keys =level= (e.g. =warnings=, =errors=), and

sub _getSetParams {
my ( $params, $root ) = @_;

if ( $params->{set} ) {
while ( my ( $k, $v ) = each %{ $params->{set} } ) {
if ( defined $v && $v ne '' ) {
Expand Down Expand Up @@ -562,7 +543,8 @@ sub check_current_value {
next if $done{$dep};
$check{$dep} = 1;
$done{$dep} = 1;
push( @dep_keys, @{ $deps{forward}->{$dep} } );
push( @dep_keys, @{ $deps{forward}->{$dep} } )
if $deps{forward}->{$dep};
}
}

Expand Down Expand Up @@ -620,7 +602,7 @@ sub _purgecfg {
my $root = shift;

# All specced keys are OK
my %ok = map { $_ => 1 } $root->getAllKeys();
my %ok = map { $_ => 1 } $root->getAllValueKeys();

# See if there are any dependencies between OK keys and other
# keys
Expand Down Expand Up @@ -692,8 +674,7 @@ sub changecfg {
if ( defined $deletions ) {
foreach my $key (@$deletions) {
die "Bad key '$key'"
unless $key =~
/^($Foswiki::Plugins::ConfigurePlugin::SpecEntry::configItemRegex)$/;
unless $key =~ /^($Foswiki::Configure::Load::ITEMREGEX)$/;

# Implicit untaint
$key = _safeKeys($1);
Expand All @@ -707,8 +688,7 @@ sub changecfg {
if ( defined $changes ) {
while ( my ( $key, $value ) = each %$changes ) {
die "Bad key '$key'"
unless $key =~
/^($Foswiki::Plugins::ConfigurePlugin::SpecEntry::configItemRegex)$/;
unless $key =~ /^($Foswiki::Configure::Load::ITEMREGEX)$/;

# Implicit untaint
$key = _safeKeys($1);
Expand Down Expand Up @@ -741,8 +721,12 @@ sub changecfg {
$Foswiki::cfg{ConfigurationFinished} = 0;
Foswiki::Configure::Load::readConfig( 0, 1 );

return
"Added: $added; Changed: $changed; Cleared: $cleared; Purged: $purged";
return {
added => $added,
changed => $changed,
cleared => $cleared,
purged => $purged
};
}

# Traverse LSC generating LSC format output
Expand All @@ -753,7 +737,7 @@ sub _lscify {
if ( scalar(@path) ) {
my $keypath = '{' . join( '}{', @path ) . '}';

my $spec = ( $specs->_keyCache->{$keypath} );
my $spec = ( $specs->getValueObject($keypath) );
if ( $spec && defined $spec->{keys} ) {

# This is a specced level; we will take the entire data
Expand Down Expand Up @@ -801,8 +785,7 @@ sub _value2string {
}

sub _save {
my $lsc = Foswiki::Plugins::ConfigurePlugin::SpecEntry::findFileOnPath(
'Foswiki.spec')
my $lsc = Foswiki::Configure::FileUtil::findFileOnPath('Foswiki.spec')
|| '';
$lsc =~ s/Foswiki\.spec/LocalSite.cfg/;
print STDERR "LSC is at $lsc\n" if TRACE;
Expand Down Expand Up @@ -982,7 +965,7 @@ while POSTing a request encoded according to the JSON-RPC 2.0 specification:
jsonrpc: "2.0",
method: "getspec",
params: {
get : { keys: [ "{DataDir}", "Store" ] },
get : { keys: "{DataDir}" },
depth : 0
},
id: "caller's id"
Expand Down
Expand Up @@ -3,7 +3,9 @@
# ---+++ Testing
# The following key specs are only used in testing the ConfigurePlugin
# and do nothing.
# **STRING FEEDBACK="label='Test';wizard='Test';method='test';auth=1"**
# **STRING FEEDBACK="label='Test one';wizard='Test';method='test';auth=1" \
# FEEDBACK="label='Test two';wizard='Gandalf';method='wand'" \
# CHECK="min:3 max:20" **
# When you press the Test button, expect the value to change to "ROPE" and
# there to be one each of the different levels of report.
$Foswiki::cfg{Plugins}{ConfigurePlugin}{Test}{STRING} = 'STRING';
Expand Down Expand Up @@ -34,8 +36,8 @@ $Foswiki::cfg{Plugins}{ConfigurePlugin}{Test}{PATH} = 'PATH';
# **PERL**
$Foswiki::cfg{Plugins}{ConfigurePlugin}{Test}{PERL} = '\'PERL\';';
# **REGEX**
# Should be /^regex$/
$Foswiki::cfg{Plugins}{ConfigurePlugin}{Test}{REGEX} = qr/^regex$/;
# Should be '^regex$'
$Foswiki::cfg{Plugins}{ConfigurePlugin}{Test}{REGEX} = '^regex$';
# **SELECTCLASS none,Foswiki::Confi* **
# Should be Foswiki::Configure
$Foswiki::cfg{Plugins}{ConfigurePlugin}{Test}{SELECTCLASS} = 'Foswiki::Configure';
Expand Down
155 changes: 86 additions & 69 deletions ConfigurePlugin/test/unit/ConfigurePlugin/ConfigurePluginTests.pm
Expand Up @@ -4,8 +4,8 @@ use warnings;

package ConfigurePluginTests;

use FoswikiTestCase;
our @ISA = qw( FoswikiTestCase );
use FoswikiFnTestCase;
our @ISA = qw( FoswikiFnTestCase );

use strict;
use warnings;
Expand Down Expand Up @@ -51,12 +51,12 @@ sub test_getcfg {
);
}

sub test_getcfg_all {
my $this = shift;
my $params = {};
my $result = Foswiki::Plugins::ConfigurePlugin::getcfg($params);
$this->assert_deep_equals( \%Foswiki::cfg, $result );
}
#sub test_getcfg_all {
# my $this = shift;
# my $params = {};
# my $result = Foswiki::Plugins::ConfigurePlugin::getcfg($params);
# $this->assert_deep_equals( \%Foswiki::cfg, $result );
#}

sub test_getcfg_badkey {
my $this = shift;
Expand Down Expand Up @@ -107,23 +107,70 @@ sub unparent {
return $what;
}

sub test_getspec {
sub test_getspec_headline {
my $this = shift;
my %params = ( keys => '{Plugins}{ConfigurePlugin}{Test}{STRING}' );
my %params = ( get => { headline => 'ConfigurePlugin' } );
my $spec = Foswiki::Plugins::ConfigurePlugin::getspec( \%params );
$this->assert_num_equals( 1, scalar @$spec );
$spec = $spec->[0];
$this->assert_str_equals( 'STRING', $spec->{type} );
$this->assert_str_equals( $params{keys}, $spec->{keys} );
$this->assert_str_equals( 'STRING', $spec->{spec_value} );
$this->assert_str_equals( 'ConfigurePlugin', $spec->[0]->{headline} );
$params{depth} = 1;
$spec = Foswiki::Plugins::ConfigurePlugin::getspec( \%params );
$this->assert_num_equals( 1, scalar @$spec );
$this->assert_str_equals( 'ConfigurePlugin', $spec->[0]->{headline} );
$this->assert_num_equals( 1, scalar @{ $spec->[0]->{children} } );
$this->assert_str_equals( 'Testing',
$spec->[0]->{children}->[0]->{headline} );
$this->assert_null( $spec->[0]->{children}->[0]->{children} );
}

$params{keys} = '{Plugins}{ConfigurePlugin}{Test}{empty}';
sub test_getspec_parent {
my $this = shift;
my %params = ( get => { parent => { headline => 'ConfigurePlugin' } } );
my $spec = Foswiki::Plugins::ConfigurePlugin::getspec( \%params );
$this->assert_num_equals( 1, scalar @$spec );
$this->assert_str_equals( 'Testing', $spec->[0]->{headline} );
}

sub test_getspec {
my $this = shift;
my %params =
( get => { keys => '{Plugins}{ConfigurePlugin}{Test}{STRING}' } );
my $spec = Foswiki::Plugins::ConfigurePlugin::getspec( \%params );
$this->assert_num_equals( 1, scalar @$spec );
$spec = $spec->[0];
$this->assert_str_equals( 'STRING', $spec->{typename} );
$this->assert_str_equals( 'STRING', $spec->{default} );
$this->assert_str_equals( '{Plugins}{ConfigurePlugin}{Test}{STRING}',
$spec->{keys} );
$this->assert_str_equals( 'STRING', $spec->{current_value} );
$this->assert_matches( qr/^When you press the.*of report.$/s,
$spec->{desc} );
$this->assert_num_equals( 4, $spec->{depth} );
$this->assert_num_equals( 2, scalar @{ $spec->{defined_at} } );
$this->assert_num_equals( 2, scalar @{ $spec->{FEEDBACK} } );
my $fb = $spec->{FEEDBACK}->[0];
$this->assert( $fb->{auth} );
$this->assert_str_equals( 'Test', $fb->{wizard} );
$this->assert_str_equals( 'test', $fb->{method} );
$this->assert_str_equals( 'Test one', $fb->{label} );
$fb = $spec->{FEEDBACK}->[1];
$this->assert( !$fb->{auth} );
$this->assert_str_equals( 'Gandalf', $fb->{wizard} );
$this->assert_str_equals( 'wand', $fb->{method} );
$this->assert_str_equals( 'Test two', $fb->{label} );

my $ch = $spec->{CHECK};
$this->assert_num_equals( 1, scalar @$ch );
$this->assert_num_equals( 3, $ch->[0]->{min}->[0] );
$this->assert_num_equals( 20, $ch->[0]->{max}->[0] );

$params{get}->{keys} = '{Plugins}{ConfigurePlugin}{Test}{empty}';
$spec = Foswiki::Plugins::ConfigurePlugin::getspec( \%params );
$this->assert_num_equals( 1, scalar @$spec );
$spec = $spec->[0];
$this->assert_str_equals( 'PATH', $spec->{type} );
$this->assert_str_equals( $params{keys}, $spec->{keys} );
$this->assert_str_equals( 'empty', $spec->{spec_value} );
$this->assert_str_equals( $params{get}->{keys}, $spec->{keys} );
$this->assert_str_equals( 'PATH', $spec->{typename} );
$this->assert_str_equals( 'empty', $spec->{default} );
}

sub test_getspec_no_LSC {
Expand All @@ -137,44 +184,7 @@ sub test_getspec_no_LSC {
my $spec = Foswiki::Plugins::ConfigurePlugin::getspec( {} );
$this->assert_num_equals( 1, scalar @$spec );
$spec = $spec->[0];
$this->assert_str_equals( 'ROOT', $spec->{type} );
}

sub test_getspec_children {
my $this = shift;
my $use_section;
my $params = { children => 1 };
my $ss = Foswiki::Plugins::ConfigurePlugin::getspec($params);
$this->assert_num_equals( 1, scalar(@$ss) );
$this->assert_equals( "ROOT", $ss->[0]->{type} );
$this->assert_null( $ss->[0]->{title} );
$this->assert( scalar( @{ $ss->[0]->{children} } ) );

foreach my $spec ( @{ $ss->[0]->{children} } ) {
$this->assert( $spec->{type} eq 'SECTION', $spec->{type} );
$this->assert_null( $spec->{children} );
if ( !$use_section ) {
$use_section = $spec->{title};
}
}

$params = { parent => { title => $use_section }, children => 0 };
$ss = Foswiki::Plugins::ConfigurePlugin::getspec($params);
foreach my $spec (@$ss) {
$this->assert_equals( $use_section, $spec->{parent}->{title} );
$this->assert_null( $spec->{children} );
}

$params = { title => $use_section, children => 1 };
$ss = Foswiki::Plugins::ConfigurePlugin::getspec($params);
foreach my $spec (@$ss) {
$this->assert_not_null( $spec->{children} );
foreach my $subspec ( @{ $spec->{children} } ) {
$this->assert_null( $subspec->{children} );
}
}

# Check pluggables
$this->assert_str_equals( 'SECTION', $spec->{typename} );
}

sub test_getspec_badkey {
Expand Down Expand Up @@ -209,31 +219,38 @@ use Foswiki::Configure::Checker;
}

sub test_check {
my $this = shift;
my $params = { "{Plugins}{ConfigurePlugin}{Test}{STRING}" => 'Theory' };
my $report = Foswiki::Plugins::ConfigurePlugin::check($params);
my $this = shift;

# force an error - STRING length
my $params = {
keys => ["{Plugins}{ConfigurePlugin}{Test}{STRING}"],
set => { "{Plugins}{ConfigurePlugin}{Test}{STRING}" => 'no' }
};
my $report =
Foswiki::Plugins::ConfigurePlugin::check_current_value($params);
$this->assert_num_equals( 1, scalar @$report );
$report = $report->[0];
$this->assert_str_equals( '{Plugins}{ConfigurePlugin}{Test}{STRING}',
$report->{keys} );
$this->assert_str_equals( 'errors', $report->{level} );
$this->assert_str_equals( 'Extensions', $report->{sections}->[0] );
$this->assert_str_equals( 'ConfigurePlugin', $report->{sections}->[1] );
$this->assert_str_equals( 'Testing', $report->{sections}->[2] );
$this->assert_matches( qr/Error/, $report->{message} );
$this->assert_matches( qr/Warning/, $report->{message} );
$this->assert_matches( qr/Note/, $report->{message} );
$this->assert_str_equals( 'Extensions', $report->{path}->[0] );
$this->assert_str_equals( 'ConfigurePlugin', $report->{path}->[1] );
$this->assert_str_equals( 'Testing', $report->{path}->[2] );
$this->assert_num_equals( 1, scalar( @{ $report->{reports} } ) );
$this->assert_str_equals( 'errors', $report->{reports}->[0]->{level} );
$this->assert_matches( qr/3/, $report->{reports}->[0]->{text} );
}

sub test_check_dependencies {
my $this = shift;

# DEPENDS depends on H and EXPERT
my $params = {
'{Plugins}{ConfigurePlugin}{Test}{H}' => 'fruitbat',
'check_dependent' => 1
keys => ['{Plugins}{ConfigurePlugin}{Test}{H}'],
set => { '{Plugins}{ConfigurePlugin}{Test}{H}' => 'fruitbat' },
check_dependencies => 1
};
my $report = Foswiki::Plugins::ConfigurePlugin::check($params);
my $report =
Foswiki::Plugins::ConfigurePlugin::check_current_value($params);
$this->assert_num_equals( 2, scalar @$report );
my ( $first, $second );
if ( $report->[0]->{keys} =~ /DEPENDS/ ) {
Expand Down

0 comments on commit b3932c8

Please sign in to comment.