Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403]

   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   * Performance helper tests.
  19   *
  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
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  global $CFG;
  27  
  28  use core_competency\external\performance_helper;
  29  
  30  /**
  31   * Performance helper testcase.
  32   *
  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
  36   */
  37  class core_competency_performance_helper_testcase extends advanced_testcase {
  38  
  39      public function test_get_context_from_competency() {
  40          global $DB;
  41  
  42          $this->resetAfterTest(true);
  43          $dg = $this->getDataGenerator();
  44          $lpg = $dg->get_plugin_generator('core_competency');
  45  
  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')]);
  50  
  51          $context = $competency->get_context();
  52          $helper = new performance_helper();
  53          $initdbqueries = $DB->perf_get_queries();
  54  
  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);
  62  
  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);
  67  
  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);
  72      }
  73  
  74      public function test_get_framework_from_competency() {
  75          global $DB;
  76  
  77          $this->resetAfterTest(true);
  78          $dg = $this->getDataGenerator();
  79          $lpg = $dg->get_plugin_generator('core_competency');
  80  
  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')]);
  87  
  88          $helper = new performance_helper();
  89          $initdbqueries = $DB->perf_get_queries();
  90  
  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);
  98  
  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);
 103  
 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);
 108  
 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);
 115  
 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);
 120      }
 121  
 122      public function test_get_scale_from_competency() {
 123          global $DB;
 124  
 125          $this->resetAfterTest(true);
 126          $dg = $this->getDataGenerator();
 127          $lpg = $dg->get_plugin_generator('core_competency');
 128  
 129          $scale1 = $dg->create_scale();
 130          $scale2 = $dg->create_scale();
 131          $cat1 = $dg->create_category();
 132  
 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')]);
 137  
 138          $helper = new performance_helper();
 139          $initdbqueries = $DB->perf_get_queries();
 140  
 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());
 146  
 147          $secondrunscale = $helper->get_scale_from_competency($comp3);
 148          $this->assertEquals($firstrundbqueries, $DB->perf_get_queries());
 149          $this->assertSame($firstrunscale, $secondrunscale);
 150  
 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());
 158      }
 159  }