Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 39 and 311]

   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 mod_workshop;
  18  
  19  use testable_workshop;
  20  
  21  defined('MOODLE_INTERNAL') || die();
  22  
  23  global $CFG;
  24  
  25  require_once($CFG->dirroot . '/mod/workshop/locallib.php');
  26  require_once (__DIR__ . '/fixtures/testable.php');
  27  require_once($CFG->dirroot . '/mod/workshop/classes/portfolio_caller.php');
  28  
  29  /**
  30   * Unit tests for mod_workshop_portfolio_caller class
  31   *
  32   * @package    mod_workshop
  33   * @copyright  2016 An Pham Van <an.phamvan@harveynash.vn>
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class portfolio_caller_test extends \advanced_testcase {
  37  
  38      /** @var \stdClass $workshop Basic workshop data stored in an object. */
  39      protected $workshop;
  40      /** @var stdClass mod info */
  41      protected $cm;
  42  
  43      /**
  44       * Setup testing environment.
  45       */
  46      protected function setUp(): void {
  47          parent::setUp();
  48          $this->setAdminUser();
  49          $course = $this->getDataGenerator()->create_course();
  50          $workshop = $this->getDataGenerator()->create_module('workshop', ['course' => $course]);
  51          $this->cm = get_coursemodule_from_instance('workshop', $workshop->id, $course->id, false, MUST_EXIST);
  52          $this->workshop = new testable_workshop($workshop, $this->cm, $course);
  53      }
  54  
  55      /**
  56       * Tear down.
  57       */
  58      protected function tearDown(): void {
  59          $this->workshop = null;
  60          $this->cm = null;
  61          parent::tearDown();
  62      }
  63  
  64      /**
  65       * Test the method mod_workshop_portfolio_caller::load_data()
  66       */
  67      public function test_load_data() {
  68          $this->resetAfterTest(true);
  69  
  70          $student1 = $this->getDataGenerator()->create_user();
  71          $student2 = $this->getDataGenerator()->create_user();
  72          $this->getDataGenerator()->enrol_user($student1->id, $this->workshop->course->id);
  73          $this->getDataGenerator()->enrol_user($student2->id, $this->workshop->course->id);
  74          $workshopgenerator = $this->getDataGenerator()->get_plugin_generator('mod_workshop');
  75          $subid1 = $workshopgenerator->create_submission($this->workshop->id, $student1->id);
  76          $asid1 = $workshopgenerator->create_assessment($subid1, $student2->id);
  77  
  78          $portfoliocaller = new \mod_workshop_portfolio_caller(['id' => $this->workshop->cm->id, 'submissionid' => $subid1]);
  79          $portfoliocaller->set_formats_from_button([]);
  80          $portfoliocaller->load_data();
  81  
  82          $reflector = new \ReflectionObject($portfoliocaller);
  83          $propertysubmission = $reflector->getProperty('submission');
  84          $propertysubmission->setAccessible(true);
  85          $submission = $propertysubmission->getValue($portfoliocaller);
  86  
  87          $this->assertEquals($subid1, $submission->id);
  88      }
  89  
  90      /**
  91       * Test the method mod_workshop_portfolio_caller::get_return_url()
  92       */
  93      public function test_get_return_url() {
  94          $this->resetAfterTest(true);
  95  
  96          $student1 = $this->getDataGenerator()->create_user();
  97          $this->getDataGenerator()->enrol_user($student1->id, $this->workshop->course->id);
  98          $workshopgenerator = $this->getDataGenerator()->get_plugin_generator('mod_workshop');
  99          $subid1 = $workshopgenerator->create_submission($this->workshop->id, $student1->id);
 100  
 101          $portfoliocaller = new \mod_workshop_portfolio_caller(['id' => $this->workshop->cm->id, 'submissionid' => $subid1]);
 102          $portfoliocaller->set_formats_from_button([]);
 103          $portfoliocaller->load_data();
 104  
 105          $expected = new \moodle_url('/mod/workshop/submission.php', ['cmid' => $this->workshop->cm->id, 'id' => $subid1]);
 106          $actual = new \moodle_url($portfoliocaller->get_return_url());
 107          $this->assertTrue($expected->compare($actual));
 108      }
 109  
 110      /**
 111       * Test the method mod_workshop_portfolio_caller::get_navigation()
 112       */
 113      public function test_get_navigation() {
 114          $this->resetAfterTest(true);
 115  
 116          $student1 = $this->getDataGenerator()->create_user();
 117          $this->getDataGenerator()->enrol_user($student1->id, $this->workshop->course->id);
 118          $workshopgenerator = $this->getDataGenerator()->get_plugin_generator('mod_workshop');
 119          $subid1 = $workshopgenerator->create_submission($this->workshop->id, $student1->id);
 120  
 121          $portfoliocaller = new \mod_workshop_portfolio_caller(['id' => $this->workshop->cm->id, 'submissionid' => $subid1]);
 122          $portfoliocaller->set_formats_from_button([]);
 123          $portfoliocaller->load_data();
 124  
 125          $this->assertTrue(is_array($portfoliocaller->get_navigation()));
 126      }
 127  
 128      /**
 129       * Test the method mod_workshop_portfolio_caller::check_permissions()
 130       */
 131      public function test_check_permissions_exportownsubmissionassessment() {
 132          global $DB;
 133          $this->resetAfterTest(true);
 134  
 135          $context = \context_module::instance($this->cm->id);
 136          $student1 = $this->getDataGenerator()->create_user();
 137          $student2 = $this->getDataGenerator()->create_user();
 138          $roleids = $DB->get_records_menu('role', null, '', 'shortname, id');
 139          $this->getDataGenerator()->enrol_user($student1->id, $this->workshop->course->id, $roleids['student']);
 140          $this->getDataGenerator()->enrol_user($student2->id, $this->workshop->course->id, $roleids['student']);
 141          $workshopgenerator = $this->getDataGenerator()->get_plugin_generator('mod_workshop');
 142          $subid1 = $workshopgenerator->create_submission($this->workshop->id, $student1->id);
 143          $asid1 = $workshopgenerator->create_assessment($subid1, $student2->id);
 144          $this->setUser($student1);
 145  
 146          $portfoliocaller = new \mod_workshop_portfolio_caller(['id' => $this->workshop->cm->id, 'submissionid' => $subid1]);
 147  
 148          role_change_permission($roleids['student'], $context, 'mod/workshop:exportsubmissions', CAP_PREVENT);
 149          $this->assertFalse($portfoliocaller->check_permissions());
 150  
 151          role_change_permission($roleids['student'], $context, 'mod/workshop:exportsubmissions', CAP_ALLOW);
 152          $this->assertTrue($portfoliocaller->check_permissions());
 153      }
 154  
 155      /**
 156       * Test the method mod_workshop_portfolio_caller::get_sha1()
 157       */
 158      public function test_get_sha1() {
 159          $this->resetAfterTest(true);
 160  
 161          $student1 = $this->getDataGenerator()->create_user();
 162          $student2 = $this->getDataGenerator()->create_user();
 163          $this->getDataGenerator()->enrol_user($student1->id, $this->workshop->course->id);
 164          $this->getDataGenerator()->enrol_user($student2->id, $this->workshop->course->id);
 165          $workshopgenerator = $this->getDataGenerator()->get_plugin_generator('mod_workshop');
 166          $subid1 = $workshopgenerator->create_submission($this->workshop->id, $student1->id);
 167          $asid1 = $workshopgenerator->create_assessment($subid1, $student2->id);
 168  
 169          $portfoliocaller = new \mod_workshop_portfolio_caller(['id' => $this->workshop->cm->id, 'submissionid' => $subid1]);
 170          $portfoliocaller->set_formats_from_button([]);
 171          $portfoliocaller->load_data();
 172  
 173          $this->assertTrue(is_string($portfoliocaller->get_sha1()));
 174      }
 175  
 176      /**
 177       * Test function display_name()
 178       * Assert that this function can return the name of the module ('Workshop').
 179       */
 180      public function test_display_name() {
 181          $this->resetAfterTest(true);
 182  
 183          $name = \mod_workshop_portfolio_caller::display_name();
 184          $this->assertEquals(get_string('pluginname', 'mod_workshop'), $name);
 185      }
 186  }