From 5d5cd02239ef72a7ddc4ac61d3903d1ecc684d12 Mon Sep 17 00:00:00 2001 From: Laurent David Date: Thu, 23 Sep 2021 17:39:34 +0200 Subject: [PATCH] MDL-72496 question tests: fix assertTag if tag not found * 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 --- lib/phpunit/classes/base_testcase.php | 2 +- lib/phpunit/tests/basic_test.php | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/phpunit/classes/base_testcase.php b/lib/phpunit/classes/base_testcase.php index f24c5624e2f..3fb2b9ac6c3 100644 --- a/lib/phpunit/classes/base_testcase.php +++ b/lib/phpunit/classes/base_testcase.php @@ -55,7 +55,7 @@ abstract class base_testcase extends PHPUnit\Framework\TestCase { public static function assertTag($matcher, $actual, $message = '', $ishtml = true) { $dom = PHPUnit\Util\XML::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); } diff --git a/lib/phpunit/tests/basic_test.php b/lib/phpunit/tests/basic_test.php index df989ee6a77..e09d8784b72 100644 --- a/lib/phpunit/tests/basic_test.php +++ b/lib/phpunit/tests/basic_test.php @@ -146,6 +146,16 @@ STRING; $this->testassertexecuted = false; } + /** + * Test assert Tag + */ + public function test_assert_tag() { + // This should succeed. + self::assertTag(['id' => 'testid'], "
"); + $this->expectException(\PHPUnit\Framework\ExpectationFailedException::class); + self::assertTag(['id' => 'testid'], "
"); + } + // Uncomment following tests to see logging of unexpected changes in global state and database. /* public function test_db_modification() {