MDL-68974 admin: Unit tests for context parent/child test
This commit is contained in:
@@ -4067,6 +4067,204 @@ class core_accesslib_testcase extends advanced_testcase {
|
||||
$this->assertEquals($expectedteacher, get_profile_roles($coursecontext));
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for is_parent_of context checks.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function is_parent_of_provider(): array {
|
||||
$provideboth = function(string $desc, string $contextpath, string $testpath, bool $expected): array {
|
||||
return [
|
||||
"includeself: true; {$desc}" => [
|
||||
$contextpath,
|
||||
$testpath,
|
||||
true,
|
||||
$expected,
|
||||
],
|
||||
"includeself: false; {$desc}" => [
|
||||
$contextpath,
|
||||
$testpath,
|
||||
false,
|
||||
$expected,
|
||||
],
|
||||
];
|
||||
};
|
||||
|
||||
return array_merge(
|
||||
[
|
||||
'includeself: true, testing self' => [
|
||||
'/1/4/17/291/1001/17105',
|
||||
'/1/4/17/291/1001/17105',
|
||||
true,
|
||||
true,
|
||||
],
|
||||
'includeself: false, testing self' => [
|
||||
'/1/4/17/291/1001/17105',
|
||||
'/1/4/17/291/1001/17105',
|
||||
false,
|
||||
false,
|
||||
],
|
||||
],
|
||||
$provideboth(
|
||||
'testing parent',
|
||||
'/1/4/17/291/1001/17105',
|
||||
'/1/4/17/291/1001',
|
||||
false
|
||||
),
|
||||
$provideboth(
|
||||
'testing child',
|
||||
'/1/4/17/291/1001',
|
||||
'/1/4/17/291/1001/17105',
|
||||
true
|
||||
),
|
||||
$provideboth(
|
||||
'testing grandchild',
|
||||
'/1',
|
||||
'/1/4/17/291/1001/17105',
|
||||
true
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure that the is_parent_of() function works as anticipated.
|
||||
*
|
||||
* @dataProvider is_parent_of_provider
|
||||
* @param string $contextpath The path of the context being compared with
|
||||
* @param string $testpath The path of the context being compared
|
||||
* @param bool $testself Whether to check the current context
|
||||
* @param bool $expected The expected result
|
||||
*/
|
||||
public function test_is_parent_of(string $contextpath, string $testpath, bool $testself, bool $expected): void {
|
||||
$context = $this->getMockBuilder(\context::class)
|
||||
->disableOriginalConstructor()
|
||||
->setMethods([
|
||||
'get_url',
|
||||
'get_capabilities',
|
||||
])
|
||||
->getMock();
|
||||
|
||||
$rcp = new ReflectionProperty($context, '_path');
|
||||
$rcp->setAccessible(true);
|
||||
$rcp->setValue($context, $contextpath);
|
||||
|
||||
$comparisoncontext = $this->getMockBuilder(\context::class)
|
||||
->disableOriginalConstructor()
|
||||
->setMethods([
|
||||
'get_url',
|
||||
'get_capabilities',
|
||||
])
|
||||
->getMock();
|
||||
|
||||
$rcp = new ReflectionProperty($comparisoncontext, '_path');
|
||||
$rcp->setAccessible(true);
|
||||
$rcp->setValue($comparisoncontext, $testpath);
|
||||
|
||||
$this->assertEquals($expected, $context->is_parent_of($comparisoncontext, $testself));
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for is_child_of context checks.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function is_child_of_provider(): array {
|
||||
$provideboth = function(string $desc, string $contextpath, string $testpath, bool $expected): array {
|
||||
return [
|
||||
"includeself: true; {$desc}" => [
|
||||
$contextpath,
|
||||
$testpath,
|
||||
true,
|
||||
$expected,
|
||||
],
|
||||
"includeself: false; {$desc}" => [
|
||||
$contextpath,
|
||||
$testpath,
|
||||
false,
|
||||
$expected,
|
||||
],
|
||||
];
|
||||
};
|
||||
|
||||
return array_merge(
|
||||
[
|
||||
'includeself: true, testing self' => [
|
||||
'/1/4/17/291/1001/17105',
|
||||
'/1/4/17/291/1001/17105',
|
||||
true,
|
||||
true,
|
||||
],
|
||||
'includeself: false, testing self' => [
|
||||
'/1/4/17/291/1001/17105',
|
||||
'/1/4/17/291/1001/17105',
|
||||
false,
|
||||
false,
|
||||
],
|
||||
],
|
||||
$provideboth(
|
||||
'testing child',
|
||||
'/1/4/17/291/1001/17105',
|
||||
'/1/4/17/291/1001',
|
||||
true
|
||||
),
|
||||
$provideboth(
|
||||
'testing parent',
|
||||
'/1/4/17/291/1001',
|
||||
'/1/4/17/291/1001/17105',
|
||||
false
|
||||
),
|
||||
$provideboth(
|
||||
'testing grandchild',
|
||||
'/1/4/17/291/1001/17105',
|
||||
'/1',
|
||||
true
|
||||
),
|
||||
$provideboth(
|
||||
'testing grandparent',
|
||||
'/1',
|
||||
'/1/4/17/291/1001/17105',
|
||||
false
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure that the is_child_of() function works as anticipated.
|
||||
*
|
||||
* @dataProvider is_child_of_provider
|
||||
* @param string $contextpath The path of the context being compared with
|
||||
* @param string $testpath The path of the context being compared
|
||||
* @param bool $testself Whether to check the current context
|
||||
* @param bool $expected The expected result
|
||||
*/
|
||||
public function test_is_child_of(string $contextpath, string $testpath, bool $testself, bool $expected): void {
|
||||
$context = $this->getMockBuilder(\context::class)
|
||||
->disableOriginalConstructor()
|
||||
->setMethods([
|
||||
'get_url',
|
||||
'get_capabilities',
|
||||
])
|
||||
->getMock();
|
||||
|
||||
$rcp = new ReflectionProperty($context, '_path');
|
||||
$rcp->setAccessible(true);
|
||||
$rcp->setValue($context, $contextpath);
|
||||
|
||||
$comparisoncontext = $this->getMockBuilder(\context::class)
|
||||
->disableOriginalConstructor()
|
||||
->setMethods([
|
||||
'get_url',
|
||||
'get_capabilities',
|
||||
])
|
||||
->getMock();
|
||||
|
||||
$rcp = new ReflectionProperty($comparisoncontext, '_path');
|
||||
$rcp->setAccessible(true);
|
||||
$rcp->setValue($comparisoncontext, $testpath);
|
||||
|
||||
$this->assertEquals($expected, $context->is_child_of($comparisoncontext, $testself));
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure that the get_parent_contexts() function limits the number of queries it performs.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user