Refactor Session/Icon to remove a bunch of copy/paste code. Update test to remove deprecated profileField calls.

This commit is contained in:
Colin Kuskie 2011-12-28 11:11:21 -08:00
parent a20dd83ae0
commit b8e088ff7a
2 changed files with 54 additions and 95 deletions

View file

@ -75,6 +75,30 @@ sub getBaseURL {
} }
#-------------------------------------------------------------------
=head2 _basic ( )
Returns a basic icon with i18n title and alt text.
=cut
sub _basic {
my $self = shift;
my $i18n_tag = shift;
my $icon_name = shift;
my $url_params = shift;
my $pageURL = shift || $self->session->url->getRequestedUrl;
my $i18n = WebGUI::International->new($self->session,'Icon');
my $tag = $i18n->get($i18n_tag);
my $output = '<span class="toolbarIcon" style="vertical-align:middle;"><a href="'
. $self->session->url->gateway($pageURL,$url_params)
. '">';
$output .= '<img src="'.$self->getBaseURL().$icon_name.'" style="vertical-align:middle;border: 0px;" alt="'.$tag.'" title="'.$tag.'" /></a></span>';
return $output;
}
#------------------------------------------------------------------- #-------------------------------------------------------------------
=head2 copy ( urlParameters [, pageURL ] ) =head2 copy ( urlParameters [, pageURL ] )
@ -93,13 +117,8 @@ file will be prepended to it.
=cut =cut
sub copy { sub copy {
my $self = shift; my $self = shift;
my $urlParams = shift; return $self->_basic('Copy', 'copy.gif', @_);
my $pageURL = shift || $self->session->url->getRequestedUrl;
my $i18n = WebGUI::International->new($self->session,'Icon');
my $output = '<span class="toolbarIcon" style="vertical-align:middle;"><a href="'.$self->session->url->gateway($pageURL,$urlParams).'">';
$output .= '<img src="'.$self->getBaseURL().'copy.gif" style="vertical-align:middle;border: 0px;" alt="'.$i18n->get('Copy').'" title="'.$i18n->get('Copy').'" /></a></span>';
return $output;
} }
#------------------------------------------------------------------- #-------------------------------------------------------------------
@ -120,13 +139,8 @@ file will be prepended to it.
=cut =cut
sub cut { sub cut {
my $self = shift; my $self = shift;
my $urlParams = shift; return $self->_basic('Cut', 'cut.gif', @_);
my $pageURL = shift || $self->session->url->getRequestedUrl;
my $i18n = WebGUI::International->new($self->session,'Icon');
my $output = '<span class="toolbarIcon" style="vertical-align:middle;"><a href="'.$self->session->url->gateway($pageURL,$urlParams).'">';
$output .= '<img src="'.$self->getBaseURL().'cut.gif" style="vertical-align:middle;border: 0px;" alt="'.$i18n->get('Cut').'" title="'.$i18n->get('Cut').'" /></a></span>';
return $output;
} }
#------------------------------------------------------------------- #-------------------------------------------------------------------
@ -202,13 +216,8 @@ file will be prepended to it.
=cut =cut
sub edit { sub edit {
my $self = shift; my $self = shift;
my $urlParams = shift; return $self->_basic('Edit', 'edit.gif', @_);
my $pageURL = shift || $self->session->url->getRequestedUrl;
my $i18n = WebGUI::International->new($self->session,'Icon');
my $output = '<span class="toolbarIcon" style="vertical-align:middle;"><a href="'.$self->session->url->gateway($pageURL,$urlParams).'">';
$output .= '<img src="'.$self->getBaseURL().'edit.gif" style="vertical-align:middle;border: 0px;" alt="'.$i18n->get('Edit').'" title="'.$i18n->get('Edit').'" /></a></span>';
return $output;
} }
#------------------------------------------------------------------- #-------------------------------------------------------------------
@ -230,13 +239,8 @@ file will be prepended to it.
=cut =cut
sub export { sub export {
my $self = shift; my $self = shift;
my $urlParams = shift; return $self->_basic('Export', 'export.gif', @_);
my $pageURL = shift || $self->session->url->getRequestedUrl;
my $i18n = WebGUI::International->new($self->session,'Icon');
my $output = '<span class="toolbarIcon" style="vertical-align:middle;"><a href="'.$self->session->url->gateway($pageURL,$urlParams).'">';
$output .= '<img src="'.$self->getBaseURL().'export.gif" style="vertical-align:middle;border: 0px;" alt="'.$i18n->get('Export').'" title="'.$i18n->get('Export').'" /></a></span>';
return $output;
} }
#------------------------------------------------------------------- #-------------------------------------------------------------------
@ -283,13 +287,8 @@ file will be prepended to it.
=cut =cut
sub locked { sub locked {
my $self = shift; my $self = shift;
my $urlParams = shift; return $self->_basic('locked', 'locked.gif', @_);
my $pageURL = shift || $self->session->url->getRequestedUrl;
my $i18n = WebGUI::International->new($self->session,'Icon');
my $output = '<span class="toolbarIcon" style="vertical-align:middle;"><a href="'.$self->session->url->gateway($pageURL,$urlParams).'">';
$output .= '<img src="'.$self->getBaseURL().'locked.gif" style="vertical-align:middle;border: 0px;" alt="'.$i18n->get('locked').'" title="'.$i18n->get('locked').'" /></a></span>';
return $output;
} }
#------------------------------------------------------------------- #-------------------------------------------------------------------
@ -311,13 +310,8 @@ file will be prepended to it.
=cut =cut
sub manage { sub manage {
my $self = shift; my $self = shift;
my $urlParams = shift; return $self->_basic('Manage', 'manage.gif', @_);
my $pageURL = shift || $self->session->url->getRequestedUrl;
my $i18n = WebGUI::International->new($self->session,'Icon');
my $output = '<span class="toolbarIcon" style="vertical-align:middle;"><a href="'.$self->session->url->gateway($pageURL,$urlParams).'">';
$output .= '<img src="'.$self->getBaseURL().'manage.gif" style="vertical-align:middle;border: 0px;" alt="'.$i18n->get('Manage').'" title="'.$i18n->get('Manage').'" /></a></span>';
return $output;
} }
#------------------------------------------------------------------- #-------------------------------------------------------------------
@ -339,13 +333,8 @@ file will be prepended to it.
=cut =cut
sub moveBottom { sub moveBottom {
my $self = shift; my $self = shift;
my $urlParams = shift; return $self->_basic('Move To Bottom', 'moveBotom.gif', @_);
my $pageURL = shift || $self->session->url->getRequestedUrl;
my $i18n = WebGUI::International->new($self->session,'Icon');
my $output = '<span class="toolbarIcon" style="vertical-align:middle;"><a href="'.$self->session->url->gateway($pageURL,$urlParams).'">';
$output .= '<img src="'.$self->getBaseURL().'moveBottom.gif" style="vertical-align:middle;border: 0px;" alt="'.$i18n->get('Move To Bottom').'" title="'.$i18n->get('Move To Bottom').'" /></a></span>';
return $output;
} }
#------------------------------------------------------------------- #-------------------------------------------------------------------
@ -403,13 +392,8 @@ file will be prepended to it.
=cut =cut
sub moveLeft { sub moveLeft {
my $self = shift; my $self = shift;
my $urlParams = shift; return $self->_basic('Move Left', 'moveLeft.gif', @_);
my $pageURL = shift || $self->session->url->getRequestedUrl;
my $i18n = WebGUI::International->new($self->session,'Icon');
my $output = '<span class="toolbarIcon" style="vertical-align:middle;"><a href="'.$self->session->url->gateway($pageURL,$urlParams).'">';
$output .= '<img src="'.$self->getBaseURL().'moveLeft.gif" style="vertical-align:middle;border: 0px;" alt="'.$i18n->get('Move Left').'" title="'.$i18n->get('Move Left').'" /></a></span>';
return $output;
} }
#------------------------------------------------------------------- #-------------------------------------------------------------------
@ -431,13 +415,8 @@ file will be prepended to it.
=cut =cut
sub moveRight { sub moveRight {
my $self = shift; my $self = shift;
my $urlParams = shift; return $self->_basic('Move Right', 'moveRight.gif', @_);
my $pageURL = shift || $self->session->url->getRequestedUrl;
my $i18n = WebGUI::International->new($self->session,'Icon');
my $output = '<span class="toolbarIcon" style="vertical-align:middle;"><a href="'.$self->session->url->gateway($pageURL,$urlParams).'">';
$output .= '<img src="'.$self->getBaseURL().'moveRight.gif" style="vertical-align:middle;border: 0px;" alt="'.$i18n->get('Move Right').'" title="'.$i18n->get('Move Right').'" /></a></span>';
return $output;
} }
#------------------------------------------------------------------- #-------------------------------------------------------------------
@ -459,13 +438,8 @@ file will be prepended to it.
=cut =cut
sub moveTop { sub moveTop {
my $self = shift; my $self = shift;
my $urlParams = shift; return $self->_basic('Move To Top', 'moveTop.gif', @_);
my $pageURL = shift || $self->session->url->getRequestedUrl;
my $i18n = WebGUI::International->new($self->session,'Icon');
my $output = '<span class="toolbarIcon" style="vertical-align:middle;"><a href="'.$self->session->url->gateway($pageURL,$urlParams).'">';
$output .= '<img src="'.$self->getBaseURL().'moveTop.gif" style="vertical-align:middle;border: 0px;" alt="'.$i18n->get('Move To Top').'" title="'.$i18n->get('Move To Top').'" /></a></span>';
return $output;
} }
#------------------------------------------------------------------- #-------------------------------------------------------------------
@ -559,13 +533,8 @@ file will be prepended to it.
=cut =cut
sub shortcut { sub shortcut {
my $self = shift; my $self = shift;
my $urlParams = shift; return $self->_basic('Create Shortcut', 'shortcut.gif', @_);
my $pageURL = shift || $self->session->url->getRequestedUrl;
my $i18n = WebGUI::International->new($self->session,'Icon');
my $output = '<span class="toolbarIcon" style="vertical-align:middle;"><a href="'.$self->session->url->gateway($pageURL,$urlParams).'">';
$output .= '<img src="'.$self->getBaseURL().'shortcut.gif" style="vertical-align:middle;border: 0px;" alt="'.$i18n->get('Create Shortcut').'" title="'.$i18n->get('Create Shortcut').'" /></a></span>';
return $output;
} }
#------------------------------------------------------------------- #-------------------------------------------------------------------
@ -587,13 +556,8 @@ file will be prepended to it.
=cut =cut
sub view { sub view {
my $self = shift; my $self = shift;
my $urlParams = shift; return $self->_basic('View', 'view.gif', @_);
my $pageURL = shift || $self->session->url->getRequestedUrl;
my $i18n = WebGUI::International->new($self->session,'Icon');
my $output = '<span class="toolbarIcon" style="vertical-align:middle;"><a href="'.$self->session->url->gateway($pageURL,$urlParams).'">';
$output .= '<img src="'.$self->getBaseURL().'view.gif" style="vertical-align:middle;border: 0px;" alt="'.$i18n->get('View').'" title="'.$i18n->get('View').'" /></a></span>';
return $output;
} }

View file

@ -18,14 +18,8 @@ use Test::Deep;
use Test::MockObject; use Test::MockObject;
use HTML::TokeParser; use HTML::TokeParser;
my $numTests = 10;
my @iconTests = fetchTestSet(); my @iconTests = fetchTestSet();
$numTests += scalar(@iconTests) * 4;
plan tests => $numTests;
my $session = WebGUI::Test->session; my $session = WebGUI::Test->session;
#################################################### ####################################################
@ -36,8 +30,8 @@ my $session = WebGUI::Test->session;
# #
#################################################### ####################################################
my $origToolbar = $session->user->profileField('toolbar'); my $origToolbar = $session->user->get('toolbar');
WebGUI::Test->addToCleanup(sub { $session->user->profileField('toolbar', $origToolbar); },); WebGUI::Test->addToCleanup(sub { $session->user->update('toolbar', $origToolbar); },);
my $toolbars = $session->url->extras('toolbar/'); my $toolbars = $session->url->extras('toolbar/');
my $request = $session->request; my $request = $session->request;
@ -52,13 +46,13 @@ my $i18n = WebGUI::International->new($session, 'Icon');
# #
#################################################### ####################################################
$session->user->profileField('toolbar', 'useLanguageDefault'); $session->user->update('toolbar', 'useLanguageDefault');
is($session->icon->getBaseURL, $toolbars.'bullet/', 'getBaseUrl: default English toolbar is bullet'); is($session->icon->getBaseURL, $toolbars.'bullet/', 'getBaseUrl: default English toolbar is bullet');
$session->user->profileField('toolbar', 'mullet'); $session->user->update('toolbar', 'mullet');
is($session->icon->getBaseURL, $toolbars.'mullet/', 'getBaseUrl: fetch user preference of mullet toolbar'); is($session->icon->getBaseURL, $toolbars.'mullet/', 'getBaseUrl: fetch user preference of mullet toolbar');
$session->user->profileField('toolbar', $origToolbar); $session->user->update('toolbar', $origToolbar);
#################################################### ####################################################
# #
@ -154,6 +148,7 @@ SKIP: {
cmp_bag(\@toolbarDirs, \@toolbarOptionDirs, 'getToolbarOptions'); cmp_bag(\@toolbarDirs, \@toolbarOptionDirs, 'getToolbarOptions');
} }
done_testing;
sub linkAndText { sub linkAndText {
my ($text, $tag, @params) = @_; my ($text, $tag, @params) = @_;