Merge "Enforce calling HTMLForm::prepareForm before displayForm"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Wed, 27 Apr 2016 20:19:39 +0000 (20:19 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Wed, 27 Apr 2016 20:19:39 +0000 (20:19 +0000)
includes/htmlform/HTMLForm.php
tests/phpunit/includes/htmlform/HTMLFormTest.php [new file with mode: 0644]

index 8f8caf2..d671029 100644 (file)
@@ -1474,13 +1474,22 @@ class HTMLForm extends ContextSource {
         * @param string $fieldsetIDPrefix ID prefix for the "<fieldset>" tag of
         *   each subsection, ignored if empty.
         * @param bool &$hasUserVisibleFields Whether the section had user-visible fields.
+        * @throws LogicException When called on uninitialized field data, e.g. When
+        *  HTMLForm::displayForm was called without calling HTMLForm::prepareForm
+        *  first.
         *
         * @return string
         */
        public function displaySection( $fields,
                $sectionName = '',
                $fieldsetIDPrefix = '',
-               &$hasUserVisibleFields = false ) {
+               &$hasUserVisibleFields = false
+       ) {
+               if ( $this->mFieldData === null ) {
+                       throw new LogicException( 'HTMLForm::displaySection() called on uninitialized field data. '
+                               . 'You probably called displayForm() without calling prepareForm() first.' );
+               }
+
                $displayFormat = $this->getDisplayFormat();
 
                $html = [];
diff --git a/tests/phpunit/includes/htmlform/HTMLFormTest.php b/tests/phpunit/includes/htmlform/HTMLFormTest.php
new file mode 100644 (file)
index 0000000..b7e0053
--- /dev/null
@@ -0,0 +1,21 @@
+<?php
+
+
+class HTMLFormTest extends MediaWikiTestCase {
+       public function testGetHTML_empty() {
+               $form = new HTMLForm( [] );
+               $form->setTitle( Title::newFromText( 'Foo' ) );
+               $form->prepareForm();
+               $html = $form->getHTML( false );
+               $this->assertRegExp( '/<form\b/', $html );
+       }
+
+       /**
+        * @expectedException LogicException
+        */
+       public function testGetHTML_noPrepare() {
+               $form = new HTMLForm( [] );
+               $form->setTitle( Title::newFromText( 'Foo' ) );
+               $form->getHTML( false );
+       }
+}