2009-05-06 08:36:16 +02:00
|
|
|
<?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();
|
2009-10-26 21:56:54 +01:00
|
|
|
|
2009-05-06 08:36:16 +02:00
|
|
|
// 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'));
|
2009-10-26 21:56:54 +01:00
|
|
|
|
2010-01-13 00:21:52 +01:00
|
|
|
// Check numHistoricalChildren
|
|
|
|
$this->assertEquals(3, singleton('Page')->numHistoricalChildren());
|
|
|
|
|
2009-05-06 08:36:16 +02:00
|
|
|
// Check that both page 2 children are returned
|
|
|
|
$page2 = $this->objFromFixture('Page', 'page2');
|
|
|
|
$this->assertEquals(array("Page 2a", "Page 2b"),
|
|
|
|
$page2->AllHistoricalChildren()->column('Title'));
|
2010-01-13 00:21:52 +01:00
|
|
|
|
|
|
|
// Check numHistoricalChildren
|
|
|
|
$this->assertEquals(2, $page2->numHistoricalChildren());
|
|
|
|
|
2009-05-06 08:36:16 +02:00
|
|
|
|
|
|
|
// Page 3 has been deleted; let's bring it back from the grave
|
2009-09-30 00:04:32 +02:00
|
|
|
$page3 = Versioned::get_including_deleted("SiteTree", "\"Title\" = 'Page 3'")->First();
|
2009-10-26 21:56:54 +01:00
|
|
|
|
2009-05-06 08:36:16 +02:00
|
|
|
// Check that both page 3 children are returned
|
|
|
|
$this->assertEquals(array("Page 3a", "Page 3b"),
|
|
|
|
$page3->AllHistoricalChildren()->column('Title'));
|
2010-01-13 00:21:52 +01:00
|
|
|
|
|
|
|
// Check numHistoricalChildren
|
|
|
|
$this->assertEquals(2, $page3->numHistoricalChildren());
|
2009-05-06 08:36:16 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2009-09-30 00:04:32 +02:00
|
|
|
$pages = DataObject::get("Page", '', '"ID" ASC');
|
2009-05-06 08:36:16 +02:00
|
|
|
$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);
|
2009-10-23 00:18:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function testNumChildren() {
|
|
|
|
$this->assertEquals($this->objFromFixture('Page', 'page1')->numChildren(), 0);
|
|
|
|
$this->assertEquals($this->objFromFixture('Page', 'page2')->numChildren(), 2);
|
|
|
|
$this->assertEquals($this->objFromFixture('Page', 'page3')->numChildren(), 2);
|
|
|
|
$this->assertEquals($this->objFromFixture('Page', 'page2a')->numChildren(), 0);
|
|
|
|
$this->assertEquals($this->objFromFixture('Page', 'page2b')->numChildren(), 0);
|
|
|
|
$this->assertEquals($this->objFromFixture('Page', 'page3a')->numChildren(), 0);
|
|
|
|
$this->assertEquals($this->objFromFixture('Page', 'page3b')->numChildren(), 0);
|
2009-10-26 21:56:54 +01:00
|
|
|
|
|
|
|
$page1 = $this->objFromFixture('Page', 'page1');
|
|
|
|
$this->assertEquals($page1->numChildren(), 0);
|
|
|
|
$page1Child1 = new Page();
|
|
|
|
$page1Child1->ParentID = $page1->ID;
|
|
|
|
$page1Child1->write();
|
|
|
|
$this->assertEquals($page1->numChildren(false), 1,
|
|
|
|
'numChildren() caching can be disabled through method parameter'
|
|
|
|
);
|
|
|
|
$page1Child2 = new Page();
|
|
|
|
$page1Child2->ParentID = $page1->ID;
|
|
|
|
$page1Child2->write();
|
|
|
|
$page1->flushCache();
|
|
|
|
$this->assertEquals($page1->numChildren(), 2,
|
|
|
|
'numChildren() caching can be disabled by flushCache()'
|
|
|
|
);
|
2009-10-23 00:18:02 +02:00
|
|
|
}
|
2010-02-08 03:09:35 +01:00
|
|
|
|
2010-02-08 03:12:24 +01:00
|
|
|
function testLoadDescendantIDListIntoArray() {
|
2010-02-08 03:09:35 +01:00
|
|
|
$page2 = $this->objFromFixture('Page', 'page2');
|
|
|
|
$idList = $page2->getDescendantIDList();
|
|
|
|
$page2a = $this->objFromFixture('Page', 'page2a');
|
|
|
|
$page2b = $this->objFromFixture('Page', 'page2b');
|
|
|
|
$this->assertContains($page2a->ID, $idList, 'Page 2a is a descendant of Page 2');
|
|
|
|
$this->assertContains($page2b->ID, $idList, 'Page 2b is a descendant of Page 2');
|
|
|
|
$this->assertEquals(2, count($idList), 'There are 2 descendants of Page 2');
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|