/analytics/tests/course_test.php

https://github.com/sbourget/moodle · PHP · 141 lines · 74 code · 20 blank · 47 comment · 2 complexity · f2b02bc677cddf069f3cca0ce8b9677f MD5 · raw file

  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. namespace core_analytics;
  17. /**
  18. * Unit tests for course.
  19. *
  20. * @package core_analytics
  21. * @copyright 2016 David Monllaó {@link http://www.davidmonllao.com}
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. */
  24. class course_test extends \advanced_testcase {
  25. public function setUp(): void {
  26. global $DB;
  27. $this->course = $this->getDataGenerator()->create_course(['startdate' => 0]);
  28. $this->stu1 = $this->getDataGenerator()->create_user();
  29. $this->stu2 = $this->getDataGenerator()->create_user();
  30. $this->both = $this->getDataGenerator()->create_user();
  31. $this->editingteacher = $this->getDataGenerator()->create_user();
  32. $this->teacher = $this->getDataGenerator()->create_user();
  33. $this->studentroleid = $DB->get_field('role', 'id', array('shortname' => 'student'));
  34. $this->editingteacherroleid = $DB->get_field('role', 'id', array('shortname' => 'editingteacher'));
  35. $this->teacherroleid = $DB->get_field('role', 'id', array('shortname' => 'teacher'));
  36. $this->getDataGenerator()->enrol_user($this->stu1->id, $this->course->id, $this->studentroleid);
  37. $this->getDataGenerator()->enrol_user($this->stu2->id, $this->course->id, $this->studentroleid);
  38. $this->getDataGenerator()->enrol_user($this->both->id, $this->course->id, $this->studentroleid);
  39. $this->getDataGenerator()->enrol_user($this->both->id, $this->course->id, $this->editingteacherroleid);
  40. $this->getDataGenerator()->enrol_user($this->editingteacher->id, $this->course->id, $this->editingteacherroleid);
  41. $this->getDataGenerator()->enrol_user($this->teacher->id, $this->course->id, $this->teacherroleid);
  42. }
  43. /**
  44. * Users tests.
  45. */
  46. public function test_users() {
  47. global $DB;
  48. $this->resetAfterTest(true);
  49. $courseman = new \core_analytics\course($this->course->id);
  50. $this->assertCount(3, $courseman->get_user_ids(array($this->studentroleid)));
  51. $this->assertCount(2, $courseman->get_user_ids(array($this->editingteacherroleid)));
  52. $this->assertCount(1, $courseman->get_user_ids(array($this->teacherroleid)));
  53. // Distinct is applied.
  54. $this->assertCount(3, $courseman->get_user_ids(array($this->editingteacherroleid, $this->teacherroleid)));
  55. $this->assertCount(4, $courseman->get_user_ids(array($this->editingteacherroleid, $this->studentroleid)));
  56. }
  57. /**
  58. * Course validation tests.
  59. *
  60. * @return void
  61. */
  62. public function test_course_validation() {
  63. global $DB;
  64. $this->resetAfterTest(true);
  65. $courseman = new \core_analytics\course($this->course->id);
  66. $this->assertFalse($courseman->was_started());
  67. $this->assertFalse($courseman->is_finished());
  68. // Nothing should change when assigning as teacher.
  69. for ($i = 0; $i < 10; $i++) {
  70. $user = $this->getDataGenerator()->create_user();
  71. $this->getDataGenerator()->enrol_user($user->id, $this->course->id, $this->teacherroleid);
  72. }
  73. $courseman = new \core_analytics\course($this->course->id);
  74. $this->assertFalse($courseman->was_started());
  75. $this->assertFalse($courseman->is_finished());
  76. // More students now.
  77. for ($i = 0; $i < 10; $i++) {
  78. $user = $this->getDataGenerator()->create_user();
  79. $this->getDataGenerator()->enrol_user($user->id, $this->course->id, $this->studentroleid);
  80. }
  81. $courseman = new \core_analytics\course($this->course->id);
  82. $this->assertFalse($courseman->was_started());
  83. $this->assertFalse($courseman->is_finished());
  84. // Valid start date unknown end date.
  85. $this->course->startdate = gmmktime('0', '0', '0', 10, 24, 2015);
  86. $DB->update_record('course', $this->course);
  87. $courseman = new \core_analytics\course($this->course->id);
  88. $this->assertTrue($courseman->was_started());
  89. $this->assertFalse($courseman->is_finished());
  90. // Valid start and end date.
  91. $this->course->enddate = gmmktime('0', '0', '0', 8, 27, 2016);
  92. $DB->update_record('course', $this->course);
  93. $courseman = new \core_analytics\course($this->course->id);
  94. $this->assertTrue($courseman->was_started());
  95. $this->assertTrue($courseman->is_finished());
  96. // Valid start and ongoing course.
  97. $this->course->enddate = gmmktime('0', '0', '0', 8, 27, 2286);
  98. $DB->update_record('course', $this->course);
  99. $courseman = new \core_analytics\course($this->course->id);
  100. $this->assertTrue($courseman->was_started());
  101. $this->assertFalse($courseman->is_finished());
  102. }
  103. /**
  104. * Get the minimum time that is considered valid according to guess_end logic.
  105. *
  106. * @param int $time
  107. * @return int
  108. */
  109. protected function time_greater_than($time) {
  110. return $time - (WEEKSECS * 2);
  111. }
  112. /**
  113. * Get the maximum time that is considered valid according to guess_end logic.
  114. *
  115. * @param int $time
  116. * @return int
  117. */
  118. protected function time_less_than($time) {
  119. return $time + (WEEKSECS * 2);
  120. }
  121. }