[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/mod/workshop/tests/ -> locallib_test.php (source)

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


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1