Ricordisamoa has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/270278

Change subject: Add DOMTestTrait to replace deprecated assertTag()
......................................................................

Add DOMTestTrait to replace deprecated assertTag()

Bug: T69122
Change-Id: Ie83f6ed9c4f8dc68e146a9985f11ae78507fba87
---
A tests/phpunit/DOMTestTrait.php
1 file changed, 76 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/78/270278/1

diff --git a/tests/phpunit/DOMTestTrait.php b/tests/phpunit/DOMTestTrait.php
new file mode 100644
index 0000000..347d010
--- /dev/null
+++ b/tests/phpunit/DOMTestTrait.php
@@ -0,0 +1,76 @@
+<?php
+/**
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ */
+
+/**
+ * Trait for DOM testing.
+ *
+ * @since 1.27
+ */
+trait DOMTestTrait {
+
+       private static function assertTagSimple( array $matcher, $actual, 
$message = '', $isHtml = true ) {
+               $this->assertEmpty( array_diff(
+                       array_keys( $matcher ),
+                       array( 'tag', 'attributes', 'content' )
+               ) );
+               $doc = new DOMDocument();
+               if ( $isHtml ) {
+                       $doc->loadHTML( $actual );
+               } else {
+                       $doc->loadXML( $actual );
+               }
+               $found = false;
+               $elements = $doc->getElementsByTagName( $matcher['tag'] );
+               foreach ( $elements as $node ) {
+                       $valid = true;
+                       if ( isset( $matcher['attributes'] ) ) {
+                               foreach ( $matcher['attributes'] as $name => 
$value ) {
+                                       if ( $name === 'class' ) {
+                                               $expected = preg_split( 
'/\s+/', $value, null, PREG_SPLIT_NO_EMPTY );
+                                               $got = preg_split( '/\s+/', 
$node->getAttribute( $name ), null, PREG_SPLIT_NO_EMPTY );
+                                               // make sure each class given 
is in the actual node
+                                               if ( array_diff( $expected, 
$got ) ) {
+                                                       $valid = false;
+                                                       break;
+                                               }
+                                       } elseif ( $node->getAttribute( $name ) 
!== $value ) {
+                                               $valid = false;
+                                               break;
+                                       }
+                               }
+                       }
+                       if ( isset( $matcher['content'] ) ) {
+                               if ( $matcher['content'] === '' ) {
+                                       if ( $node->nodeValue !== '' ) {
+                                               $valid = false;
+                                       }
+                               } elseif ( strstr( $node->nodeValue, 
$matcher['content'] ) === false ) {
+                                       $valid = false;
+                               }
+                       }
+                       if ( $valid ) {
+                               $found = true;
+                               break;
+                       }
+               }
+               $this->assertTrue( $found, $message );
+       }
+
+}

-- 
To view, visit https://gerrit.wikimedia.org/r/270278
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ie83f6ed9c4f8dc68e146a9985f11ae78507fba87
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Ricordisamoa <ricordisa...@openmailbox.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to