jenkins-bot has submitted this change and it was merged. (
https://gerrit.wikimedia.org/r/362264 )
Change subject: Basic Unit Tests for Quiz Extension
..
Basic Unit Tests for Quiz Extension
Initial tests class for Quiz.class.php and Question.php is added.
Bug:T159691
Change-Id: Ide874476812dba70c1d2a56aa8a27dfbb99f
---
M Question.php
M Quiz.class.php
A tests/phpunit/QuestionTest.php
A tests/phpunit/QuizTest.php
4 files changed, 301 insertions(+), 5 deletions(-)
Approvals:
jenkins-bot: Verified
Mvolz: Looks good to me, approved
diff --git a/Question.php b/Question.php
index c73a33d..9065c3f 100644
--- a/Question.php
+++ b/Question.php
@@ -80,9 +80,10 @@
);
$splitHeaderPattern = '`\n\|\|`';
$unparsedHeader = preg_split( $splitHeaderPattern, $input );
-$output = $this->mParser->recursiveTagParse( trim(
$unparsedHeader[0] ) . "\n" );
+$output = $this->mParser->recursiveTagParse( trim(
$unparsedHeader[0] ) );
if ( array_key_exists( 1, $unparsedHeader ) &&
$this->mBeingCorrected ) {
-$output .= '';
+ $output .="\n";
+ $output .= '';
$output .= '';
$output .= $this->mParser->recursiveTagParse( trim(
$unparsedHeader[1] ) );
$output .= '';
@@ -326,7 +327,6 @@
$this->mProposalPattern .= '([+-])? ?';
}
}
- $output .= '';
$output .= '' . "\n";
$this->mProposalPattern .= '(.*)`';
return $output;
@@ -509,7 +509,6 @@
}
}
$name = $wqInputId;
-
$temp = $templateParser->processTemplate(
'Answer',
[
diff --git a/Quiz.class.php b/Quiz.class.php
index 0a1aadd..0a43cfa 100644
--- a/Quiz.class.php
+++ b/Quiz.class.php
@@ -26,7 +26,7 @@
$this->mParser = $parser;
$this->mRequest = &$wgRequest;
// Allot a unique identifier to the quiz.
- $this->mQuizId = self::$sQuizId;
+ $this->mQuizId = $this->getQuizId();
self::$sQuizId++;
// Reset the unique identifier of the questions.
$this->mQuestionId = 0;
@@ -95,6 +95,13 @@
}
/**
+* @return int Quiz Id
+*/
+ public function getQuizId() {
+ return self::$sQuizId;
+ }
+
+ /**
* Accessor for the color array
* Displays an error message if the colorId doesn't exists.
*
diff --git a/tests/phpunit/QuestionTest.php b/tests/phpunit/QuestionTest.php
new file mode 100644
index 000..1761954
--- /dev/null
+++ b/tests/phpunit/QuestionTest.php
@@ -0,0 +1,234 @@
+getParser();
+ $title = $wgParser->getTitle();
+ $this->parser = &$wgParser;
+ $this->parser->startExternalParse( $title, $options, 'text',
true );
+ }
+
+ protected function tearDown() {
+ parent::tearDown();
+ unset( $this->parser );
+ }
+
+ private function getParser() {
+ return new StubObject(
+ 'wgParser', $GLOBALS['wgParserConf']['class'],
+ [ $GLOBALS['wgParserConf'] ]
+ );
+ }
+
+ private function getQuestion( $beingCorrected, $caseSensitive,
$questionId ) {
+ return new Question( $beingCorrected, $caseSensitive,
$questionId, $this->parser );
+ }
+
+ private function getRequest() {
+ global $wgRequest;
+ return $wgRequest;
+ }
+ public function testGetState() {
+ $this->question = $this->getQuestion( 1, 1, 2 );
+ $state = $this->question->getState();
+ $this->assertThat(
+ $state,
+ $this->logicalOr(
+ $this->equalTo( 'right' ),
+ $this->equalTo( 'error' ),
+ $this->equalTo( 'NA' ),
+ $this->equalTo( 'wrong' )
+ )
+ );
+ }
+
+ public function provideSetState() {
+ return [
+ [ 'NA', 'error', 'error' ],
+ [ 'NA', 'na_wrong', 'na_wrong' ],
+ [ 'na_right', 'na_wrong', 'na_wrong' ],
+ [ 'na_wrong', 'na_right', 'na_wrong' ],
+ [ 'right', 'na_wrong', 'wrong' ],
+ [ 'na_wrong', 'right', 'wrong' ],
+ [ 'error', 'na_wrong', 'error' ]
+ ];
+ }
+
+ /**
+* @dataProvider