Permalink
Browse files

Fixing issue where value of '$and' was appended to output string even…

… when list array contained only one element. Fixed #285
  • Loading branch information...
ADmad committed Jan 31, 2010
1 parent 3f9813a commit 59a4732eb8a1252174ba1b01624128526fa3b411
Showing with 11 additions and 1 deletion.
  1. +5 −1 cake/libs/view/helpers/text.php
  2. +6 −0 cake/tests/cases/libs/view/helpers/text.test.php
@@ -336,7 +336,11 @@ function excerpt($text, $phrase, $radius = 100, $ending = '...') {
* @access public
*/
function toList($list, $and = 'and', $separator = ', ') {
- return implode($separator, array_slice($list, null, -1)) . ' ' . $and . ' ' . array_pop($list);
+ if (count($list) > 1) {
+ return implode($separator, array_slice($list, null, -1)) . ' ' . $and . ' ' . array_pop($list);
+ } else {
+ return array_pop($list);
+ }
}
}
?>
@@ -355,6 +355,12 @@ function testExcerptCaseInsensitivity() {
* @return void
*/
function testListGeneration() {
+ $result = $this->Text->toList(array());
+ $this->assertEqual($result, '');
+
+ $result = $this->Text->toList(array('One'));
+ $this->assertEqual($result, 'One');
+
$result = $this->Text->toList(array('Larry', 'Curly', 'Moe'));
$this->assertEqual($result, 'Larry, Curly and Moe');

0 comments on commit 59a4732

Please sign in to comment.