PageRenderTime 47ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/question/format/gift/tests/giftformat_test.php

https://bitbucket.org/kudutest1/moodlegit
PHP | 1074 lines | 944 code | 86 blank | 44 comment | 1 complexity | c23550e1e4162215a9a23c48533bf85a 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. * Unit tests for the Moodle GIFT format.
  18. *
  19. * @package qformat
  20. * @subpackage gift
  21. * @copyright 2010 The Open University
  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->libdir . '/questionlib.php');
  27. require_once($CFG->dirroot . '/question/format.php');
  28. require_once($CFG->dirroot . '/question/format/gift/format.php');
  29. require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
  30. /**
  31. * Unit tests for the GIFT import/export format.
  32. *
  33. * @copyright 2010 The Open University
  34. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35. */
  36. class qformat_gift_test extends question_testcase {
  37. public function assert_same_gift($expectedtext, $text) {
  38. $this->assertEquals(str_replace("\r\n", "\n", $expectedtext),
  39. str_replace("\r\n", "\n", $text));
  40. }
  41. public function test_import_essay() {
  42. $gift = '
  43. // essay
  44. ::Q8:: How are you? {}';
  45. $lines = preg_split('/[\\n\\r]/', str_replace("\r\n", "\n", $gift));
  46. $importer = new qformat_gift();
  47. $q = $importer->readquestion($lines);
  48. $expectedq = (object) array(
  49. 'name' => 'Q8',
  50. 'questiontext' => 'How are you?',
  51. 'questiontextformat' => FORMAT_MOODLE,
  52. 'generalfeedback' => '',
  53. 'generalfeedbackformat' => FORMAT_MOODLE,
  54. 'qtype' => 'essay',
  55. 'defaultmark' => 1,
  56. 'penalty' => 0.3333333,
  57. 'length' => 1,
  58. 'responseformat' => 'editor',
  59. 'responsefieldlines' => 15,
  60. 'attachments' => 0,
  61. 'graderinfo' => array(
  62. 'text' => '',
  63. 'format' => FORMAT_HTML,
  64. 'files' => array()),
  65. );
  66. $this->assert(new question_check_specified_fields_expectation($expectedq), $q);
  67. }
  68. public function test_export_essay() {
  69. $qdata = (object) array(
  70. 'id' => 666 ,
  71. 'name' => 'Q8',
  72. 'questiontext' => 'How are you?',
  73. 'questiontextformat' => FORMAT_MOODLE,
  74. 'generalfeedback' => '',
  75. 'generalfeedbackformat' => FORMAT_MOODLE,
  76. 'defaultmark' => 1,
  77. 'penalty' => 0.3333333,
  78. 'length' => 1,
  79. 'qtype' => 'essay',
  80. 'options' => (object) array(
  81. 'responseformat' => 'editor',
  82. 'responsefieldlines' => 15,
  83. 'attachments' => 0,
  84. 'graderinfo' => '',
  85. 'graderinfoformat' => FORMAT_HTML,
  86. ),
  87. );
  88. $exporter = new qformat_gift();
  89. $gift = $exporter->writequestion($qdata);
  90. $expectedgift = "// question: 666 name: Q8
  91. ::Q8::How are you?{}
  92. ";
  93. $this->assert_same_gift($expectedgift, $gift);
  94. }
  95. public function test_import_match() {
  96. $gift = '
  97. // question: 2 name: Moodle activities
  98. ::Moodle activities::[html]Match the <b>activity</b> to the description.{
  99. =[html]An activity supporting asynchronous discussions. -> Forum
  100. =[moodle]A teacher asks a question and specifies a choice of multiple responses. -> Choice
  101. =[plain]A bank of record entries which participants can add to. -> Database
  102. =[markdown]A collection of web pages that anyone can add to or edit. -> Wiki
  103. = -> Chat
  104. }';
  105. $lines = preg_split('/[\\n\\r]/', str_replace("\r\n", "\n", $gift));
  106. $importer = new qformat_gift();
  107. $q = $importer->readquestion($lines);
  108. $expectedq = (object) array(
  109. 'name' => 'Moodle activities',
  110. 'questiontext' => 'Match the <b>activity</b> to the description.',
  111. 'questiontextformat' => FORMAT_HTML,
  112. 'generalfeedback' => '',
  113. 'generalfeedbackformat' => FORMAT_HTML,
  114. 'qtype' => 'match',
  115. 'defaultmark' => 1,
  116. 'penalty' => 0.3333333,
  117. 'length' => 1,
  118. 'shuffleanswers' => '1',
  119. 'correctfeedback' => array(
  120. 'text' => '',
  121. 'format' => FORMAT_HTML,
  122. 'files' => array(),
  123. ),
  124. 'partiallycorrectfeedback' => array(
  125. 'text' => '',
  126. 'format' => FORMAT_HTML,
  127. 'files' => array(),
  128. ),
  129. 'incorrectfeedback' => array(
  130. 'text' => '',
  131. 'format' => FORMAT_HTML,
  132. 'files' => array(),
  133. ),
  134. 'subquestions' => array(
  135. 0 => array(
  136. 'text' => 'An activity supporting asynchronous discussions.',
  137. 'format' => FORMAT_HTML,
  138. 'files' => array(),
  139. ),
  140. 1 => array(
  141. 'text' => 'A teacher asks a question and specifies a choice of multiple responses.',
  142. 'format' => FORMAT_MOODLE,
  143. 'files' => array(),
  144. ),
  145. 2 => array(
  146. 'text' => 'A bank of record entries which participants can add to.',
  147. 'format' => FORMAT_PLAIN,
  148. 'files' => array(),
  149. ),
  150. 3 => array(
  151. 'text' => 'A collection of web pages that anyone can add to or edit.',
  152. 'format' => FORMAT_MARKDOWN,
  153. 'files' => array(),
  154. ),
  155. 4 => array(
  156. 'text' => '',
  157. 'format' => FORMAT_HTML,
  158. 'files' => array(),
  159. ),
  160. ),
  161. 'subanswers' => array(
  162. 0 => 'Forum',
  163. 1 => 'Choice',
  164. 2 => 'Database',
  165. 3 => 'Wiki',
  166. 4 => 'Chat',
  167. ),
  168. );
  169. // Repeated test for better failure messages.
  170. $this->assertEquals($expectedq->subquestions, $q->subquestions);
  171. $this->assert(new question_check_specified_fields_expectation($expectedq), $q);
  172. }
  173. public function test_export_match() {
  174. $qdata = (object) array(
  175. 'id' => 666 ,
  176. 'name' => 'Moodle activities',
  177. 'questiontext' => 'Match the <b>activity</b> to the description.',
  178. 'questiontextformat' => FORMAT_HTML,
  179. 'generalfeedback' => '',
  180. 'generalfeedbackformat' => FORMAT_HTML,
  181. 'defaultmark' => 1,
  182. 'penalty' => 0.3333333,
  183. 'length' => 1,
  184. 'qtype' => 'match',
  185. 'options' => (object) array(
  186. 'id' => 123,
  187. 'question' => 666,
  188. 'shuffleanswers' => 1,
  189. 'subquestions' => array(
  190. 42 => (object) array(
  191. 'id' => 1234,
  192. 'code' => 12341234,
  193. 'question' => 666,
  194. 'questiontext' => '<div class="frog">An activity supporting asynchronous discussions.</div>',
  195. 'questiontextformat' => FORMAT_HTML,
  196. 'answertext' => 'Forum',
  197. ),
  198. 43 => (object) array(
  199. 'id' => 1234,
  200. 'code' => 12341234,
  201. 'question' => 666,
  202. 'questiontext' => 'A teacher asks a question and specifies a choice of multiple responses.',
  203. 'questiontextformat' => FORMAT_MOODLE,
  204. 'answertext' => 'Choice',
  205. ),
  206. 44 => (object) array(
  207. 'id' => 1234,
  208. 'code' => 12341234,
  209. 'question' => 666,
  210. 'questiontext' => 'A bank of record entries which participants can add to.',
  211. 'questiontextformat' => FORMAT_PLAIN,
  212. 'answertext' => 'Database',
  213. ),
  214. 45 => (object) array(
  215. 'id' => 1234,
  216. 'code' => 12341234,
  217. 'question' => 666,
  218. 'questiontext' => 'A collection of web pages that anyone can add to or edit.',
  219. 'questiontextformat' => FORMAT_MARKDOWN,
  220. 'answertext' => 'Wiki',
  221. ),
  222. 46 => (object) array(
  223. 'id' => 1234,
  224. 'code' => 12341234,
  225. 'question' => 666,
  226. 'questiontext' => '',
  227. 'questiontextformat' => FORMAT_MARKDOWN,
  228. 'answertext' => 'Chat',
  229. ),
  230. ),
  231. ),
  232. );
  233. $exporter = new qformat_gift();
  234. $gift = $exporter->writequestion($qdata);
  235. $expectedgift = "// question: 666 name: Moodle activities
  236. ::Moodle activities::[html]Match the <b>activity</b> to the description.{
  237. \t=<div class\\=\"frog\">An activity supporting asynchronous discussions.</div> -> Forum
  238. \t=[moodle]A teacher asks a question and specifies a choice of multiple responses. -> Choice
  239. \t=[plain]A bank of record entries which participants can add to. -> Database
  240. \t=[markdown]A collection of web pages that anyone can add to or edit. -> Wiki
  241. \t= -> Chat
  242. }
  243. ";
  244. $this->assert_same_gift($expectedgift, $gift);
  245. }
  246. public function test_import_multichoice() {
  247. $gift = "
  248. // multiple choice with specified feedback for right and wrong answers
  249. ::Q2:: What's between orange and green in the spectrum?
  250. {
  251. =yellow # right; good!
  252. ~red # [html]wrong, it's yellow
  253. ~[plain]blue # wrong, it's yellow
  254. }";
  255. $lines = preg_split('/[\\n\\r]/', str_replace("\r\n", "\n", $gift));
  256. $importer = new qformat_gift();
  257. $q = $importer->readquestion($lines);
  258. $expectedq = (object) array(
  259. 'name' => 'Q2',
  260. 'questiontext' => "What's between orange and green in the spectrum?",
  261. 'questiontextformat' => FORMAT_MOODLE,
  262. 'generalfeedback' => '',
  263. 'generalfeedbackformat' => FORMAT_MOODLE,
  264. 'qtype' => 'multichoice',
  265. 'defaultmark' => 1,
  266. 'penalty' => 0.3333333,
  267. 'length' => 1,
  268. 'single' => 1,
  269. 'shuffleanswers' => '1',
  270. 'answernumbering' => 'abc',
  271. 'correctfeedback' => array(
  272. 'text' => '',
  273. 'format' => FORMAT_MOODLE,
  274. 'files' => array(),
  275. ),
  276. 'partiallycorrectfeedback' => array(
  277. 'text' => '',
  278. 'format' => FORMAT_MOODLE,
  279. 'files' => array(),
  280. ),
  281. 'incorrectfeedback' => array(
  282. 'text' => '',
  283. 'format' => FORMAT_MOODLE,
  284. 'files' => array(),
  285. ),
  286. 'answer' => array(
  287. 0 => array(
  288. 'text' => 'yellow',
  289. 'format' => FORMAT_MOODLE,
  290. 'files' => array(),
  291. ),
  292. 1 => array(
  293. 'text' => 'red',
  294. 'format' => FORMAT_MOODLE,
  295. 'files' => array(),
  296. ),
  297. 2 => array(
  298. 'text' => 'blue',
  299. 'format' => FORMAT_PLAIN,
  300. 'files' => array(),
  301. ),
  302. ),
  303. 'fraction' => array(1, 0, 0),
  304. 'feedback' => array(
  305. 0 => array(
  306. 'text' => 'right; good!',
  307. 'format' => FORMAT_MOODLE,
  308. 'files' => array(),
  309. ),
  310. 1 => array(
  311. 'text' => "wrong, it's yellow",
  312. 'format' => FORMAT_HTML,
  313. 'files' => array(),
  314. ),
  315. 2 => array(
  316. 'text' => "wrong, it's yellow",
  317. 'format' => FORMAT_MOODLE,
  318. 'files' => array(),
  319. ),
  320. ),
  321. );
  322. // Repeated test for better failure messages.
  323. $this->assertEquals($expectedq->answer, $q->answer);
  324. $this->assertEquals($expectedq->feedback, $q->feedback);
  325. $this->assert(new question_check_specified_fields_expectation($expectedq), $q);
  326. }
  327. public function test_import_multichoice_multi() {
  328. $gift = "
  329. // multiple choice, multiple response with specified feedback for right and wrong answers
  330. ::colours:: What's between orange and green in the spectrum?
  331. {
  332. ~%50%yellow # right; good!
  333. ~%-100%red # [html]wrong
  334. ~%50%off-beige # right; good!
  335. ~%-100%[plain]blue # wrong
  336. }";
  337. $lines = preg_split('/[\\n\\r]/', str_replace("\r\n", "\n", $gift));
  338. $importer = new qformat_gift();
  339. $q = $importer->readquestion($lines);
  340. $expectedq = (object) array(
  341. 'name' => 'colours',
  342. 'questiontext' => "What's between orange and green in the spectrum?",
  343. 'questiontextformat' => FORMAT_MOODLE,
  344. 'generalfeedback' => '',
  345. 'generalfeedbackformat' => FORMAT_MOODLE,
  346. 'qtype' => 'multichoice',
  347. 'defaultmark' => 1,
  348. 'penalty' => 0.3333333,
  349. 'length' => 1,
  350. 'single' => 0,
  351. 'shuffleanswers' => '1',
  352. 'answernumbering' => 'abc',
  353. 'correctfeedback' => array(
  354. 'text' => '',
  355. 'format' => FORMAT_MOODLE,
  356. 'files' => array(),
  357. ),
  358. 'partiallycorrectfeedback' => array(
  359. 'text' => '',
  360. 'format' => FORMAT_MOODLE,
  361. 'files' => array(),
  362. ),
  363. 'incorrectfeedback' => array(
  364. 'text' => '',
  365. 'format' => FORMAT_MOODLE,
  366. 'files' => array(),
  367. ),
  368. 'answer' => array(
  369. 0 => array(
  370. 'text' => 'yellow',
  371. 'format' => FORMAT_MOODLE,
  372. 'files' => array(),
  373. ),
  374. 1 => array(
  375. 'text' => 'red',
  376. 'format' => FORMAT_MOODLE,
  377. 'files' => array(),
  378. ),
  379. 2 => array(
  380. 'text' => 'off-beige',
  381. 'format' => FORMAT_MOODLE,
  382. 'files' => array(),
  383. ),
  384. 3 => array(
  385. 'text' => 'blue',
  386. 'format' => FORMAT_PLAIN,
  387. 'files' => array(),
  388. ),
  389. ),
  390. 'fraction' => array(0.5, -1, 0.5, -1),
  391. 'feedback' => array(
  392. 0 => array(
  393. 'text' => 'right; good!',
  394. 'format' => FORMAT_MOODLE,
  395. 'files' => array(),
  396. ),
  397. 1 => array(
  398. 'text' => "wrong",
  399. 'format' => FORMAT_HTML,
  400. 'files' => array(),
  401. ),
  402. 2 => array(
  403. 'text' => "right; good!",
  404. 'format' => FORMAT_MOODLE,
  405. 'files' => array(),
  406. ),
  407. 3 => array(
  408. 'text' => "wrong",
  409. 'format' => FORMAT_MOODLE,
  410. 'files' => array(),
  411. ),
  412. ),
  413. );
  414. // Repeated test for better failure messages.
  415. $this->assertEquals($expectedq->answer, $q->answer);
  416. $this->assertEquals($expectedq->feedback, $q->feedback);
  417. $this->assert(new question_check_specified_fields_expectation($expectedq), $q);
  418. }
  419. public function test_export_multichoice() {
  420. $qdata = (object) array(
  421. 'id' => 666 ,
  422. 'name' => 'Q8',
  423. 'questiontext' => "What's between orange and green in the spectrum?",
  424. 'questiontextformat' => FORMAT_MOODLE,
  425. 'generalfeedback' => '',
  426. 'generalfeedbackformat' => FORMAT_MOODLE,
  427. 'defaultmark' => 1,
  428. 'penalty' => 0.3333333,
  429. 'length' => 1,
  430. 'qtype' => 'multichoice',
  431. 'options' => (object) array(
  432. 'single' => 1,
  433. 'shuffleanswers' => '1',
  434. 'answernumbering' => 'abc',
  435. 'correctfeedback' => '',
  436. 'correctfeedbackformat' => FORMAT_MOODLE,
  437. 'partiallycorrectfeedback' => '',
  438. 'partiallycorrectfeedbackformat' => FORMAT_MOODLE,
  439. 'incorrectfeedback' => '',
  440. 'incorrectfeedbackformat' => FORMAT_MOODLE,
  441. 'answers' => array(
  442. 123 => (object) array(
  443. 'id' => 123,
  444. 'answer' => 'yellow',
  445. 'answerformat' => FORMAT_MOODLE,
  446. 'fraction' => 1,
  447. 'feedback' => 'right; good!',
  448. 'feedbackformat' => FORMAT_MOODLE,
  449. ),
  450. 124 => (object) array(
  451. 'id' => 124,
  452. 'answer' => 'red',
  453. 'answerformat' => FORMAT_MOODLE,
  454. 'fraction' => 0,
  455. 'feedback' => "wrong, it's yellow",
  456. 'feedbackformat' => FORMAT_HTML,
  457. ),
  458. 125 => (object) array(
  459. 'id' => 125,
  460. 'answer' => 'blue',
  461. 'answerformat' => FORMAT_PLAIN,
  462. 'fraction' => 0,
  463. 'feedback' => "wrong, it's yellow",
  464. 'feedbackformat' => FORMAT_MOODLE,
  465. ),
  466. ),
  467. ),
  468. );
  469. $exporter = new qformat_gift();
  470. $gift = $exporter->writequestion($qdata);
  471. $expectedgift = "// question: 666 name: Q8
  472. ::Q8::What's between orange and green in the spectrum?{
  473. \t=yellow#right; good!
  474. \t~red#[html]wrong, it's yellow
  475. \t~[plain]blue#wrong, it's yellow
  476. }
  477. ";
  478. $this->assert_same_gift($expectedgift, $gift);
  479. }
  480. public function test_import_numerical() {
  481. $gift = "
  482. // math range question
  483. ::Q5:: What is a number from 1 to 5? {#3:2~#Completely wrong}";
  484. $lines = preg_split('/[\\n\\r]/', str_replace("\r\n", "\n", $gift));
  485. $importer = new qformat_gift();
  486. $q = $importer->readquestion($lines);
  487. $expectedq = (object) array(
  488. 'name' => 'Q5',
  489. 'questiontext' => "What is a number from 1 to 5?",
  490. 'questiontextformat' => FORMAT_MOODLE,
  491. 'generalfeedback' => '',
  492. 'generalfeedbackformat' => FORMAT_MOODLE,
  493. 'qtype' => 'numerical',
  494. 'defaultmark' => 1,
  495. 'penalty' => 0.3333333,
  496. 'length' => 1,
  497. 'answer' => array(
  498. '3',
  499. '*',
  500. ),
  501. 'fraction' => array(1, 0),
  502. 'feedback' => array(
  503. 0 => array(
  504. 'text' => '',
  505. 'format' => FORMAT_MOODLE,
  506. 'files' => array(),
  507. ),
  508. 1 => array(
  509. 'text' => "Completely wrong",
  510. 'format' => FORMAT_MOODLE,
  511. 'files' => array(),
  512. ),
  513. ),
  514. 'tolerance' => array(2, 0),
  515. );
  516. // Repeated test for better failure messages.
  517. $this->assertEquals($expectedq->answer, $q->answer);
  518. $this->assertEquals($expectedq->fraction, $q->fraction);
  519. $this->assertEquals($expectedq->feedback, $q->feedback);
  520. $this->assert(new question_check_specified_fields_expectation($expectedq), $q);
  521. }
  522. public function test_export_numerical() {
  523. $qdata = (object) array(
  524. 'id' => 666 ,
  525. 'name' => 'Q5',
  526. 'questiontext' => "What is a number from 1 to 5?",
  527. 'questiontextformat' => FORMAT_MOODLE,
  528. 'generalfeedback' => '',
  529. 'generalfeedbackformat' => FORMAT_MOODLE,
  530. 'defaultmark' => 1,
  531. 'penalty' => 1,
  532. 'length' => 1,
  533. 'qtype' => 'numerical',
  534. 'options' => (object) array(
  535. 'id' => 123,
  536. 'question' => 666,
  537. 'showunits' => 0,
  538. 'unitsleft' => 0,
  539. 'showunits' => 2,
  540. 'unitgradingtype' => 0,
  541. 'unitpenalty' => 0,
  542. 'answers' => array(
  543. 1 => (object) array(
  544. 'id' => 123,
  545. 'answer' => '3',
  546. 'answerformat' => 0,
  547. 'fraction' => 1,
  548. 'tolerance' => 2,
  549. 'feedback' => '',
  550. 'feedbackformat' => FORMAT_MOODLE,
  551. ),
  552. 2 => (object) array(
  553. 'id' => 124,
  554. 'answer' => '*',
  555. 'answerformat' => 0,
  556. 'fraction' => 0,
  557. 'tolerance' => 0,
  558. 'feedback' => "Completely wrong",
  559. 'feedbackformat' => FORMAT_MOODLE,
  560. ),
  561. ),
  562. ),
  563. );
  564. $exporter = new qformat_gift();
  565. $gift = $exporter->writequestion($qdata);
  566. $expectedgift = "// question: 666 name: Q5
  567. ::Q5::What is a number from 1 to 5?{#
  568. \t=%100%3:2#
  569. \t~#Completely wrong
  570. }
  571. ";
  572. $this->assert_same_gift($expectedgift, $gift);
  573. }
  574. public function test_import_shortanswer() {
  575. $gift = "
  576. // question: 666 name: Shortanswer
  577. ::Shortanswer::Which is the best animal?{
  578. =Frog#Good!
  579. =%50%Cat#What is it with Moodlers and cats?
  580. =%0%*#Completely wrong
  581. }";
  582. $lines = preg_split('/[\\n\\r]/', str_replace("\r\n", "\n", $gift));
  583. $importer = new qformat_gift();
  584. $q = $importer->readquestion($lines);
  585. $expectedq = (object) array(
  586. 'name' => 'Shortanswer',
  587. 'questiontext' => "Which is the best animal?",
  588. 'questiontextformat' => FORMAT_MOODLE,
  589. 'generalfeedback' => '',
  590. 'generalfeedbackformat' => FORMAT_MOODLE,
  591. 'qtype' => 'shortanswer',
  592. 'defaultmark' => 1,
  593. 'penalty' => 0.3333333,
  594. 'length' => 1,
  595. 'answer' => array(
  596. 'Frog',
  597. 'Cat',
  598. '*',
  599. ),
  600. 'fraction' => array(1, 0.5, 0),
  601. 'feedback' => array(
  602. 0 => array(
  603. 'text' => 'Good!',
  604. 'format' => FORMAT_MOODLE,
  605. 'files' => array(),
  606. ),
  607. 1 => array(
  608. 'text' => "What is it with Moodlers and cats?",
  609. 'format' => FORMAT_MOODLE,
  610. 'files' => array(),
  611. ),
  612. 2 => array(
  613. 'text' => "Completely wrong",
  614. 'format' => FORMAT_MOODLE,
  615. 'files' => array(),
  616. ),
  617. ),
  618. );
  619. // Repeated test for better failure messages.
  620. $this->assertEquals($expectedq->answer, $q->answer);
  621. $this->assertEquals($expectedq->fraction, $q->fraction);
  622. $this->assertEquals($expectedq->feedback, $q->feedback);
  623. $this->assert(new question_check_specified_fields_expectation($expectedq), $q);
  624. }
  625. public function test_import_shortanswer_with_general_feedback() {
  626. $gift = "
  627. // question: 666 name: Shortanswer
  628. ::Shortanswer::Which is the best animal?{
  629. =Frog#Good!
  630. =%50%Cat#What is it with Moodlers and cats?
  631. =%0%*#Completely wrong
  632. ####[html]Here is some general feedback!
  633. }";
  634. $lines = preg_split('/[\\n\\r]/', str_replace("\r\n", "\n", $gift));
  635. $importer = new qformat_gift();
  636. $q = $importer->readquestion($lines);
  637. $expectedq = (object) array(
  638. 'name' => 'Shortanswer',
  639. 'questiontext' => "Which is the best animal?",
  640. 'questiontextformat' => FORMAT_MOODLE,
  641. 'generalfeedback' => 'Here is some general feedback!',
  642. 'generalfeedbackformat' => FORMAT_HTML,
  643. 'qtype' => 'shortanswer',
  644. 'defaultmark' => 1,
  645. 'penalty' => 0.3333333,
  646. 'length' => 1,
  647. 'answer' => array(
  648. 'Frog',
  649. 'Cat',
  650. '*',
  651. ),
  652. 'fraction' => array(1, 0.5, 0),
  653. 'feedback' => array(
  654. 0 => array(
  655. 'text' => 'Good!',
  656. 'format' => FORMAT_MOODLE,
  657. 'files' => array(),
  658. ),
  659. 1 => array(
  660. 'text' => "What is it with Moodlers and cats?",
  661. 'format' => FORMAT_MOODLE,
  662. 'files' => array(),
  663. ),
  664. 2 => array(
  665. 'text' => "Completely wrong",
  666. 'format' => FORMAT_MOODLE,
  667. 'files' => array(),
  668. ),
  669. ),
  670. );
  671. // Repeated test for better failure messages.
  672. $this->assertEquals($expectedq->answer, $q->answer);
  673. $this->assertEquals($expectedq->fraction, $q->fraction);
  674. $this->assertEquals($expectedq->feedback, $q->feedback);
  675. $this->assert(new question_check_specified_fields_expectation($expectedq), $q);
  676. }
  677. public function test_export_shortanswer() {
  678. $qdata = (object) array(
  679. 'id' => 666 ,
  680. 'name' => 'Shortanswer',
  681. 'questiontext' => "Which is the best animal?",
  682. 'questiontextformat' => FORMAT_MOODLE,
  683. 'generalfeedback' => '',
  684. 'generalfeedbackformat' => FORMAT_MOODLE,
  685. 'defaultmark' => 1,
  686. 'penalty' => 1,
  687. 'length' => 1,
  688. 'qtype' => 'shortanswer',
  689. 'options' => (object) array(
  690. 'id' => 123,
  691. 'questionid' => 666,
  692. 'usecase' => 1,
  693. 'answers' => array(
  694. 1 => (object) array(
  695. 'id' => 1,
  696. 'answer' => 'Frog',
  697. 'answerformat' => 0,
  698. 'fraction' => 1,
  699. 'feedback' => 'Good!',
  700. 'feedbackformat' => FORMAT_MOODLE,
  701. ),
  702. 2 => (object) array(
  703. 'id' => 2,
  704. 'answer' => 'Cat',
  705. 'answerformat' => 0,
  706. 'fraction' => 0.5,
  707. 'feedback' => "What is it with Moodlers and cats?",
  708. 'feedbackformat' => FORMAT_MOODLE,
  709. ),
  710. 3 => (object) array(
  711. 'id' => 3,
  712. 'answer' => '*',
  713. 'answerformat' => 0,
  714. 'fraction' => 0,
  715. 'feedback' => "Completely wrong",
  716. 'feedbackformat' => FORMAT_MOODLE,
  717. ),
  718. ),
  719. ),
  720. );
  721. $exporter = new qformat_gift();
  722. $gift = $exporter->writequestion($qdata);
  723. $expectedgift = "// question: 666 name: Shortanswer
  724. ::Shortanswer::Which is the best animal?{
  725. \t=%100%Frog#Good!
  726. \t=%50%Cat#What is it with Moodlers and cats?
  727. \t=%0%*#Completely wrong
  728. }
  729. ";
  730. $this->assert_same_gift($expectedgift, $gift);
  731. }
  732. public function test_export_shortanswer_with_general_feedback() {
  733. $qdata = (object) array(
  734. 'id' => 666 ,
  735. 'name' => 'Shortanswer',
  736. 'questiontext' => "Which is the best animal?",
  737. 'questiontextformat' => FORMAT_MOODLE,
  738. 'generalfeedback' => 'Here is some general feedback!',
  739. 'generalfeedbackformat' => FORMAT_HTML,
  740. 'defaultmark' => 1,
  741. 'penalty' => 1,
  742. 'length' => 1,
  743. 'qtype' => 'shortanswer',
  744. 'options' => (object) array(
  745. 'id' => 123,
  746. 'questionid' => 666,
  747. 'usecase' => 1,
  748. 'answers' => array(
  749. 1 => (object) array(
  750. 'id' => 1,
  751. 'answer' => 'Frog',
  752. 'answerformat' => 0,
  753. 'fraction' => 1,
  754. 'feedback' => 'Good!',
  755. 'feedbackformat' => FORMAT_MOODLE,
  756. ),
  757. 2 => (object) array(
  758. 'id' => 2,
  759. 'answer' => 'Cat',
  760. 'answerformat' => 0,
  761. 'fraction' => 0.5,
  762. 'feedback' => "What is it with Moodlers and cats?",
  763. 'feedbackformat' => FORMAT_MOODLE,
  764. ),
  765. 3 => (object) array(
  766. 'id' => 3,
  767. 'answer' => '*',
  768. 'answerformat' => 0,
  769. 'fraction' => 0,
  770. 'feedback' => "Completely wrong",
  771. 'feedbackformat' => FORMAT_MOODLE,
  772. ),
  773. ),
  774. ),
  775. );
  776. $exporter = new qformat_gift();
  777. $gift = $exporter->writequestion($qdata);
  778. $expectedgift = "// question: 666 name: Shortanswer
  779. ::Shortanswer::Which is the best animal?{
  780. \t=%100%Frog#Good!
  781. \t=%50%Cat#What is it with Moodlers and cats?
  782. \t=%0%*#Completely wrong
  783. \t####[html]Here is some general feedback!
  784. }
  785. ";
  786. $this->assert_same_gift($expectedgift, $gift);
  787. }
  788. public function test_import_truefalse() {
  789. $gift = "
  790. // true/false
  791. ::Q1:: 42 is the Absolute Answer to everything.{
  792. FALSE#42 is the Ultimate Answer.#You gave the right answer.}";
  793. $lines = preg_split('/[\\n\\r]/', str_replace("\r\n", "\n", $gift));
  794. $importer = new qformat_gift();
  795. $q = $importer->readquestion($lines);
  796. $expectedq = (object) array(
  797. 'name' => 'Q1',
  798. 'questiontext' => "42 is the Absolute Answer to everything.",
  799. 'questiontextformat' => FORMAT_MOODLE,
  800. 'generalfeedback' => '',
  801. 'generalfeedbackformat' => FORMAT_MOODLE,
  802. 'qtype' => 'truefalse',
  803. 'defaultmark' => 1,
  804. 'penalty' => 1,
  805. 'length' => 1,
  806. 'correctanswer' => 0,
  807. 'feedbacktrue' => array(
  808. 'text' => '42 is the Ultimate Answer.',
  809. 'format' => FORMAT_MOODLE,
  810. 'files' => array(),
  811. ),
  812. 'feedbackfalse' => array(
  813. 'text' => 'You gave the right answer.',
  814. 'format' => FORMAT_MOODLE,
  815. 'files' => array(),
  816. ),
  817. );
  818. $this->assert(new question_check_specified_fields_expectation($expectedq), $q);
  819. }
  820. public function test_import_truefalse_true_answer1() {
  821. $gift = "// name 0-11
  822. ::2-08 TSL::TSL is blablabla.{T}";
  823. $lines = preg_split('/[\\n\\r]/', str_replace("\r\n", "\n", $gift));
  824. $importer = new qformat_gift();
  825. $q = $importer->readquestion($lines);
  826. $expectedq = (object) array(
  827. 'name' => '2-08 TSL',
  828. 'questiontext' => "TSL is blablabla.",
  829. 'questiontextformat' => FORMAT_MOODLE,
  830. 'generalfeedback' => '',
  831. 'generalfeedbackformat' => FORMAT_MOODLE,
  832. 'qtype' => 'truefalse',
  833. 'defaultmark' => 1,
  834. 'penalty' => 1,
  835. 'length' => 1,
  836. 'correctanswer' => 1,
  837. 'feedbacktrue' => array(
  838. 'text' => '',
  839. 'format' => FORMAT_MOODLE,
  840. 'files' => array(),
  841. ),
  842. 'feedbackfalse' => array(
  843. 'text' => '',
  844. 'format' => FORMAT_MOODLE,
  845. 'files' => array(),
  846. ),
  847. );
  848. $this->assert(new question_check_specified_fields_expectation($expectedq), $q);
  849. }
  850. public function test_import_truefalse_true_answer2() {
  851. $gift = "// name 0-11
  852. ::2-08 TSL::TSL is blablabla.{TRUE}";
  853. $lines = preg_split('/[\\n\\r]/', str_replace("\r\n", "\n", $gift));
  854. $importer = new qformat_gift();
  855. $q = $importer->readquestion($lines);
  856. $expectedq = (object) array(
  857. 'name' => '2-08 TSL',
  858. 'questiontext' => "TSL is blablabla.",
  859. 'questiontextformat' => FORMAT_MOODLE,
  860. 'generalfeedback' => '',
  861. 'generalfeedbackformat' => FORMAT_MOODLE,
  862. 'qtype' => 'truefalse',
  863. 'defaultmark' => 1,
  864. 'penalty' => 1,
  865. 'length' => 1,
  866. 'correctanswer' => 1,
  867. 'feedbacktrue' => array(
  868. 'text' => '',
  869. 'format' => FORMAT_MOODLE,
  870. 'files' => array(),
  871. ),
  872. 'feedbackfalse' => array(
  873. 'text' => '',
  874. 'format' => FORMAT_MOODLE,
  875. 'files' => array(),
  876. ),
  877. );
  878. $this->assert(new question_check_specified_fields_expectation($expectedq), $q);
  879. }
  880. public function test_export_truefalse() {
  881. $qdata = (object) array(
  882. 'id' => 666 ,
  883. 'name' => 'Q1',
  884. 'questiontext' => "42 is the Absolute Answer to everything.",
  885. 'questiontextformat' => FORMAT_MOODLE,
  886. 'generalfeedback' => '',
  887. 'generalfeedbackformat' => FORMAT_MOODLE,
  888. 'defaultmark' => 1,
  889. 'penalty' => 1,
  890. 'length' => 1,
  891. 'qtype' => 'truefalse',
  892. 'options' => (object) array(
  893. 'id' => 123,
  894. 'question' => 666,
  895. 'trueanswer' => 1,
  896. 'falseanswer' => 2,
  897. 'answers' => array(
  898. 1 => (object) array(
  899. 'id' => 123,
  900. 'answer' => 'True',
  901. 'answerformat' => 0,
  902. 'fraction' => 1,
  903. 'feedback' => 'You gave the right answer.',
  904. 'feedbackformat' => FORMAT_MOODLE,
  905. ),
  906. 2 => (object) array(
  907. 'id' => 124,
  908. 'answer' => 'False',
  909. 'answerformat' => 0,
  910. 'fraction' => 0,
  911. 'feedback' => "42 is the Ultimate Answer.",
  912. 'feedbackformat' => FORMAT_HTML,
  913. ),
  914. ),
  915. ),
  916. );
  917. $exporter = new qformat_gift();
  918. $gift = $exporter->writequestion($qdata);
  919. $expectedgift = "// question: 666 name: Q1
  920. ::Q1::42 is the Absolute Answer to everything.{TRUE#[html]42 is the Ultimate Answer.#You gave the right answer.}
  921. ";
  922. $this->assert_same_gift($expectedgift, $gift);
  923. }
  924. public function test_export_backslash() {
  925. // There was a bug (MDL-34171) where \\ was getting exported as \\, not
  926. // \\\\, and on import, \\ in converted to \.
  927. // We need \\\\ in the test code, because of PHPs string escaping rules.
  928. $qdata = (object) array(
  929. 'id' => 666 ,
  930. 'name' => 'backslash',
  931. 'questiontext' => 'A \\ B \\\\ C',
  932. 'questiontextformat' => FORMAT_MOODLE,
  933. 'generalfeedback' => '',
  934. 'generalfeedbackformat' => FORMAT_MOODLE,
  935. 'defaultmark' => 1,
  936. 'penalty' => 0.3333333,
  937. 'length' => 1,
  938. 'qtype' => 'essay',
  939. 'options' => (object) array(
  940. 'responseformat' => 'editor',
  941. 'responsefieldlines' => 15,
  942. 'attachments' => 0,
  943. 'graderinfo' => '',
  944. 'graderinfoformat' => FORMAT_HTML,
  945. ),
  946. );
  947. $exporter = new qformat_gift();
  948. $gift = $exporter->writequestion($qdata);
  949. $expectedgift = "// question: 666 name: backslash
  950. ::backslash::A \\\\ B \\\\\\\\ C{}
  951. ";
  952. $this->assert_same_gift($expectedgift, $gift);
  953. }
  954. public function test_import_backslash() {
  955. // There was a bug (MDL-34171) where \\ in the import was getting changed
  956. // to \. This test checks for that.
  957. // We need \\\\ in the test code, because of PHPs string escaping rules.
  958. $gift = '
  959. // essay
  960. ::double backslash:: A \\\\ B \\\\\\\\ C{}';
  961. $lines = preg_split('/[\\n\\r]/', str_replace("\r\n", "\n", $gift));
  962. $importer = new qformat_gift();
  963. $q = $importer->readquestion($lines);
  964. $expectedq = (object) array(
  965. 'name' => 'double backslash',
  966. 'questiontext' => 'A \\ B \\\\ C',
  967. 'questiontextformat' => FORMAT_MOODLE,
  968. 'generalfeedback' => '',
  969. 'generalfeedbackformat' => FORMAT_MOODLE,
  970. 'qtype' => 'essay',
  971. 'defaultmark' => 1,
  972. 'penalty' => 0.3333333,
  973. 'length' => 1,
  974. 'responseformat' => 'editor',
  975. 'responsefieldlines' => 15,
  976. 'attachments' => 0,
  977. 'graderinfo' => array(
  978. 'text' => '',
  979. 'format' => FORMAT_HTML,
  980. 'files' => array()),
  981. );
  982. $this->assert(new question_check_specified_fields_expectation($expectedq), $q);
  983. }
  984. }