Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]

   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  /**
  18   * Unit tests for workshop api class defined in mod/workshop/locallib.php
  19   *
  20   * @package    mod_workshop
  21   * @category   phpunit
  22   * @copyright  2009 David Mudrak <david.mudrak@gmail.com>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  global $CFG;
  29  require_once($CFG->dirroot . '/mod/workshop/locallib.php'); // Include the code to test
  30  require_once (__DIR__ . '/fixtures/testable.php');
  31  
  32  
  33  /**
  34   * Test cases for the internal workshop api
  35   */
  36  class mod_workshop_internal_api_testcase extends advanced_testcase {
  37  
  38      /** @var object */
  39      protected $course;
  40  
  41      /** @var workshop */
  42      protected $workshop;
  43  
  44      /** setup testing environment */
  45      protected function setUp(): void {
  46          parent::setUp();
  47          $this->setAdminUser();
  48          $this->course = $this->getDataGenerator()->create_course();
  49          $workshop = $this->getDataGenerator()->create_module('workshop', array('course' => $this->course));
  50          $cm = get_coursemodule_from_instance('workshop', $workshop->id, $this->course->id, false, MUST_EXIST);
  51          $this->workshop = new testable_workshop($workshop, $cm, $this->course);
  52      }
  53  
  54      protected function tearDown(): void {
  55          $this->workshop = null;
  56          parent::tearDown();
  57      }
  58  
  59      public function test_aggregate_submission_grades_process_notgraded() {
  60          $this->resetAfterTest(true);
  61  
  62          // fixture set-up
  63          $batch = array();   // batch of a submission's assessments
  64          $batch[] = (object)array('submissionid' => 12, 'submissiongrade' => null, 'weight' => 1, 'grade' => null);
  65          //$DB->expectNever('update_record');
  66          // exercise SUT
  67          $this->workshop->aggregate_submission_grades_process($batch);
  68      }
  69  
  70      public function test_aggregate_submission_grades_process_single() {
  71          $this->resetAfterTest(true);
  72  
  73          // fixture set-up
  74          $batch = array();   // batch of a submission's assessments
  75          $batch[] = (object)array('submissionid' => 12, 'submissiongrade' => null, 'weight' => 1, 'grade' => 10.12345);
  76          $expected = 10.12345;
  77          //$DB->expectOnce('update_record');
  78          // exercise SUT
  79          $this->workshop->aggregate_submission_grades_process($batch);
  80      }
  81  
  82      public function test_aggregate_submission_grades_process_null_doesnt_influence() {
  83          $this->resetAfterTest(true);
  84  
  85          // fixture set-up
  86          $batch = array();   // batch of a submission's assessments
  87          $batch[] = (object)array('submissionid' => 12, 'submissiongrade' => null, 'weight' => 1, 'grade' => 45.54321);
  88          $batch[] = (object)array('submissionid' => 12, 'submissiongrade' => null, 'weight' => 1, 'grade' => null);
  89          $expected = 45.54321;
  90          //$DB->expectOnce('update_record');
  91          // exercise SUT
  92          $this->workshop->aggregate_submission_grades_process($batch);
  93      }
  94  
  95      public function test_aggregate_submission_grades_process_weighted_single() {
  96          $this->resetAfterTest(true);
  97  
  98          // fixture set-up
  99          $batch = array();   // batch of a submission's assessments
 100          $batch[] = (object)array('submissionid' => 12, 'submissiongrade' => null, 'weight' => 4, 'grade' => 14.00012);
 101          $expected = 14.00012;
 102          //$DB->expectOnce('update_record');
 103          // exercise SUT
 104          $this->workshop->aggregate_submission_grades_process($batch);
 105      }
 106  
 107      public function test_aggregate_submission_grades_process_mean() {
 108          $this->resetAfterTest(true);
 109  
 110          // fixture set-up
 111          $batch = array();   // batch of a submission's assessments
 112          $batch[] = (object)array('submissionid' => 45, 'submissiongrade' => null, 'weight' => 1, 'grade' => 56.12000);
 113          $batch[] = (object)array('submissionid' => 45, 'submissiongrade' => null, 'weight' => 1, 'grade' => 12.59000);
 114          $batch[] = (object)array('submissionid' => 45, 'submissiongrade' => null, 'weight' => 1, 'grade' => 10.00000);
 115          $batch[] = (object)array('submissionid' => 45, 'submissiongrade' => null, 'weight' => 1, 'grade' => 0.00000);
 116          $expected = 19.67750;
 117          //$DB->expectOnce('update_record');
 118          // exercise SUT
 119          $this->workshop->aggregate_submission_grades_process($batch);
 120      }
 121  
 122      public function test_aggregate_submission_grades_process_mean_changed() {
 123          $this->resetAfterTest(true);
 124  
 125          // fixture set-up
 126          $batch = array();   // batch of a submission's assessments
 127          $batch[] = (object)array('submissionid' => 45, 'submissiongrade' => 12.57750, 'weight' => 1, 'grade' => 56.12000);
 128          $batch[] = (object)array('submissionid' => 45, 'submissiongrade' => 12.57750, 'weight' => 1, 'grade' => 12.59000);
 129          $batch[] = (object)array('submissionid' => 45, 'submissiongrade' => 12.57750, 'weight' => 1, 'grade' => 10.00000);
 130          $batch[] = (object)array('submissionid' => 45, 'submissiongrade' => 12.57750, 'weight' => 1, 'grade' => 0.00000);
 131          $expected = 19.67750;
 132          //$DB->expectOnce('update_record');
 133          // exercise SUT
 134          $this->workshop->aggregate_submission_grades_process($batch);
 135      }
 136  
 137      public function test_aggregate_submission_grades_process_mean_nochange() {
 138          $this->resetAfterTest(true);
 139  
 140          // fixture set-up
 141          $batch = array();   // batch of a submission's assessments
 142          $batch[] = (object)array('submissionid' => 45, 'submissiongrade' => 19.67750, 'weight' => 1, 'grade' => 56.12000);
 143          $batch[] = (object)array('submissionid' => 45, 'submissiongrade' => 19.67750, 'weight' => 1, 'grade' => 12.59000);
 144          $batch[] = (object)array('submissionid' => 45, 'submissiongrade' => 19.67750, 'weight' => 1, 'grade' => 10.00000);
 145          $batch[] = (object)array('submissionid' => 45, 'submissiongrade' => 19.67750, 'weight' => 1, 'grade' => 0.00000);
 146          //$DB->expectNever('update_record');
 147          // exercise SUT
 148          $this->workshop->aggregate_submission_grades_process($batch);
 149      }
 150  
 151      public function test_aggregate_submission_grades_process_rounding() {
 152          $this->resetAfterTest(true);
 153  
 154          // fixture set-up
 155          $batch = array();   // batch of a submission's assessments
 156          $batch[] = (object)array('submissionid' => 45, 'submissiongrade' => null, 'weight' => 1, 'grade' => 4.00000);
 157          $batch[] = (object)array('submissionid' => 45, 'submissiongrade' => null, 'weight' => 1, 'grade' => 2.00000);
 158          $batch[] = (object)array('submissionid' => 45, 'submissiongrade' => null, 'weight' => 1, 'grade' => 1.00000);
 159          $expected = 2.33333;
 160          //$DB->expectOnce('update_record');
 161          // exercise SUT
 162          $this->workshop->aggregate_submission_grades_process($batch);
 163      }
 164  
 165      public function test_aggregate_submission_grades_process_weighted_mean() {
 166          $this->resetAfterTest(true);
 167  
 168          // fixture set-up
 169          $batch = array();   // batch of a submission's assessments
 170          $batch[] = (object)array('submissionid' => 45, 'submissiongrade' => null, 'weight' => 3, 'grade' => 12.00000);
 171          $batch[] = (object)array('submissionid' => 45, 'submissiongrade' => null, 'weight' => 2, 'grade' => 30.00000);
 172          $batch[] = (object)array('submissionid' => 45, 'submissiongrade' => null, 'weight' => 1, 'grade' => 10.00000);
 173          $batch[] = (object)array('submissionid' => 45, 'submissiongrade' => null, 'weight' => 0, 'grade' => 1000.00000);
 174          $expected = 17.66667;
 175          //$DB->expectOnce('update_record');
 176          // exercise SUT
 177          $this->workshop->aggregate_submission_grades_process($batch);
 178      }
 179  
 180      public function test_aggregate_grading_grades_process_nograding() {
 181          $this->resetAfterTest(true);
 182          // fixture set-up
 183          $batch = array();
 184          $batch[] = (object)array('reviewerid'=>2, 'gradinggrade'=>null, 'gradinggradeover'=>null, 'aggregationid'=>null, 'aggregatedgrade'=>null);
 185          // expectation
 186          //$DB->expectNever('update_record');
 187          // excersise SUT
 188          $this->workshop->aggregate_grading_grades_process($batch);
 189      }
 190  
 191      public function test_aggregate_grading_grades_process_single_grade_new() {
 192          $this->resetAfterTest(true);
 193          // fixture set-up
 194          $batch = array();
 195          $batch[] = (object)array('reviewerid'=>3, 'gradinggrade'=>82.87670, 'gradinggradeover'=>null, 'aggregationid'=>null, 'aggregatedgrade'=>null);
 196          // expectation
 197          $now = time();
 198          $expected = new stdclass();
 199          $expected->workshopid = $this->workshop->id;
 200          $expected->userid = 3;
 201          $expected->gradinggrade = 82.87670;
 202          $expected->timegraded = $now;
 203          //$DB->expectOnce('insert_record', array('workshop_aggregations', $expected));
 204          // excersise SUT
 205          $this->workshop->aggregate_grading_grades_process($batch, $now);
 206      }
 207  
 208      public function test_aggregate_grading_grades_process_single_grade_update() {
 209          $this->resetAfterTest(true);
 210          // fixture set-up
 211          $batch = array();
 212          $batch[] = (object)array('reviewerid'=>3, 'gradinggrade'=>90.00000, 'gradinggradeover'=>null, 'aggregationid'=>1, 'aggregatedgrade'=>82.87670);
 213          // expectation
 214          //$DB->expectOnce('update_record');
 215          // excersise SUT
 216          $this->workshop->aggregate_grading_grades_process($batch);
 217      }
 218  
 219      public function test_aggregate_grading_grades_process_single_grade_uptodate() {
 220          $this->resetAfterTest(true);
 221          // fixture set-up
 222          $batch = array();
 223          $batch[] = (object)array('reviewerid'=>3, 'gradinggrade'=>90.00000, 'gradinggradeover'=>null, 'aggregationid'=>1, 'aggregatedgrade'=>90.00000);
 224          // expectation
 225          //$DB->expectNever('update_record');
 226          // excersise SUT
 227          $this->workshop->aggregate_grading_grades_process($batch);
 228      }
 229  
 230      public function test_aggregate_grading_grades_process_single_grade_overridden() {
 231          $this->resetAfterTest(true);
 232          // fixture set-up
 233          $batch = array();
 234          $batch[] = (object)array('reviewerid'=>4, 'gradinggrade'=>91.56700, 'gradinggradeover'=>82.32105, 'aggregationid'=>2, 'aggregatedgrade'=>91.56700);
 235          // expectation
 236          //$DB->expectOnce('update_record');
 237          // excersise SUT
 238          $this->workshop->aggregate_grading_grades_process($batch);
 239      }
 240  
 241      public function test_aggregate_grading_grades_process_multiple_grades_new() {
 242          $this->resetAfterTest(true);
 243          // fixture set-up
 244          $batch = array();
 245          $batch[] = (object)array('reviewerid'=>5, 'gradinggrade'=>99.45670, 'gradinggradeover'=>null, 'aggregationid'=>null, 'aggregatedgrade'=>null);
 246          $batch[] = (object)array('reviewerid'=>5, 'gradinggrade'=>87.34311, 'gradinggradeover'=>null, 'aggregationid'=>null, 'aggregatedgrade'=>null);
 247          $batch[] = (object)array('reviewerid'=>5, 'gradinggrade'=>51.12000, 'gradinggradeover'=>null, 'aggregationid'=>null, 'aggregatedgrade'=>null);
 248          // expectation
 249          $now = time();
 250          $expected = new stdclass();
 251          $expected->workshopid = $this->workshop->id;
 252          $expected->userid = 5;
 253          $expected->gradinggrade = 79.3066;
 254          $expected->timegraded = $now;
 255          //$DB->expectOnce('insert_record', array('workshop_aggregations', $expected));
 256          // excersise SUT
 257          $this->workshop->aggregate_grading_grades_process($batch, $now);
 258      }
 259  
 260      public function test_aggregate_grading_grades_process_multiple_grades_update() {
 261          $this->resetAfterTest(true);
 262          // fixture set-up
 263          $batch = array();
 264          $batch[] = (object)array('reviewerid'=>5, 'gradinggrade'=>56.23400, 'gradinggradeover'=>null, 'aggregationid'=>2, 'aggregatedgrade'=>79.30660);
 265          $batch[] = (object)array('reviewerid'=>5, 'gradinggrade'=>87.34311, 'gradinggradeover'=>null, 'aggregationid'=>2, 'aggregatedgrade'=>79.30660);
 266          $batch[] = (object)array('reviewerid'=>5, 'gradinggrade'=>51.12000, 'gradinggradeover'=>null, 'aggregationid'=>2, 'aggregatedgrade'=>79.30660);
 267          // expectation
 268          //$DB->expectOnce('update_record');
 269          // excersise SUT
 270          $this->workshop->aggregate_grading_grades_process($batch);
 271      }
 272  
 273      public function test_aggregate_grading_grades_process_multiple_grades_overriden() {
 274          $this->resetAfterTest(true);
 275          // fixture set-up
 276          $batch = array();
 277          $batch[] = (object)array('reviewerid'=>5, 'gradinggrade'=>56.23400, 'gradinggradeover'=>99.45670, 'aggregationid'=>2, 'aggregatedgrade'=>64.89904);
 278          $batch[] = (object)array('reviewerid'=>5, 'gradinggrade'=>87.34311, 'gradinggradeover'=>null, 'aggregationid'=>2, 'aggregatedgrade'=>64.89904);
 279          $batch[] = (object)array('reviewerid'=>5, 'gradinggrade'=>51.12000, 'gradinggradeover'=>null, 'aggregationid'=>2, 'aggregatedgrade'=>64.89904);
 280          // expectation
 281          //$DB->expectOnce('update_record');
 282          // excersise SUT
 283          $this->workshop->aggregate_grading_grades_process($batch);
 284      }
 285  
 286      public function test_aggregate_grading_grades_process_multiple_grades_one_missing() {
 287          $this->resetAfterTest(true);
 288          // fixture set-up
 289          $batch = array();
 290          $batch[] = (object)array('reviewerid'=>6, 'gradinggrade'=>50.00000, 'gradinggradeover'=>null, 'aggregationid'=>3, 'aggregatedgrade'=>100.00000);
 291          $batch[] = (object)array('reviewerid'=>6, 'gradinggrade'=>null, 'gradinggradeover'=>null, 'aggregationid'=>3, 'aggregatedgrade'=>100.00000);
 292          $batch[] = (object)array('reviewerid'=>6, 'gradinggrade'=>52.20000, 'gradinggradeover'=>null, 'aggregationid'=>3, 'aggregatedgrade'=>100.00000);
 293          // expectation
 294          //$DB->expectOnce('update_record');
 295          // excersise SUT
 296          $this->workshop->aggregate_grading_grades_process($batch);
 297      }
 298  
 299      public function test_aggregate_grading_grades_process_multiple_grades_missing_overridden() {
 300          $this->resetAfterTest(true);
 301          // fixture set-up
 302          $batch = array();
 303          $batch[] = (object)array('reviewerid'=>6, 'gradinggrade'=>50.00000, 'gradinggradeover'=>null, 'aggregationid'=>3, 'aggregatedgrade'=>100.00000);
 304          $batch[] = (object)array('reviewerid'=>6, 'gradinggrade'=>null, 'gradinggradeover'=>69.00000, 'aggregationid'=>3, 'aggregatedgrade'=>100.00000);
 305          $batch[] = (object)array('reviewerid'=>6, 'gradinggrade'=>52.20000, 'gradinggradeover'=>null, 'aggregationid'=>3, 'aggregatedgrade'=>100.00000);
 306          // expectation
 307          //$DB->expectOnce('update_record');
 308          // excersise SUT
 309          $this->workshop->aggregate_grading_grades_process($batch);
 310      }
 311  
 312      public function test_percent_to_value() {
 313          $this->resetAfterTest(true);
 314          // fixture setup
 315          $total = 185;
 316          $percent = 56.6543;
 317          // exercise SUT
 318          $part = workshop::percent_to_value($percent, $total);
 319          // verify
 320          $this->assertEquals($part, $total * $percent / 100);
 321      }
 322  
 323      public function test_percent_to_value_negative() {
 324          $this->resetAfterTest(true);
 325          // fixture setup
 326          $total = 185;
 327          $percent = -7.098;
 328  
 329          // exercise SUT
 330          $this->expectException(coding_exception::class);
 331          $part = workshop::percent_to_value($percent, $total);
 332      }
 333  
 334      public function test_percent_to_value_over_hundred() {
 335          $this->resetAfterTest(true);
 336          // fixture setup
 337          $total = 185;
 338          $percent = 121.08;
 339  
 340          // exercise SUT
 341          $this->expectException(coding_exception::class);
 342          $part = workshop::percent_to_value($percent, $total);
 343      }
 344  
 345      public function test_lcm() {
 346          $this->resetAfterTest(true);
 347          // fixture setup + exercise SUT + verify in one step
 348          $this->assertEquals(workshop::lcm(1,4), 4);
 349          $this->assertEquals(workshop::lcm(2,4), 4);
 350          $this->assertEquals(workshop::lcm(4,2), 4);
 351          $this->assertEquals(workshop::lcm(2,3), 6);
 352          $this->assertEquals(workshop::lcm(6,4), 12);
 353      }
 354  
 355      public function test_lcm_array() {
 356          $this->resetAfterTest(true);
 357          // fixture setup
 358          $numbers = array(5,3,15);
 359          // excersise SUT
 360          $lcm = array_reduce($numbers, 'workshop::lcm', 1);
 361          // verify
 362          $this->assertEquals($lcm, 15);
 363      }
 364  
 365      public function test_prepare_example_assessment() {
 366          $this->resetAfterTest(true);
 367          // fixture setup
 368          $fakerawrecord = (object)array(
 369              'id'                => 42,
 370              'submissionid'      => 56,
 371              'weight'            => 0,
 372              'timecreated'       => time() - 10,
 373              'timemodified'      => time() - 5,
 374              'grade'             => null,
 375              'gradinggrade'      => null,
 376              'gradinggradeover'  => null,
 377              'feedbackauthor'    => null,
 378              'feedbackauthorformat' => 0,
 379              'feedbackauthorattachment' => 0,
 380          );
 381          // excersise SUT
 382          $a = $this->workshop->prepare_example_assessment($fakerawrecord);
 383          // verify
 384          $this->assertTrue($a instanceof workshop_example_assessment);
 385          $this->assertTrue($a->url instanceof moodle_url);
 386  
 387          // modify setup
 388          $fakerawrecord->weight = 1;
 389          $this->expectException('coding_exception');
 390          // excersise SUT
 391          $a = $this->workshop->prepare_example_assessment($fakerawrecord);
 392      }
 393  
 394      public function test_prepare_example_reference_assessment() {
 395          global $USER;
 396          $this->resetAfterTest(true);
 397          // fixture setup
 398          $fakerawrecord = (object)array(
 399              'id'                => 38,
 400              'submissionid'      => 56,
 401              'weight'            => 1,
 402              'timecreated'       => time() - 100,
 403              'timemodified'      => time() - 50,
 404              'grade'             => 0.75000,
 405              'gradinggrade'      => 1.00000,
 406              'gradinggradeover'  => null,
 407              'feedbackauthor'    => null,
 408              'feedbackauthorformat' => 0,
 409              'feedbackauthorattachment' => 0,
 410          );
 411          // excersise SUT
 412          $a = $this->workshop->prepare_example_reference_assessment($fakerawrecord);
 413          // verify
 414          $this->assertTrue($a instanceof workshop_example_reference_assessment);
 415  
 416          // modify setup
 417          $fakerawrecord->weight = 0;
 418          $this->expectException('coding_exception');
 419          // excersise SUT
 420          $a = $this->workshop->prepare_example_reference_assessment($fakerawrecord);
 421      }
 422  
 423      /**
 424       * Tests user restrictions, as they affect lists of users returned by
 425       * core API functions.
 426       *
 427       * This includes the groupingid option (when group mode is in use), and
 428       * standard activity restrictions using the availability API.
 429       */
 430      public function test_user_restrictions() {
 431          global $DB, $CFG;
 432  
 433          $this->resetAfterTest();
 434  
 435          // Use existing sample course from setUp.
 436          $courseid = $this->workshop->course->id;
 437  
 438          // Make a test grouping and two groups.
 439          $generator = $this->getDataGenerator();
 440          $grouping = $generator->create_grouping(array('courseid' => $courseid));
 441          $group1 = $generator->create_group(array('courseid' => $courseid));
 442          groups_assign_grouping($grouping->id, $group1->id);
 443          $group2 = $generator->create_group(array('courseid' => $courseid));
 444          groups_assign_grouping($grouping->id, $group2->id);
 445  
 446          // Group 3 is not in the grouping.
 447          $group3 = $generator->create_group(array('courseid' => $courseid));
 448  
 449          // Enrol some students.
 450          $roleids = $DB->get_records_menu('role', null, '', 'shortname, id');
 451          $student1 = $generator->create_user();
 452          $student2 = $generator->create_user();
 453          $student3 = $generator->create_user();
 454          $generator->enrol_user($student1->id, $courseid, $roleids['student']);
 455          $generator->enrol_user($student2->id, $courseid, $roleids['student']);
 456          $generator->enrol_user($student3->id, $courseid, $roleids['student']);
 457  
 458          // Place students in groups (except student 3).
 459          groups_add_member($group1, $student1);
 460          groups_add_member($group2, $student2);
 461          groups_add_member($group3, $student3);
 462  
 463          // The existing workshop doesn't have any restrictions, so user lists
 464          // should include all three users.
 465          $allusers = get_enrolled_users(context_course::instance($courseid));
 466          $result = $this->workshop->get_grouped($allusers);
 467          $this->assertCount(4, $result);
 468          $users = array_keys($result[0]);
 469          sort($users);
 470          $this->assertEquals(array($student1->id, $student2->id, $student3->id), $users);
 471          $this->assertEquals(array($student1->id), array_keys($result[$group1->id]));
 472          $this->assertEquals(array($student2->id), array_keys($result[$group2->id]));
 473          $this->assertEquals(array($student3->id), array_keys($result[$group3->id]));
 474  
 475          // Test get_users_with_capability_sql (via get_potential_authors).
 476          $users = $this->workshop->get_potential_authors(false);
 477          $this->assertCount(3, $users);
 478          $users = $this->workshop->get_potential_authors(false, $group2->id);
 479          $this->assertEquals(array($student2->id), array_keys($users));
 480  
 481          // Create another test workshop with grouping set.
 482          $workshopitem = $this->getDataGenerator()->create_module('workshop',
 483                  array('course' => $courseid, 'groupmode' => SEPARATEGROUPS,
 484                  'groupingid' => $grouping->id));
 485          $cm = get_coursemodule_from_instance('workshop', $workshopitem->id,
 486                  $courseid, false, MUST_EXIST);
 487          $workshopgrouping = new testable_workshop($workshopitem, $cm, $this->workshop->course);
 488  
 489          // This time the result should only include users and groups in the
 490          // selected grouping.
 491          $result = $workshopgrouping->get_grouped($allusers);
 492          $this->assertCount(3, $result);
 493          $users = array_keys($result[0]);
 494          sort($users);
 495          $this->assertEquals(array($student1->id, $student2->id), $users);
 496          $this->assertEquals(array($student1->id), array_keys($result[$group1->id]));
 497          $this->assertEquals(array($student2->id), array_keys($result[$group2->id]));
 498  
 499          // Test get_users_with_capability_sql (via get_potential_authors).
 500          $users = $workshopgrouping->get_potential_authors(false);
 501          $userids = array_keys($users);
 502          sort($userids);
 503          $this->assertEquals(array($student1->id, $student2->id), $userids);
 504          $users = $workshopgrouping->get_potential_authors(false, $group2->id);
 505          $this->assertEquals(array($student2->id), array_keys($users));
 506  
 507          // Enable the availability system and create another test workshop with
 508          // availability restriction on grouping.
 509          $CFG->enableavailability = true;
 510          $workshopitem = $this->getDataGenerator()->create_module('workshop',
 511                  array('course' => $courseid, 'availability' => json_encode(
 512                      \core_availability\tree::get_root_json(array(
 513                      \availability_grouping\condition::get_json($grouping->id)),
 514                      \core_availability\tree::OP_AND, false))));
 515          $cm = get_coursemodule_from_instance('workshop', $workshopitem->id,
 516                  $courseid, false, MUST_EXIST);
 517          $workshoprestricted = new testable_workshop($workshopitem, $cm, $this->workshop->course);
 518  
 519          // The get_grouped function isn't intended to apply this restriction,
 520          // so it should be the same as the base workshop. (Note: in reality,
 521          // get_grouped is always run with the parameter being the result of
 522          // one of the get_potential_xxx functions, so it works.)
 523          $result = $workshoprestricted->get_grouped($allusers);
 524          $this->assertCount(4, $result);
 525          $this->assertCount(3, $result[0]);
 526  
 527          // The get_users_with_capability_sql-based functions should apply it.
 528          $users = $workshoprestricted->get_potential_authors(false);
 529          $userids = array_keys($users);
 530          sort($userids);
 531          $this->assertEquals(array($student1->id, $student2->id), $userids);
 532          $users = $workshoprestricted->get_potential_authors(false, $group2->id);
 533          $this->assertEquals(array($student2->id), array_keys($users));
 534      }
 535  
 536      /**
 537       * Test the workshop reset feature.
 538       */
 539      public function test_reset_phase() {
 540          $this->resetAfterTest(true);
 541  
 542          $this->workshop->switch_phase(workshop::PHASE_CLOSED);
 543          $this->assertEquals(workshop::PHASE_CLOSED, $this->workshop->phase);
 544  
 545          $settings = (object)array(
 546              'reset_workshop_phase' => 0,
 547          );
 548          $status = $this->workshop->reset_userdata($settings);
 549          $this->assertEquals(workshop::PHASE_CLOSED, $this->workshop->phase);
 550  
 551          $settings = (object)array(
 552              'reset_workshop_phase' => 1,
 553          );
 554          $status = $this->workshop->reset_userdata($settings);
 555          $this->assertEquals(workshop::PHASE_SETUP, $this->workshop->phase);
 556          foreach ($status as $result) {
 557              $this->assertFalse($result['error']);
 558          }
 559      }
 560  
 561      /**
 562       * Test deleting assessments related data on workshop reset.
 563       */
 564      public function test_reset_userdata_assessments() {
 565          global $DB;
 566          $this->resetAfterTest(true);
 567  
 568          $student1 = $this->getDataGenerator()->create_user();
 569          $student2 = $this->getDataGenerator()->create_user();
 570  
 571          $this->getDataGenerator()->enrol_user($student1->id, $this->workshop->course->id);
 572          $this->getDataGenerator()->enrol_user($student2->id, $this->workshop->course->id);
 573  
 574          $workshopgenerator = $this->getDataGenerator()->get_plugin_generator('mod_workshop');
 575  
 576          $subid1 = $workshopgenerator->create_submission($this->workshop->id, $student1->id);
 577          $subid2 = $workshopgenerator->create_submission($this->workshop->id, $student2->id);
 578  
 579          $asid1 = $workshopgenerator->create_assessment($subid1, $student2->id);
 580          $asid2 = $workshopgenerator->create_assessment($subid2, $student1->id);
 581  
 582          $settings = (object)array(
 583              'reset_workshop_assessments' => 1,
 584          );
 585          $status = $this->workshop->reset_userdata($settings);
 586  
 587          foreach ($status as $result) {
 588              $this->assertFalse($result['error']);
 589          }
 590  
 591          $this->assertEquals(2, $DB->count_records('workshop_submissions', array('workshopid' => $this->workshop->id)));
 592          $this->assertEquals(0, $DB->count_records('workshop_assessments'));
 593      }
 594  
 595      /**
 596       * Test deleting submissions related data on workshop reset.
 597       */
 598      public function test_reset_userdata_submissions() {
 599          global $DB;
 600          $this->resetAfterTest(true);
 601  
 602          $student1 = $this->getDataGenerator()->create_user();
 603          $student2 = $this->getDataGenerator()->create_user();
 604  
 605          $this->getDataGenerator()->enrol_user($student1->id, $this->workshop->course->id);
 606          $this->getDataGenerator()->enrol_user($student2->id, $this->workshop->course->id);
 607  
 608          $workshopgenerator = $this->getDataGenerator()->get_plugin_generator('mod_workshop');
 609  
 610          $subid1 = $workshopgenerator->create_submission($this->workshop->id, $student1->id);
 611          $subid2 = $workshopgenerator->create_submission($this->workshop->id, $student2->id);
 612  
 613          $asid1 = $workshopgenerator->create_assessment($subid1, $student2->id);
 614          $asid2 = $workshopgenerator->create_assessment($subid2, $student1->id);
 615  
 616          $settings = (object)array(
 617              'reset_workshop_submissions' => 1,
 618          );
 619          $status = $this->workshop->reset_userdata($settings);
 620  
 621          foreach ($status as $result) {
 622              $this->assertFalse($result['error']);
 623          }
 624  
 625          $this->assertEquals(0, $DB->count_records('workshop_submissions', array('workshopid' => $this->workshop->id)));
 626          $this->assertEquals(0, $DB->count_records('workshop_assessments'));
 627      }
 628  
 629      /**
 630       * Test normalizing list of extensions.
 631       */
 632      public function test_normalize_file_extensions() {
 633          $this->resetAfterTest(true);
 634  
 635          workshop::normalize_file_extensions('');
 636          $this->assertDebuggingCalled();
 637      }
 638  
 639      /**
 640       * Test cleaning list of extensions.
 641       */
 642      public function test_clean_file_extensions() {
 643          $this->resetAfterTest(true);
 644  
 645          workshop::clean_file_extensions('');
 646          $this->assertDebuggingCalledCount(2);
 647      }
 648  
 649      /**
 650       * Test validation of the list of file extensions.
 651       */
 652      public function test_invalid_file_extensions() {
 653          $this->resetAfterTest(true);
 654  
 655          workshop::invalid_file_extensions('', '');
 656          $this->assertDebuggingCalledCount(3);
 657      }
 658  
 659      /**
 660       * Test checking file name against the list of allowed extensions.
 661       */
 662      public function test_is_allowed_file_type() {
 663          $this->resetAfterTest(true);
 664  
 665          workshop::is_allowed_file_type('', '');
 666          $this->assertDebuggingCalledCount(2);
 667      }
 668  
 669      /**
 670       * Test workshop::check_group_membership() functionality.
 671       */
 672      public function test_check_group_membership() {
 673          global $DB, $CFG;
 674  
 675          $this->resetAfterTest();
 676  
 677          $courseid = $this->course->id;
 678          $generator = $this->getDataGenerator();
 679  
 680          // Make test groups.
 681          $group1 = $generator->create_group(array('courseid' => $courseid));
 682          $group2 = $generator->create_group(array('courseid' => $courseid));
 683          $group3 = $generator->create_group(array('courseid' => $courseid));
 684  
 685          // Revoke the accessallgroups from non-editing teachers (tutors).
 686          $roleids = $DB->get_records_menu('role', null, '', 'shortname, id');
 687          unassign_capability('moodle/site:accessallgroups', $roleids['teacher']);
 688  
 689          // Create test use accounts.
 690          $teacher1 = $generator->create_user();
 691          $tutor1 = $generator->create_user();
 692          $tutor2 = $generator->create_user();
 693          $student1 = $generator->create_user();
 694          $student2 = $generator->create_user();
 695          $student3 = $generator->create_user();
 696  
 697          // Enrol the teacher (has the access all groups permission).
 698          $generator->enrol_user($teacher1->id, $courseid, $roleids['editingteacher']);
 699  
 700          // Enrol tutors (can not access all groups).
 701          $generator->enrol_user($tutor1->id, $courseid, $roleids['teacher']);
 702          $generator->enrol_user($tutor2->id, $courseid, $roleids['teacher']);
 703  
 704          // Enrol students.
 705          $generator->enrol_user($student1->id, $courseid, $roleids['student']);
 706          $generator->enrol_user($student2->id, $courseid, $roleids['student']);
 707          $generator->enrol_user($student3->id, $courseid, $roleids['student']);
 708  
 709          // Add users in groups.
 710          groups_add_member($group1, $tutor1);
 711          groups_add_member($group2, $tutor2);
 712          groups_add_member($group1, $student1);
 713          groups_add_member($group2, $student2);
 714          groups_add_member($group3, $student3);
 715  
 716          // Workshop with no groups.
 717          $workshopitem1 = $this->getDataGenerator()->create_module('workshop', [
 718              'course' => $courseid,
 719              'groupmode' => NOGROUPS,
 720          ]);
 721          $cm = get_coursemodule_from_instance('workshop', $workshopitem1->id, $courseid, false, MUST_EXIST);
 722          $workshop1 = new testable_workshop($workshopitem1, $cm, $this->course);
 723  
 724          $this->setUser($teacher1);
 725          $this->assertTrue($workshop1->check_group_membership($student1->id));
 726          $this->assertTrue($workshop1->check_group_membership($student2->id));
 727          $this->assertTrue($workshop1->check_group_membership($student3->id));
 728  
 729          $this->setUser($tutor1);
 730          $this->assertTrue($workshop1->check_group_membership($student1->id));
 731          $this->assertTrue($workshop1->check_group_membership($student2->id));
 732          $this->assertTrue($workshop1->check_group_membership($student3->id));
 733  
 734          // Workshop in visible groups mode.
 735          $workshopitem2 = $this->getDataGenerator()->create_module('workshop', [
 736              'course' => $courseid,
 737              'groupmode' => VISIBLEGROUPS,
 738          ]);
 739          $cm = get_coursemodule_from_instance('workshop', $workshopitem2->id, $courseid, false, MUST_EXIST);
 740          $workshop2 = new testable_workshop($workshopitem2, $cm, $this->course);
 741  
 742          $this->setUser($teacher1);
 743          $this->assertTrue($workshop2->check_group_membership($student1->id));
 744          $this->assertTrue($workshop2->check_group_membership($student2->id));
 745          $this->assertTrue($workshop2->check_group_membership($student3->id));
 746  
 747          $this->setUser($tutor1);
 748          $this->assertTrue($workshop2->check_group_membership($student1->id));
 749          $this->assertTrue($workshop2->check_group_membership($student2->id));
 750          $this->assertTrue($workshop2->check_group_membership($student3->id));
 751  
 752          // Workshop in separate groups mode.
 753          $workshopitem3 = $this->getDataGenerator()->create_module('workshop', [
 754              'course' => $courseid,
 755              'groupmode' => SEPARATEGROUPS,
 756          ]);
 757          $cm = get_coursemodule_from_instance('workshop', $workshopitem3->id, $courseid, false, MUST_EXIST);
 758          $workshop3 = new testable_workshop($workshopitem3, $cm, $this->course);
 759  
 760          $this->setUser($teacher1);
 761          $this->assertTrue($workshop3->check_group_membership($student1->id));
 762          $this->assertTrue($workshop3->check_group_membership($student2->id));
 763          $this->assertTrue($workshop3->check_group_membership($student3->id));
 764  
 765          $this->setUser($tutor1);
 766          $this->assertTrue($workshop3->check_group_membership($student1->id));
 767          $this->assertFalse($workshop3->check_group_membership($student2->id));
 768          $this->assertFalse($workshop3->check_group_membership($student3->id));
 769  
 770          $this->setUser($tutor2);
 771          $this->assertFalse($workshop3->check_group_membership($student1->id));
 772          $this->assertTrue($workshop3->check_group_membership($student2->id));
 773          $this->assertFalse($workshop3->check_group_membership($student3->id));
 774      }
 775  }