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]

   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_grades\output;
  18  
  19  use advanced_testcase;
  20  use grade_helper;
  21  use context_course;
  22  use moodle_url;
  23  
  24  /**
  25   * A test class used to test general_action_bar.
  26   *
  27   * @package    core_grades
  28   * @copyright  2021 Mihail Geshoski <mihail@moodle.com>
  29   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  30   */
  31  class general_action_bar_test extends advanced_testcase {
  32  
  33      /**
  34       * Load required test libraries
  35       */
  36      public static function setUpBeforeClass(): void {
  37          global $CFG;
  38          require_once("{$CFG->dirroot}/grade/lib.php");
  39      }
  40  
  41      /**
  42       * Search array $options for an element which is an array containing 'name' => $name.
  43       *
  44       * @param array $options the array of options.
  45       * @param string $name the name to find.
  46       * @return array|null the particular option if found, else null.
  47       */
  48      protected function find_option_by_name(array $options, string $name): ?array {
  49          foreach ($options as $option) {
  50              if ($option['name'] == $name) {
  51                  return $option;
  52              }
  53          }
  54          return null;
  55      }
  56  
  57      /**
  58       * Test the exported data for the general action bar for different user roles and settings.
  59       *
  60       * @dataProvider export_for_template_provider
  61       * @param string $userrole The user role to test
  62       * @param bool $enableoutcomes Whether to enable outcomes
  63       * @param array $expectedoptions The expected options returned in the general action selector
  64       * @covers \core_grades\output\general_action_bar::export_for_template
  65       */
  66      public function test_export_for_template(string $userrole, bool $enableoutcomes, array $expectedoptions): void {
  67          global $PAGE;
  68  
  69          // There may be additional plugins installed in the codebase where this
  70          // test is being run, therefore, we need to know which links can be
  71          // present in a standard Moodle install, and only check them.
  72          $allcorenavlinks = [
  73              get_string('view') => [
  74                  get_string('pluginname', 'gradereport_grader'),
  75                  get_string('pluginname', 'gradereport_history'),
  76                  get_string('pluginname', 'gradereport_outcomes'),
  77                  get_string('pluginname', 'gradereport_overview'),
  78                  get_string('pluginname', 'gradereport_singleview'),
  79                  get_string('pluginname', 'gradereport_summary'),
  80                  get_string('pluginname', 'gradereport_user'),
  81              ],
  82              get_string('setup', 'grades') => [
  83                  get_string('gradebooksetup', 'grades'),
  84                  get_string('coursegradesettings', 'grades'),
  85                  get_string('preferences', 'grades') . ': ' . get_string('pluginname', 'gradereport_grader'),
  86              ],
  87              get_string('moremenu') => [
  88                  get_string('scales'),
  89                  get_string('outcomes', 'grades'),
  90                  get_string('gradeletters', 'grades'),
  91                  get_string('import', 'grades'),
  92                  get_string('export', 'grades'),
  93              ],
  94          ];
  95  
  96          $this->resetAfterTest();
  97          // Reset the cache.
  98          grade_helper::reset_caches();
  99  
 100          // Create a course.
 101          $course = $this->getDataGenerator()->create_course();
 102          $coursecontext = context_course::instance($course->id);
 103  
 104          if ($userrole === 'admin') {
 105              $this->setAdminUser();
 106          } else {
 107              // Enrol user to the course.
 108              $user = $this->getDataGenerator()->create_and_enrol($course, $userrole);
 109              $this->setUser($user);
 110          }
 111  
 112          if ($enableoutcomes) {
 113              set_config('enableoutcomes', 1);
 114          }
 115  
 116          $generalactionbar = new general_action_bar($coursecontext,
 117              new moodle_url('/grade/report/user/index.php', ['id' => $course->id]), 'report', 'user');
 118          $renderer = $PAGE->get_renderer('core');
 119          $generalactionbardata = $generalactionbar->export_for_template($renderer);
 120  
 121          $this->assertCount(1, $generalactionbardata);
 122          $this->assertArrayHasKey('generalnavselector', $generalactionbardata);
 123  
 124          $generalnavselector = $generalactionbardata['generalnavselector'];
 125  
 126          // Assert that the right links are present in each group.
 127          foreach ($allcorenavlinks as $groupname => $corelinks) {
 128              $actualgroup = $this->find_option_by_name($generalnavselector->options, $groupname);
 129  
 130              if (!isset($expectedoptions[$groupname])) {
 131                  // This group should not be present.
 132                  $this->assertNull($actualgroup, "Nav link group '$groupname' should not be present, but is.");
 133                  continue;
 134              }
 135  
 136              $this->assertNotNull($actualgroup, "Nav link group '$groupname' should be present, but is not.");
 137              $this->assertTrue($actualgroup['isgroup'], "the thing claiming to be nav link group '$groupname' is not a group.");
 138  
 139              foreach ($corelinks as $corelinkname) {
 140                  $actuallink = $this->find_option_by_name($actualgroup['options'], $corelinkname);
 141  
 142                  if (!in_array($corelinkname, $expectedoptions[$groupname])) {
 143                      $this->assertNull($actuallink,
 144                              "Nav link '$corelinkname' should not be present in group '$groupname', but is.");
 145                  } else {
 146                      $this->assertNotNull($actuallink,
 147                              "Nav link '$corelinkname' should be present in group '$groupname', but is not.");
 148                  }
 149              }
 150          }
 151      }
 152  
 153      /**
 154       * Data provider for the test_export_for_template test.
 155       *
 156       * @return array
 157       */
 158      public function export_for_template_provider(): array {
 159          $graderpluginname = get_string('pluginname', 'gradereport_grader');
 160          $historypluginname = get_string('pluginname', 'gradereport_history');
 161          $outcomespluginname = get_string('pluginname', 'gradereport_outcomes');
 162          $overviewpluginname = get_string('pluginname', 'gradereport_overview');
 163          $singleviewpluginname = get_string('pluginname', 'gradereport_singleview');
 164          $summarypluginname = get_string('pluginname', 'gradereport_summary');
 165          $userpluginname = get_string('pluginname', 'gradereport_user');
 166  
 167          $viewstr = get_string('view');
 168          $setupstr = get_string('setup', 'grades');
 169          $morestr = get_string('moremenu');
 170  
 171          $gradebooksetupstr = get_string('gradebooksetup', 'grades');
 172          $coursegradesettingsstr = get_string('coursegradesettings', 'grades');
 173          $graderpreferencesstr = get_string('preferences', 'grades') . ': ' . get_string('pluginname', 'gradereport_grader');
 174  
 175          $scalesstr = get_string('scales');
 176          $outcomesstr = get_string('outcomes', 'grades');
 177          $gradelettersstr = get_string('gradeletters', 'grades');
 178          $importstr = get_string('import', 'grades');
 179          $exportstr = get_string('export', 'grades');
 180  
 181          return [
 182              'Gradebook general navigation for admin; outcomes disabled.' => [
 183                  'admin',
 184                  false,
 185                  [
 186                      $viewstr => [
 187                          $graderpluginname,
 188                          $historypluginname,
 189                          $overviewpluginname,
 190                          $singleviewpluginname,
 191                          $summarypluginname,
 192                          $userpluginname,
 193                      ],
 194                      $setupstr => [
 195                          $gradebooksetupstr,
 196                          $coursegradesettingsstr,
 197                          $graderpreferencesstr,
 198                      ],
 199                      $morestr => [
 200                          $scalesstr,
 201                          $gradelettersstr,
 202                          $importstr,
 203                          $exportstr,
 204                      ],
 205                  ],
 206              ],
 207              'Gradebook general navigation for admin; outcomes enabled.' => [
 208                  'admin',
 209                  true,
 210                  [
 211                      $viewstr => [
 212                          $graderpluginname,
 213                          $historypluginname,
 214                          $outcomespluginname,
 215                          $overviewpluginname,
 216                          $singleviewpluginname,
 217                          $summarypluginname,
 218                          $userpluginname,
 219                      ],
 220                      $setupstr => [
 221                          $gradebooksetupstr,
 222                          $coursegradesettingsstr,
 223                          $graderpreferencesstr,
 224                      ],
 225                      $morestr => [
 226                          $scalesstr,
 227                          $outcomesstr,
 228                          $gradelettersstr,
 229                          $importstr,
 230                          $exportstr,
 231                      ],
 232                  ],
 233              ],
 234              'Gradebook general navigation for editing teacher; outcomes disabled.' => [
 235                  'editingteacher',
 236                  false,
 237                  [
 238                      $viewstr => [
 239                          $graderpluginname,
 240                          $historypluginname,
 241                          $overviewpluginname,
 242                          $singleviewpluginname,
 243                          $summarypluginname,
 244                          $userpluginname,
 245                      ],
 246                      $setupstr => [
 247                          $gradebooksetupstr,
 248                          $coursegradesettingsstr,
 249                          $graderpreferencesstr,
 250                      ],
 251                      $morestr => [
 252                          $scalesstr,
 253                          $gradelettersstr,
 254                          $importstr,
 255                          $exportstr,
 256                      ],
 257                  ],
 258              ],
 259              'Gradebook general navigation for editing teacher; outcomes enabled.' => [
 260                  'editingteacher',
 261                  true,
 262                  [
 263                      $viewstr => [
 264                          $graderpluginname,
 265                          $historypluginname,
 266                          $outcomespluginname,
 267                          $overviewpluginname,
 268                          $singleviewpluginname,
 269                          $summarypluginname,
 270                          $userpluginname,
 271                      ],
 272                      $setupstr => [
 273                          $gradebooksetupstr,
 274                          $coursegradesettingsstr,
 275                          $graderpreferencesstr,
 276                      ],
 277                      $morestr => [
 278                          $scalesstr,
 279                          $outcomesstr,
 280                          $gradelettersstr,
 281                          $importstr,
 282                          $exportstr,
 283                      ],
 284                  ],
 285              ],
 286              'Gradebook general navigation for non-editing teacher; outcomes enabled.' => [
 287                  'teacher',
 288                  true,
 289                  [
 290                      $viewstr => [
 291                          $graderpluginname,
 292                          $historypluginname,
 293                          $outcomespluginname,
 294                          $overviewpluginname,
 295                          $summarypluginname,
 296                          $userpluginname,
 297                      ],
 298                      $setupstr => [
 299                          $graderpreferencesstr,
 300                      ],
 301                      $morestr => [
 302                          $exportstr,
 303                      ],
 304                  ],
 305              ],
 306              'Gradebook general navigation for student; outcomes enabled.' => [
 307                  'student',
 308                  true,
 309                  [
 310                      $viewstr => [
 311                          $overviewpluginname,
 312                          $userpluginname,
 313                      ],
 314                  ],
 315              ],
 316          ];
 317      }
 318  }