Skip to content

Commit 029ab42

Browse files
committed
iss1757 - Fix Behat test in Moodle 5+
1 parent 37c2793 commit 029ab42

2 files changed

Lines changed: 40 additions & 6 deletions

File tree

tests/behat/behat_qtype_stack.php

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
require_once(__DIR__ . '/../../../../../lib/behat/behat_base.php');
2929

3030
use Moodle\BehatExtension\Exception\SkippedException;
31-
use PHPUnit\Framework\Assert;
3231

3332
/**
3433
* Steps definitions related with the question bank management.
@@ -141,7 +140,9 @@ public function i_check_input_value($name, $value) {
141140
})();
142141
EOF;
143142
$formvalue = $this->evaluate_script($js);
144-
Assert::assertEquals($value, $formvalue);
143+
if ($formvalue !== $value) {
144+
throw new \Exception("Expected input value '$value' but got '$formvalue'.");
145+
}
145146
}
146147

147148
/**
@@ -164,7 +165,9 @@ public function i_check_element_value($id, $value) {
164165
$formvalue = $this->evaluate_script($js);
165166
$this->getSession()->switchToWindow();
166167
$formvalue = str_replace(["\r\n", "\r", "\n"], '\n', $formvalue);
167-
Assert::assertEquals($value, $formvalue);
168+
if ($formvalue !== $value) {
169+
throw new \Exception("Expected element value '$value' but got '$formvalue'.");
170+
}
168171
}
169172

170173
/**
@@ -187,7 +190,35 @@ public function i_check_element_value_contains($id, $value) {
187190
$formvalue = $this->evaluate_script($js);
188191
$this->getSession()->switchToWindow();
189192
$formvalue = str_replace(["\r\n", "\r", "\n"], '\n', $formvalue);
190-
Assert::assertStringContainsString($value, $formvalue);
193+
if (strpos($formvalue, $value) === false) {
194+
throw new \Exception("Expected element value to contain '$value' but got '$formvalue'.");
195+
}
196+
}
197+
198+
/**
199+
* Check an iframe element value contains one of two substrings.
200+
*
201+
* @param string $id id of element
202+
* @param string $value1 first expected substring
203+
* @param string $value2 second expected substring
204+
*
205+
* @Given /^I check the value of iframe element "(?P<id>[^"]*)" contains either '(?P<value1>[^']*)' or '(?P<value2>[^']*)'$/
206+
*/
207+
public function i_check_element_value_contains_either($id, $value1, $value2) {
208+
$generalcontext = behat_context_helper::get('behat_general');
209+
$generalcontext->switch_to_iframe('stack-iframe-1');
210+
$js = <<<EOF
211+
return (function() {
212+
let el = document.getElementById("$id");
213+
return el ? el.textContent : null;
214+
})();
215+
EOF;
216+
$formvalue = $this->evaluate_script($js);
217+
$this->getSession()->switchToWindow();
218+
$formvalue = str_replace(["\r\n", "\r", "\n"], '\n', $formvalue);
219+
if (strpos($formvalue, $value1) === false && strpos($formvalue, $value2) === false) {
220+
throw new \Exception("Expected element value to contain either '$value1' or '$value2' but got '$formvalue'.");
221+
}
191222
}
192223

193224
/**
@@ -275,7 +306,9 @@ public function i_check_images_are_loadable($number, $imgclass) {
275306
foreach ($urls as $url) {
276307
$this->execute('behat_general::i_visit', [$url]);
277308
}
278-
Assert::assertEquals(true, count($urls) === (int) $number);
309+
if (count($urls) !== (int) $number) {
310+
throw new \Exception("Expected $number images but found " . count($urls) . ".");
311+
}
279312
}
280313

281314
/**

tests/behat/freetext.feature

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,5 @@ Feature: Test input of correct answers on freetext inputs.
4040
And I check the input "ans3" is 'f(x) = 4sqrt(2x^2+1)+1'
4141
And I check the input "ans4" is 'f(0) = 5 => c = 1'
4242
And I check the input "ans5" is '{"matches":["f(x) = 4sqrt(2x^2+1)+c","f(x) = 4sqrt(2x^2+1)+1"]}'
43-
And I check the value of iframe element "asciiContainerRow" contains '\begin{align*}\n& & \displaystyle{f{{\left({x}\right)}}}&={4}\sqrt{{{2}{x}^{{2}}+{1}}}+{c}\\\n& & \displaystyle{f{{\left({0}\right)}}}&={5}\Rightarrow{c}={1}\\\n& & \displaystyle{f{{\left({x}\right)}}}&={4}\sqrt{{{2}{x}^{{2}}+{1}}}+{1}\\\n\end{align*}\n'
43+
# MathJax 3 will have rendered, MathJax 2 probably won't.
44+
And I check the value of iframe element "asciiContainerRow" contains either '\begin{align*}\n& & \displaystyle{f{{\left({x}\right)}}}&={4}\sqrt{{{2}{x}^{{2}}+{1}}}+{c}\\\n& & \displaystyle{f{{\left({0}\right)}}}&={5}\Rightarrow{c}={1}\\\n& & \displaystyle{f{{\left({x}\right)}}}&={4}\sqrt{{{2}{x}^{{2}}+{1}}}+{1}\\\n\end{align*}\n' or '𝑓⁡(𝑥)=4⁢√2⁢𝑥2+1+1'

0 commit comments

Comments
 (0)