PageRenderTime 54ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/grade/simpletest/testgradescale.php

http://github.com/moodle/moodle
PHP | 142 lines | 82 code | 29 blank | 31 comment | 1 complexity | b0c5b1f5ede1a4d4123810b7208fae79 MD5 | raw file
Possible License(s): MIT, AGPL-3.0, MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, Apache-2.0, LGPL-2.1, BSD-3-Clause
  1. <?php
  2. ///////////////////////////////////////////////////////////////////////////
  3. // //
  4. // NOTICE OF COPYRIGHT //
  5. // //
  6. // Moodle - Modular Object-Oriented Dynamic Learning Environment //
  7. // http://moodle.org //
  8. // //
  9. // Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //
  10. // //
  11. // This program is free software; you can redistribute it and/or modify //
  12. // it under the terms of the GNU General Public License as published by //
  13. // the Free Software Foundation; either version 2 of the License, or //
  14. // (at your option) any later version. //
  15. // //
  16. // This program is distributed in the hope that it will be useful, //
  17. // but WITHOUT ANY WARRANTY; without even the implied warranty of //
  18. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
  19. // GNU General Public License for more details: //
  20. // //
  21. // http://www.gnu.org/copyleft/gpl.html //
  22. // //
  23. ///////////////////////////////////////////////////////////////////////////
  24. /**
  25. * Unit tests for grade_scale object.
  26. *
  27. * @author nicolas@moodle.com
  28. * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
  29. * @package moodlecore
  30. */
  31. if (!defined('MOODLE_INTERNAL')) {
  32. die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
  33. }
  34. require_once($CFG->libdir.'/simpletest/fixtures/gradetest.php');
  35. class grade_scale_test extends grade_test {
  36. function test_grade_scale() {
  37. $this->sub_test_scale_construct();
  38. $this->sub_test_grade_scale_insert();
  39. $this->sub_test_grade_scale_update();
  40. $this->sub_test_grade_scale_delete();
  41. $this->sub_test_grade_scale_fetch();
  42. $this->sub_test_scale_load_items();
  43. $this->sub_test_scale_compact_items();
  44. }
  45. function sub_test_scale_construct() {
  46. $params = new stdClass();
  47. $params->name = 'unittestscale3';
  48. $params->courseid = $this->courseid;
  49. $params->userid = $this->userid;
  50. $params->scale = 'Distinction, Very Good, Good, Pass, Fail';
  51. $params->description = 'This scale is used to mark standard assignments.';
  52. $params->timemodified = mktime();
  53. $scale = new grade_scale($params, false);
  54. $this->assertEqual($params->name, $scale->name);
  55. $this->assertEqual($params->scale, $scale->scale);
  56. $this->assertEqual($params->description, $scale->description);
  57. }
  58. function sub_test_grade_scale_insert() {
  59. $grade_scale = new grade_scale();
  60. $this->assertTrue(method_exists($grade_scale, 'insert'));
  61. $grade_scale->name = 'unittestscale3';
  62. $grade_scale->courseid = $this->courseid;
  63. $grade_scale->userid = $this->userid;
  64. $grade_scale->scale = 'Distinction, Very Good, Good, Pass, Fail';
  65. $grade_scale->description = 'This scale is used to mark standard assignments.';
  66. $grade_scale->insert();
  67. $last_grade_scale = end($this->scale);
  68. $this->assertEqual($grade_scale->id, $last_grade_scale->id + 1);
  69. $this->assertTrue(!empty($grade_scale->timecreated));
  70. $this->assertTrue(!empty($grade_scale->timemodified));
  71. }
  72. function sub_test_grade_scale_update() {
  73. global $DB;
  74. $grade_scale = new grade_scale($this->scale[1]);
  75. $this->assertTrue(method_exists($grade_scale, 'update'));
  76. $grade_scale->name = 'Updated info for this unittest grade_scale';
  77. $this->assertTrue($grade_scale->update());
  78. $name = $DB->get_field('scale', 'name', array('id' => $this->scale[1]->id));
  79. $this->assertEqual($grade_scale->name, $name);
  80. }
  81. function sub_test_grade_scale_delete() {
  82. global $DB;
  83. $grade_scale = new grade_scale($this->scale[4]);//choose one we're not using elsewhere
  84. $this->assertTrue(method_exists($grade_scale, 'delete'));
  85. $this->assertTrue($grade_scale->delete());
  86. $this->assertFalse($DB->get_record('scale', array('id' => $grade_scale->id)));
  87. //keep the reference collection the same as what is in the database
  88. unset($this->scale[4]);
  89. }
  90. function sub_test_grade_scale_fetch() {
  91. $grade_scale = new grade_scale();
  92. $this->assertTrue(method_exists($grade_scale, 'fetch'));
  93. $grade_scale = grade_scale::fetch(array('id'=>$this->scale[0]->id));
  94. $this->assertEqual($this->scale[0]->id, $grade_scale->id);
  95. $this->assertEqual($this->scale[0]->name, $grade_scale->name);
  96. }
  97. function sub_test_scale_load_items() {
  98. $scale = new grade_scale($this->scale[0]);
  99. $this->assertTrue(method_exists($scale, 'load_items'));
  100. $scale->load_items();
  101. $this->assertEqual(7, count($scale->scale_items));
  102. $this->assertEqual('Fairly neutral', $scale->scale_items[2]);
  103. }
  104. function sub_test_scale_compact_items() {
  105. $scale = new grade_scale($this->scale[0]);
  106. $this->assertTrue(method_exists($scale, 'compact_items'));
  107. $scale->load_items();
  108. $scale->scale = null;
  109. $scale->compact_items();
  110. // The original string and the new string may have differences in whitespace around the delimiter, and that's OK
  111. $this->assertEqual(preg_replace('/\s*,\s*/', ',', $this->scale[0]->scale), $scale->scale);
  112. }
  113. }