diff --git a/lib/WebGUI/Asset.pm b/lib/WebGUI/Asset.pm index 8baaad444..9147c8c77 100644 --- a/lib/WebGUI/Asset.pm +++ b/lib/WebGUI/Asset.pm @@ -528,7 +528,17 @@ sub fixTitle { =head2 fixUrl ( string ) -Returns a URL, removing invalid characters and making it unique. +Returns a URL, removing invalid characters and making it unique by +adding a digit to the end if necessary. URLs are not allowed to be +children of the extrasURL, the uploadsURL, or any defined passthruURL. +If not URL is passed, a URL will be constructed from the Asset's +parent and the menuTitle. + +Assets have a maximum length of 250 characters. Any URL longer than +250 characters will be truncated to the initial 220 characters. + +URLs will be passed through $session->url->urlize to make them WebGUI compliant. +That includes any languages specific constraints set up in the default language pack. =head3 string @@ -550,12 +560,20 @@ sub fixUrl { # fix urls used by uploads and extras # and those beginning with http - my @badUrls = ($self->session->config->get("extrasURL"), $self->session->config->get("uploadsURL")); - foreach my $badUrl (@badUrls) { + my @badUrls = ( + $self->session->config->get("extrasURL"), + $self->session->config->get("uploadsURL"), + ); + if (defined $self->session->config->get('passthruUrls')) { + push @badUrls, @{ $self->session->config->get('passthruUrls') }; + } + foreach my $badUrl (@badUrls) { + $badUrl =~ s{ / $ }{}x; # Remove trailing slashes from the end of the URL if ($badUrl =~ /^http/) { - $badUrl =~ s/^http.*\/(.*)$/$1/; - } else { - $badUrl =~ s/^\/(.*)/$1/; + $badUrl =~ s{ ^ http .* / } {}x; #Remove everything but the final path fragment from the badUrl + } + else { + $badUrl =~ s{ ^ / }{}x; #Remove leading slashes from bare URLs } if ($url =~ /^$badUrl/) { $url = "_".$url; @@ -581,17 +599,18 @@ sub fixUrl { } # check to see if the url already exists or not, and increment it if it does - if ($self->urlExists($self->session, $url, {assetId=>$self->getId})) { - my @parts = split(/\./,$url); - if ($parts[0] =~ /(.*)(\d+$)/) { - $parts[0] = $1.($2+1); - } else { - $parts[0] .= "2"; - } - $url = join(".",@parts); - $url = $self->fixUrl($url); + if ($self->urlExists($self->session, $url, {assetId=>$self->getId})) { + my @parts = split(/\./,$url); + if ($parts[0] =~ /(.*)(\d+$)/) { + $parts[0] = $1.($2+1); } - return $url; + else { + $parts[0] .= "2"; + } + $url = join(".",@parts); + $url = $self->fixUrl($url); + } + return $url; } diff --git a/lib/WebGUI/Session/Url.pm b/lib/WebGUI/Session/Url.pm index d92459dd7..e6705c219 100644 --- a/lib/WebGUI/Session/Url.pm +++ b/lib/WebGUI/Session/Url.pm @@ -140,7 +140,7 @@ sub extras { my $self = shift; my $path = shift; my $url = $self->session->config->get("extrasURL").'/'.$path; - $url =~ s$(? 32; # increment this value for each test you create +use Test::More tests => 39; # increment this value for each test you create use Test::MockObject; my $session = WebGUI::Test->session; @@ -147,4 +147,37 @@ $session->{_request} = $origRequest; # ################################################################ -is($importNode->fixUrl('1234'.'-'x250 . 'abcdefghijkl'), '1234'.'-'x216, 'fixUrl truncates to 220 characters'); +is($importNode->fixUrl('1234'.'-'x235 . 'abcdefghij'), '1234'.'-'x235 . 'abcdefghij', 'fixUrl leaves long URLs under 250 characters alone'); +is($importNode->fixUrl('1234'.'-'x250 . 'abcdefghij'), '1234'.'-'x216, 'fixUrl truncates long URLs over 250 characters to 220 characters'); + +my $origExtras = $session->config->get('extrasURL'); +my $origUploads = $session->config->get('uploadsURL'); +my $origPassthru = $session->config->get('passthruUrls'); + +$session->config->set('extrasURL', '/extras'); +$session->config->set('uploadsURL', '/uploads'); +$session->config->set('passthruUrls', [qw/pass1 pass2/]); + +is($importNode->fixUrl('/extras'), '_extras', 'underscore prepended to URLs that match the extrasURL'); +is($importNode->fixUrl('/uploads'), '_uploads', 'underscore prepended to URLs that match the uploadsURL'); +is($importNode->fixUrl('/pass1'), '_pass1', 'underscore prepended to URLs that match any passthruUrl 1'); +is($importNode->fixUrl('/pass2'), '_pass2', 'underscore prepended to URLs that match any passthruUrl 2'); + +#Now that we have verified that extrasURL and uploadsURL both work, just test one. +$session->config->set('extrasURL', '/extras1/'); +is($importNode->fixUrl('/extras1'), '_extras1', 'trailing underscore in extrasURL does not defeat the check'); + +$session->config->set('extrasURL', 'http://mysite.com/extras2'); +is($importNode->fixUrl('/extras2'), '_extras2', 'underscore prepended to URLs that match the extrasURL, even with http://'); + +END: { + + $session->config->set('extrasURL', $origExtras); + $session->config->set('uploadsURL', $origUploads); + if (defined $origPassthru) { + $session->config->set('passthruUrls', $origPassthru); + } + else { + $session->config->delete('passthruUrls'); + } +}