Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 400 and 401] [Versions 400 and 402] [Versions 400 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 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_user'),
  80              ],
  81              get_string('setup', 'grades') => [
  82                  get_string('gradebooksetup', 'grades'),
  83                  get_string('coursegradesettings', 'grades'),
  84                  get_string('preferences', 'grades') . ': ' . get_string('pluginname', 'gradereport_grader'),
  85              ],
  86              get_string('moremenu') => [
  87                  get_string('scales'),
  88                  get_string('outcomes', 'grades'),
  89                  get_string('gradeletters', 'grades'),
  90                  get_string('import', 'grades'),
  91                  get_string('export', 'grades'),
  92              ],
  93          ];
  94  
  95          $this->resetAfterTest();
  96          // Reset the cache.
  97          grade_helper::reset_caches();
  98  
  99          // Create a course.
 100          $course = $this->getDataGenerator()->create_course();
 101          $coursecontext = context_course::instance($course->id);
 102  
 103          if ($userrole === 'admin') {
 104              $this->setAdminUser();
 105          } else {
 106              // Enrol user to the course.
 107              $user = $this->getDataGenerator()->create_and_enrol($course, $userrole);
 108              $this->setUser($user);
 109          }
 110  
 111          if ($enableoutcomes) {
 112              set_config('enableoutcomes', 1);
 113          }
 114  
 115          $generalactionbar = new general_action_bar($coursecontext,
 116              new moodle_url('/grade/report/user/index.php', ['id' => $course->id]), 'report', 'user');
 117          $renderer = $PAGE->get_renderer('core');
 118          $generalactionbardata = $generalactionbar->export_for_template($renderer);
 119  
 120          $this->assertCount(1, $generalactionbardata);
 121          $this->assertArrayHasKey('generalnavselector', $generalactionbardata);
 122  
 123          $generalnavselector = $generalactionbardata['generalnavselector'];
 124  
 125          // Assert that the right links are present in each group.
 126          foreach ($allcorenavlinks as $groupname => $corelinks) {
 127              $actualgroup = $this->find_option_by_name($generalnavselector->options, $groupname);
 128  
 129              if (!isset($expectedoptions[$groupname])) {
 130                  // This group should not be present.
 131                  $this->assertNull($actualgroup, "Nav link group '$groupname' should not be present, but is.");
 132                  continue;
 133              }
 134  
 135              $this->assertNotNull($actualgroup, "Nav link group '$groupname' should be present, but is not.");
 136              $this->assertTrue($actualgroup['isgroup'], "the thing claiming to be nav link group '$groupname' is not a group.");
 137  
 138              foreach ($corelinks as $corelinkname) {
 139                  $actuallink = $this->find_option_by_name($actualgroup['options'], $corelinkname);
 140  
 141                  if (!in_array($corelinkname, $expectedoptions[$groupname])) {
 142                      $this->assertNull($actuallink,
 143                              "Nav link '$corelinkname' should not be present in group '$groupname', but is.");
 144                  } else {
 145                      $this->assertNotNull($actuallink,
 146                              "Nav link '$corelinkname' should be present in group '$groupname', but is not.");
 147                  }
 148              }
 149          }
 150      }
 151  
 152      /**
 153       * Data provider for the test_export_for_template test.
 154       *
 155       * @return array
 156       */
 157      public function export_for_template_provider(): array {
 158          $graderpluginname = get_string('pluginname', 'gradereport_grader');
 159          $historypluginname = get_string('pluginname', 'gradereport_history');
 160          $outcomespluginname = get_string('pluginname', 'gradereport_outcomes');
 161          $overviewpluginname = get_string('pluginname', 'gradereport_overview');
 162          $singleviewpluginname = get_string('pluginname', 'gradereport_singleview');
 163          $userpluginname = get_string('pluginname', 'gradereport_user');
 164  
 165          $viewstr = get_string('view');
 166          $setupstr = get_string('setup', 'grades');
 167          $morestr = get_string('moremenu');
 168  
 169          $gradebooksetupstr = get_string('gradebooksetup', 'grades');
 170          $coursegradesettingsstr = get_string('coursegradesettings', 'grades');
 171          $graderpreferencesstr = get_string('preferences', 'grades') . ': ' . get_string('pluginname', 'gradereport_grader');
 172  
 173          $scalesstr = get_string('scales');
 174          $outcomesstr = get_string('outcomes', 'grades');
 175          $gradelettersstr = get_string('gradeletters', 'grades');
 176          $importstr = get_string('import', 'grades');
 177          $exportstr = get_string('export', 'grades');
 178  
 179          return [
 180              'Gradebook general navigation for admin; outcomes disabled.' => [
 181                  'admin',
 182                  false,
 183                  [
 184                      $viewstr => [
 185                          $graderpluginname,
 186                          $historypluginname,
 187                          $overviewpluginname,
 188                          $singleviewpluginname,
 189                          $userpluginname,
 190                      ],
 191                      $setupstr => [
 192                          $gradebooksetupstr,
 193                          $coursegradesettingsstr,
 194                          $graderpreferencesstr,
 195                      ],
 196                      $morestr => [
 197                          $scalesstr,
 198                          $gradelettersstr,
 199                          $importstr,
 200                          $exportstr,
 201                      ],
 202                  ],
 203              ],
 204              'Gradebook general navigation for admin; outcomes enabled.' => [
 205                  'admin',
 206                  true,
 207                  [
 208                      $viewstr => [
 209                          $graderpluginname,
 210                          $historypluginname,
 211                          $outcomespluginname,
 212                          $overviewpluginname,
 213                          $singleviewpluginname,
 214                          $userpluginname,
 215                      ],
 216                      $setupstr => [
 217                          $gradebooksetupstr,
 218                          $coursegradesettingsstr,
 219                          $graderpreferencesstr,
 220                      ],
 221                      $morestr => [
 222                          $scalesstr,
 223                          $outcomesstr,
 224                          $gradelettersstr,
 225                          $importstr,
 226                          $exportstr,
 227                      ],
 228                  ],
 229              ],
 230              'Gradebook general navigation for editing teacher; outcomes disabled.' => [
 231                  'editingteacher',
 232                  false,
 233                  [
 234                      $viewstr => [
 235                          $graderpluginname,
 236                          $historypluginname,
 237                          $overviewpluginname,
 238                          $singleviewpluginname,
 239                          $userpluginname,
 240                      ],
 241                      $setupstr => [
 242                          $gradebooksetupstr,
 243                          $coursegradesettingsstr,
 244                          $graderpreferencesstr,
 245                      ],
 246                      $morestr => [
 247                          $scalesstr,
 248                          $gradelettersstr,
 249                          $importstr,
 250                          $exportstr,
 251                      ],
 252                  ],
 253              ],
 254              'Gradebook general navigation for editing teacher; outcomes enabled.' => [
 255                  'editingteacher',
 256                  true,
 257                  [
 258                      $viewstr => [
 259                          $graderpluginname,
 260                          $historypluginname,
 261                          $outcomespluginname,
 262                          $overviewpluginname,
 263                          $singleviewpluginname,
 264                          $userpluginname,
 265                      ],
 266                      $setupstr => [
 267                          $gradebooksetupstr,
 268                          $coursegradesettingsstr,
 269                          $graderpreferencesstr,
 270                      ],
 271                      $morestr => [
 272                          $scalesstr,
 273                          $outcomesstr,
 274                          $gradelettersstr,
 275                          $importstr,
 276                          $exportstr,
 277                      ],
 278                  ],
 279              ],
 280              'Gradebook general navigation for non-editing teacher; outcomes enabled.' => [
 281                  'teacher',
 282                  true,
 283                  [
 284                      $viewstr => [
 285                          $graderpluginname,
 286                          $historypluginname,
 287                          $outcomespluginname,
 288                          $overviewpluginname,
 289                          $userpluginname,
 290                      ],
 291                      $setupstr => [
 292                          $graderpreferencesstr,
 293                      ],
 294                      $morestr => [
 295                          $exportstr,
 296                      ],
 297                  ],
 298              ],
 299              'Gradebook general navigation for student; outcomes enabled.' => [
 300                  'student',
 301                  true,
 302                  [
 303                      $viewstr => [
 304                          $overviewpluginname,
 305                          $userpluginname,
 306                      ],
 307                  ],
 308              ],
 309          ];
 310      }
 311  }