Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 400 and 402] [Versions 401 and 402] [Versions 402 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  namespace qbank_columnsortorder;
  18  
  19  defined('MOODLE_INTERNAL') || die();
  20  
  21  use advanced_testcase;
  22  use context_course;
  23  use core_question\local\bank\question_edit_contexts;
  24  use core_question\local\bank\view;
  25  use moodle_url;
  26  use qbank_columnsortorder\external\set_columnbank_order;
  27  
  28  global $CFG;
  29  require_once($CFG->dirroot . '/question/tests/fixtures/testable_core_question_column.php');
  30  require_once($CFG->dirroot . '/question/classes/external.php');
  31  
  32  /**
  33   * Test class for columnsortorder feature.
  34   *
  35   * @package    qbank_columnsortorder
  36   * @copyright  2021 Catalyst IT Australia Pty Ltd
  37   * @author     Ghaly Marc-Alexandre <marc-alexandreghaly@catalyst-ca.net>
  38   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   * @coversDefaultClass \qbank_columnsortorder\column_manager
  40   */
  41  class column_manager_test extends advanced_testcase {
  42  
  43  
  44      /** @var \stdClass course record. */
  45      protected $course;
  46  
  47      /** @var \core_question\local\bank\view  */
  48      protected $questionbank;
  49  
  50      /** @var array  */
  51      protected $columns;
  52  
  53      /** @var \qbank_columnsortorder\column_manager  */
  54      protected $columnmanager;
  55  
  56      /**
  57       * Setup testcase.
  58       */
  59      public function setUp(): void {
  60          $this->resetAfterTest(true);
  61          $this->setAdminUser();
  62          $this->course = $this->getDataGenerator()->create_course();
  63          // Creates question bank view.
  64          $this->questionbank = new view(
  65              new question_edit_contexts(context_course::instance($this->course->id)),
  66              new moodle_url('/'),
  67              $this->course
  68          );
  69  
  70          // Get current view columns.
  71          $this->columns = [];
  72          foreach ($this->questionbank->get_visiblecolumns() as $columnn) {
  73              $this->columns[] = get_class($columnn);
  74          }
  75          $this->columnmanager = new column_manager();
  76      }
  77  
  78      /**
  79       * Test function get_columns in helper class, that proper data is returned.
  80       *
  81       * @covers ::get_columns
  82       */
  83      public function test_getcolumns_function(): void {
  84          $questionlistcolumns = $this->columnmanager->get_columns();
  85          $this->assertIsArray($questionlistcolumns);
  86          foreach ($questionlistcolumns as $columnnobject) {
  87              $this->assertObjectHasAttribute('class', $columnnobject);
  88              $this->assertObjectHasAttribute('name', $columnnobject);
  89              $this->assertObjectHasAttribute('colname', $columnnobject);
  90          }
  91      }
  92  
  93      /**
  94       * Test function sort columns method.
  95       *
  96       * @covers ::get_sorted_columns
  97       */
  98      public function test_get_sorted_columns(): void {
  99          $neworder = $this->columnmanager->get_sorted_columns($this->columns);
 100          shuffle($neworder);
 101          set_columnbank_order::execute($neworder);
 102          $currentconfig = get_config('qbank_columnsortorder', 'enabledcol');
 103          $currentconfig = explode(',', $currentconfig);
 104          ksort($currentconfig);
 105          $this->assertSame($neworder, $currentconfig);
 106      }
 107  
 108      /**
 109       * Test function enabing and disablingcolumns.
 110       *
 111       * @covers ::enable_columns
 112       * @covers ::disable_columns
 113       */
 114      public function test_enable_disable_columns(): void {
 115          $neworder = $this->columnmanager->get_sorted_columns($this->columns);
 116          shuffle($neworder);
 117          set_columnbank_order::execute($neworder);
 118          $currentconfig = get_config('qbank_columnsortorder', 'enabledcol');
 119          $currentconfig = explode(',', $currentconfig);
 120          $class = $currentconfig[array_rand($currentconfig, 1)];
 121          $randomplugintodisable = explode('\\', $class)[0];
 122          $olddisabledconfig = get_config('qbank_columnsortorder', 'disabledcol');
 123          $this->columnmanager->disable_columns($randomplugintodisable);
 124          $newdisabledconfig = get_config('qbank_columnsortorder', 'disabledcol');
 125          $this->assertNotEquals($olddisabledconfig, $newdisabledconfig);
 126          $this->columnmanager->enable_columns($randomplugintodisable);
 127          $newdisabledconfig = get_config('qbank_columnsortorder', 'disabledcol');
 128          $this->assertEmpty($newdisabledconfig);
 129          $enabledconfig = get_config('qbank_columnsortorder', 'enabledcol');
 130          $contains = strpos($enabledconfig, $randomplugintodisable);
 131          $this->assertNotFalse($contains);
 132          $this->assertIsInt($contains);
 133      }
 134  }