From 3bafe8d0c4f724841ea92edbaaebac832381f0b3 Mon Sep 17 00:00:00 2001 From: Tim Hunt Date: Sat, 6 Sep 2014 15:26:23 +0100 Subject: [PATCH] MDL-46331 GIFT question import was breaking content inside
.

Also, adding a Behat tests to verify GIFT import works without errors
(but leaving the detailed testing for the unit tests).
---
 question/format/gift/format.php               |  2 +-
 .../format/gift/tests/behat/import.feature    | 30 +++++++++++++
 .../format/gift/tests/giftformat_test.php     | 45 +++++++++++++++++++
 3 files changed, 76 insertions(+), 1 deletion(-)
 create mode 100644 question/format/gift/tests/behat/import.feature

diff --git a/question/format/gift/format.php b/question/format/gift/format.php
index 156d4a888a0..19cd9e89b32 100644
--- a/question/format/gift/format.php
+++ b/question/format/gift/format.php
@@ -175,7 +175,7 @@ class qformat_gift extends qformat_default {
             }
         }
 
-        $text = trim(implode(' ', $lines));
+        $text = trim(implode("\n", $lines));
 
         if ($text == '') {
             return false;
diff --git a/question/format/gift/tests/behat/import.feature b/question/format/gift/tests/behat/import.feature
new file mode 100644
index 00000000000..89873cb395d
--- /dev/null
+++ b/question/format/gift/tests/behat/import.feature
@@ -0,0 +1,30 @@
+@qtype @qformat_gift
+Feature: Test importing questions from GIFT format.
+  In order to reuse questions
+  As an teacher
+  I need to be able to import them in GIFT format.
+
+  Background:
+    Given the following "courses" exist:
+      | fullname | shortname | format |
+      | Course 1 | C1        | topics |
+    And the following "users" exist:
+      | username | firstname |
+      | teacher  | Teacher   |
+    And the following "course enrolments" exist:
+      | user    | course | role           |
+      | teacher | C1     | editingteacher |
+    And I log in as "teacher"
+    And I follow "Course 1"
+
+  @javascript @_file_upload
+  Scenario: import some GIFT questions
+    When I navigate to "Import" node in "Course administration > Question bank"
+    And I set the field "id_format_gift" to "1"
+    And I upload "question/format/gift/tests/fixtures/questions.gift.txt" file to "Import" filemanager
+    And I press "id_submitbutton"
+    Then I should see "Parsing questions from import file."
+    And I should see "Importing 9 questions from file"
+    And I should see "What's between orange and green in the spectrum?"
+    When I press "Continue"
+    Then I should see "colours"
diff --git a/question/format/gift/tests/giftformat_test.php b/question/format/gift/tests/giftformat_test.php
index 0235afbb9de..9c7bc05d3d2 100644
--- a/question/format/gift/tests/giftformat_test.php
+++ b/question/format/gift/tests/giftformat_test.php
@@ -1070,4 +1070,49 @@ FALSE#42 is the Ultimate Answer.#You gave the right answer.}";
 
         $this->assert(new question_check_specified_fields_expectation($expectedq), $q);
     }
+
+    public function test_import_pre_content() {
+        $gift = '
+::Q001::[html]

What would running the test method print?

+
+    public void test() \{
+        method1();
+        method2();
+        method3();
+    \}
+
+{}'; + $lines = preg_split('/[\\n\\r]/', str_replace("\r\n", "\n", $gift)); + + $importer = new qformat_gift(); + $q = $importer->readquestion($lines); + + $expectedq = (object) array( + 'name' => 'Q001', + 'questiontext' => '

What would running the test method print?

+
+    public void test() {
+        method1();
+        method2();
+        method3();
+    }
+
', + 'questiontextformat' => FORMAT_HTML, + 'generalfeedback' => '', + 'generalfeedbackformat' => FORMAT_HTML, + 'qtype' => 'essay', + 'defaultmark' => 1, + 'penalty' => 0.3333333, + 'length' => 1, + 'responseformat' => 'editor', + 'responsefieldlines' => 15, + 'attachments' => 0, + 'graderinfo' => array( + 'text' => '', + 'format' => FORMAT_HTML, + 'files' => array()), + ); + + $this->assert(new question_check_specified_fields_expectation($expectedq), $q); + } }