From 7298af49ea0f41897f50bb0b89a2a5ae42a675dd Mon Sep 17 00:00:00 2001 From: Colin Kuskie Date: Wed, 25 Nov 2009 16:32:52 -0800 Subject: [PATCH] Copy Asset Helper (self only). --- lib/WebGUI/AssetHelper/Copy.pm | 82 ++++++++++++++++++++++++++++++++ lib/WebGUI/i18n/English/Asset.pm | 6 +++ t/AssetHelper/Copy.t | 73 ++++++++++++++++++++++++++++ 3 files changed, 161 insertions(+) create mode 100644 lib/WebGUI/AssetHelper/Copy.pm create mode 100644 t/AssetHelper/Copy.t diff --git a/lib/WebGUI/AssetHelper/Copy.pm b/lib/WebGUI/AssetHelper/Copy.pm new file mode 100644 index 000000000..a5363a7d6 --- /dev/null +++ b/lib/WebGUI/AssetHelper/Copy.pm @@ -0,0 +1,82 @@ +package WebGUI::AssetHelper::Copy; + +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::Copy + +=head1 DESCRIPTION + +Copy an Asset to the Clipboard, with no children. + +=head1 METHODS + +These methods are available from this class: + +=cut + +#------------------------------------------------------------------- + +=head2 duplicate ( $class, $asset ) + +Duplicates the asset. Extracted out so that it can be subclassed by copy with children, +and copy with descendants. + +=cut + +sub duplicate { + my ($class, $asset) = @_; + return $asset->duplicate; +} + +#------------------------------------------------------------------- + +=head2 process ( $class, $asset ) + +Copies the asset to the clipboard. There are no privilege or safety checks, since all operations +are done on the copy. + +=cut + +sub process { + my ($class, $asset) = @_; + my $session = $asset->session; + my $i18n = WebGUI::International->new($session, 'Asset'); + + my $newAsset = $class->duplicate($asset); + $newAsset->update({ title=>sprintf("%s (%s)",$asset->getTitle,$i18n->get('copy'))}); + $newAsset->cut; + + my $message = sprintf($i18n->get('copied asset'), $asset->getTitle); + + my $payload = { + message => $message, + }; + + if (WebGUI::VersionTag->autoCommitWorkingIfEnabled($session, { + allowComments => 1, + returnUrl => $asset->getUrl, + }) eq 'redirect') { + return undef; + $payload->{open_tab} = $session->http->getRedirectLocation; + }; + + return $payload; +} + +1; diff --git a/lib/WebGUI/i18n/English/Asset.pm b/lib/WebGUI/i18n/English/Asset.pm index a0a412ce9..128acc023 100644 --- a/lib/WebGUI/i18n/English/Asset.pm +++ b/lib/WebGUI/i18n/English/Asset.pm @@ -1376,6 +1376,12 @@ Couldn't open %-s because %-s
context => q{%s will be replaced by the title of the asset.}, }, + 'copied asset' => { + message => q{Asset %s was copied to the clipboard.}, + lastUpdated => 0, + context => q{%s will be replaced by the title of the asset.}, + }, + }; 1; diff --git a/t/AssetHelper/Copy.t b/t/AssetHelper/Copy.t new file mode 100644 index 000000000..e8d66755f --- /dev/null +++ b/t/AssetHelper/Copy.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 FindBin; +use strict; +use lib "$FindBin::Bin/../lib"; +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::Copy; + +#---------------------------------------------------------------------------- +# Init +my $session = WebGUI::Test->session; + + +#---------------------------------------------------------------------------- +# Tests + +plan tests => 3; # Increment this number for each test you create + +#---------------------------------------------------------------------------- +# put your tests here + +my $output; +my $home = WebGUI::Asset->getDefault($session); + +$output = WebGUI::AssetHelper::Copy->process($home); +cmp_deeply( + $output, + { + message => re('was copied to the clipboard'), + }, + 'AssetHelper/Copy redirects the back to the copied asset' +); + +my $clippies = $home->getAssetsInClipboard(); +is @{ $clippies }, 1, '... only copied 1 asset to the clipboard, no children'; + +foreach my $asset (@{ $clippies }) { + $asset->purge; +} + +$session->setting->set('skipCommitComments', 0); + +$output = WebGUI::AssetHelper::Copy->process($home); +cmp_deeply( + $output, + { + message => re('was copied to the clipboard'), + open_tab => $home->getUrl, + }, + 'AssetHelper/Copy opens a tab for commit comments' +); + +foreach my $asset (@{ $clippies }) { + $asset->purge; +} +#vim:ft=perl