From 0f751368e569c80a3f813332707b487391c18568 Mon Sep 17 00:00:00 2001 From: Tim Hunt Date: Wed, 22 Feb 2012 20:12:08 +0000 Subject: [PATCH] MDL-29161 weblib: moodle_ulr::out does not cope with array params. Conflicts: lib/simpletest/testweblib.php --- lib/simpletest/testweblib.php | 19 +++++++++++++++++++ lib/weblib.php | 8 +++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/lib/simpletest/testweblib.php b/lib/simpletest/testweblib.php index 8913d4a803f..02cf10683a6 100644 --- a/lib/simpletest/testweblib.php +++ b/lib/simpletest/testweblib.php @@ -105,6 +105,25 @@ class web_test extends UnitTestCase { $this->assertEqual(fix_non_standard_entities('£ä'), '£ä'); } + function test_moodle_url_round_trip() { + $strurl = 'http://moodle.org/course/view.php?id=5'; + $url = new moodle_url($strurl); + $this->assertEqual($strurl, $url->out(false)); + + $strurl = 'http://moodle.org/user/index.php?contextid=53&sifirst=M&silast=D'; + $url = new moodle_url($strurl); + $this->assertEqual($strurl, $url->out(false)); + } + + function test_moodle_url_round_trip_array_params() { + $strurl = 'http://example.com/?a%5B1%5D=1&a%5B2%5D=2'; + $url = new moodle_url($strurl); + $this->assertEqual($strurl, $url->out(false)); + + $url = new moodle_url('http://example.com/?a[1]=1&a[2]=2'); + $this->assertEqual($strurl, $url->out(false)); + } + function test_compare_url() { $url1 = new moodle_url('index.php', array('var1' => 1, 'var2' => 2)); $url2 = new moodle_url('index2.php', array('var1' => 1, 'var2' => 2, 'var3' => 3)); diff --git a/lib/weblib.php b/lib/weblib.php index 03d90861a78..875703505f4 100644 --- a/lib/weblib.php +++ b/lib/weblib.php @@ -494,7 +494,13 @@ class moodle_url { $params = $this->params; } foreach ($params as $key => $val) { - $arr[] = rawurlencode($key)."=".rawurlencode($val); + if (is_array($val)) { + foreach ($val as $index => $value) { + $arr[] = rawurlencode($key.'['.$index.']')."=".rawurlencode($value); + } + } else { + $arr[] = rawurlencode($key)."=".rawurlencode($val); + } } if ($escaped) { return implode('&', $arr);