Himeshi has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/83213


Change subject: Added unit tests for the SFFormPrinter class
......................................................................

Added unit tests for the SFFormPrinter class

Added PHPUnit tests for testing the {{{section}}} tag in the formHTML() method 
of the SFFormPrinter class.

Bug: 46662
Change-Id: I63772d3e0a04c3f20239e9b868cec1daa81c699b
---
M SemanticForms.php
M includes/SF_Utils.php
A tests/phpunit/includes/SF_FormPrinterTest.php
3 files changed, 105 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/SemanticForms 
refs/changes/13/83213/1

diff --git a/SemanticForms.php b/SemanticForms.php
index 7968090..84a2b7f 100644
--- a/SemanticForms.php
+++ b/SemanticForms.php
@@ -93,6 +93,7 @@
 $wgHooks['PageSchemasRegisterHandlers'][] = 'SFPageSchemas::registerClass';
 $wgHooks['EditPage::importFormData'][] = 'SFUtils::showFormPreview';
 $wgHooks['CanonicalNamespaces'][] = 'SFUtils::registerNamespaces';
+$wgHooks['UnitTestsList'][] = 'SFUtils:onUnitTestsList';
 
 // Using UnknownAction is deprecated from MW 1.18 onwards.
 if ( version_compare( $wgVersion, '1.18', '<' ) ) {
diff --git a/includes/SF_Utils.php b/includes/SF_Utils.php
index c1a4e27..87fcd7d 100644
--- a/includes/SF_Utils.php
+++ b/includes/SF_Utils.php
@@ -1046,4 +1046,16 @@
                return trim( sprintf( $format, $value ) ); // trim needed, when 
$hasPadding == false
        }
 
+       /**
+        * Hook to add PHPUnit test cases.
+        * From 
https://www.mediawiki.org/wiki/Manual:PHP_unit_testing/Writing_unit_tests_for_extensions
+        *
+        * @return boolean
+        */
+        public static function onUnitTestsList( &$files ) {
+               $testDir = dirname( __DIR__ ) . '/tests/phpunit/includes';
+               $files = array_merge( $files, glob( "$testDir/*Test.php" ) );
+               return true;
+        }
+
 }
diff --git a/tests/phpunit/includes/SF_FormPrinterTest.php 
b/tests/phpunit/includes/SF_FormPrinterTest.php
new file mode 100644
index 0000000..7234845
--- /dev/null
+++ b/tests/phpunit/includes/SF_FormPrinterTest.php
@@ -0,0 +1,92 @@
+<?php
+/**
+ * Tests for the SFFormPrinter class
+ *
+ * @author Himeshi De Silva
+ */
+class SFFormPrinterTest extends MediaWikiTestCase {
+
+       protected function setUp() {
+               parent::setUp();
+       }
+
+       // Tests for page sections in the formHTML() method
+
+       /**
+        * @dataProvider pageSectionDataProvider
+        */
+       public function testPageSectionsWithoutExistingPages( $form_definition, 
$expected_form_text, $expected_data_text ) {
+               global $sfgFormPrinter, $wgTitle, $wgParser;
+               $wgParser = $this->getParser();
+               $wgTitle = $this->getTitle();
+
+               list ( $form_text, $javascript_text, $data_text, 
$form_page_title, $generated_page_name ) =
+                       $sfgFormPrinter->formHTML( $form_definition, null, 
false, null, null, $this->getRandomString(), null );
+
+               $this->assertContains( $expected_form_text, $form_text );
+               $this->assertContains( $expected_data_text, $data_text );
+
+       }
+
+       /**
+        * Data provider method
+        */
+       public function pageSectionDataProvider() {
+               // returns an array of form definitions to be tested, expected 
form text and
+               // expected data text(ie. text that shows up on the actual page 
when the form is saved)
+               return array(
+                       array( "==section1==
+                                       {{{section|section1|level=2}}}",
+                                       "<span class=\"inputSpan\"><textarea 
tabindex=\"1\" name=\"_section[section1]\" id=\"input_1\" 
class=\"createboxInput\" rows=\"5\" cols=\"90\" style=\"width: 
100%\"></textarea></span>",
+                                       "==section1==" ),
+                       array( "=====section 2=====
+                                       {{{section|section 
2|level=5|rows=10|cols=5}}}",
+                                       "<span class=\"inputSpan\"><textarea 
tabindex=\"1\" name=\"_section[section 2]\" id=\"input_1\" 
class=\"createboxInput\" rows=\"10\" cols=\"5\" style=\"width: 
auto\"></textarea></span>",
+                                       "=====section 2=====" ),
+                       array( "==section 3==
+                                       {{{section|section 
3|level=2|mandatory|rows=20|cols=50|autogrow}}}",
+                                       "<span class=\"inputSpan 
mandatoryFieldSpan\"><textarea tabindex=\"1\" name=\"_section[section 3]\" 
id=\"input_1\" class=\"mandatoryField autoGrow\" rows=\"20\" cols=\"50\" 
style=\"width: auto\"></textarea></span>",
+                                       "==section 3==" ),
+                       array( "====section 4====
+                                       {{{section|section 4|level=4|hidden}}}",
+                                       "<input type=\"hidden\" 
name=\"_section[section 4]\" />",
+                                       "====section 4====" ),
+                       array( "===Section 5===
+                                       {{{section|Section 
5|level=3|restricted|class=FormTest}}}",
+                                       "<span class=\"inputSpan\"><textarea 
tabindex=\"1\" name=\"_section[Section 5]\" id=\"input_1\" 
class=\"createboxInput FormTest\" rows=\"5\" cols=\"90\" style=\"width: 100%\" 
disabled=\"\"></textarea></span>",
+                                       "===Section 5===" )
+               );
+       }
+
+       /**
+        * Returns a Title for test
+        * @return Title
+        */
+       private function getTitle() {
+               return Title::newFromText( $this->getRandomString(), SF_NS_FORM 
);
+       }
+
+       /**
+        * Returns a random string
+        * @return string
+        */
+       private function getRandomString() {
+               $alpha_numeric = 
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
+               return substr( str_shuffle( $alpha_numeric ), 0, 10 );
+       }
+
+       /**
+        * Returns a Parser for test
+        * @return Parser
+        */
+       private function getParser() {
+               return new StubObject(
+               'wgParser', $GLOBALS['wgParserConf']['class'],
+               array( $GLOBALS['wgParserConf'] ) );
+       }
+
+       protected function tearDown() {
+               parent::tearDown();
+       }
+}
+

-- 
To view, visit https://gerrit.wikimedia.org/r/83213
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I63772d3e0a04c3f20239e9b868cec1daa81c699b
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/SemanticForms
Gerrit-Branch: master
Gerrit-Owner: Himeshi <[email protected]>
Gerrit-Reviewer: jenkins-bot

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to