diff --git a/filter/displayh5p/filter.php b/filter/displayh5p/filter.php
index 59c06865b763c..9e2bae7d1749a 100644
--- a/filter/displayh5p/filter.php
+++ b/filter/displayh5p/filter.php
@@ -146,8 +146,8 @@ public function filterobject_prepare_replacement_callback($tagbegin, $tagend, $u
$sourceurl .= $urlmodifier;
}
- $h5piframesrc = $sourceurl.
- '" class="h5p-iframe" style="height:230px; width: 100%; border: 0;" allowfullscreen="allowfullscreen">';
+ $h5piframesrc = $sourceurl . '" class="h5p-iframe" name="h5pcontent"' .
+ ' style="height:230px; width: 100%; border: 0;" allowfullscreen="allowfullscreen">';
// We want to request the resizing script only once.
if (self::$loadresizerjs) {
diff --git a/filter/displayh5p/tests/behat/h5p_filter.feature b/filter/displayh5p/tests/behat/h5p_filter.feature
new file mode 100644
index 0000000000000..c57b3a0789f52
--- /dev/null
+++ b/filter/displayh5p/tests/behat/h5p_filter.feature
@@ -0,0 +1,81 @@
+@editor @filter @filter_displayh5p @core_h5p @_file_upload @_switch_iframe
+Feature: Render H5P content using filters
+ To write rich text - I need to render H5P content.
+
+ Background:
+ Given the following "courses" exist:
+ | shortname | fullname |
+ | C1 | Course 1 |
+ And the following "users" exist:
+ | username | firstname | lastname | email |
+ | teacher1 | Teacher | 1 | teacher1@example.com |
+ | student1 | Student | 1 | student1@example.com |
+ And the following "course enrolments" exist:
+ | user | course | role |
+ | teacher1 | C1 | editingteacher |
+ | student1 | C1 | student |
+ And the following "activities" exist:
+ | activity | name | intro | introformat | course | content | contentformat | idnumber |
+ | page | PageName1 | PageDesc1 | 1 | C1 | H5Ptest | 1 | 1 |
+ And the "displayh5p" filter is "on"
+
+ @javascript
+ Scenario: Render an external H5P content URL.
+ Given I log in as "teacher1"
+ And I am on "Course 1" course homepage
+ And I follow "PageName1"
+ And I navigate to "Edit settings" in current page administration
+ And I set the field "Page content" to "
Go for it
https://h5p.org/h5p/embed/576651"
+ When I click on "Save and display" "button"
+ And I wait until the page is ready
+ And I switch to "h5p-iframe" class iframe
+ Then I should see "Lorum ipsum"
+
+ @javascript
+ Scenario: Add an external H5P content URL in a link. Shouldn't be rendered.
+ Given I log in as "teacher1"
+ And I am on "Course 1" course homepage
+ And I follow "PageName1"
+ And I navigate to "Edit settings" in current page administration
+ And I set the field "Page content" to "Go to https://h5p.org/h5p/embed/576651"
+ When I click on "Save and display" "button"
+ And I wait until the page is ready
+ Then ".h5p-iframe" "css_element" should not exist
+
+ @javascript
+ Scenario: Render a server H5P file
+ Given I log in as "teacher1"
+ And I am on "Course 1" course homepage with editing mode on
+ And I add a "File" to section "1"
+ And I set the following fields to these values:
+ | Name | ipsumFile |
+ And I upload "filter/displayh5p/tests/fixtures/ipsums.h5p" file to "Select files" filemanager
+ And I press "Save and return to course"
+ 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 click on "Server files" "link" in the ".fp-repo-area" "css_element"
+ And I click on "ipsumFile (File)" "link"
+ And I click on "ipsums.h5p" "link"
+ 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
+ When I click on "Save and display" "button"
+# Switch to iframe created by filter
+ And I switch to "h5p-iframe" class iframe
+# Switch to iframe created by embed.php page
+ And I switch to "h5p-iframe" class iframe
+ Then I should see "Lorum ipsum"
+ And I switch to the main frame
+ And I log out
+ And I log in as "student1"
+ And I am on "Course 1" course homepage
+ And I follow "PageName1"
+# Switch to iframe created by filter
+ And I switch to "h5p-iframe" class iframe
+# Switch to iframe created by embed.php page
+ And I switch to "h5p-iframe" class iframe
+ And I should not see "you don't have access"
+ And I should see "Lorum ipsum"
+
diff --git a/lib/editor/atto/tests/fixtures/ipsums.h5p b/filter/displayh5p/tests/fixtures/ipsums.h5p
similarity index 100%
rename from lib/editor/atto/tests/fixtures/ipsums.h5p
rename to filter/displayh5p/tests/fixtures/ipsums.h5p
diff --git a/lib/behat/classes/partial_named_selector.php b/lib/behat/classes/partial_named_selector.php
index 86830de043dbd..87c602e85f484 100644
--- a/lib/behat/classes/partial_named_selector.php
+++ b/lib/behat/classes/partial_named_selector.php
@@ -78,6 +78,7 @@ public function __construct() {
'group_message_header' => 'group_message_header',
'group_message' => 'group_message',
'autocomplete' => 'autocomplete',
+ 'iframe' => 'iframe',
);
/**
@@ -121,6 +122,7 @@ public function __construct() {
'autocomplete_selection' => 'autocomplete_selection',
'autocomplete_suggestions' => 'autocomplete_suggestions',
'autocomplete' => 'autocomplete',
+ 'iframe' => 'iframe',
);
/**
@@ -229,6 +231,9 @@ public function __construct() {
XPATH
, 'autocomplete' => << <<(?:[^"]|\\")*)" class iframe$/
+ * @param string $classname
+ */
+ public function switch_to_class_iframe($classname) {
+ // We spin to give time to the iframe to be loaded.
+ // Using extended timeout as we don't know about which
+ // kind of iframe will be loaded.
+ $this->spin(
+ function($context, $classname) {
+ $iframe = $this->find('iframe', $classname);
+ if (!empty($iframe->getAttribute('id'))) {
+ $iframename = $iframe->getAttribute('id');
+ } else {
+ $iframename = $iframe->getAttribute('name');
+ }
+ $context->getSession()->switchToIFrame($iframename);
+
+ // If no exception we are done.
+ return true;
+ },
+ $classname,
+ behat_base::get_extended_timeout()
+ );
+ }
+
/**
* Switches to the main Moodle frame.
*