mirror of
https://github.com/silverstripe/silverstripe-multiform
synced 2024-10-22 11:05:49 +02:00
Fixing MultiFormObjectDecorator to work with SS 3.1
Conflicts: tests/MultiFormTest.php
This commit is contained in:
parent
77d6d60f45
commit
5eb68aa62c
@ -23,16 +23,27 @@ class MultiFormObjectDecorator extends DataExtension {
|
||||
private static $has_one = array(
|
||||
'MultiFormSession' => 'MultiFormSession',
|
||||
);
|
||||
|
||||
|
||||
/**
|
||||
* Augment any queries to MultiFormObjectDecorator and only
|
||||
* return anything that isn't considered temporary.
|
||||
*/
|
||||
public function augmentSQL(SQLQuery &$query) {
|
||||
// If you're querying by ID, ignore the sub-site - this is a bit ugly...
|
||||
$where = $query->getWhere();
|
||||
if(!$where && !$this->wantsTemporary($query)) {
|
||||
$from = array_values($query->getFrom());
|
||||
$query->addWhere("{$from[0]}.MultiFormIsTemporary = 0");
|
||||
return;
|
||||
}
|
||||
|
||||
if(
|
||||
strpos($query->where[0], ".`ID` = ") === false
|
||||
&& strpos($query->where[0], ".ID = ") === false
|
||||
&& strpos($query->where[0], "ID = ") !== 0
|
||||
strpos($where[0], ".`ID` = ") === false
|
||||
&& strpos($where[0], ".ID = ") === false
|
||||
&& strpos($where[0], "ID = ") !== 0
|
||||
&& !$this->wantsTemporary($query)
|
||||
) {
|
||||
$query->where[] = "\"{$query->from[0]}\".\"MultiFormIsTemporary\" = 0";
|
||||
$from = array_values($query->getFrom());
|
||||
$query->addWhere("{$from[0]}.MultiFormIsTemporary = 0");
|
||||
}
|
||||
}
|
||||
|
||||
@ -45,8 +56,10 @@ class MultiFormObjectDecorator extends DataExtension {
|
||||
* @return boolean
|
||||
*/
|
||||
protected function wantsTemporary($query) {
|
||||
foreach($query->where as $whereClause) {
|
||||
if($whereClause == "\"{$query->from[0]}\".\"MultiFormIsTemporary\" = 1") {
|
||||
foreach($query->getWhere() as $whereClause) {
|
||||
$from = array_values($query->getFrom());
|
||||
// SQLQuery will automatically add double quotes and single quotes to values, so check against that.
|
||||
if($whereClause == "{$from[0]}.\"MultiFormIsTemporary\" = '1'") {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
44
tests/MultiFormObjectDecoratorTest.php
Normal file
44
tests/MultiFormObjectDecoratorTest.php
Normal file
@ -0,0 +1,44 @@
|
||||
<?php
|
||||
class MultiFormObjectDecoratorTest extends SapphireTest {
|
||||
|
||||
protected static $fixture_file = 'MultiFormObjectDecoratorTest.yml';
|
||||
|
||||
protected $requiredExtensions = array(
|
||||
'MultiFormObjectDecorator_DataObject' => array('MultiFormObjectDecorator')
|
||||
);
|
||||
|
||||
protected $extraDataObjects = array(
|
||||
'MultiFormObjectDecorator_DataObject'
|
||||
);
|
||||
|
||||
public function testTemporaryDataFilteredQuery() {
|
||||
$records = MultiFormObjectDecorator_DataObject::get()
|
||||
->map('Name')
|
||||
->toArray();
|
||||
|
||||
$this->assertContains('Test 1', $records);
|
||||
$this->assertContains('Test 2', $records);
|
||||
$this->assertNotContains('Test 3', $records);
|
||||
|
||||
}
|
||||
|
||||
public function testTemporaryDataQuery() {
|
||||
$records = MultiFormObjectDecorator_DataObject::get()
|
||||
->filter(array('MultiFormIsTemporary' => 1))
|
||||
->map('Name')
|
||||
->toArray();
|
||||
|
||||
$this->assertNotContains('Test 1', $records);
|
||||
$this->assertNotContains('Test 2', $records);
|
||||
$this->assertContains('Test 3', $records);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class MultiFormObjectDecorator_DataObject extends DataObject {
|
||||
|
||||
private static $db = array(
|
||||
'Name' => 'Varchar'
|
||||
);
|
||||
|
||||
}
|
10
tests/MultiFormObjectDecoratorTest.yml
Normal file
10
tests/MultiFormObjectDecoratorTest.yml
Normal file
@ -0,0 +1,10 @@
|
||||
MultiFormObjectDecorator_DataObject:
|
||||
test-data-1:
|
||||
Name: Test 1
|
||||
MultiFormIsTemporary: 0
|
||||
test-data-2:
|
||||
Name: Test 2
|
||||
MultiFormIsTemporary: 0
|
||||
test-data-3:
|
||||
Name: Test 3
|
||||
MultiFormIsTemporary: 1
|
@ -91,7 +91,7 @@ class MultiFormTest extends FunctionalTest {
|
||||
session ID parameter");
|
||||
|
||||
Config::unnest();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -143,7 +143,7 @@ class MultiFormTest_StepOne extends MultiFormStep implements TestOnly {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @package multiform
|
||||
* @subpackage tests
|
||||
@ -159,7 +159,7 @@ class MultiFormTest_StepTwo extends MultiFormStep implements TestOnly {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @package multiform
|
||||
* @subpackage tests
|
||||
|
Loading…
Reference in New Issue
Block a user