From d33725bd2d7eff5d301d3e4a6a6a9adc8eb2e385 Mon Sep 17 00:00:00 2001 From: tjhunt Date: Mon, 12 May 2008 15:28:34 +0000 Subject: [PATCH] New convinience function in accesslib: get_parent_contextid. Also, the start of some unit tests for accesslib, but only this trivial funciton, and the get_parent_contexts function it is based on. Still, it is a start. --- lib/accesslib.php | 14 +++++++++ lib/simpletest/testaccesslib.php | 49 ++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 lib/simpletest/testaccesslib.php diff --git a/lib/accesslib.php b/lib/accesslib.php index 22b7c477e2..205d405564 100755 --- a/lib/accesslib.php +++ b/lib/accesslib.php @@ -3555,6 +3555,20 @@ function get_parent_contexts($context) { return array_reverse($parentcontexts); } +/** + * Return the id of the parent of this context, or false if there is no parent (only happens if this + * is the site context.) + * + * @param object $context + * @return integer the id of the parent context. + */ +function get_parent_contextid($context) { + $parentcontexts = get_parent_contexts($context); + if (count($parentcontexts) == 0) { + return false; + } + return array_shift($parentcontexts); +} /** * Recursive function which, given a context, find all its children context ids. diff --git a/lib/simpletest/testaccesslib.php b/lib/simpletest/testaccesslib.php new file mode 100644 index 0000000000..db04a70ba5 --- /dev/null +++ b/lib/simpletest/testaccesslib.php @@ -0,0 +1,49 @@ +assertEqual(get_parent_contexts($context), array()); + + $context = new stdClass; + $context->path = '/1/25'; + $this->assertEqual(get_parent_contexts($context), array(1)); + + $context = new stdClass; + $context->path = '/1/123/234/345/456'; + $this->assertEqual(get_parent_contexts($context), array(345, 234, 123, 1)); + } + + function test_get_parent_contextid() { + $context = get_context_instance(CONTEXT_SYSTEM); + $this->assertFalse(get_parent_contextid($context)); + + $context = new stdClass; + $context->path = '/1/25'; + $this->assertEqual(get_parent_contextid($context), 1); + + $context = new stdClass; + $context->path = '/1/123/234/345/456'; + $this->assertEqual(get_parent_contextid($context), 345); + } +} +?> -- 2.39.5