Enforce calling HTMLForm::prepareForm before displayForm
authorGergő Tisza <tgr.huwiki@gmail.com>
Wed, 27 Apr 2016 10:28:55 +0000 (12:28 +0200)
committerSam Smith <git@samsmith.io>
Wed, 27 Apr 2016 19:00:06 +0000 (20:00 +0100)
Bug: T133163
Change-Id: Idd5d117cb0dd65c195019dcd321cd4bf9024b426

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 );
+       }
+}