Skip to content

Commit

Permalink
MDL-68974 admin: Unit tests for context parent/child test
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewnicols authored and Jenkins committed Sep 8, 2020
1 parent c828351 commit 303119a
Showing 1 changed file with 198 additions and 0 deletions.
198 changes: 198 additions & 0 deletions lib/tests/accesslib_test.php
Expand Up @@ -4103,6 +4103,204 @@ public function test_get_profile_roles() {
$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.
*/
Expand Down

0 comments on commit 303119a

Please sign in to comment.