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

/mod/data/tests/import_test.php

https://bitbucket.org/moodle/moodle
PHP | 282 lines | 174 code | 35 blank | 73 comment | 2 complexity | e9b79736778d3c9c4f7a8e260b286db5 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, BSD-3-Clause, MIT, GPL-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. namespace mod_data;
  17. /**
  18. * Unit tests for import.php.
  19. *
  20. * @package mod_data
  21. * @category test
  22. * @copyright 2019 Tobias Reischmann
  23. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24. */
  25. class import_test extends \advanced_testcase {
  26. /**
  27. * Set up function.
  28. */
  29. protected function setUp(): void {
  30. parent::setUp();
  31. global $CFG;
  32. require_once($CFG->dirroot . '/mod/data/lib.php');
  33. require_once($CFG->dirroot . '/lib/datalib.php');
  34. require_once($CFG->dirroot . '/lib/csvlib.class.php');
  35. require_once($CFG->dirroot . '/search/tests/fixtures/testable_core_search.php');
  36. require_once($CFG->dirroot . '/mod/data/tests/generator/lib.php');
  37. }
  38. /**
  39. * Get the test data.
  40. * In this instance we are setting up database records to be used in the unit tests.
  41. *
  42. * @return array
  43. */
  44. protected function get_test_data(): array {
  45. $this->resetAfterTest(true);
  46. $generator = $this->getDataGenerator()->get_plugin_generator('mod_data');
  47. $course = $this->getDataGenerator()->create_course();
  48. $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher');
  49. $this->setUser($teacher);
  50. $student = $this->getDataGenerator()->create_and_enrol($course, 'student', array('username' => 'student'));
  51. $data = $generator->create_instance(array('course' => $course->id));
  52. $cm = get_coursemodule_from_instance('data', $data->id);
  53. // Add fields.
  54. $fieldrecord = new \stdClass();
  55. $fieldrecord->name = 'ID'; // Identifier of the records for testing.
  56. $fieldrecord->type = 'number';
  57. $generator->create_field($fieldrecord, $data);
  58. $fieldrecord->name = 'Param2';
  59. $fieldrecord->type = 'text';
  60. $generator->create_field($fieldrecord, $data);
  61. return [
  62. 'teacher' => $teacher,
  63. 'student' => $student,
  64. 'data' => $data,
  65. 'cm' => $cm,
  66. ];
  67. }
  68. /**
  69. * Test uploading entries for a data instance without userdata.
  70. * @throws dml_exception
  71. */
  72. public function test_import(): void {
  73. [
  74. 'data' => $data,
  75. 'cm' => $cm,
  76. 'teacher' => $teacher,
  77. ] = $this->get_test_data();
  78. $filecontent = file_get_contents(__DIR__ . '/fixtures/test_data_import.csv');
  79. ob_start();
  80. data_import_csv($cm, $data, $filecontent, 'UTF-8', 'comma');
  81. ob_end_clean();
  82. // No userdata is present in the file: Fallback is to assign the uploading user as author.
  83. $expecteduserids = array();
  84. $expecteduserids[1] = $teacher->id;
  85. $expecteduserids[2] = $teacher->id;
  86. $records = $this->get_data_records($data->id);
  87. $this->assertCount(2, $records);
  88. foreach ($records as $record) {
  89. $identifier = $record->items['ID']->content;
  90. $this->assertEquals($expecteduserids[$identifier], $record->userid);
  91. }
  92. }
  93. /**
  94. * Test uploading entries for a data instance with userdata.
  95. *
  96. * At least one entry has an identifiable user, which is assigned as author.
  97. * @throws dml_exception
  98. */
  99. public function test_import_with_userdata(): void {
  100. [
  101. 'data' => $data,
  102. 'cm' => $cm,
  103. 'teacher' => $teacher,
  104. 'student' => $student,
  105. ] = $this->get_test_data();
  106. $filecontent = file_get_contents(__DIR__ . '/fixtures/test_data_import_with_userdata.csv');
  107. ob_start();
  108. data_import_csv($cm, $data, $filecontent, 'UTF-8', 'comma');
  109. ob_end_clean();
  110. $expecteduserids = array();
  111. $expecteduserids[1] = $student->id; // User student exists and is assigned as author.
  112. $expecteduserids[2] = $teacher->id; // User student2 does not exist. Fallback is the uploading user.
  113. $records = $this->get_data_records($data->id);
  114. $this->assertCount(2, $records);
  115. foreach ($records as $record) {
  116. $identifier = $record->items['ID']->content;
  117. $this->assertEquals($expecteduserids[$identifier], $record->userid);
  118. }
  119. }
  120. /**
  121. * Test uploading entries for a data instance with userdata and a defined field 'Username'.
  122. *
  123. * This should test the corner case, in which a user has defined a data fields, which has the same name
  124. * as the current lang string for username. In that case, the first Username entry is used for the field.
  125. * The second one is used to identify the author.
  126. * @throws coding_exception
  127. * @throws dml_exception
  128. */
  129. public function test_import_with_field_username(): void {
  130. [
  131. 'data' => $data,
  132. 'cm' => $cm,
  133. 'teacher' => $teacher,
  134. 'student' => $student,
  135. ] = $this->get_test_data();
  136. $generator = $this->getDataGenerator()->get_plugin_generator('mod_data');
  137. // Add username field.
  138. $fieldrecord = new \stdClass();
  139. $fieldrecord->name = 'Username';
  140. $fieldrecord->type = 'text';
  141. $generator->create_field($fieldrecord, $data);
  142. $filecontent = file_get_contents(__DIR__ . '/fixtures/test_data_import_with_field_username.csv');
  143. ob_start();
  144. data_import_csv($cm, $data, $filecontent, 'UTF-8', 'comma');
  145. ob_end_clean();
  146. $expecteduserids = array();
  147. $expecteduserids[1] = $student->id; // User student exists and is assigned as author.
  148. $expecteduserids[2] = $teacher->id; // User student2 does not exist. Fallback is the uploading user.
  149. $expecteduserids[3] = $student->id; // User student exists and is assigned as author.
  150. $expectedcontent = array();
  151. $expectedcontent[1] = array(
  152. 'Username' => 'otherusername1',
  153. 'Param2' => 'My first entry',
  154. );
  155. $expectedcontent[2] = array(
  156. 'Username' => 'otherusername2',
  157. 'Param2' => 'My second entry',
  158. );
  159. $expectedcontent[3] = array(
  160. 'Username' => 'otherusername3',
  161. 'Param2' => 'My third entry',
  162. );
  163. $records = $this->get_data_records($data->id);
  164. $this->assertCount(3, $records);
  165. foreach ($records as $record) {
  166. $identifier = $record->items['ID']->content;
  167. $this->assertEquals($expecteduserids[$identifier], $record->userid);
  168. foreach ($expectedcontent[$identifier] as $field => $value) {
  169. $this->assertEquals($value, $record->items[$field]->content,
  170. "The value of field \"$field\" for the record at position \"$identifier\" ".
  171. "which is \"{$record->items[$field]->content}\" does not match the expected value \"$value\".");
  172. }
  173. }
  174. }
  175. /**
  176. * Test uploading entries for a data instance with a field 'Username' but only one occurrence in the csv file.
  177. *
  178. * This should test the corner case, in which a user has defined a data fields, which has the same name
  179. * as the current lang string for username. In that case, the only Username entry is used for the field.
  180. * The author should not be set.
  181. * @throws coding_exception
  182. * @throws dml_exception
  183. */
  184. public function test_import_with_field_username_without_userdata(): void {
  185. [
  186. 'data' => $data,
  187. 'cm' => $cm,
  188. 'teacher' => $teacher,
  189. 'student' => $student,
  190. ] = $this->get_test_data();
  191. $generator = $this->getDataGenerator()->get_plugin_generator('mod_data');
  192. // Add username field.
  193. $fieldrecord = new \stdClass();
  194. $fieldrecord->name = 'Username';
  195. $fieldrecord->type = 'text';
  196. $generator->create_field($fieldrecord, $data);
  197. $filecontent = file_get_contents(__DIR__ . '/fixtures/test_data_import_with_userdata.csv');
  198. ob_start();
  199. data_import_csv($cm, $data, $filecontent, 'UTF-8', 'comma');
  200. ob_end_clean();
  201. // No userdata is present in the file: Fallback is to assign the uploading user as author.
  202. $expecteduserids = array();
  203. $expecteduserids[1] = $teacher->id;
  204. $expecteduserids[2] = $teacher->id;
  205. $expectedcontent = array();
  206. $expectedcontent[1] = array(
  207. 'Username' => 'student',
  208. 'Param2' => 'My first entry',
  209. );
  210. $expectedcontent[2] = array(
  211. 'Username' => 'student2',
  212. 'Param2' => 'My second entry',
  213. );
  214. $records = $this->get_data_records($data->id);
  215. $this->assertCount(2, $records);
  216. foreach ($records as $record) {
  217. $identifier = $record->items['ID']->content;
  218. $this->assertEquals($expecteduserids[$identifier], $record->userid);
  219. foreach ($expectedcontent[$identifier] as $field => $value) {
  220. $this->assertEquals($value, $record->items[$field]->content,
  221. "The value of field \"$field\" for the record at position \"$identifier\" ".
  222. "which is \"{$record->items[$field]->content}\" does not match the expected value \"$value\".");
  223. }
  224. }
  225. }
  226. /**
  227. * Returns the records of the data instance.
  228. *
  229. * Each records has an item entry, which contains all fields associated with this item.
  230. * Each fields has the parameters name, type and content.
  231. * @param int $dataid Id of the data instance.
  232. * @return array The records of the data instance.
  233. * @throws dml_exception
  234. */
  235. private function get_data_records(int $dataid): array {
  236. global $DB;
  237. $records = $DB->get_records('data_records', ['dataid' => $dataid]);
  238. foreach ($records as $record) {
  239. $sql = 'SELECT f.name, f.type, con.content FROM
  240. {data_content} con JOIN {data_fields} f ON con.fieldid = f.id
  241. WHERE con.recordid = :recordid';
  242. $items = $DB->get_records_sql($sql, array('recordid' => $record->id));
  243. $record->items = $items;
  244. }
  245. return $records;
  246. }
  247. }