Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 39 and 401]

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