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 401 and 402] [Versions 401 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       * Setup testcase.
  45       */
  46      public function setUp(): void {
  47          $this->resetAfterTest(true);
  48          $this->setAdminUser();
  49          $this->course = $this->getDataGenerator()->create_course();
  50          // Creates question bank view.
  51          $this->questionbank = new view(
  52              new question_edit_contexts(context_course::instance($this->course->id)),
  53              new moodle_url('/'),
  54              $this->course
  55          );
  56  
  57          // Get current view columns.
  58          $this->columns = [];
  59          foreach ($this->questionbank->get_visiblecolumns() as $columnn) {
  60              $this->columns[] = get_class($columnn);
  61          }
  62          $this->columnmanager = new column_manager();
  63      }
  64  
  65      /**
  66       * Test function get_columns in helper class, that proper data is returned.
  67       *
  68       * @covers ::get_columns
  69       */
  70      public function test_getcolumns_function(): void {
  71          $questionlistcolumns = $this->columnmanager->get_columns();
  72          $this->assertIsArray($questionlistcolumns);
  73          foreach ($questionlistcolumns as $columnnobject) {
  74              $this->assertObjectHasAttribute('class', $columnnobject);
  75              $this->assertObjectHasAttribute('name', $columnnobject);
  76              $this->assertObjectHasAttribute('colname', $columnnobject);
  77          }
  78      }
  79  
  80      /**
  81       * Test function sort columns method.
  82       *
  83       * @covers ::get_sorted_columns
  84       */
  85      public function test_get_sorted_columns(): void {
  86          $neworder = $this->columnmanager->get_sorted_columns($this->columns);
  87          shuffle($neworder);
  88          set_columnbank_order::execute($neworder);
  89          $currentconfig = get_config('qbank_columnsortorder', 'enabledcol');
  90          $currentconfig = explode(',', $currentconfig);
  91          ksort($currentconfig);
  92          $this->assertSame($neworder, $currentconfig);
  93      }
  94  
  95      /**
  96       * Test function enabing and disablingcolumns.
  97       *
  98       * @covers ::enable_columns
  99       * @covers ::disable_columns
 100       */
 101      public function test_enable_disable_columns(): void {
 102          $neworder = $this->columnmanager->get_sorted_columns($this->columns);
 103          shuffle($neworder);
 104          set_columnbank_order::execute($neworder);
 105          $currentconfig = get_config('qbank_columnsortorder', 'enabledcol');
 106          $currentconfig = explode(',', $currentconfig);
 107          $class = $currentconfig[array_rand($currentconfig, 1)];
 108          $randomplugintodisable = explode('\\', $class)[0];
 109          $olddisabledconfig = get_config('qbank_columnsortorder', 'disabledcol');
 110          $this->columnmanager->disable_columns($randomplugintodisable);
 111          $newdisabledconfig = get_config('qbank_columnsortorder', 'disabledcol');
 112          $this->assertNotEquals($olddisabledconfig, $newdisabledconfig);
 113          $this->columnmanager->enable_columns($randomplugintodisable);
 114          $newdisabledconfig = get_config('qbank_columnsortorder', 'disabledcol');
 115          $this->assertEmpty($newdisabledconfig);
 116          $enabledconfig = get_config('qbank_columnsortorder', 'enabledcol');
 117          $contains = strpos($enabledconfig, $randomplugintodisable);
 118          $this->assertNotFalse($contains);
 119          $this->assertIsInt($contains);
 120      }
 121  }