From 8fc355ecea5d22d9ec005208308634aa3c263426 Mon Sep 17 00:00:00 2001 From: Inez Korczynski Date: Tue, 20 Oct 2015 22:42:36 +0000 Subject: [PATCH] VE-1982: Basic unit test coverage for backward compatibility added to --- services/Parser/Nodes/NodeImage.php | 110 +++++++++++++++------------- tests/nodes/NodeImageTest.php | 57 ++++++++++++++ 2 files changed, 115 insertions(+), 52 deletions(-) diff --git a/services/Parser/Nodes/NodeImage.php b/services/Parser/Nodes/NodeImage.php index 3b1c502..8de1184 100644 --- a/services/Parser/Nodes/NodeImage.php +++ b/services/Parser/Nodes/NodeImage.php @@ -10,6 +10,60 @@ class NodeImage extends Node { const CAPTION_TAG_NAME = 'caption'; const MEDIA_TYPE_VIDEO = 'VIDEO'; + public static function getMarkers( $value, $ext ) { + if ( preg_match_all('/\x7fUNIQ[A-Z0-9]*-' . $ext . '-[0-9]{8}-QINU\x7f/is', $value, $out ) ) { + return $out[0]; + } else { + return []; + } + } + + public static function getGalleryData( $html ) { + global $wgArticleAsJson; + $data = array(); + if ( $wgArticleAsJson ) { + if ( preg_match( '/data-ref=\'([^\']+)\'/', $html, $out ) ) { + $media = \ArticleAsJson::$media[$out[1]]; + for( $i = 0; $i < count( $media ); $i++ ) { + $data[] = array( 'label' => strip_tags( $media[$i]['caption'] ), 'title' => $media[$i]['title'] ); + } + } + } else { + if ( preg_match( '#\sdata-model="([^"]+)"#', $html, $galleryOut ) ) { + $model = json_decode( htmlspecialchars_decode( $galleryOut[1] ), true ); + for( $i = 0; $i < count( $model ); $i++ ) { + $data[] = array( 'label' => strip_tags( $model[$i][ 'caption' ] ), 'title' => $model[$i][ 'title' ] ); + } + } + if ( preg_match_all('#data-image-key="([^"]+)".*?\s

(.*?)<\/h2>#is', $html, $galleryOut ) ) { + for( $i = 0; $i < count( $galleryOut[0] ); $i++ ) { + $data[] = array( 'label' => $galleryOut[2][$i], 'title' => $galleryOut[1][$i] ); + } + } + } + return $data; + } + + public static function getTabberData( $html ) { + $data = array(); + $doc = new \DOMDocument(); + $doc->loadHTML( $html ); + $sxml = simplexml_import_dom( $doc ); + $divs = $sxml->xpath( '//div[@class=\'tabbertab\']' ); + foreach ( $divs as $div ) { + if ( $wgArticleAsJson ) { + if ( preg_match( '/data-ref="([^"]+)"/', $div->p->asXML(), $out ) ) { + $data[] = array( 'label' => (string) $div['title'], 'title' => \ArticleAsJson::$media[$out[1]]['title'] ); + } + } else { + if ( preg_match( '/data-image-key="([^"]+)"/', $div->p->asXML(), $out ) ) { + $data[] = array( 'label' => (string) $div['title'], 'title' => $out[1] ); + } + } + } + return $data; + } + public function getData() { if ( !isset( $this->data ) ) { $this->data = array(); @@ -54,31 +108,11 @@ class NodeImage extends Node { } private function getGalleryItems( $value ) { - global $wgArticleAsJson; $galleryItems = array(); - $galleryMarkers = $this->getMarkers( $value, 'GALLERY' ); + $galleryMarkers = self::getMarkers( $value, 'GALLERY' ); for ( $i = 0; $i < count ( $galleryMarkers ); $i++ ) { $galleryHtml = $this->getExternalParser()->parseRecursive( $galleryMarkers[$i] ); - if ( $wgArticleAsJson ) { - if ( preg_match( '/data-ref=\'([^\']+)\'/', $galleryHtml, $out ) ) { - $media = \ArticleAsJson::$media[$out[1]]; - for( $j = 0; $j < count( $media ); $j++ ) { - $galleryItems[] = array( 'label' => strip_tags( $media[$j]['caption'] ), 'title' => $media[$j]['title'] ); - } - } - } else { - if ( preg_match( '#\sdata-model="([^"]+)"#', $galleryHtml, $galleryOut ) ) { - $model = json_decode( htmlspecialchars_decode( $galleryOut[1] ), true ); - $galleryItems = array_map( function( $modelItem ) { - return array( 'label' => strip_tags( $modelItem[ 'caption' ] ), 'title' => $modelItem['title'] ); - }, $model ); - } - if ( preg_match_all('#data-image-key="([^"]+)".*?\s

(.*?)<\/h2>#is', $html, $galleryOut ) ) { - for( $j = 0; $j < count( $galleryOut[0] ); $j++ ) { - $galleryItems[] = array( 'label' => $galleryOut[2][$j], 'title' => $galleryOut[1][$j] ); - } - } - } + $galleryItems = array_merge( $galleryItems, self::getGalleryData( $galleryHtml ) ); } return $galleryItems; } @@ -86,42 +120,14 @@ class NodeImage extends Node { private function getTabberItems( $value ) { global $wgArticleAsJson; $tabberItems = array(); - $tabberMarkers = $this->getMarkers( $value, 'TABBER' ); + $tabberMarkers = self::getMarkers( $value, 'TABBER' ); for ( $i = 0; $i < count ( $tabberMarkers ); $i++ ) { $tabberHtml = $this->getExternalParser()->parseRecursive( $tabberMarkers[$i] ); - $tabberItems = array_merge( $tabberItems, $this->getTabberData( $tabberHtml ) ); + $tabberItems = array_merge( $tabberItems, self::getTabberData( $tabberHtml ) ); } return $tabberItems; } - private function getMarkers( $value, $ext ) { - if ( preg_match_all('/\x7fUNIQ[A-Z0-9]*-' . $ext . '-[0-9]{8}-QINU\x7f/is', $value, $out ) ) { - return $out[0]; - } else { - return []; - } - } - - private function getTabberData( $html ) { - $data = array(); - $doc = new \DOMDocument(); - $doc->loadHTML( $html ); - $sxml = simplexml_import_dom( $doc ); - $divs = $sxml->xpath( '//div[@class=\'tabbertab\']' ); - foreach ( $divs as $div ) { - if ( $wgArticleAsJson ) { - if ( preg_match( '/data-ref="([^"]+)"/', $div->p->asXML(), $out ) ) { - $data[] = array( 'label' => (string) $div['title'], 'title' => \ArticleAsJson::$media[$out[1]]['title'] ); - } - } else { - if ( preg_match( '/data-image-key="([^"]+)"/', $div->p->asXML(), $out ) ) { - $data[] = array( 'label' => (string) $div['title'], 'title' => $out[1] ); - } - } - } - return $data; - } - private function getImageData( $title, $alt, $caption ) { $titleObj = $this->getImageAsTitleObject( $title ); $fileObj = $this->getFilefromTitle( $titleObj ); diff --git a/tests/nodes/NodeImageTest.php b/tests/nodes/NodeImageTest.php index bfea9c2..a479062 100644 --- a/tests/nodes/NodeImageTest.php +++ b/tests/nodes/NodeImageTest.php @@ -6,6 +6,63 @@ class NodeImageTest extends WikiaBaseTest { parent::setUp(); } + /** + * @covers NodeImage::getGalleryData + */ + public function testGalleryData() { + $input = '
'; + $expected = array( + array( + 'label' => '_caption_', + 'title' => '_title_', + ) + ); + $this->assertEquals( $expected, Wikia\PortableInfobox\Parser\Nodes\NodeImage::getGalleryData( $input ) ); + } + + /** + * @covers NodeImage::getTabberData + */ + public function testTabberData() { + $input = '

'; + $expected = array( + array( + 'label' => '_title_', + 'title' => '_data-image-key_', + ) + ); + $this->assertEquals( $expected, Wikia\PortableInfobox\Parser\Nodes\NodeImage::getTabberData( $input ) ); + } + + /** + * @covers NodeImage::getMarkers + * @dataProvider markersProvider + * + * @param $markup + * @param $params + * @param $expected + */ + public function testMarkers( $ext, $value, $expected ) { + $this->assertEquals( $expected, Wikia\PortableInfobox\Parser\Nodes\NodeImage::getMarkers( $value, $ext ) ); + } + + public function markersProvider() { + return [ + [ + 'TABBER', + "
\x7fUNIQ123456789-tAbBeR-12345678-QINU\x7f
", + [ "\x7fUNIQ123456789-tAbBeR-12345678-QINU\x7f" ] + ], + [ + 'GALLERY', + "\x7fUNIQ123456789-tAbBeR-12345678-QINU\x7f
\x7fUNIQabcd-gAlLeRy-12345678-QINU\x7f
\x7fUNIQabcd-gAlLeRy-87654321-QINU\x7f", + [ "\x7fUNIQabcd-gAlLeRy-12345678-QINU\x7f", "\x7fUNIQabcd-gAlLeRy-87654321-QINU\x7f" ] + ] + ]; + } + + + /** * @covers NodeImage::getData * @dataProvider dataProvider