VE-1982: Basic unit test coverage for backward compatibility added to <image>

This commit is contained in:
Inez Korczynski 2015-10-20 22:42:36 +00:00
parent b93c9607a2
commit 8fc355ecea
2 changed files with 115 additions and 52 deletions

View file

@ -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>(.*?)<\/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>(.*?)<\/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 );

View file

@ -6,6 +6,63 @@ class NodeImageTest extends WikiaBaseTest {
parent::setUp();
}
/**
* @covers NodeImage::getGalleryData
*/
public function testGalleryData() {
$input = '<div data-model="[{&quot;caption&quot;:&quot;_caption_&quot;,&quot;title&quot;:&quot;_title_&quot;}]"></div>';
$expected = array(
array(
'label' => '_caption_',
'title' => '_title_',
)
);
$this->assertEquals( $expected, Wikia\PortableInfobox\Parser\Nodes\NodeImage::getGalleryData( $input ) );
}
/**
* @covers NodeImage::getTabberData
*/
public function testTabberData() {
$input = '<div class="tabber"><div class="tabbertab" title="_title_"><p><a><img data-image-key="_data-image-key_"></a></p></div></div>';
$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',
"<div>\x7fUNIQ123456789-tAbBeR-12345678-QINU\x7f</div>",
[ "\x7fUNIQ123456789-tAbBeR-12345678-QINU\x7f" ]
],
[
'GALLERY',
"\x7fUNIQ123456789-tAbBeR-12345678-QINU\x7f<center>\x7fUNIQabcd-gAlLeRy-12345678-QINU\x7f</center>\x7fUNIQabcd-gAlLeRy-87654321-QINU\x7f",
[ "\x7fUNIQabcd-gAlLeRy-12345678-QINU\x7f", "\x7fUNIQabcd-gAlLeRy-87654321-QINU\x7f" ]
]
];
}
/**
* @covers NodeImage::getData
* @dataProvider dataProvider