add admin plugins API and start testing

This commit is contained in:
Doug Bell 2010-10-27 22:14:03 -05:00
parent c8cff33b97
commit 250967c898
4 changed files with 215 additions and 13 deletions

View file

@ -72,19 +72,20 @@ sub getAdminPluginTemplateVars {
# If we have a class name, we've got a new WebGUI::Admin::Plugin
if ( $funcDef->{className} ) {
my $plugin = $funcDef->{className}->new( $session, $funcId, $funcDef );
next unless $plugin->canUse;
my $plugin = $funcDef->{className}->new( $session, id => $funcId, $funcDef );
next unless $plugin->canView;
$var = {
title => $plugin->getTitle,
icon => $plugin->getIcon,
'icon.small' => $plugin->getIconSmall,
url => $plugin->getUrl,
id => $funcId,
title => $plugin->title,
icon => $plugin->icon,
'icon.small' => $plugin->iconSmall,
};
# build the list of processed items
$processed{$plugin->getTitle} = $var;
$processed{$plugin->title} = $var;
}
# Don't know what we have (old admin console functions)
# NOTE: This usage is deprecated and will be removed in a future version
else {
# make title
my $title = $funcDef->{title};
@ -390,7 +391,7 @@ sub getSqlFromQueryString {
$dbh->quote($value),
;
}
elsif {
elsif ( $isValidOp{ $part->{op} } ) {
push @parts, join " ",
$dbh->quote_identifier($part->{field}),
$part->{op},
@ -668,6 +669,28 @@ sub www_processAssetHelper {
#----------------------------------------------------------------------
=head2 www_processPlugin ( )
Process the given admin console plugin
=cut
sub www_processPlugin {
my ( $self ) = @_;
my $session = $self->session;
my ( $form ) = $session->quick(qw{ form });
my $id = $form->get('id');
my $def = $session->config->get('adminConsole/' . $id );
return JSON->new->encode( { error => 'No such admin plugin: ' . $id } )
unless $def;
my $class = $def->{className};
WebGUI::Pluggable::load( $class );
return JSON->new->encode( $class->process( $session ) );
}
#----------------------------------------------------------------------
=head2 www_searchAssets ( )
Search the asset tree for the given keywords and filters
@ -804,7 +827,13 @@ __DATA__
<ul id="admin_list">
<TMPL_LOOP adminPlugins>
<li class="clickable with_icon" style="background-image: url(<tmpl_var icon.small default="^Extras('icon/cog.png');">);">
<a href="<tmpl_var url>" target="view"><tmpl_var title></a>
<TMPL_IF className>
<span onclick="window.admin.requestPlugin({ className : '<tmpl_var className>' })">
<tmpl_var title>
</span>
<TMPL_ELSE>
<a href="<tmpl_var url>" target="view"><tmpl_var title></a>
</TMPL_IF>
</li>
</TMPL_LOOP>
</ul>

View file

@ -0,0 +1,44 @@
package WebGUI::Admin::Plugin;
use Moose;
use Scalar::Util qw(blessed);
has 'id' => (
is => 'ro',
isa => 'Str',
required => 1,
);
has 'title' => (
is => 'rw',
isa => 'Str',
);
has 'icon' => (
is => 'rw',
isa => 'Str',
default => '', # Find a good default
);
has 'iconSmall' => (
is => 'rw',
isa => 'Str',
default => '', # Find a good default
);
sub BUILDARGS {
my ( $class, $session, %args ) = @_;
return { session => $session, %args };
}
sub canUse {
return 1;
}
sub getUrl {
my ( $self, $method, $params ) = @_;
$method ||= "view";
return '?op=admin;plugin=' . $self->id . ';method=' . $method . ';' . $params;
}
1;

View file

@ -49,9 +49,24 @@ sub handler {
if ( $session->form->get("op") eq "admin" ) {
if ( $session->form->get("plugin") ) {
# Load the requested plugin if necessary
# Default page is "view"
# Pass control to the right page
my $id = $session->form->get('id');
my $props = $session->config->get('adminConsole')->{ $id };
if ( !$props ) {
return "ERROR"; # die here
}
my $class = $props->{ className };
WebGUI::Pluggable::load( $class );
my $method = $session->form->get('method') || "view";
if ( $class->can( "www_" . $method ) ) {
return $class->can( "www_" . $method )->($session);
}
else {
return "ERROR"; # die here
}
}
else {
my $admin = WebGUI::Admin->new( $session );
@ -61,7 +76,7 @@ sub handler {
return $admin->can( "www_" . $method )->($admin);
}
else {
return $admin->www_view;
return "ERROR"; # die here
}
}
}

114
t/Admin.t Normal file
View file

@ -0,0 +1,114 @@
# vim:syntax=perl
#-------------------------------------------------------------------
# WebGUI is Copyright 2001-2009 Plain Black Corporation.
#-------------------------------------------------------------------
# Please read the legal notices (docs/legal.txt) and the license
# (docs/license.txt) that came with this distribution before using
# this software.
#------------------------------------------------------------------
# http://www.plainblack.com info@plainblack.com
#------------------------------------------------------------------
# Write a little about what this script tests.
#
#
use FindBin;
use strict;
use lib "$FindBin::Bin/lib";
use JSON;
use Test::More;
use Test::Deep;
use Monkey::Patch;
use WebGUI::Test; # Must use this before any other WebGUI modules
use WebGUI::Session;
use WebGUI::Test::Mechanize;
#----------------------------------------------------------------------------
# Init
# Create a new admin plugin
package WebGUI::Admin::Plugin::Test;
use Moose;
use base 'WebGUI::Admin::Plugin';
has '+title' => ( default => "title" );
has '+icon' => ( default => "icon" );
has '+iconSmall' => ( default => "iconSmall" );
has 'test_config' => ( is => 'rw', default => 'default' );
sub canView { return 1; }
sub process { return { message => 'success' } }
sub www_view { return "view" }
sub www_test { return "test" }
sub www_config { return $_[0]->test_config }
package main;
BEGIN { $INC{'WebGUI/Admin/Plugin/Test.pm'} = __FILE__; }
my $session = WebGUI::Test->session;
$session->user({ userId => 3 });
# Add a couple admin plugins to the config file
WebGUI::Test->originalConfig( "adminConsole" );
$session->config->addToHash('adminConsole', 'test', {
className => 'WebGUI::Admin::Plugin::Test',
} );
$session->config->addToHash('adminConsole', 'test2', {
url => '?op=admin;plugin=test;method=config',
} );
# Add some assets
my $snip = WebGUI::Asset->getImportNode( $session )->addChild( {
className => 'WebGUI::Asset::Snippet',
title => 'test',
groupIdEdit => '3',
} );
# Commit the tag
my $tag = WebGUI::VersionTag->getWorking( $session );
$tag->commit;
addToCleanup( $tag );
#----------------------------------------------------------------------------
# Tests
my $output;
# Test www_ methods
my $mech = WebGUI::Test::Mechanize->new( config => WebGUI::Test->config );
$mech->get('/'); # Start a session
$mech->session->user({ userId => '3' });
# www_processAssetHelper
$mech->get_ok( '/?op=admin;method=processAssetHelper;className=WebGUI::AssetHelper::Cut;assetId=' . $snip->getId );
cmp_deeply(
JSON->new->decode( $mech->content ),
WebGUI::AssetHelper::Cut->process( $snip ),
'www_processAssetHelper',
);
# www_processPlugin
$mech->get_ok( '/?op=admin;method=processPlugin;id=test' );
$output = $mech->content;
cmp_deeply(
JSON->new->decode( $output ),
WebGUI::Admin::Plugin::Test->process( $session ),
'Test plugin process()',
) || diag( $output );
# www_findUser
$mech->get_ok( '/?op=admin;method=findUser;query=Adm' );
$output = $mech->content;
cmp_deeply(
JSON->new->decode( $output ),
{ results => superbagof( superhashof( {
userId => 3,
} ) ) },
'found the Admin user',
) || diag( $output );
done_testing;
#vim:ft=perl