From: Tim Starling Date: Mon, 11 Jun 2007 14:46:18 +0000 (+0000) Subject: Fixed some errant double-slashes appearing in file paths. Added unit tests for the... X-Git-Tag: 1.31.0-rc.0~52580 X-Git-Url: http://git.cyclocoop.org/%22%2C%20generer_url_ecrire%28?a=commitdiff_plain;h=cc5611979694620cfd68cfe1fdc9877d70049f8f;p=lhc%2Fweb%2Fwiklou.git Fixed some errant double-slashes appearing in file paths. Added unit tests for the path and URL functions. --- diff --git a/includes/filerepo/FSRepo.php b/includes/filerepo/FSRepo.php index c33df9464f..dac3500220 100644 --- a/includes/filerepo/FSRepo.php +++ b/includes/filerepo/FSRepo.php @@ -318,7 +318,7 @@ class FSRepo { /** * Get a relative path including trailing slash, e.g. f/fa/ - * If the repo is not hashed, returns a slash + * If the repo is not hashed, returns an empty string */ function getHashPath( $name ) { if ( $this->isHashed() ) { @@ -329,7 +329,7 @@ class FSRepo { } return $path; } else { - return '/'; + return ''; } } diff --git a/includes/filerepo/File.php b/includes/filerepo/File.php index 35eeac93fe..35ce4c96c9 100644 --- a/includes/filerepo/File.php +++ b/includes/filerepo/File.php @@ -669,8 +669,10 @@ class File { /** Get the path of the archive directory, or a particular file if $suffix is specified */ function getArchivePath( $suffix = false ) { $path = $this->repo->getZonePath('public') . '/archive/' . $this->getHashPath(); - if ( $suffix !== false ) { - $path .= '/' . $suffix; + if ( $suffix === false ) { + $path = substr( $path, 0, -1 ); + } else { + $path .= $suffix; } return $path; } @@ -687,8 +689,10 @@ class File { /** Get the URL of the archive directory, or a particular file if $suffix is specified */ function getArchiveUrl( $suffix = false ) { $path = $this->repo->getZoneUrl('public') . '/archive/' . $this->getHashPath(); - if ( $suffix !== false ) { - $path .= '/' . urlencode( $suffix ); + if ( $suffix === false ) { + $path = substr( $path, 0, -1 ); + } else { + $path .= urlencode( $suffix ); } return $path; } @@ -705,15 +709,17 @@ class File { /** Get the virtual URL for an archive file or directory */ function getArchiveVirtualUrl( $suffix = false ) { $path = $this->repo->getVirtualUrl() . '/public/archive/' . $this->getHashPath(); - if ( $suffix !== false ) { - $path .= '/' . urlencode( $suffix ); + if ( $suffix === false ) { + $path = substr( $path, 0, -1 ); + } else { + $path .= urlencode( $suffix ); } return $path; } /** Get the virtual URL for a thumbnail file or directory */ function getThumbVirtualUrl( $suffix = false ) { - $path = $this->repo->getVirtualUrl() . '/public/thumb/' . $this->getHashPath(); + $path = $this->repo->getVirtualUrl() . '/public/thumb/' . $this->getUrlRel(); if ( $suffix !== false ) { $path .= '/' . urlencode( $suffix ); } diff --git a/tests/LocalFileTest.php b/tests/LocalFileTest.php new file mode 100644 index 0000000000..7f0853bf29 --- /dev/null +++ b/tests/LocalFileTest.php @@ -0,0 +1,90 @@ + 'test', + 'directory' => '/testdir', + 'url' => '/testurl', + 'hashLevels' => 2, + 'transformVia404' => false, + ); + $this->repo_hl0 = new LocalRepo( array( 'hashLevels' => 0 ) + $info ); + $this->repo_hl2 = new LocalRepo( array( 'hashLevels' => 2 ) + $info ); + $this->repo_lc = new LocalRepo( array( 'initialCapital' => false ) + $info ); + $this->file_hl0 = $this->repo_hl0->newFile( 'test!' ); + $this->file_hl2 = $this->repo_hl2->newFile( 'test!' ); + $this->file_lc = $this->repo_lc->newFile( 'test!' ); + } + + function testGetHashPath() { + $this->assertEquals( '', $this->file_hl0->getHashPath() ); + $this->assertEquals( 'a/a2/', $this->file_hl2->getHashPath() ); + $this->assertEquals( 'c/c4/', $this->file_lc->getHashPath() ); + } + + function testGetRel() { + $this->assertEquals( 'Test!', $this->file_hl0->getRel() ); + $this->assertEquals( 'a/a2/Test!', $this->file_hl2->getRel() ); + $this->assertEquals( 'c/c4/test!', $this->file_lc->getRel() ); + } + + function testGetUrlRel() { + $this->assertEquals( 'Test%21', $this->file_hl0->getUrlRel() ); + $this->assertEquals( 'a/a2/Test%21', $this->file_hl2->getUrlRel() ); + $this->assertEquals( 'c/c4/test%21', $this->file_lc->getUrlRel() ); + } + + function testGetArchivePath() { + $this->assertEquals( '/testdir/archive', $this->file_hl0->getArchivePath() ); + $this->assertEquals( '/testdir/archive/a/a2', $this->file_hl2->getArchivePath() ); + $this->assertEquals( '/testdir/archive/!', $this->file_hl0->getArchivePath( '!' ) ); + $this->assertEquals( '/testdir/archive/a/a2/!', $this->file_hl2->getArchivePath( '!' ) ); + } + + function testGetThumbPath() { + $this->assertEquals( '/testdir/thumb/Test!', $this->file_hl0->getThumbPath() ); + $this->assertEquals( '/testdir/thumb/a/a2/Test!', $this->file_hl2->getThumbPath() ); + $this->assertEquals( '/testdir/thumb/Test!/x', $this->file_hl0->getThumbPath( 'x' ) ); + $this->assertEquals( '/testdir/thumb/a/a2/Test!/x', $this->file_hl2->getThumbPath( 'x' ) ); + } + + function testGetArchiveUrl() { + $this->assertEquals( '/testurl/archive', $this->file_hl0->getArchiveUrl() ); + $this->assertEquals( '/testurl/archive/a/a2', $this->file_hl2->getArchiveUrl() ); + $this->assertEquals( '/testurl/archive/%21', $this->file_hl0->getArchiveUrl( '!' ) ); + $this->assertEquals( '/testurl/archive/a/a2/%21', $this->file_hl2->getArchiveUrl( '!' ) ); + } + + function testGetThumbUrl() { + $this->assertEquals( '/testurl/thumb/Test%21', $this->file_hl0->getThumbUrl() ); + $this->assertEquals( '/testurl/thumb/a/a2/Test%21', $this->file_hl2->getThumbUrl() ); + $this->assertEquals( '/testurl/thumb/Test%21/x', $this->file_hl0->getThumbUrl( 'x' ) ); + $this->assertEquals( '/testurl/thumb/a/a2/Test%21/x', $this->file_hl2->getThumbUrl( 'x' ) ); + } + + function testGetArchiveVirtualUrl() { + $this->assertEquals( 'mwrepo:///public/archive', $this->file_hl0->getArchiveVirtualUrl() ); + $this->assertEquals( 'mwrepo:///public/archive/a/a2', $this->file_hl2->getArchiveVirtualUrl() ); + $this->assertEquals( 'mwrepo:///public/archive/%21', $this->file_hl0->getArchiveVirtualUrl( '!' ) ); + $this->assertEquals( 'mwrepo:///public/archive/a/a2/%21', $this->file_hl2->getArchiveVirtualUrl( '!' ) ); + } + + function testGetThumbVirtualUrl() { + $this->assertEquals( 'mwrepo:///public/thumb/Test%21', $this->file_hl0->getThumbVirtualUrl() ); + $this->assertEquals( 'mwrepo:///public/thumb/a/a2/Test%21', $this->file_hl2->getThumbVirtualUrl() ); + $this->assertEquals( 'mwrepo:///public/thumb/Test%21/%21', $this->file_hl0->getThumbVirtualUrl( '!' ) ); + $this->assertEquals( 'mwrepo:///public/thumb/a/a2/Test%21/%21', $this->file_hl2->getThumbVirtualUrl( '!' ) ); + } + + function testGetUrl() { + $this->assertEquals( '/testurl/Test%21', $this->file_hl0->getUrl() ); + $this->assertEquals( '/testurl/a/a2/Test%21', $this->file_hl2->getUrl() ); + } +} + +?>