diff --git a/public/cohort/lib.php b/public/cohort/lib.php index 3d7ef7195ac..c0403b59e55 100644 --- a/public/cohort/lib.php +++ b/public/cohort/lib.php @@ -363,7 +363,7 @@ function cohort_can_view_cohort($cohortorid, $currentcontext) { $cohort = $cohortorid; } - if ($cohort && in_array($cohort->contextid, $currentcontext->get_parent_context_ids())) { + if ($cohort && in_array($cohort->contextid, $currentcontext->get_parent_context_ids(true))) { if ($cohort->visible) { return true; } diff --git a/public/cohort/tests/lib_test.php b/public/cohort/tests/lib_test.php index ef50c134458..c7c747a48c5 100644 --- a/public/cohort/tests/lib_test.php +++ b/public/cohort/tests/lib_test.php @@ -943,6 +943,21 @@ final class lib_test extends \advanced_testcase { } } + /** + * Test whether user can view cohort + * + * @covers ::cohort_can_view_cohort + */ + public function test_cohort_can_view_cohort(): void { + $this->resetAfterTest(); + $this->setAdminUser(); + + $context = \context_system::instance(); + $cohort = $this->getDataGenerator()->create_cohort(['contextid' => $context->id]); + + $this->assertTrue(cohort_can_view_cohort($cohort, $context)); + } + /** * Test the behaviour of cohort_get_cohort(). *