From 35446a42a92907e767442a433384472c40480763 Mon Sep 17 00:00:00 2001 From: Mark Story Date: Sat, 27 Mar 2010 16:23:46 -0400 Subject: [PATCH] Making filtering of extracted arrays remember their key. This fixes attribute selectors followed by parent selectors returning seemingly random results. Fixes #502 --- cake/libs/set.php | 2 +- cake/tests/cases/libs/set.test.php | 74 ++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 1 deletion(-) diff --git a/cake/libs/set.php b/cake/libs/set.php index b6179de1c..64f55b0e6 100644 --- a/cake/libs/set.php +++ b/cake/libs/set.php @@ -486,7 +486,7 @@ class Set extends Object { $length = count($matches); foreach ($matches as $i => $match) { if (Set::matches(array($condition), $match['item'], $i + 1, $length)) { - $filtered[] = $match; + $filtered[$i] = $match; } } $matches = $filtered; diff --git a/cake/tests/cases/libs/set.test.php b/cake/tests/cases/libs/set.test.php index 349522ece..8f983f4ec 100644 --- a/cake/tests/cases/libs/set.test.php +++ b/cake/tests/cases/libs/set.test.php @@ -974,6 +974,80 @@ class SetTest extends CakeTestCase { $expected = array('Second'); $this->assertEqual($result, $expected); } +/** + * test parent selectors with extract + * + * @return void + */ + function testExtractParentSelector() { + $a = array( + 'Model' => array( + '0' => array( + 'id' => 18, + 'SubModelsModel' => array( + 'id' => 1, + 'submodel_id' => 66, + 'model_id' => 18, + 'type' => 1 + ), + ), + '1' => array( + 'id' => 0, + 'SubModelsModel' => array( + 'id' => 2, + 'submodel_id' => 66, + 'model_id' => 0, + 'type' => 1 + ), + ), + '2' => array( + 'id' => 17, + 'SubModelsModel' => array( + 'id' => 3, + 'submodel_id' => 66, + 'model_id' => 17, + 'type' => 2 + ), + ), + '3' => array( + 'id' => 0, + 'SubModelsModel' => array( + 'id' => 4, + 'submodel_id' => 66, + 'model_id' => 0, + 'type' => 2 + ) + ) + ) + ); + + $expected = array( + array( + 'Model' => array( + 'id' => 17, + 'SubModelsModel' => array( + 'id' => 3, + 'submodel_id' => 66, + 'model_id' => 17, + 'type' => 2 + ), + ) + ), + array( + 'Model' => array( + 'id' => 0, + 'SubModelsModel' => array( + 'id' => 4, + 'submodel_id' => 66, + 'model_id' => 0, + 'type' => 2 + ) + ) + ) + ); + $r = Set::extract('/Model/SubModelsModel[type=2]/..', $a); + $this->assertEqual($r, $expected); + } /** * test that extract() still works when arrays don't contain a 0 index. *