summaryrefslogtreecommitdiff
path: root/modules
diff options
context:
space:
mode:
authorBharat Mediratta <bharat@menalto.com>2013-01-18 13:35:33 -0800
committerBharat Mediratta <bharat@menalto.com>2013-01-18 13:35:33 -0800
commit93afe52ae16b5f01a1bd9966af061ea0d6224370 (patch)
treee244c6dd635eda6c7a0a73f1ec4d7bf556a9000d /modules
parent61724f78da98d192cc86db6b3a12cdac506a916e (diff)
parent3a9009492e6d05fe657e3aacf34cf40ca3495db5 (diff)
Merge pull request #92 from shadlaws/fix_1943
#1943 - Make legal_file::change_extension more robust.
Diffstat (limited to 'modules')
-rw-r--r--modules/gallery/helpers/legal_file.php9
-rw-r--r--modules/gallery/tests/Legal_File_Helper_Test.php18
2 files changed, 21 insertions, 6 deletions
diff --git a/modules/gallery/helpers/legal_file.php b/modules/gallery/helpers/legal_file.php
index e6f4cb54..e24aca36 100644
--- a/modules/gallery/helpers/legal_file.php
+++ b/modules/gallery/helpers/legal_file.php
@@ -137,15 +137,12 @@ class legal_file_Core {
}
/**
- * Convert the extension of a filename. If the original filename has no
+ * Change the extension of a filename. If the original filename has no
* extension, add the new one to the end.
*/
static function change_extension($filename, $new_ext) {
- if (strpos($filename, ".") === false) {
- return "{$filename}.{$new_ext}";
- } else {
- return preg_replace("/\.[^\.]*?$/", ".{$new_ext}", $filename);
- }
+ $filename_no_ext = preg_replace("/\.[^\.\/]*?$/", "", $filename);
+ return "{$filename_no_ext}.{$new_ext}";
}
/**
diff --git a/modules/gallery/tests/Legal_File_Helper_Test.php b/modules/gallery/tests/Legal_File_Helper_Test.php
index d80bcafe..d2813633 100644
--- a/modules/gallery/tests/Legal_File_Helper_Test.php
+++ b/modules/gallery/tests/Legal_File_Helper_Test.php
@@ -36,6 +36,24 @@ class Legal_File_Helper_Test extends Gallery_Unit_Test_Case {
legal_file::change_extension("/website/foo.com/VID_20120513_105421.mp4", "jpg"));
}
+ public function change_extension_path_containing_dots_and_no_extension_test() {
+ $this->assert_equal(
+ "/website/foo.com/VID_20120513_105421.jpg",
+ legal_file::change_extension("/website/foo.com/VID_20120513_105421", "jpg"));
+ }
+
+ public function change_extension_path_containing_dots_and_dot_extension_test() {
+ $this->assert_equal(
+ "/website/foo.com/VID_20120513_105421.jpg",
+ legal_file::change_extension("/website/foo.com/VID_20120513_105421.", "jpg"));
+ }
+
+ public function change_extension_path_containing_dots_and_non_standard_chars_test() {
+ $this->assert_equal(
+ "/j'écris@un#nom/bizarre(mais quand.même/ça_passe.jpg",
+ legal_file::change_extension("/j'écris@un#nom/bizarre(mais quand.même/ça_passe.\$ÇÀ@€#_", "jpg"));
+ }
+
public function smash_extensions_test() {
$this->assert_equal("foo_bar.jpg", legal_file::smash_extensions("foo.bar.jpg"));
$this->assert_equal("foo_bar_baz.jpg", legal_file::smash_extensions("foo.bar.baz.jpg"));