Mwjames has uploaded a new change for review.
https://gerrit.wikimedia.org/r/77504
Change subject: \SMW\ContentParser use ContentHandler where available
......................................................................
\SMW\ContentParser use ContentHandler where available
Change-Id: I64533d46a46d0f62d1f58ec056410c4403afea57
---
M includes/ContentParser.php
M tests/phpunit/includes/ContentParserTest.php
2 files changed, 148 insertions(+), 22 deletions(-)
git pull
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/SemanticMediaWiki
refs/changes/04/77504/1
diff --git a/includes/ContentParser.php b/includes/ContentParser.php
index 936c278..8d00c0b 100644
--- a/includes/ContentParser.php
+++ b/includes/ContentParser.php
@@ -75,7 +75,7 @@
*
* @return Title
*/
- public function getTitel() {
+ public function getTitle() {
return $this->title;
}
@@ -115,10 +115,11 @@
$this->parseText();
} else {
- // if ( class_exists( 'ContentHandler') ) {
- // $this->fetchFromContentHandler(); // Add unit
test
- // } else {}
- $this->fetchFromParser();
+ if ( class_exists( 'ContentHandler') ) {
+ $this->fetchFromContent();
+ } else {
+ $this->fetchFromParser();
+ }
}
return $this;
@@ -132,7 +133,7 @@
protected function parseText() {
Profiler::In( __METHOD__ );
- $this->parserOutput = $this->getParser()->parse( $this->text,
$this->getTitel(), $this->getParserOptions() );
+ $this->parserOutput = $this->getParser()->parse( $this->text,
$this->getTitle(), $this->getParserOptions() );
Profiler::Out( __METHOD__ );
}
@@ -142,18 +143,22 @@
*
* @since 1.9
*/
- protected function fetchFromContentHandler() {
+ protected function fetchFromContent() {
Profiler::In( __METHOD__ );
- $content = $this->getRevision()->getContent( Revision::RAW );
+ if ( $this->getRevision() !== null ) {
+ $content = $this->getRevision()->getContent(
Revision::RAW );
- if ( !$content ) {
- // if there is no content, pretend the content is empty
- $content =
$this->getRevision()->getContentHandler()->makeEmptyContent();
+ if ( !$content ) {
+ // if there is no content, pretend the content
is empty
+ $content =
$this->getRevision()->getContentHandler()->makeEmptyContent();
+ }
+
+ // Revision ID must be passed to the parser output to
get revision variables correct
+ $this->parserOutput = $content->getParserOutput(
$this->getTitle(), $this->getRevision()->getId(), null, true );
+ } else {
+ $this->errors = array( __METHOD__ . " No revision
available for {$this->getTitle()->getPrefixedDBkey()}" );
}
-
- // Revision ID must be passed to the parser output to get
revision variables correct
- $this->parserOutput = $content->getParserOutput(
$this->getTitle(), $this->getRevision()->getId(), null, false );
Profiler::Out( __METHOD__ );
}
@@ -169,14 +174,14 @@
if ( $this->getRevision() !== null ) {
$this->parserOutput = $this->getParser()->parse(
$this->getRevision()->getText(),
- $this->getTitel(),
+ $this->getTitle(),
$this->getParserOptions(),
true,
true,
$this->getRevision()->getID()
);
} else {
- $this->errors = array( __METHOD__ . " No revision
available for {$this->getTitel()->getPrefixedDBkey()}" );
+ $this->errors = array( __METHOD__ . " No revision
available for {$this->getTitle()->getPrefixedDBkey()}" );
}
Profiler::Out( __METHOD__ );
@@ -215,9 +220,9 @@
if ( $this->revision === null ) {
if ( class_exists( 'ContentHandler') ) {
- $this->revision = Revision::newFromTitle(
$this->getTitel(), false, Revision::READ_NORMAL );
+ $this->revision = Revision::newFromTitle(
$this->getTitle(), false, Revision::READ_NORMAL );
} else{
- $this->revision = Revision::newFromTitle(
$this->getTitel() );
+ $this->revision = Revision::newFromTitle(
$this->getTitle() );
}
}
diff --git a/tests/phpunit/includes/ContentParserTest.php
b/tests/phpunit/includes/ContentParserTest.php
index b557bbf..7ec05e9 100644
--- a/tests/phpunit/includes/ContentParserTest.php
+++ b/tests/phpunit/includes/ContentParserTest.php
@@ -3,6 +3,7 @@
namespace SMW\Test;
use SMW\ContentParser;
+use TextContent;
use Title;
@@ -75,12 +76,12 @@
}
/**
- * @test ContentParser::parse
+ * @test ContentParser::fetchFromParser
* @dataProvider titleRevisionDataProvider
*
* @since 1.9
*/
- public function testParseFromRevision( $setup, $expected ) {
+ public function testFetchFromParser( $setup, $expected ) {
$reflector = $this->newReflector();
$instance = $this->getInstance( $setup['title'] );
@@ -89,7 +90,11 @@
$revision->setAccessible( true );
$revision->setValue( $instance, $setup['revision'] );
- $instance->parse();
+ $fetchFromParser = $reflector->getMethod( 'fetchFromParser' );
+ $fetchFromParser->setAccessible( true );
+ $fetchFromParser->invoke( $instance );
+
+ $this->assertEquals( $setup['title'], $instance->getTitle() );
if ( $expected['error'] ) {
$this->assertInternalType( 'array',
$instance->getErrors() );
@@ -101,8 +106,43 @@
}
/**
- * Provides title and wikiPage samples
+ * @test ContentParser::fetchFromContent
+ * @dataProvider contentDataProvider
*
+ * @since 1.9
+ */
+ public function testFetchFromContent( $setup, $expected ) {
+
+ if ( class_exists( 'ContentHandler') ) {
+
+ $reflector = $this->newReflector();
+ $instance = $this->getInstance( $setup['title'] );
+
+ $revision = $reflector->getProperty( 'revision' );
+ $revision->setAccessible( true );
+ $revision->setValue( $instance, $setup['revision'] );
+
+ $fetchFromContent = $reflector->getMethod(
'fetchFromContent' );
+ $fetchFromContent->setAccessible( true );
+ $fetchFromContent->invoke( $instance );
+
+ $this->assertEquals( $setup['title'],
$instance->getTitle() );
+
+ if ( $expected['error'] ) {
+ $this->assertInternalType( 'array',
$instance->getErrors() );
+ } else {
+ $this->assertInstanceOf( 'ParserOutput',
$instance->getOutput() );
+ $this->assertEquals( $expected['text'],
$instance->getOutput()->getText() );
+ }
+
+ } else {
+ $this->markTestSkipped(
+ 'Skipping test due to a missing class (probably
MW 1.19 or lower).'
+ );
+ }
+ }
+
+ /**
* @return array
*/
public function titleRevisionDataProvider() {
@@ -158,4 +198,85 @@
return $provider;
}
+ /**
+ * @return array
+ */
+ public function contentDataProvider() {
+
+ $provider = array();
+
+ $text = $this->newRandomString( 20, __METHOD__ );
+ //$expected ='<p>' . $text . "\n" . '</p>';
+
+ // #0 Title does not exists
+ $title = $this->newMockObject( array(
+ 'getDBkey' => 'Lila',
+ 'exists' => false,
+ 'getText' => null,
+ 'getPageLanguage' => $this->getLanguage()
+ ) )->getMockTitle();
+
+ $provider[] = array(
+ array(
+ 'title' => $title,
+ 'revision' => null,
+ ),
+ array(
+ 'error' => true,
+ 'text' => ''
+ )
+ );
+
+ // #1 Valid revision
+ $title = $this->newMockObject( array(
+ 'getDBkey' => 'Lula',
+ 'exists' => true,
+ 'getPageLanguage' => $this->getLanguage()
+ ) )->getMockTitle();
+
+ $revision = $this->newMockObject( array(
+ 'getId' => 9001,
+ 'getUser' => 'Lala',
+ 'getContent' => new TextContent( $text )
+ ) )->getMockRevision();
+
+ $provider[] = array(
+ array(
+ 'title' => $title,
+ 'revision' => $revision,
+ ),
+ array(
+ 'error' => false,
+ 'text' => $text
+ )
+ );
+
+ // #1 Empty content
+ $title = $this->newMockObject( array(
+ 'getDBkey' => 'Lula',
+ 'exists' => true,
+ 'getPageLanguage' => $this->getLanguage(),
+ 'getContentModel' => CONTENT_MODEL_WIKITEXT
+ ) )->getMockTitle();
+
+ $revision = $this->newMockObject( array(
+ 'getId' => 9001,
+ 'getUser' => 'Lala',
+ 'getContent' => new TextContent( '' )
+ ) )->getMockRevision();
+
+ $provider[] = array(
+ array(
+ 'title' => $title,
+ 'revision' => $revision,
+ ),
+ array(
+ 'error' => false,
+ 'text' => ''
+ )
+ );
+
+ return $provider;
+ }
+
}
--
To view, visit https://gerrit.wikimedia.org/r/77504
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I64533d46a46d0f62d1f58ec056410c4403afea57
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/SemanticMediaWiki
Gerrit-Branch: master
Gerrit-Owner: Mwjames <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits