From 09fc929e1375cfba939dffe7c9d45c98760302a9 Mon Sep 17 00:00:00 2001
From: Jean-Michel Vedrine <vedrine@vedrine.org>
Date: Sat, 20 Oct 2018 18:29:10 +0200
Subject: [PATCH 1/2] MDL-60897 qtype_multianswer: Invalid cloze-questions
 saved to db

---
 .../multianswer/edit_multianswer_form.php     |  6 +--
 .../type/multianswer/tests/behat/add.feature  | 40 +++++++++++++++++++
 2 files changed, 42 insertions(+), 4 deletions(-)
 create mode 100644 question/type/multianswer/tests/behat/add.feature

diff --git a/question/type/multianswer/edit_multianswer_form.php b/question/type/multianswer/edit_multianswer_form.php
index 80e12eddc45..141d1f1ad4c 100644
--- a/question/type/multianswer/edit_multianswer_form.php
+++ b/question/type/multianswer/edit_multianswer_form.php
@@ -512,14 +512,12 @@ class qtype_multianswer_edit_form extends question_edit_form {
                             }
                         }
                     }
-                    if ($answercount == 0) {
-                        if ($subquestion->qtype == 'multichoice') {
+                    if ($subquestion->qtype == 'multichoice' && $answercount < 2) {
                             $errors[$prefix.'answer[0]'] =
                                     get_string('notenoughanswers', 'qtype_multichoice', 2);
-                        } else {
+                    } else if ($answercount == 0) {
                             $errors[$prefix.'answer[0]'] =
                                     get_string('notenoughanswers', 'question', 1);
-                        }
                     }
                     if ($maxgrade == false) {
                         $errors[$prefix.'fraction[0]'] =
diff --git a/question/type/multianswer/tests/behat/add.feature b/question/type/multianswer/tests/behat/add.feature
new file mode 100644
index 00000000000..5bcb71f7155
--- /dev/null
+++ b/question/type/multianswer/tests/behat/add.feature
@@ -0,0 +1,40 @@
+@qtype @qtype_multianswer
+Feature: Test creating a Multianswer (Cloze) question
+  As a teacher
+  In order to test my students
+  I need to be able to create a Cloze question
+
+  Background:
+    Given the following "users" exist:
+      | username | firstname | lastname | email               |
+      | teacher1 | T1        | Teacher1 | teacher1@moodle.com |
+    And the following "courses" exist:
+      | fullname | shortname | category |
+      | Course 1 | C1        | 0        |
+    And the following "course enrolments" exist:
+      | user     | course | role           |
+      | teacher1 | C1     | editingteacher |
+    And I log in as "teacher1"
+    And I am on "Course 1" course homepage
+    And I navigate to "Question bank" in current page administration
+
+  Scenario: Create a Cloze question
+    When I add a "Embedded answers (Cloze)" question filling the form with:
+      | Question name        | multianswer-001                                     |
+      | Question text        | {1:SHORTANSWER:=Berlin} is the capital of Germany.  |
+      | General feedback     | The capital of Germany is Berlin.                   |
+    Then I should see "multianswer-001"
+
+  Scenario: Create a broken Cloze question and correct it
+    When I press "Create a new question ..."
+    And I set the field "Embedded answers (Cloze)" to "1"
+    And I press "Add"
+    And I set the field "Question name" to "multianswer-002"
+    And I set the field "Question text" to "Please select the fruits {1:MULTICHOICE:=Apple#Correct}"
+    And I set the field "General feedback" to "Apple are delicious."
+    And I press "id_submitbutton"
+    Then I should see "This type of question requires at least 2 choices"
+    When I set the following fields to these values:
+      | Question text | Please select the fruits {1:MULTICHOICE:=Apple#Correct~Banana#Wrong} |
+    And I press "id_submitbutton"
+    Then I should not see "This type of question requires at least 2 choices"

From 6ffeabfdff286ac6ce1ad3aa9be1b2673eb4c9a7 Mon Sep 17 00:00:00 2001
From: Jun Pataleta <jun@moodle.com>
Date: Tue, 30 Oct 2018 14:22:03 +0800
Subject: [PATCH 2/2] MDL-60897 qtype_multianswer: Code style fixes

And Behat steps improvement, too!
---
 .../multianswer/edit_multianswer_form.php     |  9 +++------
 .../type/multianswer/tests/behat/add.feature  | 20 ++++++++++++++-----
 2 files changed, 18 insertions(+), 11 deletions(-)

diff --git a/question/type/multianswer/edit_multianswer_form.php b/question/type/multianswer/edit_multianswer_form.php
index 141d1f1ad4c..3644514c535 100644
--- a/question/type/multianswer/edit_multianswer_form.php
+++ b/question/type/multianswer/edit_multianswer_form.php
@@ -513,15 +513,12 @@ class qtype_multianswer_edit_form extends question_edit_form {
                         }
                     }
                     if ($subquestion->qtype == 'multichoice' && $answercount < 2) {
-                            $errors[$prefix.'answer[0]'] =
-                                    get_string('notenoughanswers', 'qtype_multichoice', 2);
+                        $errors[$prefix.'answer[0]'] = get_string('notenoughanswers', 'qtype_multichoice', 2);
                     } else if ($answercount == 0) {
-                            $errors[$prefix.'answer[0]'] =
-                                    get_string('notenoughanswers', 'question', 1);
+                        $errors[$prefix.'answer[0]'] = get_string('notenoughanswers', 'question', 1);
                     }
                     if ($maxgrade == false) {
-                        $errors[$prefix.'fraction[0]'] =
-                                get_string('fractionsnomax', 'question');
+                        $errors[$prefix.'fraction[0]'] = get_string('fractionsnomax', 'question');
                     }
                     $sub++;
                 }
diff --git a/question/type/multianswer/tests/behat/add.feature b/question/type/multianswer/tests/behat/add.feature
index 5bcb71f7155..f371d7e3ac0 100644
--- a/question/type/multianswer/tests/behat/add.feature
+++ b/question/type/multianswer/tests/behat/add.feature
@@ -23,18 +23,28 @@ Feature: Test creating a Multianswer (Cloze) question
       | Question name        | multianswer-001                                     |
       | Question text        | {1:SHORTANSWER:=Berlin} is the capital of Germany.  |
       | General feedback     | The capital of Germany is Berlin.                   |
-    Then I should see "multianswer-001"
+    Then I should see "multianswer-001" in the "categoryquestions" "table"
 
   Scenario: Create a broken Cloze question and correct it
-    When I press "Create a new question ..."
+    Given I press "Create a new question ..."
     And I set the field "Embedded answers (Cloze)" to "1"
     And I press "Add"
     And I set the field "Question name" to "multianswer-002"
     And I set the field "Question text" to "Please select the fruits {1:MULTICHOICE:=Apple#Correct}"
     And I set the field "General feedback" to "Apple are delicious."
-    And I press "id_submitbutton"
+    When I press "id_submitbutton"
     Then I should see "This type of question requires at least 2 choices"
-    When I set the following fields to these values:
+    And I set the following fields to these values:
       | Question text | Please select the fruits {1:MULTICHOICE:=Apple#Correct~Banana#Wrong} |
     And I press "id_submitbutton"
-    Then I should not see "This type of question requires at least 2 choices"
+    And I should see "multianswer-002" in the "categoryquestions" "table"
+
+  Scenario: Try to create a Cloze question that has no answer
+    Given I press "Create a new question ..."
+    And I set the field "Embedded answers (Cloze)" to "1"
+    And I press "Add"
+    And I set the following fields to these values:
+      | Question name | multianswer-003                                |
+      | Question text | {1:SHORTANSWER:=  } is the capital of Germany. |
+    And I press "id_submitbutton"
+    And I should see "This type of question requires at least 1 answers"