Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1759 from SemanticMediaWiki/ask-error
Improve Special:Ask error output
- Loading branch information
Showing
5 changed files
with
157 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
namespace SMW\MediaWiki\Specials\Ask; | ||
|
||
use SMWQuery as Query; | ||
use Html; | ||
|
||
/** | ||
* @license GNU GPL v2+ | ||
* @since 2.5 | ||
* | ||
* @author mwjames | ||
*/ | ||
class HtmlContentBuilder { | ||
|
||
/** | ||
* @since 2.5 | ||
* | ||
* @param Query|null $query | ||
* | ||
* @return string | ||
*/ | ||
public function getFormattedErrorString( Query $query = null ) { | ||
|
||
if ( $query === null || !is_array( $query->getErrors() ) || $query->getErrors() === array() ) { | ||
return ''; | ||
} | ||
|
||
if ( count( $query->getErrors() ) == 1 ) { | ||
$error = implode( ' ', $query->getErrors() ); | ||
} else { | ||
|
||
// Filter any duplicate messages | ||
$errors = array(); | ||
foreach ( $query->getErrors() as $key => $value ) { | ||
|
||
if ( is_array( $value ) ) { | ||
$value = implode( ' ', $value ); | ||
} | ||
|
||
$errors[md5( $value )] = $value; | ||
} | ||
|
||
$error = '<li>' . implode( '</li><li>', $errors ) . '</li>'; | ||
} | ||
|
||
return Html::rawElement( 'div', array( 'class' => 'smw-callout smw-callout-error' ), $error ); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
tests/phpunit/Unit/MediaWiki/Specials/Ask/HtmlContentBuilderTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
|
||
namespace SMW\Tests\MediaWiki\Specials\Ask; | ||
|
||
use SMW\MediaWiki\Specials\Ask\HtmlContentBuilder; | ||
|
||
/** | ||
* @covers \SMW\MediaWiki\Specials\Ask\HtmlContentBuilder | ||
* @group semantic-mediawiki | ||
* | ||
* @license GNU GPL v2+ | ||
* @since 2.5 | ||
* | ||
* @author mwjames | ||
*/ | ||
class HtmlContentBuilderTest extends \PHPUnit_Framework_TestCase { | ||
|
||
public function testCanConstruct() { | ||
|
||
$this->assertInstanceOf( | ||
'\SMW\MediaWiki\Specials\Ask\HtmlContentBuilder', | ||
new HtmlContentBuilder() | ||
); | ||
} | ||
|
||
/** | ||
* @dataProvider queryErrorProvider | ||
*/ | ||
public function testGetFormattedErrorString( $errors, $expected ) { | ||
|
||
$query = $this->getMockBuilder( '\SMWQuery' ) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
|
||
$query->expects( $this->atLeastOnce() ) | ||
->method( 'getErrors' ) | ||
->will( $this->returnValue( $errors ) ); | ||
|
||
$instance = new HtmlContentBuilder(); | ||
|
||
$this->assertEquals( | ||
$expected, | ||
$instance->getFormattedErrorString( $query ) | ||
); | ||
} | ||
|
||
public function queryErrorProvider() { | ||
|
||
$provider[] = array( | ||
'', | ||
'' | ||
); | ||
|
||
$provider[] = array( | ||
array( 'Foo' ), | ||
'<div class="smw-callout smw-callout-error">Foo</div>' | ||
); | ||
|
||
$provider[] = array( | ||
array( 'Foo', 'Bar' ), | ||
'<div class="smw-callout smw-callout-error"><li>Foo</li><li>Bar</li></div>' | ||
); | ||
|
||
$provider[] = array( | ||
array( 'Foo', array( 'Bar' ) ), | ||
'<div class="smw-callout smw-callout-error"><li>Foo</li><li>Bar</li></div>' | ||
); | ||
|
||
// Filter duplicate | ||
$provider[] = array( | ||
array( 'Foo', array( 'Bar' ), 'Bar' ), | ||
'<div class="smw-callout smw-callout-error"><li>Foo</li><li>Bar</li></div>' | ||
); | ||
|
||
return $provider; | ||
} | ||
|
||
} |