Skip to content

Commit

Permalink
Merge branch 'MDL-75985-39' of https://github.com/andrewnicols/moodle
Browse files Browse the repository at this point in the history
…into MOODLE_39_STABLE
  • Loading branch information
junpataleta committed Oct 19, 2022
2 parents 651a97e + 1b506c9 commit c3a318e
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 5 deletions.
57 changes: 55 additions & 2 deletions lib/behat/form_field/behat_form_editor.php
Expand Up @@ -93,7 +93,60 @@ public function select_text() {
* @return bool The provided value matches the field value?
*/
public function matches($expectedvalue) {
// A text editor may silently wrap the content in p tags (or not). Neither is an error.
return $this->text_matches($expectedvalue) || $this->text_matches('<p>' . $expectedvalue . '</p>');
// Fetch the actual value to save fetching it multiple times.
$actualvalue = $this->get_value();

if ($this->text_matches($expectedvalue, $actualvalue)) {
// The text is an exact match already.
return true;
}

if ($this->text_matches("<p>{$expectedvalue}</p>", $actualvalue)) {
// A text editor may silently wrap the content in p tags.
return true;
}

// Standardise both the expected value and the actual field value.
// We are likely dealing with HTML content, given this is an editor.
$expectedvalue = $this->standardise_html($expectedvalue);
$actualvalue = $this->standardise_html($actualvalue);

// Note: We don't need to worry about the floats here that we care about in text_matches.
// That condition isn't relevant to the content of an editor.
if ($expectedvalue === $actualvalue) {
return true;
}

return false;
}

/**
* Standardises the HTML content for comparison.
*
* @param string $html The HTML content to standardise
* @return string The standardised HTML content
*/
protected function standardise_html(string $html): string {
$document = new DOMDocument();
$errorstate = libxml_use_internal_errors(true);

// Format the whitespace nicely.
$document->preserveWhiteSpace = false;
$document->formatOutput = true;

// Wrap the content in a DIV element so that it is not parsed weirdly.
// Note: We must remove newlines too because DOMDocument does not do so, despite preserveWhiteSpace being false.
// Unfortunately this is slightly limited in that it will also remove newlines from <pre> content and similar.
$document->loadHTML(str_replace("\n", "", "<div>{$html}</div>"), LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$document->normalizeDocument();
libxml_clear_errors();
libxml_use_internal_errors($errorstate);

// Save the content of the 'div' element, removing the <div> and </div> tags at the start and end.
return trim(substr(
$document->saveHTML($document->getElementsByTagName('div')->item(0)),
5,
-6
));
}
}
8 changes: 6 additions & 2 deletions lib/behat/form_field/behat_form_field.php
Expand Up @@ -244,10 +244,14 @@ protected function get_internal_field_id() {
* Checks if the provided text matches the field value.
*
* @param string $expectedvalue
* @param string|null $actualvalue The actual value. If not specified, this will be fetched from $this->get_value().
* @return bool
*/
protected function text_matches($expectedvalue) {
if (trim($expectedvalue) != trim($this->get_value())) {
protected function text_matches($expectedvalue, ?string $actualvalue = null): bool {
$actualvalue = $actualvalue ?? $this->get_value();

// Non strict string comparison.
if (trim($expectedvalue) != trim($actualvalue)) {
return false;
}
return true;
Expand Down
12 changes: 11 additions & 1 deletion lib/tests/behat/behat_general.php
Expand Up @@ -1713,7 +1713,17 @@ public function i_press_in_the_browser($button) {
* @param string $keys The key, or list of keys, to type
*/
public function i_type(string $keys): void {
behat_base::type_keys($this->getSession(), str_split($keys));
// Certain keys, such as the newline character, must be converted to the appropriate character code.
// Without this, keys will behave differently depending on the browser.
$keylist = array_map(function($key): string {
switch ($key) {
case '\n':
behat_keys::ENTER;
default:
return $key;
}
}, str_split($keys));
behat_base::type_keys($this->getSession(), $keylist);
}

/**
Expand Down

0 comments on commit c3a318e

Please sign in to comment.