[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/grade/report/history/tests/ -> report_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   * Grade history report test.
  19   *
  20   * @package    gradereport_history
  21   * @copyright  2014 Frédéric Massart - FMCorz.net
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  /**
  28   * Grade history report test class.
  29   *
  30   * @package    gradereport_history
  31   * @copyright  2014 Frédéric Massart - FMCorz.net
  32   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class gradereport_history_report_testcase extends advanced_testcase {
  35  
  36      /**
  37       * Create some grades.
  38       */
  39      public function test_query_db() {
  40          $this->resetAfterTest();
  41  
  42          // Making the setup.
  43          $c1 = $this->getDataGenerator()->create_course();
  44          $c2 = $this->getDataGenerator()->create_course();
  45          $c1ctx = context_course::instance($c1->id);
  46          $c2ctx = context_course::instance($c2->id);
  47  
  48          // Users.
  49          $u1 = $this->getDataGenerator()->create_user();
  50          $u2 = $this->getDataGenerator()->create_user();
  51          $u3 = $this->getDataGenerator()->create_user();
  52          $u4 = $this->getDataGenerator()->create_user();
  53          $u5 = $this->getDataGenerator()->create_user();
  54          $grader1 = $this->getDataGenerator()->create_user();
  55          $grader2 = $this->getDataGenerator()->create_user();
  56  
  57          // Modules.
  58          $c1m1 = $this->getDataGenerator()->create_module('assign', array('course' => $c1));
  59          $c1m2 = $this->getDataGenerator()->create_module('assign', array('course' => $c1));
  60          $c1m3 = $this->getDataGenerator()->create_module('assign', array('course' => $c1));
  61          $c2m1 = $this->getDataGenerator()->create_module('assign', array('course' => $c2));
  62          $c2m2 = $this->getDataGenerator()->create_module('assign', array('course' => $c2));
  63  
  64          // Creating fake history data.
  65          $giparams = array('itemtype' => 'mod', 'itemmodule' => 'assign');
  66          $grades = array();
  67  
  68          $gi = grade_item::fetch($giparams + array('iteminstance' => $c1m1->id));
  69          $grades['c1m1u1'] = $this->create_grade_history(array('itemid' => $gi->id, 'userid' => $u1->id,
  70                  'timemodified' => time() - 3600));
  71          $grades['c1m1u2'] = $this->create_grade_history(array('itemid' => $gi->id, 'userid' => $u2->id,
  72                  'timemodified' => time() + 3600));
  73          $grades['c1m1u3'] = $this->create_grade_history(array('itemid' => $gi->id, 'userid' => $u3->id));
  74          $grades['c1m1u4'] = $this->create_grade_history(array('itemid' => $gi->id, 'userid' => $u4->id));
  75          $grades['c1m1u5'] = $this->create_grade_history(array('itemid' => $gi->id, 'userid' => $u5->id));
  76  
  77          $gi = grade_item::fetch($giparams + array('iteminstance' => $c1m2->id));
  78          $grades['c1m2u1'] = $this->create_grade_history(array('itemid' => $gi->id, 'userid' => $u1->id));
  79          $grades['c1m2u2'] = $this->create_grade_history(array('itemid' => $gi->id, 'userid' => $u2->id));
  80  
  81          $gi = grade_item::fetch($giparams + array('iteminstance' => $c1m3->id));
  82          $grades['c1m3u1'] = $this->create_grade_history(array('itemid' => $gi->id, 'userid' => $u1->id));
  83  
  84          $gi = grade_item::fetch($giparams + array('iteminstance' => $c2m1->id));
  85          $grades['c2m1u1'] = $this->create_grade_history(array('itemid' => $gi->id, 'userid' => $u1->id,
  86              'usermodified' => $grader1->id));
  87          $grades['c2m1u2'] = $this->create_grade_history(array('itemid' => $gi->id, 'userid' => $u2->id,
  88              'usermodified' => $grader1->id));
  89          $grades['c2m1u3'] = $this->create_grade_history(array('itemid' => $gi->id, 'userid' => $u3->id,
  90              'usermodified' => $grader1->id));
  91          $grades['c2m1u4'] = $this->create_grade_history(array('itemid' => $gi->id, 'userid' => $u4->id,
  92              'usermodified' => $grader2->id));
  93  
  94          // Histories where grades have not been revised..
  95          $grades['c2m1u5a'] = $this->create_grade_history(array('itemid' => $gi->id, 'userid' => $u5->id,
  96              'timemodified' => time() - 60));
  97          $grades['c2m1u5b'] = $this->create_grade_history(array('itemid' => $gi->id, 'userid' => $u5->id,
  98              'timemodified' => time()));
  99          $grades['c2m1u5c'] = $this->create_grade_history(array('itemid' => $gi->id, 'userid' => $u5->id,
 100              'timemodified' => time() + 60));
 101  
 102          // Histories where grades have been revised and not revised.
 103          $now = time();
 104          $gi = grade_item::fetch($giparams + array('iteminstance' => $c2m2->id));
 105          $grades['c2m2u1a'] = $this->create_grade_history(array('itemid' => $gi->id, 'userid' => $u1->id,
 106              'timemodified' => $now - 60, 'finalgrade' => 50));
 107          $grades['c2m2u1b'] = $this->create_grade_history(array('itemid' => $gi->id, 'userid' => $u1->id,
 108              'timemodified' => $now - 50, 'finalgrade' => 50));      // Not revised.
 109          $grades['c2m2u1c'] = $this->create_grade_history(array('itemid' => $gi->id, 'userid' => $u1->id,
 110              'timemodified' => $now, 'finalgrade' => 75));
 111          $grades['c2m2u1d'] = $this->create_grade_history(array('itemid' => $gi->id, 'userid' => $u1->id,
 112              'timemodified' => $now + 10, 'finalgrade' => 75));      // Not revised.
 113          $grades['c2m2u1e'] = $this->create_grade_history(array('itemid' => $gi->id, 'userid' => $u1->id,
 114              'timemodified' => $now + 60, 'finalgrade' => 25));
 115          $grades['c2m2u1f'] = $this->create_grade_history(array('itemid' => $gi->id, 'userid' => $u1->id,
 116              'timemodified' => $now + 70, 'finalgrade' => 25));      // Not revised.
 117  
 118          // TODO MDL-46736 Handle deleted/non-existing grade items.
 119          // Histories with missing grade items, considered as deleted.
 120          // $grades['c2x1u5'] = $this->create_grade_history($giparams + array('itemid' => -1, 'userid' => $u5->id, 'courseid' => $c1->id));
 121          // $grades['c2x2u5'] = $this->create_grade_history($giparams + array('itemid' => 999999, 'userid' => $u5->id, 'courseid' => $c1->id));
 122  
 123          // Basic filtering based on course id.
 124          $this->assertEquals(8, $this->get_tablelog_results($c1ctx, array(), true));
 125          $this->assertEquals(13, $this->get_tablelog_results($c2ctx, array(), true));
 126  
 127          // Filtering on 1 user.
 128          $this->assertEquals(3, $this->get_tablelog_results($c1ctx, array('userids' => $u1->id), true));
 129  
 130          // Filtering on more users.
 131          $this->assertEquals(4, $this->get_tablelog_results($c1ctx, array('userids' => "$u1->id,$u3->id"), true));
 132  
 133          // Filtering based on one grade item.
 134          $gi = grade_item::fetch($giparams + array('iteminstance' => $c1m1->id));
 135          $this->assertEquals(5, $this->get_tablelog_results($c1ctx, array('itemid' => $gi->id), true));
 136          $gi = grade_item::fetch($giparams + array('iteminstance' => $c1m3->id));
 137          $this->assertEquals(1, $this->get_tablelog_results($c1ctx, array('itemid' => $gi->id), true));
 138  
 139          // Filtering based on the grader.
 140          $this->assertEquals(3, $this->get_tablelog_results($c2ctx, array('grader' => $grader1->id), true));
 141          $this->assertEquals(1, $this->get_tablelog_results($c2ctx, array('grader' => $grader2->id), true));
 142  
 143          // Filtering based on date.
 144          $results = $this->get_tablelog_results($c1ctx, array('datefrom' => time() + 1800));
 145          $this->assertGradeHistoryIds(array($grades['c1m1u2']->id), $results);
 146          $results = $this->get_tablelog_results($c1ctx, array('datetill' => time() - 1800));
 147          $this->assertGradeHistoryIds(array($grades['c1m1u1']->id), $results);
 148          $results = $this->get_tablelog_results($c1ctx, array('datefrom' => time() - 1800, 'datetill' => time() + 1800));
 149          $this->assertGradeHistoryIds(array($grades['c1m1u3']->id, $grades['c1m1u4']->id, $grades['c1m1u5']->id,
 150              $grades['c1m2u1']->id, $grades['c1m2u2']->id, $grades['c1m3u1']->id), $results);
 151  
 152          // Filtering based on revised only.
 153          $this->assertEquals(3, $this->get_tablelog_results($c2ctx, array('userids' => $u5->id), true));
 154          $this->assertEquals(1, $this->get_tablelog_results($c2ctx, array('userids' => $u5->id, 'revisedonly' => true), true));
 155  
 156          // More filtering based on revised only.
 157          $gi = grade_item::fetch($giparams + array('iteminstance' => $c2m2->id));
 158          $this->assertEquals(6, $this->get_tablelog_results($c2ctx, array('userids' => $u1->id, 'itemid' => $gi->id), true));
 159          $results = $this->get_tablelog_results($c2ctx, array('userids' => $u1->id, 'itemid' => $gi->id, 'revisedonly' => true));
 160          $this->assertGradeHistoryIds(array($grades['c2m2u1a']->id, $grades['c2m2u1c']->id, $grades['c2m2u1e']->id), $results);
 161  
 162          // Checking the value of the previous grade.
 163          $this->assertEquals(null, $results[$grades['c2m2u1a']->id]->prevgrade);
 164          $this->assertEquals($grades['c2m2u1a']->finalgrade, $results[$grades['c2m2u1c']->id]->prevgrade);
 165          $this->assertEquals($grades['c2m2u1c']->finalgrade, $results[$grades['c2m2u1e']->id]->prevgrade);
 166      }
 167  
 168      /**
 169       * Test the get users helper method.
 170       */
 171      public function test_get_users() {
 172          $this->resetAfterTest();
 173  
 174          // Making the setup.
 175          $c1 = $this->getDataGenerator()->create_course();
 176          $c2 = $this->getDataGenerator()->create_course();
 177          $c1ctx = context_course::instance($c1->id);
 178          $c2ctx = context_course::instance($c2->id);
 179  
 180          $c1m1 = $this->getDataGenerator()->create_module('assign', array('course' => $c1));
 181          $c2m1 = $this->getDataGenerator()->create_module('assign', array('course' => $c2));
 182  
 183          // Users.
 184          $u1 = $this->getDataGenerator()->create_user(array('firstname' => 'Eric', 'lastname' => 'Cartman'));
 185          $u2 = $this->getDataGenerator()->create_user(array('firstname' => 'Stan', 'lastname' => 'Marsh'));
 186          $u3 = $this->getDataGenerator()->create_user(array('firstname' => 'Kyle', 'lastname' => 'Broflovski'));
 187          $u4 = $this->getDataGenerator()->create_user(array('firstname' => 'Kenny', 'lastname' => 'McCormick'));
 188  
 189          // Creating grade history for some users.
 190          $gi = grade_item::fetch(array('iteminstance' => $c1m1->id, 'itemtype' => 'mod', 'itemmodule' => 'assign'));
 191          $this->create_grade_history(array('itemid' => $gi->id, 'userid' => $u1->id));
 192          $this->create_grade_history(array('itemid' => $gi->id, 'userid' => $u2->id));
 193          $this->create_grade_history(array('itemid' => $gi->id, 'userid' => $u3->id));
 194  
 195          $gi = grade_item::fetch(array('iteminstance' => $c2m1->id, 'itemtype' => 'mod', 'itemmodule' => 'assign'));
 196          $this->create_grade_history(array('itemid' => $gi->id, 'userid' => $u4->id));
 197  
 198          // Checking fetching some users.
 199          $users = \gradereport_history\helper::get_users($c1ctx);
 200          $this->assertCount(3, $users);
 201          $this->assertArrayHasKey($u3->id, $users);
 202          $users = \gradereport_history\helper::get_users($c2ctx);
 203          $this->assertCount(1, $users);
 204          $this->assertArrayHasKey($u4->id, $users);
 205          $users = \gradereport_history\helper::get_users($c1ctx, 'c');
 206          $this->assertCount(1, $users);
 207          $this->assertArrayHasKey($u1->id, $users);
 208          $users = \gradereport_history\helper::get_users($c1ctx, '', 0, 2);
 209          $this->assertCount(2, $users);
 210          $this->assertArrayHasKey($u3->id, $users);
 211          $this->assertArrayHasKey($u1->id, $users);
 212          $users = \gradereport_history\helper::get_users($c1ctx, '', 1, 2);
 213          $this->assertCount(1, $users);
 214          $this->assertArrayHasKey($u2->id, $users);
 215  
 216          // Checking the count of users.
 217          $this->assertEquals(3, \gradereport_history\helper::get_users_count($c1ctx));
 218          $this->assertEquals(1, \gradereport_history\helper::get_users_count($c2ctx));
 219          $this->assertEquals(1, \gradereport_history\helper::get_users_count($c1ctx, 'c'));
 220      }
 221  
 222      /**
 223       * Test the get graders helper method.
 224       */
 225      public function test_graders() {
 226          $this->resetAfterTest();
 227  
 228          // Making the setup.
 229          $c1 = $this->getDataGenerator()->create_course();
 230          $c2 = $this->getDataGenerator()->create_course();
 231  
 232          $c1m1 = $this->getDataGenerator()->create_module('assign', array('course' => $c1));
 233          $c2m1 = $this->getDataGenerator()->create_module('assign', array('course' => $c2));
 234  
 235          // Users.
 236          $u1 = $this->getDataGenerator()->create_user(array('firstname' => 'Eric', 'lastname' => 'Cartman'));
 237          $u2 = $this->getDataGenerator()->create_user(array('firstname' => 'Stan', 'lastname' => 'Marsh'));
 238          $u3 = $this->getDataGenerator()->create_user(array('firstname' => 'Kyle', 'lastname' => 'Broflovski'));
 239          $u4 = $this->getDataGenerator()->create_user(array('firstname' => 'Kenny', 'lastname' => 'McCormick'));
 240  
 241          // Creating grade history for some users.
 242          $gi = grade_item::fetch(array('iteminstance' => $c1m1->id, 'itemtype' => 'mod', 'itemmodule' => 'assign'));
 243          $this->create_grade_history(array('itemid' => $gi->id, 'userid' => $u1->id, 'usermodified' => $u1->id));
 244          $this->create_grade_history(array('itemid' => $gi->id, 'userid' => $u1->id, 'usermodified' => $u2->id));
 245          $this->create_grade_history(array('itemid' => $gi->id, 'userid' => $u1->id, 'usermodified' => $u3->id));
 246  
 247          $gi = grade_item::fetch(array('iteminstance' => $c2m1->id, 'itemtype' => 'mod', 'itemmodule' => 'assign'));
 248          $this->create_grade_history(array('itemid' => $gi->id, 'userid' => $u1->id, 'usermodified' => $u4->id));
 249  
 250          // Checking fetching some users.
 251          $graders = \gradereport_history\helper::get_graders($c1->id);
 252          $this->assertCount(4, $graders); // Including "all graders" .
 253          $this->assertArrayHasKey($u1->id, $graders);
 254          $this->assertArrayHasKey($u2->id, $graders);
 255          $this->assertArrayHasKey($u3->id, $graders);
 256          $graders = \gradereport_history\helper::get_graders($c2->id);
 257          $this->assertCount(2, $graders); // Including "all graders" .
 258          $this->assertArrayHasKey($u4->id, $graders);
 259      }
 260  
 261      /**
 262       * Asserts that the array of grade objects contains exactly the right IDs.
 263       *
 264       * @param array $expectedids Array of expected IDs.
 265       * @param array $objects Array of objects returned by the table.
 266       */
 267      protected function assertGradeHistoryIds(array $expectedids, array $objects) {
 268          $this->assertCount(count($expectedids), $objects);
 269          $expectedids = array_flip($expectedids);
 270          foreach ($objects as $object) {
 271              $this->assertArrayHasKey($object->id, $expectedids);
 272              unset($expectedids[$object->id]);
 273          }
 274          $this->assertCount(0, $expectedids);
 275      }
 276  
 277      /**
 278       * Create a new grade history entry.
 279       *
 280       * @param array $params Of values.
 281       * @return object The grade object.
 282       */
 283      protected function create_grade_history($params) {
 284          global $DB;
 285          $params = (array) $params;
 286  
 287          if (!isset($params['itemid'])) {
 288              throw new coding_exception('Missing itemid key.');
 289          }
 290          if (!isset($params['userid'])) {
 291              throw new coding_exception('Missing userid key.');
 292          }
 293  
 294          // Default object.
 295          $grade = new stdClass();
 296          $grade->itemid = 0;
 297          $grade->userid = 0;
 298          $grade->oldid = 123;
 299          $grade->rawgrade = 50;
 300          $grade->finalgrade = 50;
 301          $grade->timecreated = time();
 302          $grade->timemodified = time();
 303          $grade->information = '';
 304          $grade->informationformat = FORMAT_PLAIN;
 305          $grade->feedback = '';
 306          $grade->feedbackformat = FORMAT_PLAIN;
 307          $grade->usermodified = 2;
 308  
 309          // Merge with data passed.
 310          $grade = (object) array_merge((array) $grade, $params);
 311  
 312          // Insert record.
 313          $grade->id = $DB->insert_record('grade_grades_history', $grade);
 314  
 315          return $grade;
 316      }
 317  
 318      /**
 319       * Returns a table log object.
 320       *
 321       * @param context_course $coursecontext The course context.
 322       * @param array $filters An array of filters.
 323       * @param boolean $count When true, returns a count rather than an array of objects.
 324       * @return mixed Count or array of objects.
 325       */
 326      protected function get_tablelog_results($coursecontext, $filters = array(), $count = false) {
 327          $table = new gradereport_history_tests_tablelog('something', $coursecontext, new moodle_url(''), $filters);
 328          return $table->get_test_results($count);
 329      }
 330  
 331  }
 332  
 333  /**
 334   * Extended table log class.
 335   */
 336  class gradereport_history_tests_tablelog extends \gradereport_history\output\tablelog {
 337  
 338      /**
 339       * Get the test results.
 340       *
 341       * @param boolean $count Whether or not we want the count.
 342       * @return mixed Count or array of objects.
 343       */
 344      public function get_test_results($count = false) {
 345          global $DB;
 346          if ($count) {
 347              list($sql, $params) = $this->get_sql_and_params(true);
 348              return $DB->count_records_sql($sql, $params);
 349          } else {
 350              $this->setup();
 351              list($sql, $params) = $this->get_sql_and_params();
 352              return $DB->get_records_sql($sql, $params);
 353          }
 354      }
 355  
 356  }


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