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.
   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_badges\output;
  18  
  19  defined('MOODLE_INTERNAL') || die();
  20  global $CFG;
  21  require_once($CFG->libdir . '/badgeslib.php');
  22  
  23  /**
  24   * Class manage_badge_action_bar_test
  25   *
  26   * Unit test for the badges tertiary navigation
  27   *
  28   * @coversDefaultClass \core_badges\output\manage_badge_action_bar
  29   * @package     core_badges
  30   * @copyright   2021 onwards Peter Dias
  31   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32   */
  33  class manage_badge_action_bar_test extends \advanced_testcase {
  34      /**
  35       * Data provider for test_generate_badge_navigation
  36       *
  37       * @return array
  38       */
  39      public function generate_badge_navigation_provider(): array {
  40          return [
  41              "Test tertiary nav as an editing teacher" => [
  42                  "editingteacher", [
  43                      'Overview',
  44                      'Edit details',
  45                      'Criteria',
  46                      'Message',
  47                      'Recipients (0)',
  48                      'Endorsement',
  49                      'Related badges (0)',
  50                      'Alignments (0)'
  51                  ],
  52              ],
  53              "Test tertiary nav as an non-editing teacher" => [
  54                  "teacher", [
  55                      'Overview',
  56                      'Recipients (0)'
  57                  ],
  58              ],
  59              "Test tertiary nav as an admin" => [
  60                  "admin", [
  61                      'Overview',
  62                      'Edit details',
  63                      'Criteria',
  64                      'Message',
  65                      'Recipients (0)',
  66                      'Endorsement',
  67                      'Related badges (0)',
  68                      'Alignments (0)'
  69                  ]
  70              ],
  71              "Test tertiary nav as a student" => [
  72                  "student", [
  73                      'Overview'
  74                  ]
  75              ]
  76          ];
  77      }
  78  
  79      /**
  80       * Test the generate_badge_navigation function
  81       *
  82       * @dataProvider generate_badge_navigation_provider
  83       * @param string $role
  84       * @param array $expected
  85       * @covers ::generate_badge_navigation
  86       */
  87      public function test_generate_badge_navigation(string $role, array $expected) {
  88          global $DB;
  89          $this->resetAfterTest();
  90          $course = $this->getDataGenerator()->create_course();
  91          $teacher = self::getDataGenerator()->create_and_enrol($course, 'editingteacher');
  92          if ($role != 'admin') {
  93              $user = $this->getDataGenerator()->create_and_enrol($course, $role);
  94              $this->setUser($user);
  95          } else {
  96              $this->setAdminUser();
  97          }
  98  
  99          // Mock up a course badge.
 100          $now = time();
 101          $badge = new \stdClass();
 102          $badge->id = null;
 103          $badge->name = "Test badge course";
 104          $badge->description = "Testing badges course";
 105          $badge->type = BADGE_TYPE_COURSE;
 106          $badge->courseid = $course->id;
 107          $badge->timecreated = $now - 12;
 108          $badge->timemodified = $now - 12;
 109          $badge->usercreated = $teacher->id;
 110          $badge->usermodified = $teacher->id;
 111          $badge->issuername = "Test issuer";
 112          $badge->issuerurl = "http://issuer-url.domain.co.nz";
 113          $badge->issuercontact = "issuer@example.com";
 114          $badge->expiredate = null;
 115          $badge->expireperiod = null;
 116          $badge->messagesubject = "Test message subject for badge";
 117          $badge->message = "Test message body for badge";
 118          $badge->attachment = 1;
 119          $badge->notification = 0;
 120          $badge->status = BADGE_STATUS_ACTIVE;
 121          $badge->version = '1';
 122          $badge->language = 'en';
 123          $badge->imageauthorname = 'Image author';
 124          $badge->imageauthoremail = 'imageauthor@example.com';
 125          $badge->imageauthorurl = 'http://image-author-url.domain.co.nz';
 126          $badge->imagecaption = 'Caption';
 127          $coursebadgeid = $DB->insert_record('badge', $badge, true);
 128          $badge = new \core_badges\badge($coursebadgeid);
 129  
 130          $context = \context_course::instance($course->id);
 131          $page = new \moodle_page();
 132          $page->set_context($context);
 133          $actionbar = new manage_badge_action_bar($badge, $page);
 134  
 135          $rc = new \ReflectionClass(manage_badge_action_bar::class);
 136          $rcm = $rc->getMethod('generate_badge_navigation');
 137          $rcm->setAccessible(true);
 138          $content = $rcm->invoke($actionbar);
 139          $this->assertEquals($expected, array_values($content));
 140      }
 141  }