Skip to content

Commit

Permalink
Item8438: allow users to define a parent for the child topic; added s…
Browse files Browse the repository at this point in the history
…ome basic unit tests for %TOPICCREATE%

git-svn-id: http://svn.foswiki.org/trunk/TopicCreatePlugin@6183 0b4bb1d4-4e5a-0410-9cc4-b2b747904278
  • Loading branch information
AndrewJones authored and AndrewJones committed Jan 28, 2010
1 parent 1069d09 commit 9de9fad
Show file tree
Hide file tree
Showing 6 changed files with 224 additions and 11 deletions.
14 changes: 8 additions & 6 deletions data/System/TopicCreatePlugin.txt
Expand Up @@ -13,7 +13,8 @@ This Plugin allows you to automatically create topics or attach files at topic s
* Attributes recognized
| *Attribute* | *Meaning* | *Valid input* | *Default* |
| =template=""= | Name of Foswiki topic to be used as template | Any valid Foswiki topic | (required) |
| =topic=""= | Name of new Foswiki topic to create | Any valid <nop>WikiWord | (required) |
| =topic=""= | Name of new Foswiki topic to create | Any valid topic name | (required) |
| =parent=""= | Parent of the newly created topic | Any existing topic | Current topic |
| =disable=""= | Name of the template topic containing this use of TOPICCREATE | Any valid topic name (<nop>WikiWord) | _recommended_ |
| =parameters=""= | List of parameters to pass to a child topic to initialize any urlparams.| Following syntax:%BR% =&lt;parameter-name&gt;=&lt;parameter-value&gt;= followed by any number of parameter name, value pairs separated by =&= | No Parameters |
* The action to create the Foswiki topic will not occur until one hits the =Save Changes= button in preview
Expand All @@ -31,7 +32,7 @@ This Plugin allows you to automatically create topics or attach files at topic s
| *Attribute* | *Meaning* | *Valid input* | *Default* |
| =fromtopic=""= | Topic (or Web.Topic) that contains the attachment | Any valid Foswiki topic | (required) |
| =fromfile=""= | The attachment to copy from | Any attached file from =fromtopic= | (required) |
| =disable=""= | Name of the template topic containing this use of TOPICATTACH | Any valid topic name (<nop>WikiWord) | (required) |
| =disable=""= | Name of the template topic containing this use of TOPICATTACH | Any valid topic name | (required) |
| =comment=""= | Comment to appear for the attachment within this topic, replacing the comment that was in the =fromtopic= | a one line string | {omit parameter if no change desired} |
* Write =%<nop>TOPICATTACH{ fromtopic="<nop>SourceTopic" fromfile="Sample.jpg" name="Snoopy.jpg" disable="..."}%= to attach =Sample.jpg= from topic =<nop>SourceTopic= as =Snoopy.jpg=
* The parameter value comment is used to change the comment (description) that shows for the topic in the table of attachments
Expand Down Expand Up @@ -60,14 +61,15 @@ Plugin settings are stored as preferences variables. To reference a plugin setti
---++ Plugin Info

| Plugin Author(s): | Foswiki:Main.AndrewJones |
| Previous Author(s): | TWiki:Main.StanleyKnutson, TWiki:Main.PeterThoeny, TWiki:Main.PaulineCheung |
| Copyright: | &copy; 2009, Andrew Jones, &copy; 2005 - 2006, Peter Thoeny |
| Previous Author(s): | TWiki:Main.StanleyKnutson, TWiki:Main.PeterThoeny, Foswiki:Main.PaulineCheung |
| Copyright: | &copy; 2009 - 2010, Andrew Jones, &copy; 2005 - 2006, Peter Thoeny |
| License: | GPL ([[http://www.gnu.org/copyleft/gpl.html][GNU General Public License]]) |
| Plugin Release: | %$RELEASE% |
| Plugin Version: | %$VERSION% |
| Change&nbsp;History: | <!-- versions below in reverse order -->&nbsp; |
| 29 Sep 2009: | Foswikitask:Item4757 - Fixed warning when using =%<nop>TOPICATTACH%= -- Foswiki:Main.AndrewJones |
| 31 Mar 2009: | Foswikitask:Item1262 - Ported to Foswiki and released as 1.0; Removed dependency on Unix utilities =cp= and =mkdir=, should now work on any OS that Foswiki does; Improved code to make it cleaner and use more of the official API; Removed depreciated API calls -- Foswiki:Main.AndrewJones |
| 28 Jan 2010: | v1.2: Foswikitask:Item8438 - =%<nop>TOPICCREATE%= now allows you to define a parent for the new topic -- Foswiki:Main.AndrewJones |
| 29 Sep 2009: | v1.1: Foswikitask:Item4757 - Fixed warning when using =%<nop>TOPICATTACH%= -- Foswiki:Main.AndrewJones |
| 31 Mar 2009: | v1.0: Foswikitask:Item1262 - Ported to Foswiki and released as 1.0; Removed dependency on Unix utilities =cp= and =mkdir=, should now work on any OS that Foswiki does; Improved code to make it cleaner and use more of the official API; Removed depreciated API calls -- Foswiki:Main.AndrewJones |
| 11686 | TWikibug:Item2956 - Freeze !TWikiRelease04x00 and copy it to new branch MAIN |
| 10613 | TWikibug:Item2473 - files in pub and data directories shouldn't be marked executable in svn (Ordnung muss sein) |
| 7960 | TWikibug:Item1238 - plugins cleanup: added BuildContrib support/infrastructure |
Expand Down
9 changes: 5 additions & 4 deletions lib/Foswiki/Plugins/TopicCreatePlugin.pm
@@ -1,6 +1,6 @@
# Plugin for Foswiki - The Free and Open Source Wiki, http://foswiki.org/
#
# Copyright (C) 2009 Andrew Jones, andrewjones86@gmail.com
# Copyright (C) 2009 - 2010 Andrew Jones, http://andrew-jones.com
# Copyright (C) 2005-2006 Peter Thoeny, peter@thoeny.org
#
# For licensing info read LICENSE file in the Foswiki root.
Expand All @@ -24,9 +24,10 @@ use vars qw(
$web $topic $user $installWeb $debug $doInit $VERSION $RELEASE $SHORTDESCRIPTION $pluginName $NO_PREFS_IN_TOPIC
);

our $VERSION = '$Rev$';
our $RELEASE = '1.1';
our $SHORTDESCRIPTION = 'Displays the hostname of the server serving Foswiki.';
our $VERSION = '$Rev$';
our $RELEASE = '1.2';
our $SHORTDESCRIPTION =
'Automatically create a set of topics and attachments at topic save time';
our $NO_PREFS_IN_TOPIC = 0;
our $pluginName = 'TopicCreatePlugin';

Expand Down
4 changes: 3 additions & 1 deletion lib/Foswiki/Plugins/TopicCreatePlugin/Func.pm
Expand Up @@ -63,6 +63,8 @@ sub handleTopicCreate {
Foswiki::Func::extractNameValuePair( $theArgs, "topic" )
|| Foswiki::Func::extractNameValuePair( $theArgs, "name" )
|| return _errorMsg( $errVar, "Parameter =topic= is missing or empty." );
my $parent = Foswiki::Func::extractNameValuePair( $theArgs, "parent" )
|| $theTopic;
my $disable = Foswiki::Func::extractNameValuePair( $theArgs, "disable" )
|| "";

Expand Down Expand Up @@ -106,7 +108,7 @@ sub handleTopicCreate {
my ( $meta, $text ) = &Foswiki::Func::readTopic( $templateWeb, $template );

# Set topic parent
$meta->putKeyed( 'TOPICPARENT', { name => $theTopic } );
$meta->putKeyed( 'TOPICPARENT', { name => $parent } );

# SMELL: replace with expandVariablesOnTopicCreation( $text );
# but then we seem to loose our parameters... Leaving it as it is for now
Expand Down
31 changes: 31 additions & 0 deletions test/unit/TopicCreatePlugin/TopicAttachTests.pm
@@ -0,0 +1,31 @@
# Tests %TOPICATTACH%
# TODO: this is just a stub...
use strict;

package TopicAttachTests;

use base qw(FoswikiFnTestCase);

use strict;
use Foswiki;
use Foswiki::Func;
use Foswiki::Plugins::TopicCreatePlugin;

sub new {
my $self = shift()->SUPER::new(@_);
return $self;
}

# Set up the test fixture
sub set_up {
my $this = shift;

$this->SUPER::set_up();
}

sub tear_down {
my $this = shift;
$this->SUPER::tear_down();
}

1;
9 changes: 9 additions & 0 deletions test/unit/TopicCreatePlugin/TopicCreatePluginSuite.pm
@@ -0,0 +1,9 @@
package TopicCreatePluginSuite;

use base qw(Unit::TestSuite);

sub name { 'TopicCreatePluginSuite' }

sub include_tests { qw(TopicCreateTests) }

1;
168 changes: 168 additions & 0 deletions test/unit/TopicCreatePlugin/TopicCreateTests.pm
@@ -0,0 +1,168 @@
# Tests %TOPICCREATE%
# still more that could be written, including:
# - recursive %TOPICCREATE%'s
package TopicCreateTests;

use base qw( FoswikiFnTestCase );

use strict;
use Foswiki;
use Foswiki::Func;
use Foswiki::Plugins::TopicCreatePlugin;

my $simpleTemplate = "SimpleTemplateTopic";

sub new {
my $self = shift()->SUPER::new(@_);
return $self;
}

# Set up the test fixture
sub set_up {
my $this = shift;

$this->SUPER::set_up();

# create a simple template topic
Foswiki::Func::saveTopic( $this->{test_web}, $simpleTemplate, undef,
<<'HERE');
---++ Template Topic
A simple template topic
HERE

}

sub tear_down {
my $this = shift;
$this->SUPER::tear_down();
}

# test the simplest use of %TOPICCREATE%
sub test_simple_create {
my $this = shift;

my $testTopic = "SimpleCreateTest";

my $sampleText = <<"HERE";
%META:TOPICINFO{author="guest" date="1053267450" format="1.0" version="1.35"}%
%META:TOPICPARENT{name="WebHome"}%
%TOPICCREATE{template="$simpleTemplate" topic="$testTopic"}%
HERE

Foswiki::Plugins::TopicCreatePlugin::beforeSaveHandler( $sampleText,
$this->{test_topic}, $this->{test_web} );

# child topic should now exist
$this->assert( Foswiki::Func::topicExists( $this->{test_web}, $testTopic ),
"$testTopic was not created" );

my ( $meta, $text ) =
Foswiki::Func::readTopic( $this->{test_web}, $testTopic );

# check the text from the template has been copied
$this->assert_matches( "simple template topic",
$text, "Template text does not appear in the new topic" );

# parent of newly created topic should be same as the topic it was created from
$this->assert_equals( $this->{test_topic}, $meta->getParent(),
"Parent of new child topic is incorrect" );
}

# test the use of %TOPICCREATE{ parent="WebHome" }%
sub test_parent {
my $this = shift;

my $testTopic = "ParentTest";

my $sampleText = <<"HERE";
%META:TOPICINFO{author="guest" date="1053267450" format="1.0" version="1.35"}%
%META:TOPICPARENT{name="WebHome"}%
%TOPICCREATE{template="$simpleTemplate" topic="$testTopic" parent="WebHome"}%
HERE

Foswiki::Plugins::TopicCreatePlugin::beforeSaveHandler( $sampleText,
$this->{test_topic}, $this->{test_web} );

# child topic should now exist
$this->assert( Foswiki::Func::topicExists( $this->{test_web}, $testTopic ),
"$testTopic was not created" );

# parent of newly created topic should be WebHome
my ( $meta, undef ) =
Foswiki::Func::readTopic( $this->{test_web}, $testTopic );
$this->assert_equals( "WebHome", $meta->getParent(),
"Parent of new child topic is incorrect" );
}

# test the use of %TOPICCREATE{ disable="ThisTopic" }%
sub test_disable {
my $this = shift;

my $testTopic = "DisableTest";

# WebHome is the current topic when running tests
my $sampleText = <<"HERE";
%META:TOPICINFO{author="guest" date="1053267450" format="1.0" version="1.35"}%
%META:TOPICPARENT{name="WebHome"}%
%TOPICCREATE{template="$simpleTemplate" topic="$testTopic" disable="WebHome"}%
HERE

Foswiki::Plugins::TopicCreatePlugin::beforeSaveHandler( $sampleText,
$this->{test_topic}, $this->{test_web} );

# child topic should not exist
$this->assert(
!Foswiki::Func::topicExists( $this->{test_web}, $testTopic ),
"$testTopic was created, when it should not have been"
);
}

# test creating a topic with parameters
sub test_parameters {
my $this = shift;

my $template = "ParamsTemplateTopic";
my $testTopic = "ParamsTest";

# create a simple template topic
Foswiki::Func::saveTopic( $this->{test_web}, $template, undef, <<'HERE');
---++ Template Topic
A template topic with parameters
* Param1: %URLPARAM{"param1"}%
* Param2: %URLPARAM{"param2"}%
HERE

my $sampleText = <<"HERE";
%META:TOPICINFO{author="guest" date="1053267450" format="1.0" version="1.35"}%
%META:TOPICPARENT{name="WebHome"}%
%TOPICCREATE{template="$template" topic="$testTopic" parameters="param1=Bergkamp&param2=Henry"}%
HERE

Foswiki::Plugins::TopicCreatePlugin::beforeSaveHandler( $sampleText,
$this->{test_topic}, $this->{test_web} );

# child topic should now exist
$this->assert( Foswiki::Func::topicExists( $this->{test_web}, $testTopic ),
"$testTopic was not created" );

# parent of newly created topic should be same as the topic it was created from
my ( undef, $text ) =
Foswiki::Func::readTopic( $this->{test_web}, $testTopic );

$this->assert_matches( "Bergkamp", $text,
"param1 does not appear in the new topic" );
$this->assert_matches( "Henry", $text,
"param2 does not appear in the new topic" );
}

1;

0 comments on commit 9de9fad

Please sign in to comment.