PageRenderTime 54ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/grade/tests/grade_scale_test.php

https://bitbucket.org/synergylearning/campusconnect
PHP | 130 lines | 80 code | 28 blank | 22 comment | 1 complexity | ddf57142d0b5b1ce5a71d9432d525e65 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, LGPL-2.1, Apache-2.0, BSD-3-Clause, AGPL-3.0
  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. * @package core_grades
  18. * @category phpunit
  19. * @copyright nicolas@moodle.com
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. */
  22. defined('MOODLE_INTERNAL') || die();
  23. require_once(__DIR__.'/fixtures/lib.php');
  24. class core_grade_scale_testcase extends grade_base_testcase {
  25. public function test_grade_scale() {
  26. $this->sub_test_scale_construct();
  27. $this->sub_test_grade_scale_insert();
  28. $this->sub_test_grade_scale_update();
  29. $this->sub_test_grade_scale_delete();
  30. $this->sub_test_grade_scale_fetch();
  31. $this->sub_test_scale_load_items();
  32. $this->sub_test_scale_compact_items();
  33. }
  34. protected function sub_test_scale_construct() {
  35. $params = new stdClass();
  36. $params->name = 'unittestscale3';
  37. $params->courseid = $this->course->id;
  38. $params->userid = $this->userid;
  39. $params->scale = 'Distinction, Very Good, Good, Pass, Fail';
  40. $params->description = 'This scale is used to mark standard assignments.';
  41. $params->timemodified = time();
  42. $scale = new grade_scale($params, false);
  43. $this->assertEquals($params->name, $scale->name);
  44. $this->assertEquals($params->scale, $scale->scale);
  45. $this->assertEquals($params->description, $scale->description);
  46. }
  47. protected function sub_test_grade_scale_insert() {
  48. $grade_scale = new grade_scale();
  49. $this->assertTrue(method_exists($grade_scale, 'insert'));
  50. $grade_scale->name = 'unittestscale3';
  51. $grade_scale->courseid = $this->courseid;
  52. $grade_scale->userid = $this->userid;
  53. $grade_scale->scale = 'Distinction, Very Good, Good, Pass, Fail';
  54. $grade_scale->description = 'This scale is used to mark standard assignments.';
  55. $grade_scale->insert();
  56. $last_grade_scale = end($this->scale);
  57. $this->assertEquals($grade_scale->id, $last_grade_scale->id + 1);
  58. $this->assertNotEmpty($grade_scale->timecreated);
  59. $this->assertNotEmpty($grade_scale->timemodified);
  60. }
  61. protected function sub_test_grade_scale_update() {
  62. global $DB;
  63. $grade_scale = new grade_scale($this->scale[1], false);
  64. $this->assertTrue(method_exists($grade_scale, 'update'));
  65. $grade_scale->name = 'Updated info for this unittest grade_scale';
  66. $this->assertTrue($grade_scale->update());
  67. $name = $DB->get_field('scale', 'name', array('id' => $this->scale[1]->id));
  68. $this->assertEquals($grade_scale->name, $name);
  69. }
  70. protected function sub_test_grade_scale_delete() {
  71. global $DB;
  72. $grade_scale = new grade_scale($this->scale[4], false); // Choose one we're not using elsewhere.
  73. $this->assertTrue(method_exists($grade_scale, 'delete'));
  74. $this->assertTrue($grade_scale->delete());
  75. $this->assertFalse($DB->get_record('scale', array('id' => $grade_scale->id)));
  76. // Keep the reference collection the same as what is in the database.
  77. unset($this->scale[4]);
  78. }
  79. protected function sub_test_grade_scale_fetch() {
  80. $grade_scale = new grade_scale();
  81. $this->assertTrue(method_exists($grade_scale, 'fetch'));
  82. $grade_scale = grade_scale::fetch(array('id'=>$this->scale[0]->id));
  83. $this->assertEquals($this->scale[0]->id, $grade_scale->id);
  84. $this->assertEquals($this->scale[0]->name, $grade_scale->name);
  85. }
  86. protected function sub_test_scale_load_items() {
  87. $scale = new grade_scale($this->scale[0], false);
  88. $this->assertTrue(method_exists($scale, 'load_items'));
  89. $scale->load_items();
  90. $this->assertCount(7, $scale->scale_items);
  91. $this->assertEquals('Fairly neutral', $scale->scale_items[2]);
  92. }
  93. protected function sub_test_scale_compact_items() {
  94. $scale = new grade_scale($this->scale[0], false);
  95. $this->assertTrue(method_exists($scale, 'compact_items'));
  96. $scale->load_items();
  97. $scale->scale = null;
  98. $scale->compact_items();
  99. // The original string and the new string may have differences in whitespace around the delimiter, and that's OK.
  100. $this->assertEquals(preg_replace('/\s*,\s*/', ',', $this->scale[0]->scale), $scale->scale);
  101. }
  102. }