Skip to content

Commit

Permalink
Item1130: unit tests
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.foswiki.org/trunk/SetTopicValuesPlugin@2708 0b4bb1d4-4e5a-0410-9cc4-b2b747904278
  • Loading branch information
SvenDowideit authored and SvenDowideit committed Feb 25, 2009
1 parent 73ac028 commit c58aa46
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 11 deletions.
11 changes: 7 additions & 4 deletions lib/Foswiki/Plugins/SetTopicValuesPlugin.pm
Expand Up @@ -49,11 +49,11 @@ sub afterSaveHandler {
return if (defined($beforeSaveHandlerONCE));
$beforeSaveHandlerONCE = 1;

#print STDERR "afterSaveHandler - (".$_[2].".".$_[1].") ".$_[3]->getEmbeddedStoreForm()."\n";
#print STDERR "afterSaveHandler - (".$_[2].".".$_[1].") ";#.$_[3]->getEmbeddedStoreForm()."\n";

# do not uncomment, use $_[0], $_[1]... instead
### my ( $text, $topic, $web, $meta ) = @_;
Foswiki::Func::writeDebug( "- SetTopicValuesPlugin::beforeSaveHandler( $_[2].$_[1] )" ) if $debug;
Foswiki::Func::writeDebug( "- SetTopicValuesPlugin::afterSaveHandler( $_[2].$_[1] )" ) if $debug;
my $cgi = Foswiki::Func::getCgiQuery();

#TODO: we should be transactional by default - test that we can write to all topics, and take out leases
Expand All @@ -64,9 +64,12 @@ sub afterSaveHandler {
#http://quad/airdrilling/bin/save/Sandbox/TestTopic55?set+Sandbox.TestTopic5Edit:fields[EditDocumentState]=SomeValue
my @paramKeys = $cgi->param();
foreach my $key (@paramKeys) {
if ($key =~ /^[Ss]et\+(.*)$/ ) {
#print STDERR "====== $key\n";
if ($key =~ /^[Ss]et[\+ ](.*)$/ ) {
my $addr = $1;

#TODO: this code is going to be replaced with the nodeParser ideas from my RestPlugin

my $webTopic = $_[2].'.'.$_[1];
if ($addr =~ /^(.*):(.*)$/ ) {
$webTopic = $1;
Expand All @@ -79,8 +82,8 @@ sub afterSaveHandler {
}

my $value = $cgi->param($key);
#print STDERR "Set ($webTopic)($type)[$addr] = ($value)\n";
my ($sWeb, $sTopic) = Foswiki::Func::normalizeWebTopicName($_[2], $webTopic);
#print STDERR "Set ($sWeb.$sTopic)($type)[$addr] = ($value)\n";
if (Foswiki::Func::topicExists($sWeb, $sTopic)) {
my( $sMeta, $sText ) = Foswiki::Func::readTopic($sWeb, $sTopic);
$type =~ s/S$//;
Expand Down
96 changes: 89 additions & 7 deletions test/unit/SetTopicValuesPlugin/SetTopicValuesPluginTests.pm
Expand Up @@ -64,8 +64,17 @@ sub saveValues {
foreach my $setKey (keys(%{$settingRef})) {
my $type = 'PREFERENCE';
my $key = $setKey;

$key =~ s/^[Ss]et\+//;

if ($key =~ /^(.*)\[(.*)\]$/ ) {
$type = uc($1);
$key = $2;

$type =~ s/S$//; #remove the trailing S (fields[] == META:FIELD)
}
#print STDERR "---- ($type)[$key]\n";

my $ma = $meta->get($type, $key );
$this->assert_not_null($ma);
$this->assert_equals( $settingRef->{$setKey}, $ma->{value} );
Expand Down Expand Up @@ -96,15 +105,11 @@ sub test_SetTopicValuesPluginEnabled {
# Verifies: ?Set+SOMEPREF=something works
sub test_set_new_preference_on_new_topic {
my $this = shift;
$this->saveValues(
'DeleteTestSaveScriptTopic',
$this->saveValues( 'DeleteTestSaveScriptTopic',
"CORRECT",
{
'Set+NEWPREFERENCE' => 'someValue'
}
{ 'Set+NEWPREFERENCE' => 'someValue' }
);

print STDERR "DONE\n";
print STDERR "DONE\n";
}

# ----------------------------------------------------------------------
Expand Down Expand Up @@ -146,7 +151,84 @@ sub test_set_new_preference_on_existing_topic {
);

print STDERR "DONE\n";
}

# ----------------------------------------------------------------------
# Purpose: create a new topic with a Field set
# Verifies: see if setting a FormField will add a field to the topic
sub test_set_FIELD_on_new_topic {
my $this = shift;
$this->saveValues( 'DeleteTestSaveScriptTopic',
"CORRECT",
{ 'Set+fields[TestField]' => 'someValue' }
);

print STDERR "DONE\n";
}

# ----------------------------------------------------------------------
# Purpose: create a new topic with a Field set, and then add a preference
# Verifies: makes sure 2 sequential operations don't lose info
sub test_set_FIELD_and_then_PREF_on_new_topic {
my $this = shift;

my $text = "CORRECT";
$this->saveValues( 'DeleteTestSaveScriptTopic',
$text,
{ 'Set+fields[TestField]' => 'someValue' }
);
$this->saveValues( 'DeleteTestSaveScriptTopic',
undef,
{ 'Set+preferences[BANANA]' => 'no, we have no bananas' }
);

my ( $meta, $sText ) = Foswiki::Func::readTopic($this->{test_web}, 'DeleteTestSaveScriptTopic' );
if (defined($text)) {
$this->assert_matches( $text, $sText );
}
$this->assert_null( $meta->get('FORM') );
my $ma = $meta->get('PREFERENCE', 'BANANA' );
$this->assert_not_null($ma);
$this->assert_equals( 'no, we have no bananas', $ma->{value} );
#I _THINK_ this test failes because save removes form fields that are not in the current form (ie, none)
#but i'm not that sure this is a good thing, just historical
$ma = $meta->get('FIELD', 'TestField' );
# $this->assert_not_null($ma);
# $this->assert_equals( 'someValue', $ma->{value} );

print STDERR "DONE\n";
}

# ----------------------------------------------------------------------
# Purpose: create a new topic with a Field set, and then add a preference
# Verifies: makes sure 2 sequential operations don't lose info
sub test_set_PREF_and_then_PREF_on_new_topic {
my $this = shift;

my $text = "CORRECT";
$this->saveValues( 'DeleteTestSaveScriptTopic',
$text,
{ 'Set+preferences[APPLE]' => 'keep the dentist happy' }
);
$this->saveValues( 'DeleteTestSaveScriptTopic',
undef,
{ 'Set+preferences[BANANA]' => 'no, we have no bananas' }
);

my ( $meta, $sText ) = Foswiki::Func::readTopic($this->{test_web}, 'DeleteTestSaveScriptTopic' );
if (defined($text)) {
$this->assert_matches( $text, $sText );
}
$this->assert_null( $meta->get('FORM') );
my $ma = $meta->get('PREFERENCE', 'BANANA' );
$this->assert_not_null($ma);
$this->assert_equals( 'no, we have no bananas', $ma->{value} );

$ma = $meta->get('PREFERENCE', 'APPLE' );
$this->assert_not_null($ma);
$this->assert_equals( 'keep the dentist happy', $ma->{value} );

print STDERR "DONE\n";
}

1;

0 comments on commit c58aa46

Please sign in to comment.