PageRenderTime 26ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/enrol/imsenterprise/tests/imsenterprise_test.php

https://bitbucket.org/kudutest1/moodlegit
PHP | 327 lines | 189 code | 68 blank | 70 comment | 6 complexity | 77595a7fe2ef1b29296f764ce5629582 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. * IMS Enterprise enrolment tests.
  18. *
  19. * @package enrol_imsenterprise
  20. * @category phpunit
  21. * @copyright 2012 David MonllaĆ³
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. */
  24. defined('MOODLE_INTERNAL') || die();
  25. global $CFG;
  26. require_once($CFG->dirroot . '/enrol/imsenterprise/locallib.php');
  27. require_once($CFG->dirroot . '/enrol/imsenterprise/lib.php');
  28. /**
  29. * IMS Enterprise test case
  30. *
  31. * @package enrol_imsenterprise
  32. * @category phpunit
  33. * @copyright 2012 David MonllaĆ³
  34. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35. */
  36. class enrol_imsenterprise_testcase extends advanced_testcase {
  37. protected $imsplugin;
  38. protected function setUp() {
  39. $this->resetAfterTest(true);
  40. $this->imsplugin = enrol_get_plugin('imsenterprise');
  41. $this->set_test_config();
  42. }
  43. /**
  44. * With an empty IMS enterprise file
  45. */
  46. public function test_emptyfile() {
  47. global $DB;
  48. $prevncourses = $DB->count_records('course');
  49. $prevnusers = $DB->count_records('user');
  50. $this->set_xml_file(false, false);
  51. $this->imsplugin->cron();
  52. $this->assertEquals($prevncourses, $DB->count_records('course'));
  53. $this->assertEquals($prevnusers, $DB->count_records('user'));
  54. }
  55. /**
  56. * Existing users are not created again
  57. */
  58. public function test_users_existing() {
  59. global $DB;
  60. $user1 = $this->getDataGenerator()->create_user();
  61. $user2 = $this->getDataGenerator()->create_user();
  62. $prevnusers = $DB->count_records('user');
  63. $users = array($user1, $user2);
  64. $this->set_xml_file($users);
  65. $this->imsplugin->cron();
  66. $this->assertEquals($prevnusers, $DB->count_records('user'));
  67. }
  68. /**
  69. * Add new users
  70. */
  71. public function test_users_add() {
  72. global $DB;
  73. $prevnusers = $DB->count_records('user');
  74. $user1 = new StdClass();
  75. $user1->username = 'u1';
  76. $user1->email = 'u1@u1.org';
  77. $user1->firstname = 'U';
  78. $user1->lastname = '1';
  79. $users = array($user1);
  80. $this->set_xml_file($users);
  81. $this->imsplugin->cron();
  82. $this->assertEquals(($prevnusers + 1), $DB->count_records('user'));
  83. }
  84. /**
  85. * Existing courses are not created again
  86. */
  87. public function test_courses_existing() {
  88. global $DB;
  89. $course1 = $this->getDataGenerator()->create_course(array('idnumber' => 'id1'));
  90. $course2 = $this->getDataGenerator()->create_course(array('idnumber' => 'id2'));
  91. // Default mapping according to default course attributes - IMS description tags mapping.
  92. $course1->imsshort = $course1->fullname;
  93. $course2->imsshort = $course2->fullname;
  94. $prevncourses = $DB->count_records('course');
  95. $courses = array($course1, $course2);
  96. $this->set_xml_file(false, $courses);
  97. $this->imsplugin->cron();
  98. $this->assertEquals($prevncourses, $DB->count_records('course'));
  99. }
  100. /**
  101. * Add new courses
  102. */
  103. public function test_courses_add() {
  104. global $DB;
  105. $prevncourses = $DB->count_records('course');
  106. $course1 = new StdClass();
  107. $course1->idnumber = 'id1';
  108. $course1->imsshort = 'id1';
  109. $course1->category = 'DEFAULT CATNAME';
  110. $course2 = new StdClass();
  111. $course2->idnumber = 'id2';
  112. $course2->imsshort = 'id2';
  113. $course2->category = 'DEFAULT CATNAME';
  114. $courses = array($course1, $course2);
  115. $this->set_xml_file(false, $courses);
  116. $this->imsplugin->cron();
  117. $this->assertEquals(($prevncourses + 2), $DB->count_records('course'));
  118. }
  119. /**
  120. * Course attributes mapping to IMS enterprise group description tags
  121. */
  122. public function test_courses_attrmapping() {
  123. global $DB;
  124. // Setting a all = coursecode (idnumber) mapping.
  125. $this->imsplugin->set_config('imscoursemapshortname', 'coursecode');
  126. $this->imsplugin->set_config('imscoursemapfullname', 'coursecode');
  127. $this->imsplugin->set_config('imscoursemapsummary', 'coursecode');
  128. $course1 = new StdClass();
  129. $course1->idnumber = 'id1';
  130. $course1->imsshort = 'description_short1';
  131. $course1->imslong = 'description_long';
  132. $course1->imsfull = 'description_full';
  133. $course1->category = 'DEFAULT CATNAME';
  134. $this->set_xml_file(false, array($course1));
  135. $this->imsplugin->cron();
  136. $dbcourse = $DB->get_record('course', array('idnumber' => $course1->idnumber));
  137. $this->assertFalse(!$dbcourse);
  138. $this->assertEquals($dbcourse->shortname, $course1->idnumber);
  139. $this->assertEquals($dbcourse->fullname, $course1->idnumber);
  140. $this->assertEquals($dbcourse->summary, $course1->idnumber);
  141. // Setting a mapping using all the description tags.
  142. $this->imsplugin->set_config('imscoursemapshortname', 'short');
  143. $this->imsplugin->set_config('imscoursemapfullname', 'long');
  144. $this->imsplugin->set_config('imscoursemapsummary', 'full');
  145. $course2 = new StdClass();
  146. $course2->idnumber = 'id2';
  147. $course2->imsshort = 'description_short2';
  148. $course2->imslong = 'description_long';
  149. $course2->imsfull = 'description_full';
  150. $course2->category = 'DEFAULT CATNAME';
  151. $this->set_xml_file(false, array($course2));
  152. $this->imsplugin->cron();
  153. $dbcourse = $DB->get_record('course', array('idnumber' => $course2->idnumber));
  154. $this->assertFalse(!$dbcourse);
  155. $this->assertEquals($dbcourse->shortname, $course2->imsshort);
  156. $this->assertEquals($dbcourse->fullname, $course2->imslong);
  157. $this->assertEquals($dbcourse->summary, $course2->imsfull);
  158. // Setting a mapping where the specified description tags doesn't exist in the XML file (must delegate into idnumber).
  159. $this->imsplugin->set_config('imscoursemapshortname', 'short');
  160. $this->imsplugin->set_config('imscoursemapfullname', 'long');
  161. $this->imsplugin->set_config('imscoursemapsummary', 'full');
  162. $course3 = new StdClass();
  163. $course3->idnumber = 'id3';
  164. $course3->imsshort = 'description_short3';
  165. $course3->category = 'DEFAULT CATNAME';
  166. $this->set_xml_file(false, array($course3));
  167. $this->imsplugin->cron();
  168. $dbcourse = $DB->get_record('course', array('idnumber' => $course3->idnumber));
  169. $this->assertFalse(!$dbcourse);
  170. $this->assertEquals($dbcourse->shortname, $course3->imsshort);
  171. $this->assertEquals($dbcourse->fullname, $course3->idnumber);
  172. $this->assertEquals($dbcourse->summary, $course3->idnumber);
  173. }
  174. /**
  175. * Sets the plugin configuration for testing
  176. */
  177. protected function set_test_config() {
  178. $this->imsplugin->set_config('mailadmins', false);
  179. $this->imsplugin->set_config('prev_path', '');
  180. $this->imsplugin->set_config('createnewusers', true);
  181. $this->imsplugin->set_config('createnewcourses', true);
  182. $this->imsplugin->set_config('createnewcategories', true);
  183. }
  184. /**
  185. * Creates an IMS enterprise XML file and adds it's path to config settings
  186. *
  187. * @param array Array of users StdClass
  188. * @param array Array of courses StdClass
  189. */
  190. protected function set_xml_file($users = false, $courses = false) {
  191. global $DB;
  192. $xmlcontent = '<enterprise>';
  193. // Users.
  194. if (!empty($users)) {
  195. foreach ($users as $user) {
  196. $xmlcontent .= '
  197. <person>
  198. <sourcedid>
  199. <source>TestSource</source>
  200. <id>'.$user->username.'</id>
  201. </sourcedid>
  202. <userid>'.$user->username.'</userid>
  203. <name>
  204. <fn>'.$user->firstname.' '.$user->lastname.'</fn>
  205. <n>
  206. <family>'.$user->lastname.'</family>
  207. <given>'.$user->firstname.'</given>
  208. </n>
  209. </name>
  210. <email>'.$user->email.'</email>
  211. </person>';
  212. }
  213. }
  214. // Courses.
  215. // Mapping based on default course attributes - IMS group tags mapping.
  216. if (!empty($courses)) {
  217. foreach ($courses as $course) {
  218. $xmlcontent .= '
  219. <group>
  220. <sourcedid>
  221. <source>TestSource</source>
  222. <id>'.$course->idnumber.'</id>
  223. </sourcedid>
  224. <description>';
  225. // Optional to test course attributes mappings.
  226. if (!empty($course->imsshort)) {
  227. $xmlcontent .= '
  228. <short>'.$course->imsshort.'</short>';
  229. }
  230. // Optional to test course attributes mappings.
  231. if (!empty($course->imslong)) {
  232. $xmlcontent .= '
  233. <long>'.$course->imslong.'</long>';
  234. }
  235. // Optional to test course attributes mappings.
  236. if (!empty($course->imsfull)) {
  237. $xmlcontent .= '
  238. <full>'.$course->imsfull.'</full>';
  239. }
  240. // orgunit tag value is used by moodle as category name.
  241. $xmlcontent .= '
  242. </description>
  243. <org>
  244. <orgunit>'.$course->category.'</orgunit>
  245. </org>
  246. </group>';
  247. }
  248. }
  249. $xmlcontent .= '
  250. </enterprise>';
  251. // Creating the XML file.
  252. $filename = 'ims_' . rand(1000, 9999) . '.xml';
  253. $tmpdir = make_temp_directory('enrol_imsenterprise');
  254. $xmlfilepath = $tmpdir . '/' . $filename;
  255. file_put_contents($xmlfilepath, $xmlcontent);
  256. // Setting the file path in CFG.
  257. $this->imsplugin->set_config('imsfilelocation', $xmlfilepath);
  258. }
  259. }