From b296667ebf980e6301c2894324aa6e0b1032db5a Mon Sep 17 00:00:00 2001
From: Juan Segarra Montesinos <juan.segarra@uji.es>
Date: Fri, 17 Mar 2023 06:49:50 +0100
Subject: [PATCH] MDL-77670 course: Preserve course summary format

---
 course/lib.php                                |  2 +-
 .../tests/behat/course_summary_format.feature | 33 +++++++++++++++++++
 2 files changed, 34 insertions(+), 1 deletion(-)
 create mode 100644 course/tests/behat/course_summary_format.feature

diff --git a/course/lib.php b/course/lib.php
index b6745221c70..529a01855c5 100644
--- a/course/lib.php
+++ b/course/lib.php
@@ -2214,7 +2214,7 @@ function create_course($data, $editoroptions = NULL) {
     if ($editoroptions) {
         // summary text is updated later, we need context to store the files first
         $data->summary = '';
-        $data->summary_format = FORMAT_HTML;
+        $data->summary_format = $data->summary_editor['format'];
     }
 
     // Get default completion settings as a fallback in case the enablecompletion field is not set.
diff --git a/course/tests/behat/course_summary_format.feature b/course/tests/behat/course_summary_format.feature
new file mode 100644
index 00000000000..cbfbb361bfe
--- /dev/null
+++ b/course/tests/behat/course_summary_format.feature
@@ -0,0 +1,33 @@
+@core @core_course
+Feature: Summary text format should be preserved on edit and set by preferred editor format on creation
+  In order to edit the course summary
+  As a course creator
+  The format specified for the summary must be honored
+
+  @javascript
+  Scenario: Preferred editor format should be used for summary field on course creation
+    Given the following "user preferences" exist:
+      | user  | preference  | value     |
+      | admin | htmleditor  | textarea  |
+    And I log in as "admin"
+    And I go to the courses management page
+    When I click on "Create new course" "link"
+    Then I should see "Moodle auto-format" in the "#menusummary_editorformat > option[selected]" "css_element"
+
+  @javascript
+  Scenario: Summary format must be preserved on course edit
+    Given the following "user preferences" exist:
+      | user  | preference  | value     |
+      | admin | htmleditor  | textarea  |
+    And I log in as "admin"
+    And I go to the courses management page
+    And I click on "Create new course" "link"
+    And I set the following fields to these values:
+      | Course full name   | C1                          |
+      | Course short name  | C1                          |
+      | Course summary     | Course description          |
+    # 4 is assumed to be Markdown format.
+    And I set the field with xpath "//select[@name='summary_editor[format]']" to "4"
+    And I press "Save and display"
+    When I click on "Settings" "link"
+    Then I should see "Markdown format" in the "#menusummary_editorformat > option[selected]" "css_element"