From 8650e714b509bcb668e721f803f462ac912463a9 Mon Sep 17 00:00:00 2001 From: Doug Bell Date: Wed, 23 Feb 2011 16:36:34 -0600 Subject: [PATCH] move createShortcut to an assetHelper --- lib/WebGUI/Asset.pm | 4 ++ lib/WebGUI/AssetHelper/CreateShortcut.pm | 82 ++++++++++++++++++++++++ lib/WebGUI/i18n/English/WebGUI.pm | 6 ++ t/AssetHelper/CreateShortcut.t | 73 +++++++++++++++++++++ 4 files changed, 165 insertions(+) create mode 100644 lib/WebGUI/AssetHelper/CreateShortcut.pm create mode 100644 t/AssetHelper/CreateShortcut.t diff --git a/lib/WebGUI/Asset.pm b/lib/WebGUI/Asset.pm index e73dec470..e3e55b7c9 100644 --- a/lib/WebGUI/Asset.pm +++ b/lib/WebGUI/Asset.pm @@ -1204,6 +1204,10 @@ sub getHelpers { class => 'WebGUI::AssetHelper::CopyBranch', label => 'Copy Branch', }, + { + class => 'WebGUI::AssetHelper::CreateShortcut', + label => 'Create Shortcut', + }, { class => 'WebGUI::AssetHelper::Cut', label => 'Cut', diff --git a/lib/WebGUI/AssetHelper/CreateShortcut.pm b/lib/WebGUI/AssetHelper/CreateShortcut.pm new file mode 100644 index 000000000..2de7a4923 --- /dev/null +++ b/lib/WebGUI/AssetHelper/CreateShortcut.pm @@ -0,0 +1,82 @@ +package WebGUI::AssetHelper::CreateShortcut; + +use strict; +use Class::C3; +use base qw/WebGUI::AssetHelper/; + +=head1 LEGAL + + ------------------------------------------------------------------- + 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 + ------------------------------------------------------------------- + +=head1 NAME + +Package WebGUI::AssetHelper::CreateShortcut + +=head1 DESCRIPTION + +Create a shortcut to the asset and put it on the clipboard + +=head1 METHODS + +These methods are available from this class: + +=cut + +#------------------------------------------------------------------- + +=head2 process ( $class, $asset ) + +Create a shortcut to the asset on the clipboard. + +=cut + +sub process { + my ($class, $asset) = @_; + my $session = $asset->session; + my $i18n = WebGUI::International->new( $session, 'WebGUI' ); + + return { error => $i18n->get('39') } if !$asset->canView; + my $import = WebGUI::Asset->getImportNode( $session ); + my $tag = WebGUI::VersionTag->getWorking( $session ); + my $child = $import->addChild({ + tagId => $tag->getId, + status => 'pending', + className => 'WebGUI::Asset::Shortcut', + shortcutToAssetId => $asset->getId, + title => $asset->getTitle, + menuTitle => $asset->getMenuTitle, + isHidden => $asset->isHidden, + newWindow => $asset->newWindow, + ownerUserId => $asset->ownerUserId, + groupIdEdit => $asset->groupIdEdit, + groupIdView => $asset->groupIdView, + url => $asset->title, + templateId => 'PBtmpl0000000000000140', + }); + + $child->cut; + + if (WebGUI::VersionTag->autoCommitWorkingIfEnabled($session, { + allowComments => 1, + returnUrl => $asset->getUrl, + }) eq 'redirect') { + return { + message => $i18n->get('shortcut created'), + redirect => $session->request->location, + }; + }; + + return { + message => $i18n->get('shortcut created'), + }; +} + +1; diff --git a/lib/WebGUI/i18n/English/WebGUI.pm b/lib/WebGUI/i18n/English/WebGUI.pm index b2dc7373c..c501792f3 100644 --- a/lib/WebGUI/i18n/English/WebGUI.pm +++ b/lib/WebGUI/i18n/English/WebGUI.pm @@ -4720,6 +4720,12 @@ Users may override this setting in their profile. lastUpdated => 0, context => q{Label for button to log out.}, }, + + 'shortcut created' => { + message => q{Shortcut created}, + lastUpdated => 0, + context => q{Message after a shortcut is created successfully}, + }, }; 1; diff --git a/t/AssetHelper/CreateShortcut.t b/t/AssetHelper/CreateShortcut.t new file mode 100644 index 000000000..904de43c4 --- /dev/null +++ b/t/AssetHelper/CreateShortcut.t @@ -0,0 +1,73 @@ +# 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 strict; +use Test::More; +use Test::Deep; +use WebGUI::Test; # Must use this before any other WebGUI modules +use WebGUI::Session; +use WebGUI::Asset; +use WebGUI::AssetHelper::CreateShortcut; +use WebGUI::Test::Mechanize; + +#---------------------------------------------------------------------------- +# Init +my $session = WebGUI::Test->session; + + +#---------------------------------------------------------------------------- +# Tests + +my $output; +my $import = WebGUI::Asset->getImportNode($session); + +my $priv_page = WebGUI::Test->asset( groupIdView => '3' ); +$session->user({userId => 1}); +$output = WebGUI::AssetHelper::CreateShortcut->process($priv_page); +cmp_deeply( + $output, + { + error => re('You do not have sufficient privileges'), + }, + 'AssetHelper/CreateShortcut checks for editing privileges' +); + +$session->setting->set( versionTagMode => 'autoCommit' ); +$session->setting->set( skipCommitComments => '1' ); +$session->user({userId => 3}); +my $safe_page = WebGUI::Test->asset; +$output = WebGUI::AssetHelper::CreateShortcut->process($safe_page); +cmp_deeply( + $output, + { + message => re( '.' ), # message exists + }, + 'AssetHelper/CreateShortcut returns a message' +); + +my $shortcutId = $session->db->quickScalar( + 'SELECT assetId FROM Shortcut WHERE shortcutToAssetId=?', + [ $safe_page->getId ], +); + +ok( $shortcutId, 'shortcut exists' ); +ok( my $shortcut = WebGUI::Asset->newById( $session, $shortcutId ), 'can be instanced' ); +WebGUI::Test::addToCleanup( $shortcut ); +is( $shortcut->state, 'clipboard', 'is on clipboard' ); +is( $shortcut->status, 'approved', 'was auto-committed' ); + +done_testing(); + +#vim:ft=perl