PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/testing/tests/generator_test.php

https://bitbucket.org/kudutest1/moodlegit
PHP | 224 lines | 150 code | 43 blank | 31 comment | 3 complexity | d8cb3592a233e007d903880cca9bec27 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. /**
  17. * Data generators tests
  18. *
  19. * @package core
  20. * @category test
  21. * @copyright 2012 Petr Skoda {@link http://skodak.org}
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. */
  24. defined('MOODLE_INTERNAL') || die();
  25. /**
  26. * Test data generator
  27. *
  28. * @package core
  29. * @category test
  30. * @copyright 2012 Petr Skoda {@link http://skodak.org}
  31. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32. */
  33. class core_test_generator_testcase extends advanced_testcase {
  34. public function test_get_plugin_generator_good_case() {
  35. $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
  36. $this->assertInstanceOf('core_question_generator', $generator);
  37. }
  38. public function test_get_plugin_generator_sloppy_name() {
  39. $generator = $this->getDataGenerator()->get_plugin_generator('quiz');
  40. $this->assertDebuggingCalled('Please specify the component you want a generator for as ' .
  41. 'mod_quiz, not quiz.', DEBUG_DEVELOPER);
  42. $this->assertInstanceOf('mod_quiz_generator', $generator);
  43. }
  44. public function test_get_plugin_generator_no_component_dir() {
  45. $this->setExpectedException('coding_exception', 'Component core_completion does not support ' .
  46. 'generators yet. Missing tests/generator/lib.php.');
  47. $generator = $this->getDataGenerator()->get_plugin_generator('core_completion');
  48. }
  49. public function test_create() {
  50. global $DB;
  51. $this->resetAfterTest(true);
  52. $generator = $this->getDataGenerator();
  53. $count = $DB->count_records('user');
  54. $user = $generator->create_user();
  55. $this->assertEquals($count+1, $DB->count_records('user'));
  56. $this->assertSame($user->username, clean_param($user->username, PARAM_USERNAME));
  57. $this->assertSame($user->email, clean_param($user->email, PARAM_EMAIL));
  58. $user = $generator->create_user(array('firstname'=>'Žluťoučký', 'lastname'=>'Koníček'));
  59. $this->assertSame($user->username, clean_param($user->username, PARAM_USERNAME));
  60. $this->assertSame($user->email, clean_param($user->email, PARAM_EMAIL));
  61. $count = $DB->count_records('course_categories');
  62. $category = $generator->create_category();
  63. $this->assertEquals($count+1, $DB->count_records('course_categories'));
  64. $this->assertRegExp('/^Course category \d/', $category->name);
  65. $this->assertSame('', $category->idnumber);
  66. $this->assertRegExp('/^Test course category \d/', $category->description);
  67. $this->assertSame(FORMAT_MOODLE, $category->descriptionformat);
  68. $count = $DB->count_records('cohort');
  69. $cohort = $generator->create_cohort();
  70. $this->assertEquals($count+1, $DB->count_records('cohort'));
  71. $this->assertEquals(context_system::instance()->id, $cohort->contextid);
  72. $this->assertRegExp('/^Cohort \d/', $cohort->name);
  73. $this->assertSame('', $cohort->idnumber);
  74. $this->assertRegExp('/^Test cohort \d/', $cohort->description);
  75. $this->assertSame(FORMAT_MOODLE, $cohort->descriptionformat);
  76. $this->assertSame('', $cohort->component);
  77. $this->assertLessThanOrEqual(time(), $cohort->timecreated);
  78. $this->assertSame($cohort->timecreated, $cohort->timemodified);
  79. $count = $DB->count_records('course');
  80. $course = $generator->create_course();
  81. $this->assertEquals($count+1, $DB->count_records('course'));
  82. $this->assertRegExp('/^Test course \d/', $course->fullname);
  83. $this->assertRegExp('/^tc_\d/', $course->shortname);
  84. $this->assertSame('', $course->idnumber);
  85. $this->assertSame('topics', $course->format);
  86. $this->assertEquals(0, $course->newsitems);
  87. $this->assertEquals(5, $course->numsections);
  88. $this->assertRegExp('/^Test course \d/', $course->summary);
  89. $this->assertSame(FORMAT_MOODLE, $course->summaryformat);
  90. $section = $generator->create_course_section(array('course'=>$course->id, 'section'=>3));
  91. $this->assertEquals($course->id, $section->course);
  92. $scale = $generator->create_scale();
  93. $this->assertNotEmpty($scale);
  94. }
  95. public function test_create_module() {
  96. global $CFG, $SITE;
  97. if (!file_exists("$CFG->dirroot/mod/page/")) {
  98. $this->markTestSkipped('Can not find standard Page module');
  99. }
  100. $this->resetAfterTest(true);
  101. $generator = $this->getDataGenerator();
  102. $page = $generator->create_module('page', array('course'=>$SITE->id));
  103. $this->assertNotEmpty($page);
  104. $cm = get_coursemodule_from_instance('page', $page->id, $SITE->id, true);
  105. $this->assertEquals(0, $cm->sectionnum);
  106. $page = $generator->create_module('page', array('course'=>$SITE->id), array('section'=>3));
  107. $this->assertNotEmpty($page);
  108. $cm = get_coursemodule_from_instance('page', $page->id, $SITE->id, true);
  109. $this->assertEquals(3, $cm->sectionnum);
  110. }
  111. public function test_create_block() {
  112. global $CFG;
  113. if (!file_exists("$CFG->dirroot/blocks/online_users/")) {
  114. $this->markTestSkipped('Can not find standard Online users block');
  115. }
  116. $this->resetAfterTest(true);
  117. $generator = $this->getDataGenerator();
  118. $page = $generator->create_block('online_users');
  119. $this->assertNotEmpty($page);
  120. }
  121. public function test_enrol_user() {
  122. global $DB;
  123. $this->resetAfterTest();
  124. $selfplugin = enrol_get_plugin('self');
  125. $this->assertNotEmpty($selfplugin);
  126. $manualplugin = enrol_get_plugin('manual');
  127. $this->assertNotEmpty($manualplugin);
  128. // Prepare some data.
  129. $studentrole = $DB->get_record('role', array('shortname'=>'student'));
  130. $this->assertNotEmpty($studentrole);
  131. $teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
  132. $this->assertNotEmpty($teacherrole);
  133. $course1 = $this->getDataGenerator()->create_course();
  134. $course2 = $this->getDataGenerator()->create_course();
  135. $course3 = $this->getDataGenerator()->create_course();
  136. $context1 = context_course::instance($course1->id);
  137. $context2 = context_course::instance($course2->id);
  138. $context3 = context_course::instance($course3->id);
  139. $user1 = $this->getDataGenerator()->create_user();
  140. $user2 = $this->getDataGenerator()->create_user();
  141. $user3 = $this->getDataGenerator()->create_user();
  142. $this->assertEquals(3, $DB->count_records('enrol', array('enrol'=>'self')));
  143. $instance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'self'), '*', MUST_EXIST);
  144. $instance2 = $DB->get_record('enrol', array('courseid'=>$course2->id, 'enrol'=>'self'), '*', MUST_EXIST);
  145. $instance3 = $DB->get_record('enrol', array('courseid'=>$course3->id, 'enrol'=>'self'), '*', MUST_EXIST);
  146. $this->assertEquals($studentrole->id, $instance1->roleid);
  147. $this->assertEquals($studentrole->id, $instance2->roleid);
  148. $this->assertEquals($studentrole->id, $instance3->roleid);
  149. $this->assertEquals(3, $DB->count_records('enrol', array('enrol'=>'manual')));
  150. $maninstance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'manual'), '*', MUST_EXIST);
  151. $maninstance2 = $DB->get_record('enrol', array('courseid'=>$course2->id, 'enrol'=>'manual'), '*', MUST_EXIST);
  152. $maninstance3 = $DB->get_record('enrol', array('courseid'=>$course3->id, 'enrol'=>'manual'), '*', MUST_EXIST);
  153. $maninstance3->roleid = $teacherrole->id;
  154. $DB->update_record('enrol', $maninstance3, array('id'=>$maninstance3->id));
  155. $this->assertEquals($studentrole->id, $maninstance1->roleid);
  156. $this->assertEquals($studentrole->id, $maninstance2->roleid);
  157. $this->assertEquals($teacherrole->id, $maninstance3->roleid);
  158. $result = $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
  159. $this->assertTrue($result);
  160. $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$maninstance1->id, 'userid'=>$user1->id)));
  161. $this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>$context1->id, 'userid'=>$user1->id, 'roleid'=>$studentrole->id)));
  162. $result = $this->getDataGenerator()->enrol_user($user1->id, $course2->id, $teacherrole->id, 'manual');
  163. $this->assertTrue($result);
  164. $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$maninstance2->id, 'userid'=>$user1->id)));
  165. $this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>$context2->id, 'userid'=>$user1->id, 'roleid'=>$teacherrole->id)));
  166. $result = $this->getDataGenerator()->enrol_user($user1->id, $course3->id, 0, 'manual');
  167. $this->assertTrue($result);
  168. $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$maninstance3->id, 'userid'=>$user1->id)));
  169. $this->assertFalse($DB->record_exists('role_assignments', array('contextid'=>$context3->id, 'userid'=>$user1->id)));
  170. $result = $this->getDataGenerator()->enrol_user($user2->id, $course1->id, null, 'self');
  171. $this->assertTrue($result);
  172. $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$instance1->id, 'userid'=>$user2->id)));
  173. $this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>$context1->id, 'userid'=>$user2->id, 'roleid'=>$studentrole->id)));
  174. $selfplugin->add_instance($course2, array('status'=>ENROL_INSTANCE_ENABLED, 'roleid'=>$teacherrole->id));
  175. $result = $this->getDataGenerator()->enrol_user($user2->id, $course2->id, null, 'self');
  176. $this->assertFalse($result);
  177. $DB->delete_records('enrol', array('enrol'=>'self', 'courseid'=>$course3->id));
  178. $result = $this->getDataGenerator()->enrol_user($user2->id, $course3->id, null, 'self');
  179. $this->assertFalse($result);
  180. }
  181. }