Skip to content

Commit 591dd4e

Browse files
committed
BUGFIX SS_Map::keys() and SS_Map::values() are identical, keys() should return the *keys* not the values silverstripe#6818
1 parent 1d4ac68 commit 591dd4e

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

model/Map.php

+9-8
Original file line numberDiff line numberDiff line change
@@ -47,28 +47,29 @@ function toArray() {
4747
}
4848
return $array;
4949
}
50-
50+
5151
/**
5252
* Return all the keys of this map
5353
*/
5454
function keys() {
55-
$array = array();
55+
$output = array();
5656
foreach($this as $k => $v) {
57-
$array[] = $v;
57+
$output[] = $k;
5858
}
59-
return $array;
59+
return $output;
6060
}
61+
6162
/**
6263
* Return all the values of this map
6364
*/
6465
function values() {
65-
$array = array();
66+
$output = array();
6667
foreach($this as $k => $v) {
67-
$array[] = $v;
68+
$output[] = $v;
6869
}
69-
return $array;
70+
return $output;
7071
}
71-
72+
7273
/**
7374
* Unshift an item onto the start of the map
7475
*/

tests/model/MapTest.php

+20
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,26 @@ function testToArray() {
5757
"Phil" => "Phil is a unique guy, and comments on team2"), $map->toArray());
5858
}
5959

60+
function testKeys() {
61+
$list = DataList::create('DataObjectTest_TeamComment');
62+
$map = new SS_Map($list, 'Name', 'Comment');
63+
$this->assertEquals(array(
64+
'Joe',
65+
'Bob',
66+
'Phil'
67+
), $map->keys());
68+
}
69+
70+
function testValues() {
71+
$list = DataList::create('DataObjectTest_TeamComment');
72+
$map = new SS_Map($list, 'Name', 'Comment');
73+
$this->assertEquals(array(
74+
'This is a team comment by Joe',
75+
'This is a team comment by Bob',
76+
'Phil is a unique guy, and comments on team2'
77+
), $map->values());
78+
}
79+
6080
function testUnshift() {
6181
$list = DataList::create("DataObjectTest_TeamComment");
6282
$map = new SS_Map($list, 'Name', 'Comment');

0 commit comments

Comments
 (0)