FIX do not show HiddenClass pages in allowed children (#1555)

* FIX do not show HiddenClass pages in allowed children

* Resolves #1553

* * Update SiteTreeTest to include HiddenClass checks
* Refactor testAllowedChildren to use a data provider
This commit is contained in:
Robbie Averill 2016-07-22 20:45:14 +12:00 committed by Daniel Hensby
parent c7b1a4ded8
commit 82e54314bf
2 changed files with 79 additions and 42 deletions

View File

@ -2694,7 +2694,10 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid
} else {
$subclasses = ClassInfo::subclassesFor($candidate);
foreach($subclasses as $subclass) {
if($subclass != "SiteTree_root") $allowedChildren[] = $subclass;
if ($subclass == 'SiteTree_root' || singleton($subclass) instanceof HiddenClass) {
continue;
}
$allowedChildren[] = $subclass;
}
}
}

View File

@ -889,40 +889,70 @@ class SiteTreeTest extends SapphireTest {
);
}
public function testAllowedChildren() {
/**
* Tests that core subclasses of SiteTree are included in allowedChildren() by default, but not instances of
* HiddenClass
*/
public function testAllowedChildrenContainsCoreSubclassesButNotHiddenClass()
{
$page = new SiteTree();
$allowedChildren = $page->allowedChildren();
$this->assertContains(
'VirtualPage',
$page->allowedChildren(),
$allowedChildren,
'Includes core subclasses by default'
);
$classA = new SiteTreeTest_ClassA();
$this->assertEquals(
$this->assertNotContains(
'SiteTreeTest_ClassE',
$allowedChildren,
'HiddenClass instances should not be returned'
);
}
/**
* Tests that various types of SiteTree classes will or will not be returned from the allowedChildren method
* @dataProvider allowedChildrenProvider
* @param string $className
* @param array $expected
* @param string $assertionMessage
*/
public function testAllowedChildren($className, $expected, $assertionMessage)
{
$class = new $className;
$this->assertEquals($expected, $class->allowedChildren(), $assertionMessage);
}
/**
* @return array
*/
public function allowedChildrenProvider()
{
return array(
array(
// Class name
'SiteTreeTest_ClassA',
// Expected
array('SiteTreeTest_ClassB'),
$classA->allowedChildren(),
// Assertion message
'Direct setting of allowed children'
);
$classB = new SiteTreeTest_ClassB();
$this->assertEquals(
),
array(
'SiteTreeTest_ClassB',
array('SiteTreeTest_ClassC', 'SiteTreeTest_ClassCext'),
$classB->allowedChildren(),
'Includes subclasses'
);
$classD = new SiteTreeTest_ClassD();
$this->assertEquals(
array('SiteTreeTest_ClassC'),
$classD->allowedChildren(),
'Excludes subclasses if class is prefixed by an asterisk'
);
$classC = new SiteTreeTest_ClassC();
$this->assertEquals(
),
array(
'SiteTreeTest_ClassC',
array(),
$classC->allowedChildren(),
'Null setting'
),
array(
'SiteTreeTest_ClassD',
array('SiteTreeTest_ClassC'),
'Excludes subclasses if class is prefixed by an asterisk'
)
);
}
@ -1235,6 +1265,10 @@ class SiteTreeTest_ClassD extends Page implements TestOnly {
private static $allowed_children = array('*SiteTreeTest_ClassC');
}
class SiteTreeTest_ClassE extends Page implements TestOnly, HiddenClass {
}
class SiteTreeTest_ClassCext extends SiteTreeTest_ClassC implements TestOnly {
// Override SiteTreeTest_ClassC definitions
private static $allowed_children = array('SiteTreeTest_ClassB');