2018-09-25 02:25:23 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace SilverStripe\Versioned\Tests;
|
|
|
|
|
|
|
|
use SilverStripe\Dev\SapphireTest;
|
|
|
|
use SilverStripe\ORM\DataObject;
|
|
|
|
use SilverStripe\ORM\Hierarchy\Hierarchy;
|
|
|
|
use SilverStripe\Versioned\Versioned;
|
|
|
|
use SilverStripe\ORM\Tests\HierarchyTest\TestObject;
|
|
|
|
use SilverStripe\ORM\Tests\HierarchyTest\HideTestObject;
|
|
|
|
use SilverStripe\ORM\Tests\HierarchyTest\HideTestSubObject;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal Only test the right values are returned, not that the cache is actually used.
|
|
|
|
*/
|
|
|
|
class HierachyCacheTest extends SapphireTest
|
|
|
|
{
|
|
|
|
|
|
|
|
protected static $fixture_file = 'HierarchyTest.yml';
|
|
|
|
|
2020-04-20 19:58:09 +02:00
|
|
|
protected static $extra_dataobjects = [
|
2018-09-25 02:25:23 +02:00
|
|
|
TestObject::class,
|
|
|
|
HideTestObject::class,
|
|
|
|
HideTestSubObject::class,
|
2020-04-20 19:58:09 +02:00
|
|
|
];
|
2018-09-25 02:25:23 +02:00
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
TestObject::singleton()->flushCache();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function setUpBeforeClass()
|
|
|
|
{
|
|
|
|
parent::setUpBeforeClass();
|
|
|
|
HideTestObject::config()->update(
|
|
|
|
'hide_from_hierarchy',
|
|
|
|
[ HideTestSubObject::class ]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function cacheNumChildrenDataProvider()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
[TestObject::class, 'obj1', false, 0, 'childless object should have a numChildren of 0'],
|
|
|
|
[TestObject::class, 'obj1', true, 0, 'childless object should have a numChildren of 0 when cache'],
|
|
|
|
[TestObject::class, 'obj2', false, 2, 'Root object numChildren should count direct children'],
|
|
|
|
[TestObject::class, 'obj2', true, 2, 'Root object numChildren should count direct children when cache'],
|
|
|
|
[TestObject::class, 'obj3a', false, 2, 'Sub object numChildren should count direct children'],
|
|
|
|
[TestObject::class, 'obj3a', true, 2, 'Sub object numChildren should count direct children when cache'],
|
2021-09-24 12:29:43 +02:00
|
|
|
[TestObject::class, 'obj3d', false, 0, 'Childless Sub object numChildren should be 0'],
|
|
|
|
[TestObject::class, 'obj3d', true, 0, 'Childless Sub object numChildren should be 0 when cache'],
|
2018-09-25 02:25:23 +02:00
|
|
|
[HideTestObject::class, 'obj4', false, 1, 'Hidden object should not be included in count'],
|
2021-09-24 12:29:43 +02:00
|
|
|
[HideTestObject::class, 'obj4', true, 1, 'Hidden object should not be included in count when cache']
|
2018-09-25 02:25:23 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider cacheNumChildrenDataProvider
|
|
|
|
*/
|
|
|
|
public function testNumChildrenCache($className, $identifier, $cache, $expected, $message)
|
|
|
|
{
|
|
|
|
$node = $this->objFromFixture($className, $identifier);
|
|
|
|
|
|
|
|
$actual = $node->numChildren($cache);
|
|
|
|
|
|
|
|
$this->assertEquals($expected, $actual, $message);
|
|
|
|
|
|
|
|
if ($cache) {
|
2021-09-24 12:29:43 +02:00
|
|
|
// When caching is enabled, try re-accessing the numChildren value to make sure it doesn't change.
|
2018-09-25 02:25:23 +02:00
|
|
|
$actual = $node->numChildren($cache);
|
|
|
|
$this->assertEquals($expected, $actual, $message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function prepopulateCacheNumChildrenDataProvider()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
[
|
|
|
|
TestObject::class, [],
|
|
|
|
'obj1', false, 0, 'childless object should have a numChildren of 0'
|
|
|
|
],
|
|
|
|
[
|
|
|
|
TestObject::class, [],
|
|
|
|
'obj1', true, 0, 'childless object should have a numChildren of 0 when cache'
|
|
|
|
],
|
|
|
|
[
|
|
|
|
TestObject::class, [2],
|
|
|
|
'obj1', false, 0, 'childless object should have a numChildren of 0'
|
|
|
|
],
|
|
|
|
[
|
|
|
|
TestObject::class, [2],
|
|
|
|
'obj1', true, 0, 'childless object should have a numChildren of 0 when cache'
|
|
|
|
],
|
|
|
|
[
|
|
|
|
TestObject::class, [],
|
|
|
|
'obj2', false, 2, 'Root object numChildren should count direct children'
|
|
|
|
],
|
|
|
|
[
|
|
|
|
TestObject::class, [],
|
|
|
|
'obj2', true, 2, 'Root object numChildren should count direct children when cache'
|
|
|
|
],
|
|
|
|
[
|
|
|
|
TestObject::class, [2],
|
|
|
|
'obj2', false, 2, 'Root object numChildren should count direct children'
|
|
|
|
],
|
|
|
|
[
|
|
|
|
TestObject::class, [2],
|
|
|
|
'obj2', true, 2, 'Root object numChildren should count direct children when cache'
|
|
|
|
],
|
|
|
|
[
|
|
|
|
HideTestObject::class, [],
|
|
|
|
'obj4', false, 1, 'Hidden object should not be included in count'
|
|
|
|
],
|
|
|
|
[
|
|
|
|
HideTestObject::class, [],
|
|
|
|
'obj4', true, 1, 'Hidden object should not be included in count when cache'
|
|
|
|
],
|
|
|
|
[
|
|
|
|
HideTestObject::class, [2],
|
|
|
|
'obj4', false, 1, 'Hidden object should not be included in count'
|
|
|
|
],
|
|
|
|
[
|
|
|
|
HideTestObject::class, [2],
|
|
|
|
'obj4', true, 1, 'Hidden object should not be included in count when cache'
|
|
|
|
]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider prepopulateCacheNumChildrenDataProvider
|
|
|
|
*/
|
|
|
|
public function testPrepopulatedNumChildrenCache(
|
|
|
|
$className,
|
|
|
|
$idList,
|
|
|
|
$identifier,
|
|
|
|
$cache,
|
|
|
|
$expected,
|
|
|
|
$message
|
|
|
|
) {
|
|
|
|
DataObject::singleton($className)->prepopulateTreeDataCache($idList, ['numChildrenMethod' => 'numChildren']);
|
|
|
|
$node = $this->objFromFixture($className, $identifier);
|
|
|
|
|
|
|
|
$actual = $node->numChildren($cache);
|
|
|
|
|
|
|
|
$this->assertEquals($expected, $actual, $message);
|
|
|
|
}
|
|
|
|
}
|