mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
ENHANCEMENT Using fixtures in CheckboxSetFieldTest?, restructured from hard-to-debug manymany-join on self to a Article<->Tag relation
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@63995 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
4da7a70cdd
commit
de1218e8db
@ -2,56 +2,81 @@
|
|||||||
|
|
||||||
class CheckboxSetFieldTest extends SapphireTest {
|
class CheckboxSetFieldTest extends SapphireTest {
|
||||||
|
|
||||||
|
static $fixture_file = 'sapphire/tests/forms/CheckboxSetFieldTest.yml';
|
||||||
|
|
||||||
function testSaveWithNothingSelected() {
|
function testSaveWithNothingSelected() {
|
||||||
/* Set up a test data object */
|
$article = $this->fixture->objFromFixture('CheckboxSetFieldTest_Article', 'articlewithouttags');
|
||||||
$one = new CheckboxSetFieldTest_Obj();
|
|
||||||
$one->A = "A1";
|
|
||||||
$one->write();
|
|
||||||
|
|
||||||
/* Create a CheckboxSetField with nothing selected */
|
/* Create a CheckboxSetField with nothing selected */
|
||||||
$field = new CheckboxSetField("Relation", "Test field", DataObject::get("CheckboxSetFieldTest_Obj")->map());
|
$field = new CheckboxSetField("Tags", "Test field", DataObject::get("CheckboxSetFieldTest_Tag")->map());
|
||||||
|
|
||||||
/* Saving should work */
|
/* Saving should work */
|
||||||
$field->saveInto($one);
|
$field->saveInto($article);
|
||||||
|
|
||||||
/* Nothing should go into CheckboxSetFieldTest_Obj_Relation */
|
$this->assertNull(
|
||||||
$this->assertNull(DB::query("SELECT * FROM CheckboxSetFieldTest_Obj_Relation")->value());
|
DB::query("
|
||||||
|
SELECT *
|
||||||
|
FROM CheckboxSetFieldTest_Article_Tags
|
||||||
|
WHERE CheckboxSetFieldTest_Article_Tags.CheckboxSetFieldTest_ArticleID = $article->ID
|
||||||
|
")->value(),
|
||||||
|
'Nothing should go into manymany join table for a saved field without any ticked boxes'
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function testSaveWithArrayValueSet() {
|
function testSaveWithArrayValueSet() {
|
||||||
/* Set up two test data object */
|
$article = $this->fixture->objFromFixture('CheckboxSetFieldTest_Article', 'articlewithouttags');
|
||||||
$one = new CheckboxSetFieldTest_Obj();
|
$articleWithTags = $this->fixture->objFromFixture('CheckboxSetFieldTest_Article', 'articlewithtags');
|
||||||
$one->A = "A1";
|
$tag1 = $this->fixture->objFromFixture('CheckboxSetFieldTest_Tag', 'tag1');
|
||||||
$one->write();
|
$tag2 = $this->fixture->objFromFixture('CheckboxSetFieldTest_Tag', 'tag2');
|
||||||
|
|
||||||
$two = new CheckboxSetFieldTest_Obj();
|
|
||||||
$two->A = "A2";
|
|
||||||
$two->write();
|
|
||||||
|
|
||||||
/* Create a CheckboxSetField with 2 items selected. Note that the array is in the format (key) => (selected) */
|
/* Create a CheckboxSetField with 2 items selected. Note that the array is in the format (key) => (selected) */
|
||||||
$field = new CheckboxSetField("Relation", "Test field", DataObject::get("CheckboxSetFieldTest_Obj")->map());
|
$field = new CheckboxSetField("Tags", "Test field", DataObject::get("CheckboxSetFieldTest_Tag")->map());
|
||||||
$field->setValue(array(
|
$field->setValue(array(
|
||||||
1 => true,
|
$tag1->ID => true,
|
||||||
2 => true));
|
$tag2->ID => true
|
||||||
|
));
|
||||||
|
|
||||||
/* Saving should work */
|
/* Saving should work */
|
||||||
$field->saveInto($one);
|
$field->saveInto($article);
|
||||||
|
|
||||||
/* Data shold be saved into CheckboxSetField */
|
$this->assertEquals(
|
||||||
$this->assertEquals(array($one->ID,$one->ID), DB::query("SELECT CheckboxSetFieldTest_ObjID FROM CheckboxSetFieldTest_Obj_Relation")->column());
|
array($tag1->ID,$tag2->ID),
|
||||||
$this->assertEquals(array(1,2), DB::query("SELECT ChildID FROM CheckboxSetFieldTest_Obj_Relation")->column());
|
DB::query("
|
||||||
|
SELECT CheckboxSetFieldTest_TagID
|
||||||
|
FROM CheckboxSetFieldTest_Article_Tags
|
||||||
|
WHERE CheckboxSetFieldTest_Article_Tags.CheckboxSetFieldTest_ArticleID = $article->ID
|
||||||
|
")->column(),
|
||||||
|
'Data shold be saved into CheckboxSetField manymany relation table on the "right end"'
|
||||||
|
);
|
||||||
|
$this->assertEquals(
|
||||||
|
array($articleWithTags->ID,$article->ID),
|
||||||
|
DB::query("
|
||||||
|
SELECT CheckboxSetFieldTest_ArticleID
|
||||||
|
FROM CheckboxSetFieldTest_Article_Tags
|
||||||
|
WHERE CheckboxSetFieldTest_Article_Tags.CheckboxSetFieldTest_TagID = $tag1->ID
|
||||||
|
")->column(),
|
||||||
|
'Data shold be saved into CheckboxSetField manymany relation table on the "left end"'
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function testLoadDataFrom() {
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class CheckboxSetFieldTest_Obj extends DataObject implements TestOnly {
|
class CheckboxSetFieldTest_Article extends DataObject implements TestOnly {
|
||||||
static $db = array(
|
static $db = array(
|
||||||
"A" => "Varchar",
|
"Content" => "Text",
|
||||||
"B" => "Varchar",
|
|
||||||
);
|
);
|
||||||
|
|
||||||
static $many_many = array(
|
static $many_many = array(
|
||||||
"Relation" => "CheckboxSetFieldTest_Obj",
|
"Tags" => "CheckboxSetFieldTest_Tag",
|
||||||
);
|
);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
class CheckboxSetFieldTest_Tag extends DataObject implements TestOnly {
|
||||||
|
static $belongs_many_many = array(
|
||||||
|
'Articles' => 'CheckboxSetFieldTest_Article'
|
||||||
|
);
|
||||||
}
|
}
|
22
tests/forms/CheckboxSetFieldTest.yml
Normal file
22
tests/forms/CheckboxSetFieldTest.yml
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
CheckboxSetFieldTest_Tag:
|
||||||
|
tag1:
|
||||||
|
Title: Tag 1
|
||||||
|
tag2:
|
||||||
|
Title: Tag 2
|
||||||
|
CheckboxSetFieldTest_Article:
|
||||||
|
articlewithouttags:
|
||||||
|
Content: Article 1
|
||||||
|
articlewithtags:
|
||||||
|
Content: Article 2
|
||||||
|
Tags: =>CheckboxSetFieldTest_Tag.tag1,=>CheckboxSetFieldTest_Tag.tag2
|
||||||
|
CheckboxSetFieldTest_Tag:
|
||||||
|
tag1:
|
||||||
|
Title: Tag 1
|
||||||
|
tag2:
|
||||||
|
Title: Tag 2
|
||||||
|
CheckboxSetFieldTest_Article:
|
||||||
|
articlewithouttags:
|
||||||
|
Content: Article 1
|
||||||
|
articlewithtags:
|
||||||
|
Content: Article 2
|
||||||
|
Tags: =>CheckboxSetFieldTest_Tag.tag1,=>CheckboxSetFieldTest_Tag.tag2
|
Loading…
Reference in New Issue
Block a user