Skip to content

Commit

Permalink
MDL-72496 question tests: fix assertTag if tag not found
Browse files Browse the repository at this point in the history
* The basic_test::assertTag method will issue a warning as $tag is not
found, failing the PHP Unit test that uses this method.
* Add tests to check that assertTag is working
  • Loading branch information
laurentdavid committed Sep 30, 2021
1 parent 1a63bcc commit d0958a8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/phpunit/classes/base_testcase.php
Expand Up @@ -55,7 +55,7 @@ abstract class base_testcase extends PHPUnit\Framework\TestCase {
public static function assertTag($matcher, $actual, $message = '', $ishtml = true) {
$dom = (new PHPUnit\Util\Xml\Loader)->load($actual, $ishtml);
$tags = self::findNodes($dom, $matcher, $ishtml);
$matched = count($tags) > 0 && $tags[0] instanceof DOMNode;
$matched = (is_array($tags) && count($tags) > 0) && $tags[0] instanceof DOMNode;
self::assertTrue($matched, $message);
}

Expand Down
10 changes: 10 additions & 0 deletions lib/phpunit/tests/basic_test.php
Expand Up @@ -145,6 +145,16 @@ public function test_setup_assert() {
$this->testassertexecuted = false;
}

/**
* Test assert Tag
*/
public function test_assert_tag() {
// This should succeed.
self::assertTag(['id' => 'testid'], "<div><span id='testid'></span></div>");
$this->expectException(\PHPUnit\Framework\ExpectationFailedException::class);
self::assertTag(['id' => 'testid'], "<div><div>");
}

// Uncomment following tests to see logging of unexpected changes in global state and database.
/*
public function test_db_modification() {
Expand Down

0 comments on commit d0958a8

Please sign in to comment.