[ Index ]

PHP Cross Reference of moodle-2.8

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 <[email protected]>
  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      public function test_percent_to_value_negative() {
 321          $this->resetAfterTest(true);
 322          // fixture setup
 323          $total = 185;
 324          $percent = -7.098;
 325          // set expectation
 326          $this->setExpectedException('coding_exception');
 327          // exercise SUT
 328          $part = workshop::percent_to_value($percent, $total);
 329      }
 330  
 331      public function test_percent_to_value_over_hundred() {
 332          $this->resetAfterTest(true);
 333          // fixture setup
 334          $total = 185;
 335          $percent = 121.08;
 336          // set expectation
 337          $this->setExpectedException('coding_exception');
 338          // exercise SUT
 339          $part = workshop::percent_to_value($percent, $total);
 340      }
 341  
 342      public function test_lcm() {
 343          $this->resetAfterTest(true);
 344          // fixture setup + exercise SUT + verify in one step
 345          $this->assertEquals(workshop::lcm(1,4), 4);
 346          $this->assertEquals(workshop::lcm(2,4), 4);
 347          $this->assertEquals(workshop::lcm(4,2), 4);
 348          $this->assertEquals(workshop::lcm(2,3), 6);
 349          $this->assertEquals(workshop::lcm(6,4), 12);
 350      }
 351  
 352      public function test_lcm_array() {
 353          $this->resetAfterTest(true);
 354          // fixture setup
 355          $numbers = array(5,3,15);
 356          // excersise SUT
 357          $lcm = array_reduce($numbers, 'workshop::lcm', 1);
 358          // verify
 359          $this->assertEquals($lcm, 15);
 360      }
 361  
 362      public function test_prepare_example_assessment() {
 363          $this->resetAfterTest(true);
 364          // fixture setup
 365          $fakerawrecord = (object)array(
 366              'id'                => 42,
 367              'submissionid'      => 56,
 368              'weight'            => 0,
 369              'timecreated'       => time() - 10,
 370              'timemodified'      => time() - 5,
 371              'grade'             => null,
 372              'gradinggrade'      => null,
 373              'gradinggradeover'  => null,
 374              'feedbackauthor'    => null,
 375              'feedbackauthorformat' => 0,
 376              'feedbackauthorattachment' => 0,
 377          );
 378          // excersise SUT
 379          $a = $this->workshop->prepare_example_assessment($fakerawrecord);
 380          // verify
 381          $this->assertTrue($a instanceof workshop_example_assessment);
 382          $this->assertTrue($a->url instanceof moodle_url);
 383  
 384          // modify setup
 385          $fakerawrecord->weight = 1;
 386          $this->setExpectedException('coding_exception');
 387          // excersise SUT
 388          $a = $this->workshop->prepare_example_assessment($fakerawrecord);
 389      }
 390  
 391      public function test_prepare_example_reference_assessment() {
 392          global $USER;
 393          $this->resetAfterTest(true);
 394          // fixture setup
 395          $fakerawrecord = (object)array(
 396              'id'                => 38,
 397              'submissionid'      => 56,
 398              'weight'            => 1,
 399              'timecreated'       => time() - 100,
 400              'timemodified'      => time() - 50,
 401              'grade'             => 0.75000,
 402              'gradinggrade'      => 1.00000,
 403              'gradinggradeover'  => null,
 404              'feedbackauthor'    => null,
 405              'feedbackauthorformat' => 0,
 406              'feedbackauthorattachment' => 0,
 407          );
 408          // excersise SUT
 409          $a = $this->workshop->prepare_example_reference_assessment($fakerawrecord);
 410          // verify
 411          $this->assertTrue($a instanceof workshop_example_reference_assessment);
 412  
 413          // modify setup
 414          $fakerawrecord->weight = 0;
 415          $this->setExpectedException('coding_exception');
 416          // excersise SUT
 417          $a = $this->workshop->prepare_example_reference_assessment($fakerawrecord);
 418      }
 419  
 420      /**
 421       * Tests user restrictions, as they affect lists of users returned by
 422       * core API functions.
 423       *
 424       * This includes the groupingid option (when group mode is in use), and
 425       * standard activity restrictions using the availability API.
 426       */
 427      public function test_user_restrictions() {
 428          global $DB, $CFG;
 429  
 430          $this->resetAfterTest();
 431  
 432          // Use existing sample course from setUp.
 433          $courseid = $this->workshop->course->id;
 434  
 435          // Make a test grouping and two groups.
 436          $generator = $this->getDataGenerator();
 437          $grouping = $generator->create_grouping(array('courseid' => $courseid));
 438          $group1 = $generator->create_group(array('courseid' => $courseid));
 439          groups_assign_grouping($grouping->id, $group1->id);
 440          $group2 = $generator->create_group(array('courseid' => $courseid));
 441          groups_assign_grouping($grouping->id, $group2->id);
 442  
 443          // Group 3 is not in the grouping.
 444          $group3 = $generator->create_group(array('courseid' => $courseid));
 445  
 446          // Enrol some students.
 447          $roleids = $DB->get_records_menu('role', null, '', 'shortname, id');
 448          $student1 = $generator->create_user();
 449          $student2 = $generator->create_user();
 450          $student3 = $generator->create_user();
 451          $generator->enrol_user($student1->id, $courseid, $roleids['student']);
 452          $generator->enrol_user($student2->id, $courseid, $roleids['student']);
 453          $generator->enrol_user($student3->id, $courseid, $roleids['student']);
 454  
 455          // Place students in groups (except student 3).
 456          groups_add_member($group1, $student1);
 457          groups_add_member($group2, $student2);
 458          groups_add_member($group3, $student3);
 459  
 460          // The existing workshop doesn't have any restrictions, so user lists
 461          // should include all three users.
 462          $allusers = get_enrolled_users(context_course::instance($courseid));
 463          $result = $this->workshop->get_grouped($allusers);
 464          $this->assertCount(4, $result);
 465          $users = array_keys($result[0]);
 466          sort($users);
 467          $this->assertEquals(array($student1->id, $student2->id, $student3->id), $users);
 468          $this->assertEquals(array($student1->id), array_keys($result[$group1->id]));
 469          $this->assertEquals(array($student2->id), array_keys($result[$group2->id]));
 470          $this->assertEquals(array($student3->id), array_keys($result[$group3->id]));
 471  
 472          // Test get_users_with_capability_sql (via get_potential_authors).
 473          $users = $this->workshop->get_potential_authors(false);
 474          $this->assertCount(3, $users);
 475          $users = $this->workshop->get_potential_authors(false, $group2->id);
 476          $this->assertEquals(array($student2->id), array_keys($users));
 477  
 478          // Create another test workshop with grouping set.
 479          $workshopitem = $this->getDataGenerator()->create_module('workshop',
 480                  array('course' => $courseid, 'groupmode' => SEPARATEGROUPS,
 481                  'groupingid' => $grouping->id));
 482          $cm = get_coursemodule_from_instance('workshop', $workshopitem->id,
 483                  $courseid, false, MUST_EXIST);
 484          $workshopgrouping = new testable_workshop($workshopitem, $cm, $this->workshop->course);
 485  
 486          // This time the result should only include users and groups in the
 487          // selected grouping.
 488          $result = $workshopgrouping->get_grouped($allusers);
 489          $this->assertCount(3, $result);
 490          $users = array_keys($result[0]);
 491          sort($users);
 492          $this->assertEquals(array($student1->id, $student2->id), $users);
 493          $this->assertEquals(array($student1->id), array_keys($result[$group1->id]));
 494          $this->assertEquals(array($student2->id), array_keys($result[$group2->id]));
 495  
 496          // Test get_users_with_capability_sql (via get_potential_authors).
 497          $users = $workshopgrouping->get_potential_authors(false);
 498          $userids = array_keys($users);
 499          sort($userids);
 500          $this->assertEquals(array($student1->id, $student2->id), $userids);
 501          $users = $workshopgrouping->get_potential_authors(false, $group2->id);
 502          $this->assertEquals(array($student2->id), array_keys($users));
 503  
 504          // Enable the availability system and create another test workshop with
 505          // availability restriction on grouping.
 506          $CFG->enableavailability = true;
 507          $workshopitem = $this->getDataGenerator()->create_module('workshop',
 508                  array('course' => $courseid, 'availability' => json_encode(
 509                      \core_availability\tree::get_root_json(array(
 510                      \availability_grouping\condition::get_json($grouping->id)),
 511                      \core_availability\tree::OP_AND, false))));
 512          $cm = get_coursemodule_from_instance('workshop', $workshopitem->id,
 513                  $courseid, false, MUST_EXIST);
 514          $workshoprestricted = new testable_workshop($workshopitem, $cm, $this->workshop->course);
 515  
 516          // The get_grouped function isn't intended to apply this restriction,
 517          // so it should be the same as the base workshop. (Note: in reality,
 518          // get_grouped is always run with the parameter being the result of
 519          // one of the get_potential_xxx functions, so it works.)
 520          $result = $workshoprestricted->get_grouped($allusers);
 521          $this->assertCount(4, $result);
 522          $this->assertCount(3, $result[0]);
 523  
 524          // The get_users_with_capability_sql-based functions should apply it.
 525          $users = $workshoprestricted->get_potential_authors(false);
 526          $userids = array_keys($users);
 527          sort($userids);
 528          $this->assertEquals(array($student1->id, $student2->id), $userids);
 529          $users = $workshoprestricted->get_potential_authors(false, $group2->id);
 530          $this->assertEquals(array($student2->id), array_keys($users));
 531      }
 532  
 533      /**
 534       * Test the workshop reset feature.
 535       */
 536      public function test_reset_phase() {
 537          $this->resetAfterTest(true);
 538  
 539          $this->workshop->switch_phase(workshop::PHASE_CLOSED);
 540          $this->assertEquals(workshop::PHASE_CLOSED, $this->workshop->phase);
 541  
 542          $settings = (object)array(
 543              'reset_workshop_phase' => 0,
 544          );
 545          $status = $this->workshop->reset_userdata($settings);
 546          $this->assertEquals(workshop::PHASE_CLOSED, $this->workshop->phase);
 547  
 548          $settings = (object)array(
 549              'reset_workshop_phase' => 1,
 550          );
 551          $status = $this->workshop->reset_userdata($settings);
 552          $this->assertEquals(workshop::PHASE_SETUP, $this->workshop->phase);
 553          foreach ($status as $result) {
 554              $this->assertFalse($result['error']);
 555          }
 556      }
 557  
 558      /**
 559       * Test deleting assessments related data on workshop reset.
 560       */
 561      public function test_reset_userdata_assessments() {
 562          global $DB;
 563          $this->resetAfterTest(true);
 564  
 565          $student1 = $this->getDataGenerator()->create_user();
 566          $student2 = $this->getDataGenerator()->create_user();
 567  
 568          $this->getDataGenerator()->enrol_user($student1->id, $this->workshop->course->id);
 569          $this->getDataGenerator()->enrol_user($student2->id, $this->workshop->course->id);
 570  
 571          $workshopgenerator = $this->getDataGenerator()->get_plugin_generator('mod_workshop');
 572  
 573          $subid1 = $workshopgenerator->create_submission($this->workshop->id, $student1->id);
 574          $subid2 = $workshopgenerator->create_submission($this->workshop->id, $student2->id);
 575  
 576          $asid1 = $workshopgenerator->create_assessment($subid1, $student2->id);
 577          $asid2 = $workshopgenerator->create_assessment($subid2, $student1->id);
 578  
 579          $settings = (object)array(
 580              'reset_workshop_assessments' => 1,
 581          );
 582          $status = $this->workshop->reset_userdata($settings);
 583  
 584          foreach ($status as $result) {
 585              $this->assertFalse($result['error']);
 586          }
 587  
 588          $this->assertEquals(2, $DB->count_records('workshop_submissions', array('workshopid' => $this->workshop->id)));
 589          $this->assertEquals(0, $DB->count_records('workshop_assessments'));
 590      }
 591  
 592      /**
 593       * Test deleting submissions related data on workshop reset.
 594       */
 595      public function test_reset_userdata_submissions() {
 596          global $DB;
 597          $this->resetAfterTest(true);
 598  
 599          $student1 = $this->getDataGenerator()->create_user();
 600          $student2 = $this->getDataGenerator()->create_user();
 601  
 602          $this->getDataGenerator()->enrol_user($student1->id, $this->workshop->course->id);
 603          $this->getDataGenerator()->enrol_user($student2->id, $this->workshop->course->id);
 604  
 605          $workshopgenerator = $this->getDataGenerator()->get_plugin_generator('mod_workshop');
 606  
 607          $subid1 = $workshopgenerator->create_submission($this->workshop->id, $student1->id);
 608          $subid2 = $workshopgenerator->create_submission($this->workshop->id, $student2->id);
 609  
 610          $asid1 = $workshopgenerator->create_assessment($subid1, $student2->id);
 611          $asid2 = $workshopgenerator->create_assessment($subid2, $student1->id);
 612  
 613          $settings = (object)array(
 614              'reset_workshop_submissions' => 1,
 615          );
 616          $status = $this->workshop->reset_userdata($settings);
 617  
 618          foreach ($status as $result) {
 619              $this->assertFalse($result['error']);
 620          }
 621  
 622          $this->assertEquals(0, $DB->count_records('workshop_submissions', array('workshopid' => $this->workshop->id)));
 623          $this->assertEquals(0, $DB->count_records('workshop_assessments'));
 624      }
 625  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1