silverstripe-framework/tests/model/HierarchyTest.php
Ingo Schommer 8698e94ae0 MINOR Added test case for Hierarchy::getDescendantIDList() which also tests Hierarchy::loadDescendantIDListInto() (merged from r98369)
MINOR Testing of grand-children items in HierarchyTest::testLoadDescendantIDListIntoArray() and HierarchyTest::testNumChildren() (merged from r98376)
BUGFIX #5044 Hierarchy::loadDescendantIDListInto() now uses Object::getExtensionInstance('Hierarchy') instead of going through call(), as PHP 5.3 has issues converting references to values
BUGFIX Fixed Hierarchy->loadDescendantIdList() to call setOwner() on the extension instance. This was necessary due to underlying Object/Extension changes in 2.4. (merged from r98403)
MINOR Fixed HierarchyTest assertions around including grand children counts (merged from r98403)

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.3@98405 467b73ca-7a2a-4603-9d3b-597d59a354a9
2011-02-02 14:27:35 +13:00

77 lines
2.8 KiB
PHP

<?php
class HierarchyTest extends SapphireTest {
static $fixture_file = 'sapphire/tests/model/HierarchyTest.yml';
/**
* Test Hierarchy::AllHistoricalChildren().
*/
function testAllHistoricalChildren() {
// Delete some pages
$this->objFromFixture('Page', 'page2b')->delete();
$this->objFromFixture('Page', 'page3a')->delete();
$this->objFromFixture('Page', 'page3')->delete();
// Check that page1-3 appear at the top level of the AllHistoricalChildren tree
$this->assertEquals(array("Page 1", "Page 2", "Page 3"),
singleton('Page')->AllHistoricalChildren()->column('Title'));
// Check that both page 2 children are returned
$page2 = $this->objFromFixture('Page', 'page2');
$this->assertEquals(array("Page 2a", "Page 2b"),
$page2->AllHistoricalChildren()->column('Title'));
// Page 3 has been deleted; let's bring it back from the grave
$page3 = Versioned::get_including_deleted("SiteTree", "Title = 'Page 3'")->First();
// Check that both page 3 children are returned
$this->assertEquals(array("Page 3a", "Page 3b"),
$page3->AllHistoricalChildren()->column('Title'));
}
/**
* Test that you can call Hierarchy::markExpanded/Unexpanded/Open() on a page, and that
* calling Hierarchy::isMarked() on a different instance of that object will return true.
*/
function testItemMarkingIsntRestrictedToSpecificInstance() {
// Mark a few pages
$this->objFromFixture('Page', 'page2')->markExpanded();
$this->objFromFixture('Page', 'page2a')->markExpanded();
$this->objFromFixture('Page', 'page2b')->markExpanded();
$this->objFromFixture('Page', 'page3')->markUnexpanded();
// Query some pages in a different context and check their m
$pages = DataObject::get("Page");
$marked = $expanded = array();
foreach($pages as $page) {
if($page->isMarked()) $marked[] = $page->Title;
if($page->isExpanded()) $expanded[] = $page->Title;
}
$this->assertEquals(array('Page 2', 'Page 3', 'Page 2a', 'Page 2b'), $marked);
$this->assertEquals(array('Page 2', 'Page 2a', 'Page 2b'), $expanded);
}
function testLoadDescendantIDListInto() {
$page2 = $this->objFromFixture('Page', 'page2');
$page2a = $this->objFromFixture('Page', 'page2a');
$page2b = $this->objFromFixture('Page', 'page2b');
$page2aa = $this->objFromFixture('Page', 'page2aa');
$page2ab = $this->objFromFixture('Page', 'page2ab');
$page2IdList = $page2->getDescendantIDList();
$page2aIdList = $page2a->getDescendantIDList();
$this->assertContains($page2a->ID, $page2IdList);
$this->assertContains($page2b->ID, $page2IdList);
$this->assertContains($page2aa->ID, $page2IdList);
$this->assertContains($page2ab->ID, $page2IdList);
$this->assertEquals(4, count($page2IdList));
$this->assertContains($page2aa->ID, $page2aIdList);
$this->assertContains($page2ab->ID, $page2aIdList);
$this->assertEquals(2, count($page2aIdList));
}
}