From 19bf1f4e8a32670b512cf76cc88842c7eb86626e Mon Sep 17 00:00:00 2001 From: Roy Johnson Date: Tue, 30 Jan 2007 15:34:43 +0000 Subject: [PATCH] fix - export fails with strange urls. Factored url to path translation out into a seperate method. Added beginnings of a smoke test for AssetExportHtml.pm. --- docs/changelog/7.x.x.txt | 4 +- lib/WebGUI/AssetExportHtml.pm | 114 ++++++++++++++++++++++++++++------ t/Asset/AssetExportHtml.t | 80 ++++++++++++++++++++++++ 3 files changed, 179 insertions(+), 19 deletions(-) create mode 100644 t/Asset/AssetExportHtml.t diff --git a/docs/changelog/7.x.x.txt b/docs/changelog/7.x.x.txt index 34e5c5ca9..a1b452448 100644 --- a/docs/changelog/7.x.x.txt +++ b/docs/changelog/7.x.x.txt @@ -5,7 +5,9 @@ - Fixed a typo in testEnvironment.pl version number for DBD::mysql - Fixed a Recurring Payments processing bug: illegal division by zero - fix: How to hose your assets (perlDreamer Consulting, LLC) - + - fix: Exporting fails with strange URLs. Also factored the URL to path/file + translation out into a seperate method so it can be more easily maintained + and tested. 7.3.8 - Fixed a template variable rewriting problem with HTML::Template::Expr diff --git a/lib/WebGUI/AssetExportHtml.pm b/lib/WebGUI/AssetExportHtml.pm index 7fa388149..04578a6b1 100644 --- a/lib/WebGUI/AssetExportHtml.pm +++ b/lib/WebGUI/AssetExportHtml.pm @@ -116,26 +116,14 @@ sub _exportAsHtml { next; } - my $path; - my $filename; - - if ($url =~ /\./) { - if ($url =~ /^(.*)\/(.*)$/) { - $path = $1; - $filename = $2; - if ($filename eq "") { - $filename = $path; - $path = undef; - } - } else { - $path = undef; - $filename = $url; - } - } else { - $path = $url; - $filename = $index; + my $pathData = $self->_translateUrlToPath($url, $index); + if (my $error = $pathData->{'error'}) { + return (0, $error); } + my $path = $pathData->{'path'}; + my $filename = $pathData->{'filename'}; + my $fullPath = (length($path)? "$path/" : "").$filename; if ($asset->getId eq $defaultAssetId) { $defaultAssetPath = $fullPath; @@ -215,6 +203,69 @@ sub _exportAsHtml { #------------------------------------------------------------------- +=head2 _translateUrlToPath ( url, index ) + +Translates a url into an appropriate path and filename for exporting + +=head3 url + +URL of the asset we need an export path for + +=head3 index + +index filename passed in from the UI + +=cut + +sub _translateUrlToPath { + my $self = shift; + my $url = shift; + my $index = shift; + my $dataRef; + + if ($url !~ m{\.}) { # If there is not a dot in the URL, this is easy + $dataRef->{'path'} = $url; + $dataRef->{'filename'} = $index; + } + elsif ($url =~ /^(.*)\/(.*)$/) { # If there is a dot and a slash in the url + my $dotCounter = 0; # Track how many dots we found + my $preSlash = $1; + my $postSlash = $2; + + if ($preSlash =~ /\./) { # webgui url index.html/foo becomes folder foo, filename index user specified + $dotCounter++; + $dataRef->{'path'} = $postSlash; + $dataRef->{'filename'} = $index; + } + + if ($postSlash =~ /\./) { # webgui url foo/page.html becomes folder foo, filename page.html + $dotCounter++; + $dataRef->{'path'} = $preSlash; + $dataRef->{'filename'} = $postSlash; + } + + if ($postSlash eq "") { # webgui url foo.html/ becomes no path, filename foo.html + $dataRef->{'path'} = undef; + $dataRef->{'filename'} = $preSlash; + } + + if ($dotCounter == 2) { # webgui url foo.html/page.html becomes an error because this is non-sensical + $self->session->errorHandler->error("Cannot generate path for url $url. Ambiguious."); + $dataRef->{'path'} = undef; + $dataRef->{'filename'} = undef; + $dataRef->{'error'} = "Cannot generate path for url $url. Ambiguious."; + } + } + else { # No slash in the url and no dots + $dataRef->{'path'} = undef; + $dataRef->{'filename'} = $url; # webgui url foo.html becomes filename foo.html + } + + return $dataRef; +} + +#------------------------------------------------------------------- + =head2 exportAsHtml Same as www_exportGenerate except without the output. Returns @@ -355,4 +406,31 @@ sub www_exportGenerate { return; } + + + +# if ($url =~ /\./) { # If the URL has a dot in it somewhere (i.e., /foo/index.html) +# if ($url =~ /^(.*)\/(.*)$/) { +# $path = $1; # part before the slash is "path" +# $filename = $2; # part after the slash is "filename" (becomes folder foo, file index.html) +# if ($path =~ /\./) { # unless of course the dot is in the path (i.e., /index.html/foo) +# $filename = $index; # then we want to use index as the filename +# $path = $2; # and filename as the path (becomes folder foo, file index.html or whatever they set index to) +# } +# if ($filename eq "") { # If filename is blank, use path as filename and set path to null +# $filename = $path; +# $path = undef; +# } +# } else { # If filename is *not* blank, discard the path +# $path = undef; +# $filename = $url; # and set the filename equal to the url +# } +# } else { # No dot in the url, use the url as the path +# $path = $url; +# $filename = $index; # and make the filename whatever the user specifies for the index filename. +# } + + + + 1; diff --git a/t/Asset/AssetExportHtml.t b/t/Asset/AssetExportHtml.t new file mode 100644 index 000000000..0fae3e3c8 --- /dev/null +++ b/t/Asset/AssetExportHtml.t @@ -0,0 +1,80 @@ +#------------------------------------------------------------------- +# WebGUI is Copyright 2001-2006 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 +#------------------------------------------------------------------- + +use FindBin; +use strict; +use lib "$FindBin::Bin/../lib"; +use WebGUI::Test; +use WebGUI::Session; + +# load your modules here + +use Test::More; + +my $session = WebGUI::Test->session; + +## Test the url to path/file translation +my $index = "home.html"; +my $urlMap = { + 'index.html' => { + path => undef, + filename => 'index.html', + }, + + + 'index.html/' => { + path => undef, + filename => 'index.html', + }, + + 'foo/page.html' => { + path => 'foo', + filename => 'page.html', + }, + + + 'foo' => { + path => 'foo', + filename => $index, + }, + + 'index.html/foo' => { + path => 'foo', + filename => $index, + }, + + 'index.html/foo.html' => { + path => undef, + filename => undef, + }, +}; + +# two tests for each key in the urlMap hashRef plus any other tests that are added later. +plan tests => (0 + scalar(keys %{$urlMap}) * 2); + +foreach my $urlToTest ( keys %{$urlMap} ) { + my $expectedPath = $urlMap->{$urlToTest}->{'path'}; + my $expectedFilename = $urlMap->{$urlToTest}->{'filename'}; + + # we need a dummy asset to test this private method. + my $asset = WebGUI::Asset->newByPropertyHashRef($session,{className=>'WebGUI::Asset'}); + + # test this url + my $dataRef = $asset->_translateUrlToPath($urlToTest, $index); + my $returnedPath = $dataRef->{'path'}; + my $returnedFilename = $dataRef->{'filename'}; + + is ($returnedPath, $expectedPath, "path $expectedPath was returned for url $urlToTest"); + is ($returnedFilename, $expectedFilename, "filename $expectedFilename was returned for url $urlToTest"); +} + +## Test something else + +