MDL-51025 tool_lp: Add context support to count_frameworks
[moodle.git] / admin / tool / lp / tests / api_test.php
CommitLineData
f610a957
FM
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * API tests.
19 *
20 * @package tool_lp
21 * @copyright 2015 Frédéric Massart - FMCorz.net
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25defined('MOODLE_INTERNAL') || die();
26global $CFG;
27
28use tool_lp\api;
29
30/**
31 * API tests.
32 *
33 * @package tool_lp
34 * @copyright 2015 Frédéric Massart - FMCorz.net
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 */
37class tool_lp_api_testcase extends advanced_testcase {
38
39 public function test_get_framework_related_contexts() {
40 $this->resetAfterTest(true);
41 $dg = $this->getDataGenerator();
42 $cat1 = $dg->create_category();
43 $cat2 = $dg->create_category(array('parent' => $cat1->id));
44 $cat3 = $dg->create_category(array('parent' => $cat2->id));
45
46 $cat1ctx = context_coursecat::instance($cat1->id);
47 $cat2ctx = context_coursecat::instance($cat2->id);
48 $cat3ctx = context_coursecat::instance($cat3->id);
49 $sysctx = context_system::instance();
50
51 $expected = array($cat1ctx->id => $cat1ctx);
52 $this->assertEquals($expected, api::get_framework_related_contexts($cat1ctx, 'self'));
53
54 $expected = array($cat1ctx->id => $cat1ctx, $cat2ctx->id => $cat2ctx, $cat3ctx->id => $cat3ctx);
55 $this->assertEquals($expected, api::get_framework_related_contexts($cat1ctx, 'children'));
56
57 $expected = array($sysctx->id => $sysctx, $cat1ctx->id => $cat1ctx, $cat2ctx->id => $cat2ctx);
58 $this->assertEquals($expected, api::get_framework_related_contexts($cat2ctx, 'parents'));
59 }
60
61 public function test_get_framework_related_contexts_with_capabilities() {
62 $this->resetAfterTest(true);
63 $dg = $this->getDataGenerator();
64 $user = $dg->create_user();
65 $cat1 = $dg->create_category();
66 $cat2 = $dg->create_category(array('parent' => $cat1->id));
67 $cat3 = $dg->create_category(array('parent' => $cat2->id));
68
69 $cat1ctx = context_coursecat::instance($cat1->id);
70 $cat2ctx = context_coursecat::instance($cat2->id);
71 $cat3ctx = context_coursecat::instance($cat3->id);
72 $sysctx = context_system::instance();
73
74 $roleallow = create_role('Allow', 'allow', 'Allow read');
75 assign_capability('tool/lp:competencyread', CAP_ALLOW, $roleallow, $sysctx->id);
76 role_assign($roleallow, $user->id, $sysctx->id);
77
78 $roleprevent = create_role('Prevent', 'prevent', 'Prevent read');
79 assign_capability('tool/lp:competencyread', CAP_PROHIBIT, $roleprevent, $sysctx->id);
80 role_assign($roleprevent, $user->id, $cat2ctx->id);
81
82 accesslib_clear_all_caches_for_unit_testing();
83 $this->setUser($user);
84 $this->assertFalse(has_capability('tool/lp:competencyread', $cat2ctx));
85
86 $requiredcap = array('tool/lp:competencyread');
87
88 $expected = array();
89 $this->assertEquals($expected, api::get_framework_related_contexts($cat2ctx, 'self', $requiredcap));
90
91 $expected = array($cat1ctx->id => $cat1ctx);
92 $this->assertEquals($expected, api::get_framework_related_contexts($cat1ctx, 'children', $requiredcap));
93
94 $expected = array($sysctx->id => $sysctx, $cat1ctx->id => $cat1ctx);
95 $this->assertEquals($expected, api::get_framework_related_contexts($cat2ctx, 'parents', $requiredcap));
96 }
97}