MDL-68873 core_h5p: fix editor H5P file update
authorFerran Recio <ferran@moodle.com>
Wed, 3 Jun 2020 11:19:07 +0000 (13:19 +0200)
committerJake Dallimore <jake@moodle.com>
Mon, 8 Jun 2020 02:44:23 +0000 (10:44 +0800)
h5p/classes/editor.php
repository/contentbank/tests/behat/file_update.feature [new file with mode: 0644]

index 606cc14..a4b4af7 100644 (file)
@@ -306,15 +306,16 @@ class editor {
         if ($file) {
             $fields['contenthash'] = $file->get_contenthash();
 
-            // Delete old file if any.
-            if (!empty($this->oldfile)) {
-                $this->oldfile->delete();
-            }
-            // Create new file.
+            // Create or update H5P file.
             if (empty($this->filearea['filename'])) {
                 $this->filearea['filename'] = $contentarray['slug'] . '.h5p';
             }
-            $newfile = $fs->create_file_from_storedfile($this->filearea, $file);
+            if (!empty($this->oldfile)) {
+                $this->oldfile->replace_file_with($file);
+                $newfile = $this->oldfile;
+            } else {
+                $newfile = $fs->create_file_from_storedfile($this->filearea, $file);
+            }
             if (empty($this->oldcontent)) {
                 $pathnamehash = $newfile->get_pathnamehash();
             } else {
diff --git a/repository/contentbank/tests/behat/file_update.feature b/repository/contentbank/tests/behat/file_update.feature
new file mode 100644 (file)
index 0000000..96cb40a
--- /dev/null
@@ -0,0 +1,103 @@
+@repository @repository_contentbank @javascript @core_h5p
+Feature: Updating a file in the content bank after using in a course
+  In order to use file alias
+  As a user
+  Updated files must update references when is an alias
+
+  Background:
+    Given the following "categories" exist:
+      | name      | category | idnumber |
+      | Category1 | 0        | CAT1     |
+    And the following "courses" exist:
+      | fullname | shortname | category |
+      | Course1  | C1        | CAT1     |
+    And the following "contentbank content" exist:
+      | contextlevel | reference | contenttype     | user  | contentname | filepath                                  |
+      | Course       | C1        | contenttype_h5p | admin | package.h5p | /h5p/tests/fixtures/guess-the-answer.h5p  |
+    And the following "activities" exist:
+      | activity | name       | intro      | introformat | course | content  | contentformat | idnumber |
+      | page     | PageName1  | PageDesc1  | 1           | C1     | H5Ptest  | 1             | 1        |
+    And I log in as "admin"
+
+  Scenario: Referenced files updates alias as well
+    Given I am on "Course1" course homepage
+    And I follow "PageName1"
+    And I navigate to "Edit settings" in current page administration
+    And I click on "Insert H5P" "button" in the "#fitem_id_page" "css_element"
+    And I click on "Browse repositories..." "button" in the "Insert H5P" "dialogue"
+    And I select "Content bank" repository in file picker
+    And I click on "package.h5p" "file" in repository content area
+    And I click on "Create an alias/shortcut to the file" "radio"
+    And I click on "Select this file" "button"
+    And I click on "Insert H5P" "button" in the "Insert H5P" "dialogue"
+    And I wait until the page is ready
+    And I click on "Save and display" "button"
+    And I switch to "h5p-iframe" class iframe
+    And I switch to "h5p-iframe" class iframe
+    And I should see "Press here to reveal answer"
+    And I switch to the main frame
+    # Now edit the content in the content bank.
+    When I am on "Course1" course homepage with editing mode on
+    And I add the "Navigation" block if not present
+    And I expand "Site pages" node
+    And I click on "Content bank" "link"
+    And I click on "package.h5p" "link"
+    And I click on "Edit" "link"
+    And I wait until the page is ready
+    And I switch to "h5p-editor-iframe" class iframe
+    And I set the field "Title" to "Required title"
+    And I set the field "Descriptive solution label" to "This is a new text"
+    And I switch to the main frame
+    And I click on "Save" "button"
+    And I switch to "h5p-player" class iframe
+    And I switch to "h5p-iframe" class iframe
+    And I should see "This is a new text"
+    And I switch to the main frame
+    # Check the course page is updated.
+    Then I am on "Course1" course homepage
+    And I follow "PageName1"
+    And I switch to "h5p-iframe" class iframe
+    And I switch to "h5p-iframe" class iframe
+    And I should see "This is a new text"
+    And I switch to the main frame
+
+  Scenario: Copied files should not be updated if the original is edited
+    Given I am on "Course1" course homepage
+    And I follow "PageName1"
+    And I navigate to "Edit settings" in current page administration
+    And I click on "Insert H5P" "button" in the "#fitem_id_page" "css_element"
+    And I click on "Browse repositories..." "button" in the "Insert H5P" "dialogue"
+    And I select "Content bank" repository in file picker
+    And I click on "package.h5p" "file" in repository content area
+    And I click on "Select this file" "button"
+    And I click on "Insert H5P" "button" in the "Insert H5P" "dialogue"
+    And I wait until the page is ready
+    And I click on "Save and display" "button"
+    And I switch to "h5p-iframe" class iframe
+    And I switch to "h5p-iframe" class iframe
+    And I should see "Press here to reveal answer"
+    And I switch to the main frame
+    # Now edit the content in the content bank.
+    When I am on "Course1" course homepage with editing mode on
+    And I add the "Navigation" block if not present
+    And I expand "Site pages" node
+    And I click on "Content bank" "link"
+    And I click on "package.h5p" "link"
+    And I click on "Edit" "link"
+    And I wait until the page is ready
+    And I switch to "h5p-editor-iframe" class iframe
+    And I set the field "Title" to "Required title"
+    And I set the field "Descriptive solution label" to "This is a new text"
+    And I switch to the main frame
+    And I click on "Save" "button"
+    And I switch to "h5p-player" class iframe
+    And I switch to "h5p-iframe" class iframe
+    And I should see "This is a new text"
+    And I switch to the main frame
+    # Check the course page is not updated.
+    Then I am on "Course1" course homepage
+    And I follow "PageName1"
+    And I switch to "h5p-iframe" class iframe
+    And I switch to "h5p-iframe" class iframe
+    And I should see "Press here to reveal answer"
+    And I switch to the main frame