Skip to content

Commit

Permalink
Item9551: Add some initial unit tests
Browse files Browse the repository at this point in the history
Tidy them all.

git-svn-id: http://svn.foswiki.org/trunk/ImmediateNotifyPlugin@13893 0b4bb1d4-4e5a-0410-9cc4-b2b747904278
  • Loading branch information
GeorgeClark authored and GeorgeClark committed Feb 1, 2012
1 parent ec72ef1 commit 555c286
Show file tree
Hide file tree
Showing 6 changed files with 648 additions and 68 deletions.
9 changes: 7 additions & 2 deletions lib/Foswiki/Plugins/ImmediateNotifyPlugin.pm
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ Write debug messages if debug is enabled.
=cut

sub debug { Foswiki::Func::writeDebug(@_) if $debug; }
sub debug {

#print STDERR @_;
#print STDERR "\n";
Foswiki::Func::writeDebug(@_) if $debug;
}

=begin TML
Expand Down Expand Up @@ -299,7 +304,7 @@ sub afterSaveHandler {
unless ($notifyTopic);

while ( $notifyTopic =~
/(\t+|( )+)\* (?:\%MAINWEB\%|$Foswiki::cfg{UsersWebName})\.([^\r\n]+)/go
/(\t+|( )+)\* (?:\%MAINWEB\%\.|$Foswiki::cfg{UsersWebName}\.)?([^\r\n]+)/go
)
{
push @names, $3 if $3;
Expand Down
98 changes: 56 additions & 42 deletions lib/Foswiki/Plugins/ImmediateNotifyPlugin/IRC.pm
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ package Foswiki::Plugins::ImmediateNotifyPlugin::IRC;
use strict;
use warnings;

#use Net::IRC; DEPRECATED
#use Net::IRC; DEPRECATED

use Bot::BasicBot;
use Data::Dumper;
Expand Down Expand Up @@ -50,29 +50,39 @@ sub new {
return $this;
}



sub afterSaveHandler {

# $text = $[0]
my ( $topic, $web, $error, $meta ) = @_[1..4];
my ( $topic, $web, $error, $meta ) = @_[ 1 .. 4 ];

Foswiki::Func::writeDebug( "- ${pluginName}::afterSaveHandler( ${web}.${topic} )" ) if $debug;
Foswiki::Func::writeDebug(
"- ${pluginName}::afterSaveHandler( ${web}.${topic} )")
if $debug;

my $topicInfo = $meta->get( 'TOPICINFO' );
# Strip the front 1. part of rcs version numbers to get simple revision numbers
my $topicInfo = $meta->get('TOPICINFO');

# Strip the front 1. part of rcs version numbers to get simple revision numbers
( my $version = $topicInfo->{version} ) =~ s/^[\d]+\.//;

_writeIrc({ newconn => {
Server => Foswiki::Func::getPluginPreferencesValue( 'SERVER' ) || 'localhost',
Port => Foswiki::Func::getPluginPreferencesValue( 'PORT' ) || '6667',
Nick => 'FoswikiIrcPlugin' || Foswiki::Func::getPluginPreferencesValue( 'NICK' ) || 'FoswikiIrcPlugin',
# Ircname => 'This bot brought to you by Net::IRC.',
# Username => 'FoswikiIrcPlugin',
},
_writeIrc(
{
newconn => {
Server => Foswiki::Func::getPluginPreferencesValue('SERVER')
|| 'localhost',
Port => Foswiki::Func::getPluginPreferencesValue('PORT')
|| '6667',
Nick => 'FoswikiIrcPlugin'
|| Foswiki::Func::getPluginPreferencesValue('NICK')
|| 'FoswikiIrcPlugin',

# Ircname => 'This bot brought to you by Net::IRC.',
# Username => 'FoswikiIrcPlugin',
},
msg => Foswiki::Func::getScriptUrl( $web, $topic, 'view' ) . ' '
. ( $version == 1 ? 'created' : "updated to r$version" )
. " by $topicInfo->{author}",
});
. ( $version == 1 ? 'created' : "updated to r$version" )
. " by $topicInfo->{author}",
}
);

# more code here
}
Expand All @@ -82,27 +92,30 @@ my $looping;

sub _writeIrc {
my $p = shift;
print STDERR "_writeIrc:".Data::Dumper::Dumper( $p );
print STDERR "_writeIrc:" . Data::Dumper::Dumper($p);

my $irc = Net::IRC->new() or die $!;
my $conn = $irc->newconn( %{$p->{newconn}} );
my $conn = $irc->newconn( %{ $p->{newconn} } );

# SMELL: make exception
if ( !$conn ) {
# SMELL: write to queue
print STDERR "IrcPlugin: Can't connect to IRC server : " . Data::Dumper::Dumper( $p );
return;

# SMELL: write to queue
print STDERR "IrcPlugin: Can't connect to IRC server : "
. Data::Dumper::Dumper($p);
return;
}
$conn->{msg} = $p->{msg};

$conn->add_handler('msg', \&on_msg);
$conn->add_handler( 'msg', \&on_msg );

$conn->add_global_handler([ 251,252,253,254,302,255 ], \&on_init);
$conn->add_global_handler(376, \&on_connect);
$conn->add_global_handler(433, \&on_nick_taken);
$conn->add_global_handler( [ 251, 252, 253, 254, 302, 255 ], \&on_init );
$conn->add_global_handler( 376, \&on_connect );
$conn->add_global_handler( 433, \&on_nick_taken );

# $irc->start();
for ( $looping = 1; $looping; ) {
$irc->do_one_loop();
# $irc->start();
for ( $looping = 1 ; $looping ; ) {
$irc->do_one_loop();
}
}

Expand All @@ -112,38 +125,39 @@ sub _writeIrc {
sub on_connect {
my $self = shift;

my $CHANNEL = Foswiki::Func::getPluginPreferencesValue( 'CHANNEL' ) || 'test';
$self->join( $CHANNEL );
# print STDERR "nick=[" . $self->nick . "]\n";
my $CHANNEL = Foswiki::Func::getPluginPreferencesValue('CHANNEL') || 'test';
$self->join($CHANNEL);

# print STDERR "nick=[" . $self->nick . "]\n";
foreach ( $CHANNEL, $self->nick ) {
$self->privmsg( $_, $self->{msg} );
$self->privmsg( $_, $self->{msg} );
}
}

# Handles some messages you get when you connect
sub on_init {
my ($self, $event) = @_;
my (@args) = ($event->args);
shift (@args);
# print "*** @args\n";
my ( $self, $event ) = @_;
my (@args) = ( $event->args );
shift(@args);

# print "*** @args\n";
}

# Change our nick if someone stole it.
sub on_nick_taken {
my ($self) = shift;

print STDERR "argh! nick [" . $self->nick . "] taken!!!\n";
$self->nick(substr($self->nick, -1) . substr($self->nick, 0, 8));
$self->nick( substr( $self->nick, -1 ) . substr( $self->nick, 0, 8 ) );
}

sub on_msg {
my ($self, $event) = @_;
my ( $self, $event ) = @_;
my ($nick) = $event->nick;

# print "*$nick* ", ($event->args), "\n";
# we've received the message we broadcasted; we're done
# SMELL: unless some talked to us, and we didn't actually get back the message back yet...
# we've received the message we broadcasted; we're done
# SMELL: unless some talked to us, and we didn't actually get back the message back yet...
$looping = 0;
}

Expand Down
116 changes: 116 additions & 0 deletions lib/Foswiki/Plugins/ImmediateNotifyPlugin/TEST.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
# See bottom of file for default license and copyright information

=begin TML
---+ package Foswiki::Plugins::ImmediateNotifyPlugin::TEST
This plugin module supports the unit tests. It on of topic records all actions to STDERR for analysis
.
=cut

package Foswiki::Plugins::ImmediateNotifyPlugin::TEST;
use FoswikiFnTestCase;

use strict;
use warnings;
no warnings 'redefine';

my $debug;
my $warning;

sub new {
my ($class) = @_;

my $this = bless( {}, $class );

$debug = \&Foswiki::Plugins::ImmediateNotifyPlugin::debug;
$warning = \&Foswiki::Plugins::ImmediateNotifyPlugin::warning;

return $this;
}

# ========================
# initMethed - initializes a single notification method
sub connect {
my $this = shift;

&$debug("ImmediateNotify:TEST - Simulating connection\n");
return 1;
}

# ========================
# handleNotify - handles notification for a single notification method
# Parameters: $userHash, $info
# $userHash is a hash reference of the form username->user topic text
# $info is a hash reference for an extended topicRevisionInfo of the saved topic
sub notify {
my $this = shift;
my $userHash = shift;
my $info = shift;

my ($skin) = Foswiki::Func::getPreferencesValue("SKIN");
my ($template) = Foswiki::Func::readTemplate( 'xmpp', 'immediatenotify' );

&$debug("- TEST: template read $template");

# Expand Legacy variables - not used in latest templates
my ($from) = Foswiki::Func::getPreferencesValue("WIKIWEBMASTER");
$template =~ s/%EMAILFROM%/$from/go;
$template =~ s/%USER%/$info->{user}/go;
$template =~ s/%TOPICNAME%/$info->{topic}/go;
$template =~ s/%REV%/$info->{version}/go;

my $body =
Foswiki::Func::expandCommonVariables( $template, $info->{topic},
$info->{web} );

my $toolName = Foswiki::Func::getPreferencesValue("WIKITOOLNAME")
|| "Foswiki";
foreach my $user ( keys %$userHash ) {

&$debug(" processing $user");

my %uHash = %{ $userHash->{$user} };
my $uParams = 'none';

if ( $uHash{PARMS} ) {
$uParams = $uHash{PARMS};
}

push( @FoswikiFnTestCase::mails,
"TEST Sending To: $user, PARAMS $uParams, BODY: $body" );
}
}

# ========================
# disconnect - Close any persistent connection to the server
sub disconnect {
my $this = shift;

&$debug(" TEST - disconnect completed\n");
return 0;
}

1;

__END__
Foswiki - The Free and Open Source Wiki, http://foswiki.org/
Copyright (C) 2008-2011 Foswiki Contributors. Foswiki Contributors
are listed in the AUTHORS file in the root of this distribution.
Copyright (C) 2010-2011 George Clark
Copyright (C) 2003 Walter Mundt, emage@spamcop.net
Copyright (C) 2003 Akkaya Consulting GmbH, jpabel@akkaya.de
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version. For
more details read LICENSE in the root of this distribution.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
As per the GPL, removal of this notice is prohibited.
51 changes: 28 additions & 23 deletions lib/Foswiki/Plugins/ImmediateNotifyPlugin/Twitter.pm
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ require Foswiki::Func; # The plugins API
require Net::Twitter;
require WWW::Shorten::Bitly;


=begin TML
---++ afterSaveHandler($text, $topic, $web, $error, $meta )
Expand All @@ -42,46 +41,52 @@ sub afterSaveHandler {
# as if it was passed by reference; for example:
# $_[0] =~ s/SpecialString/my alternative/ge;

return if(grep(/^$web$/,@excludeWebs));
return if ( grep( /^$web$/, @excludeWebs ) );

my $tweet=$Foswiki::cfg{Plugins}{ImmediateNotifyPlugin}{Template} || '$user $action topic $web.$topic $url';
my $tweet = $Foswiki::cfg{Plugins}{ImmediateNotifyPlugin}{Template}
|| '$user $action topic $web.$topic $url';

my $action='saved';
$action='created' if($isNewTopic{$$});
my $action = 'saved';
$action = 'created' if ( $isNewTopic{$$} );

my $user=$Foswiki::Plugins::SESSION->{'user'};
my $user = $Foswiki::Plugins::SESSION->{'user'};

# only shorten url if url is actually used
my $url=Foswiki::Func::getViewUrl($web,$topic);
if( ($tweet=~/\$url/) and (defined($Foswiki::cfg{Plugins}{ImmediateNotifyPlugin}{BitlyUser})) ) {
$url=WWW::Shorten::Bitly::makeashorterlink(
$url,
$Foswiki::cfg{Plugins}{ImmediateNotifyPlugin}{BitlyUser},
$Foswiki::cfg{Plugins}{ImmediateNotifyPlugin}{BitlyKey}
);
my $url = Foswiki::Func::getViewUrl( $web, $topic );
if ( ( $tweet =~ /\$url/ )
and
( defined( $Foswiki::cfg{Plugins}{ImmediateNotifyPlugin}{BitlyUser} ) )
)
{
$url = WWW::Shorten::Bitly::makeashorterlink(
$url,
$Foswiki::cfg{Plugins}{ImmediateNotifyPlugin}{BitlyUser},
$Foswiki::cfg{Plugins}{ImmediateNotifyPlugin}{BitlyKey}
);
}

$tweet=~s/\$web/$web/g;
$tweet=~s/\$topic/$topic/g;
$tweet=~s/\$action/$action/g;
$tweet=~s/\$user/$user/g;
$tweet=~s/\$url/$url/g;
$tweet =~ s/\$web/$web/g;
$tweet =~ s/\$topic/$topic/g;
$tweet =~ s/\$action/$action/g;
$tweet =~ s/\$user/$user/g;
$tweet =~ s/\$url/$url/g;

Foswiki::Func::writeDebug("tweet: $tweet");

my $twitter = Net::Twitter->new(
traits => [qw/API::REST/],
username => $Foswiki::cfg{Plugins}{ImmediateNotifyPlugin}{StatusUser},
password => $Foswiki::cfg{Plugins}{ImmediateNotifyPlugin}{StatusPassword}
traits => [qw/API::REST/],
username => $Foswiki::cfg{Plugins}{ImmediateNotifyPlugin}{StatusUser},
password =>
$Foswiki::cfg{Plugins}{ImmediateNotifyPlugin}{StatusPassword}
);

my $result = $twitter->update($tweet);

Foswiki::Func::writeDebug("result for twitter update: ".join(', ',keys %{$result}));
Foswiki::Func::writeDebug(
"result for twitter update: " . join( ', ', keys %{$result} ) );

}


1;
__END__
This copyright information applies to the EmptyPlugin:
Expand Down
Loading

0 comments on commit 555c286

Please sign in to comment.