2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Performance helper tests.
20 * @package core_competency
21 * @copyright 2016 Frédéric Massart - FMCorz.net
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') ||
die();
28 use core_competency\external\performance_helper
;
31 * Performance helper testcase.
33 * @package core_competency
34 * @copyright 2016 Frédéric Massart - FMCorz.net
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 class core_competency_performance_helper_testcase
extends advanced_testcase
{
39 public function test_get_context_from_competency() {
42 $this->resetAfterTest(true);
43 $dg = $this->getDataGenerator();
44 $lpg = $dg->get_plugin_generator('core_competency');
46 $cat1 = $dg->create_category();
47 $framework = $lpg->create_framework();
48 $competency = $lpg->create_competency(['competencyframeworkid' => $framework->get('id')]);
49 $competency2 = $lpg->create_competency(['competencyframeworkid' => $framework->get('id')]);
51 $context = $competency->get_context();
52 $helper = new performance_helper();
53 $initdbqueries = $DB->perf_get_queries();
55 // Confirm that subsequent calls return a cached object.
56 // Note that here we check that the framework is not loaded more than once.
57 // The context objects are already cached in the context layer.
58 $firstruncontext = $helper->get_context_from_competency($competency);
59 $dbqueries = $DB->perf_get_queries();
60 $this->assertSame($context, $firstruncontext);
61 $this->assertNotEquals($initdbqueries, $dbqueries);
63 $secondruncontext = $helper->get_context_from_competency($competency);
64 $this->assertSame($context, $secondruncontext);
65 $this->assertSame($firstruncontext, $secondruncontext);
66 $this->assertEquals($DB->perf_get_queries(), $dbqueries);
68 $thirdruncontext = $helper->get_context_from_competency($competency2);
69 $this->assertSame($context, $thirdruncontext);
70 $this->assertSame($secondruncontext, $thirdruncontext);
71 $this->assertEquals($DB->perf_get_queries(), $dbqueries);
74 public function test_get_framework_from_competency() {
77 $this->resetAfterTest(true);
78 $dg = $this->getDataGenerator();
79 $lpg = $dg->get_plugin_generator('core_competency');
81 $cat1 = $dg->create_category();
82 $framework1 = $lpg->create_framework();
83 $comp1a = $lpg->create_competency(['competencyframeworkid' => $framework1->get('id')]);
84 $comp1b = $lpg->create_competency(['competencyframeworkid' => $framework1->get('id')]);
85 $framework2 = $lpg->create_framework();
86 $comp2a = $lpg->create_competency(['competencyframeworkid' => $framework2->get('id')]);
88 $helper = new performance_helper();
89 $initdbqueries = $DB->perf_get_queries();
91 // Confirm that we get the right framework, and that subsequent calls
92 // do not trigger DB queries, even for other competencies.
93 $firstrunframework = $helper->get_framework_from_competency($comp1a);
94 $firstrundbqueries = $DB->perf_get_queries();
95 $this->assertNotEquals($initdbqueries, $firstrundbqueries);
96 $this->assertEquals($framework1, $firstrunframework);
97 $this->assertNotSame($framework1, $firstrunframework);
99 $secondrunframework = $helper->get_framework_from_competency($comp1b);
100 $this->assertEquals($firstrundbqueries, $DB->perf_get_queries());
101 $this->assertEquals($framework1, $secondrunframework);
102 $this->assertSame($firstrunframework, $secondrunframework);
104 $thirdrunframework = $helper->get_framework_from_competency($comp1a);
105 $this->assertEquals($firstrundbqueries, $DB->perf_get_queries());
106 $this->assertEquals($framework1, $thirdrunframework);
107 $this->assertSame($firstrunframework, $thirdrunframework);
109 // Fetch another framework.
110 $fourthrunframework = $helper->get_framework_from_competency($comp2a);
111 $fourthrundbqueries = $DB->perf_get_queries();
112 $this->assertNotEquals($firstrundbqueries, $fourthrundbqueries);
113 $this->assertEquals($framework2, $fourthrunframework);
114 $this->assertNotSame($framework2, $fourthrunframework);
116 $fifthrunframework = $helper->get_framework_from_competency($comp2a);
117 $this->assertEquals($fourthrundbqueries, $DB->perf_get_queries());
118 $this->assertEquals($framework2, $fifthrunframework);
119 $this->assertSame($fourthrunframework, $fifthrunframework);
122 public function test_get_scale_from_competency() {
125 $this->resetAfterTest(true);
126 $dg = $this->getDataGenerator();
127 $lpg = $dg->get_plugin_generator('core_competency');
129 $scale1 = $dg->create_scale();
130 $scale2 = $dg->create_scale();
131 $cat1 = $dg->create_category();
133 $framework1 = $lpg->create_framework(['scaleid' => $scale1->id
]);
134 $comp1 = $lpg->create_competency(['competencyframeworkid' => $framework1->get('id')]);
135 $comp2 = $lpg->create_competency(['competencyframeworkid' => $framework1->get('id'), 'scaleid' => $scale2->id
]);
136 $comp3 = $lpg->create_competency(['competencyframeworkid' => $framework1->get('id')]);
138 $helper = new performance_helper();
139 $initdbqueries = $DB->perf_get_queries();
141 // Get the first scale.
142 $firstrunscale = $helper->get_scale_from_competency($comp1);
143 $firstrundbqueries = $DB->perf_get_queries();
144 $this->assertNotEquals($initdbqueries, $firstrundbqueries);
145 $this->assertEquals($scale1, $firstrunscale->get_record_data());
147 $secondrunscale = $helper->get_scale_from_competency($comp3);
148 $this->assertEquals($firstrundbqueries, $DB->perf_get_queries());
149 $this->assertSame($firstrunscale, $secondrunscale);
151 // Another scale, and its subsequent calls.
152 $thirdrunscale = $helper->get_scale_from_competency($comp2);
153 $thirddbqueries = $DB->perf_get_queries();
154 $this->assertNotEquals($firstrundbqueries, $thirddbqueries);
155 $this->assertEquals($scale2, $thirdrunscale->get_record_data());
156 $this->assertSame($thirdrunscale, $helper->get_scale_from_competency($comp2));
157 $this->assertEquals($thirddbqueries, $DB->perf_get_queries());