Skip to content

Commit

Permalink
Item13897: Converted EditRowPlugin.
Browse files Browse the repository at this point in the history
This conversion is an example of how to convert a plugin to the new model.
Conversion results has been checked by running unit tests.

- Added method getPluginError() to JQueryPlugin::Plugins.
  • Loading branch information
vrurg committed Jun 30, 2016
1 parent 11a27f8 commit 3b55f9c
Show file tree
Hide file tree
Showing 16 changed files with 262 additions and 221 deletions.
2 changes: 1 addition & 1 deletion EditRowPlugin/lib/Foswiki/Plugins/EditRowPlugin/Get.pm
Expand Up @@ -17,7 +17,7 @@ use constant TRACE => 0;
# * erp_row
# * erp_col
sub process {
my ( $session, $plugin, $verb, $response ) = @_;
my ( $app, $plugin, $verb, $response ) = @_;
my $query = Foswiki::Func::getCgiQuery();

unless ($query) {
Expand Down
38 changes: 14 additions & 24 deletions EditRowPlugin/lib/Foswiki/Plugins/EditRowPlugin/JQuery.pm
Expand Up @@ -14,31 +14,21 @@
# http://www.gnu.org/copyleft/gpl.html

package Foswiki::Plugins::EditRowPlugin::JQuery;
use strict;
use v5.14;

use Assert;
use Moo;
extends qw( Foswiki::Plugins::JQueryPlugin::Plugin );

use Foswiki::Plugins::JQueryPlugin::Plugin ();
our @ISA = qw( Foswiki::Plugins::JQueryPlugin::Plugin );

sub new {
my $class = shift;
my $session = shift || $Foswiki::Plugins::SESSION;

my $this = $class->SUPER::new(
$session,
name => 'EditRow',
version => '1.0',
author => 'Crawford Currie',
homepage => 'http://foswiki.org/Extensions/EditRowPlugin',
puburl => '%PUBURLPATH%/%SYSTEMWEB%/EditRowPlugin',
css => ["erp.css"],
documentation => "$Foswiki::cfg{SystemWebName}.EditRowPlugin",
javascript => [ "erp.js", "TableSort.js" ],
dependencies => [ 'UI', 'JEditable' ]
);

return $this;
}
our %pluginParams = (
name => 'EditRow',
version => '1.0',
author => 'Crawford Currie',
homepage => 'http://foswiki.org/Extensions/EditRowPlugin',
puburl => '%PUBURLPATH%/%SYSTEMWEB%/EditRowPlugin',
css => ["erp.css"],
documentation => "$Foswiki::cfg{SystemWebName}.EditRowPlugin",
javascript => [ "erp.js", "TableSort.js" ],
dependencies => [ 'UI', 'JEditable' ]
);

1;
4 changes: 2 additions & 2 deletions EditRowPlugin/lib/Foswiki/Plugins/EditRowPlugin/Save.pm
Expand Up @@ -24,7 +24,7 @@ use constant TRACE => 0;
# match the latest rev of the topic, the save will be aborted.
# * erp_stop_edit - if true, stop editing after save complete
sub process {
my ( $session, $plugin, $verb, $response ) = @_;
my ( $app, $plugin, $verb, $response ) = @_;
my $query = Foswiki::Func::getCgiQuery();

unless ($query) {
Expand Down Expand Up @@ -230,7 +230,7 @@ sub process {
require Foswiki::Validation;
my $context =
$query->url( -full => 1, -path => 1, -query => 1 ) . time();
my $cgis = $session->getCGISession();
my $cgis = $app->users->getCGISession();
my $nonce;
if ( Foswiki::Validation->can('generateValidationKey') ) {
$nonce =
Expand Down
23 changes: 14 additions & 9 deletions EditRowPlugin/lib/Foswiki/Plugins/EditRowPlugin/View.pm
Expand Up @@ -17,7 +17,7 @@ use Foswiki::Func();
# * erp_table
# * erp_row
sub process {
my ( $text, $web, $topic, $meta ) = @_;
my ( $app, $text, $web, $topic, $meta ) = @_;

my $macro = $Foswiki::cfg{Plugins}{EditRowPlugin}{Macro} || 'EDITTABLE';

Expand All @@ -33,15 +33,20 @@ sub process {
return 0
if Foswiki::Func::getPreferencesFlag('EDITROWPLUGIN_DISABLE') =~ /full/;

Foswiki::Plugins::JQueryPlugin::registerPlugin( 'EditRow',
my $pluginName = "EditRow";
Foswiki::Plugins::JQueryPlugin::registerPlugin( $pluginName,
'Foswiki::Plugins::EditRowPlugin::JQuery' );
unless (
Foswiki::Plugins::JQueryPlugin::createPlugin(
"EditRow", $Foswiki::Plugins::SESSION
)
)
unless ( Foswiki::Plugins::JQueryPlugin::createPlugin( $pluginName, $app ) )
{
die 'Failed to register JQuery plugin';
Foswiki::Exception::Fatal->throw(
text => "Failed to register JQuery plugin $pluginName: "
. (
DEBUG
? Foswiki::Plugins::JQueryPlugin::Plugins::getPluginError(
'EditRow')
: ''
)
);
}

require Foswiki::Plugins::EditRowPlugin::TableParser;
Expand Down Expand Up @@ -245,7 +250,7 @@ sub process {
}

if ($hasTables) {
$_[0] = join( "\n", @$content ) . ( $endsWithNewline ? "\n" : '' );
$_[1] = join( "\n", @$content ) . ( $endsWithNewline ? "\n" : '' );
return 1;
}
return 0;
Expand Down
102 changes: 56 additions & 46 deletions EditRowPlugin/test/unit/EditRowPlugin/HTML.pm
Expand Up @@ -15,19 +15,27 @@ around loadExtraConfig => sub {
$Foswiki::cfg{Plugins}{EditTablePlugin}{Enabled} = 0;
};

around createNewFoswikiApp => sub {
my $orig = shift;
my $this = shift;

my $app = $orig->( $this, @_ );

$app->plugins->enable;

return $app;
};

sub test_simple_view {
my $this = shift;
require Foswiki::Plugins::EditRowPlugin::View;
$this->assert( !$@, $@ );
$this->clear_test_topicObject;
$this->clear_session;
my $query = Unit::Request->new( initializer => {} );
$this->session(
Foswiki->new(
user => $this->test_user_login,
request => $query,
context => { view => 1 }
)
$this->createNewFoswikiApp(
requestParams => { initializer => {}, },
engineParams =>
{ initialAttributes => { user => $this->test_user_login, }, },
context => { view => 1 }
);
$this->test_topicObject(
( Foswiki::Func::readTopic( $this->test_web, $this->test_topic ) )[0] );
Expand All @@ -38,8 +46,9 @@ sub test_simple_view {
INPUT
$this->assert(
Foswiki::Plugins::EditRowPlugin::View::process(
$in, $this->test_web,
$this->test_topic, $this->test_topicObject
$this->app, $in,
$this->test_web, $this->test_topic,
$this->test_topicObject
)
);
$this->assert( $in =~ s/<!-- STARTINCLUDE.*?-->\s*(.*)\s*<!--.*/$1/s, $in );
Expand Down Expand Up @@ -125,18 +134,14 @@ sub test_Item12953 {
require Foswiki::Plugins::EditRowPlugin::View;
$this->assert( !$@, $@ );
$this->clear_test_topicObject;
$this->clear_session;
my $query = Unit::Request->new( initializer => {} );
$this->session(
Foswiki->new(
user => $this->test_user_login,
request => $query,
context => { view => 1 }
)
$this->createNewFoswikiApp(
requestParams => { initializer => {}, },
engineParams =>
{ initialAttributes => { user => $this->test_user_login, }, },
context => { view => 1 }
);
$this->test_topicObject(
( Foswiki::Func::readTopic( $this->{test_web}, $this->{test_topic} ) )
[0] );
( Foswiki::Func::readTopic( $this->test_web, $this->test_topic ) )[0] );

my $in = <<INPUT;
%EDITTABLE{
Expand All @@ -146,8 +151,9 @@ sub test_Item12953 {
INPUT
$this->assert(
Foswiki::Plugins::EditRowPlugin::View::process(
$in, $this->test_web,
$this->test_topic, $this->test_topicObject
$this->app, $in,
$this->test_web, $this->test_topic,
$this->test_topicObject
)
);
$this->assert( $in =~ s/\s*<!-- STARTINCLUDE.*?-->\s*(.*)\s*<!--.*/$1/s,
Expand Down Expand Up @@ -187,27 +193,29 @@ sub test_edit_view_default {
require Foswiki::Plugins::EditRowPlugin::View;
$this->assert( !$@, $@ );
$this->clear_test_topicObject;
$this->clear_session;
my $query = Unit::Request->new(
initializer => {
erp_topic => $this->test_web . "." . $this->test_topic,
erp_table => 'TABLE_0'
}
$this->createNewFoswikiApp(
requestParams => {
initializer => {
erp_topic => $this->test_web . "." . $this->test_topic,
erp_table => 'TABLE_0'
},
},
engineParams =>
{ initialAttributes => { user => $this->test_user_login, }, },
context => { view => 1 },
);
$this->session(
Foswiki->new( $this->{test_user_login}, $query, { view => 1 } ) );
$this->test_topicObject(
( Foswiki::Func::readTopic( $this->{test_web}, $this->{test_topic} ) )
[0] );
( Foswiki::Func::readTopic( $this->test_web, $this->test_topic ) )[0] );

my $in = <<INPUT;
%EDITTABLE%
| A |
INPUT
$this->assert(
Foswiki::Plugins::EditRowPlugin::View::process(
$in, $this->test_web,
$this->test_topic, $this->test_topicObject
$this->app, $in,
$this->test_web, $this->test_topic,
$this->test_topicObject
)
);
$this->assert( $in =~ s/<!-- STARTINCLUDE.*?-->\s*(.*)\s*<!--.*/$1/s, $in );
Expand Down Expand Up @@ -250,27 +258,29 @@ sub test_edit_view_no_js {
require Foswiki::Plugins::EditRowPlugin::View;
$this->assert( !$@, $@ );
$this->clear_test_topicObject;
$this->clear_session;
my $query = Unit::Request->new(
initializer => {
erp_topic => $this->test_web . "." . $this->test_topic,
erp_table => 'TABLE_0'
}
$this->createNewFoswikiApp(
requestParams => {
initializer => {
erp_topic => $this->test_web . "." . $this->test_topic,
erp_table => 'TABLE_0'
},
},
engineParams =>
{ initialAttributes => { user => $this->test_user_login, }, },
context => { view => 1 },
);
$this->session(
Foswiki->new( $this->{test_user_login}, $query, { view => 1 } ) );
$this->test_topicObject(
( Foswiki::Func::readTopic( $this->{test_web}, $this->{test_topic} ) )
[0] );
( Foswiki::Func::readTopic( $this->test_web, $this->test_topic ) )[0] );

my $in = <<INPUT;
%EDITTABLE{js="ignore"}%
| A |
INPUT
$this->assert(
Foswiki::Plugins::EditRowPlugin::View::process(
$in, $this->test_web,
$this->test_topic, $this->test_topicObject
$this->app, $in,
$this->test_web, $this->test_topic,
$this->test_topicObject
)
);
$this->assert( $in =~ s/<!-- STARTINCLUDE.*?-->\s*(.*)\s*<!--.*/$1/s, $in );
Expand Down
29 changes: 14 additions & 15 deletions EditRowPlugin/test/unit/EditRowPlugin/Rest.pm
Expand Up @@ -7,24 +7,24 @@ extends qw(FoswikiFnTestCase);

sub gettit {
my ( $this, $t, $r, $c ) = @_;
$this->clear_session;
my %qd = (
erp_version => "VERSION",
erp_topic => $this->test_web . "." . $this->test_topic,
erp_table => "TABLE_$t"
);
$qd{erp_row} = $r if defined $r;
$qd{erp_col} = $c if defined $c;
my $query = Unit::Request->new( initializer => \%qd );
$this->session(
Foswiki->new( user => $this->test_user_login, request => $query ) );
my $response = Foswiki::Response->new();
$this->createNewFoswikiApp(
requestParams => { initializer => \%qd, },
engineParams =>
{ initialAttributes => { user => $this->test_user_login, }, },
);
$this->assert_null(
Foswiki::Plugins::EditRowPlugin::Get::process(
$this->session, "EditRowPlugin", "get", $response
$this->app, "EditRowPlugin", "get", $this->app->response
)
);
return $response->body();
return $this->app->response->body;
}

sub test_rest_get {
Expand All @@ -43,7 +43,6 @@ sub test_rest_get {
INPUT
$this->test_topicObject->save();
$this->clear_test_topicObject;
$this->clear_session;

$this->assert_equals( '"1"', $this->gettit( 0, 0, 0 ) );

Expand Down Expand Up @@ -76,7 +75,6 @@ sub test_rest_save {
INPUT
$this->test_topicObject->save();
$this->clear_test_topicObject;
$this->clear_session;

my %qd = (
CELLDATA => "Spitoon",
Expand All @@ -88,16 +86,17 @@ INPUT
erp_table => "TABLE_1",
noredirect => 1, # for AJAX
);
my $query = Unit::Request->new( initializer => \%qd );
$this->session(
Foswiki->new( user => $this->test_user_login, request => $query ) );
my $response = Foswiki::Response->new();
$this->createNewFoswikiApp(
requestParams => { initializer => \%qd, },
engineParams =>
{ initialAttributes => { user => $this->test_user_login, }, },
);
$this->assert_null(
Foswiki::Plugins::EditRowPlugin::Save::process(
$this->session, "EditRowPlugin", "save", $response
$this->app, "EditRowPlugin", "save", $this->app->response
)
);
$this->assert_equals( "RESPONSESpitoon", $response->body() );
$this->assert_equals( "RESPONSESpitoon", $this->app->response->body );
$this->clear_test_topicObject;
$this->test_topicObject(
( Foswiki::Func::readTopic( $this->test_web, $this->test_topic ) )[0] );
Expand Down

0 comments on commit 3b55f9c

Please sign in to comment.