Skip to content

Commit 07d5a26

Browse files
committed
Prevent unexpected results with non-conditional string condition
1 parent 6271aa9 commit 07d5a26

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

src/xPDO/Om/xPDOQuery.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,13 @@ public function parseConditions($conditions, $conjunction = xPDOQuery::SQL_AND)
817817
$field['conjunction']= $conjunction;
818818
$result = new xPDOQueryCondition($field);
819819
}
820+
else {
821+
$result= new xPDOQueryCondition([
822+
'sql' => $conditions,
823+
'binding' => null,
824+
'conjunction' => $conjunction
825+
]);
826+
}
820827
return $result;
821828
}
822829

src/xPDO/xPDO.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,6 +1056,7 @@ public function getCount($className, $criteria = null) {
10561056
*/
10571057
public function getObjectGraph($className, $graph, $criteria= null, $cacheFlag= true) {
10581058
$object= null;
1059+
$this->sanitizePKCriteria($className, $criteria);
10591060
if ($collection= $this->getCollectionGraph($className, $graph, $criteria, $cacheFlag)) {
10601061
if (!count($collection) === 1) {
10611062
$this->log(xPDO::LOG_LEVEL_WARN, 'getObjectGraph criteria returned more than one instance.');

test/xPDO/Test/Om/xPDOObjectTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
namespace xPDO\Test\Om;
1212

1313
use xPDO\Om\xPDOObject;
14+
use xPDO\Test\Sample\Person;
1415
use xPDO\TestCase;
1516
use xPDO\xPDO;
1617

@@ -327,6 +328,13 @@ public function testCascadeSave()
327328
$person->remove();
328329
}
329330

331+
public function testGetObjectDoesNotReturnUnexpectedResults()
332+
{
333+
$person = $this->xpdo->getObject('xPDO\\Test\\Sample\\Person', 'test');
334+
335+
$this->assertNull($person, 'getObject returned an instance from an invalid key');
336+
}
337+
330338
/**
331339
* Test getting an object by the primary key.
332340
*
@@ -393,6 +401,13 @@ public function testGetObjectGraphsByPK()
393401
$this->assertTrue($phone instanceof \xPDO\Test\Sample\Phone, "Error retrieving related Phone object via getObjectGraph");
394402
}
395403

404+
public function testGetObjectGraphDoesNotReturnUnexpectedResults()
405+
{
406+
$person = $this->xpdo->getObjectGraph('xPDO\\Test\\Sample\\Person', '{"PersonPhone":{"Phone":{}}}', 'test');
407+
408+
$this->assertNull($person, 'getObjectGraph returned unexpected result from invalid key');
409+
}
410+
396411
/**
397412
* Test getObjectGraph by PK with JSON graph
398413
*/
@@ -421,6 +436,13 @@ public function testGetObjectGraphsJSONByPK()
421436
$this->assertTrue($phone instanceof \xPDO\Test\Sample\Phone, "Error retrieving related Phone object via getObjectGraph, JSON graph");
422437
}
423438

439+
public function testGetCollectionDoesNotReturnUnexpectedResults()
440+
{
441+
$person = $this->xpdo->getCollection('xPDO\\Test\\Sample\\Person', 'test');
442+
443+
$this->assertEmpty($person, 'getCollection returned data from an invalid where clause');
444+
}
445+
424446
/**
425447
* Test xPDO::getCollection
426448
*/
@@ -436,6 +458,13 @@ public function testGetCollection()
436458
$this->assertTrue(count($people) == 2, "Error retrieving all objects.");
437459
}
438460

461+
public function testGetCollectionGraphDoesNotReturnUnexpectedResults()
462+
{
463+
$person = $this->xpdo->getCollectionGraph('xPDO\\Test\\Sample\\Person', array('PersonPhone' => array('Phone' => array())), 'test');
464+
465+
$this->assertEmpty($person, 'getCollectionGraph returned data from an invalid where clause');
466+
}
467+
439468
/**
440469
* Test xPDO::getCollectionGraph
441470
*/

0 commit comments

Comments
 (0)