From e3d7978bc6005265ee08059ffe986a60143a0c3e Mon Sep 17 00:00:00 2001 From: =?utf8?q?Gerg=C5=91=20Tisza?= Date: Wed, 27 Apr 2016 12:28:55 +0200 Subject: [PATCH] Enforce calling HTMLForm::prepareForm before displayForm Bug: T133163 Change-Id: Idd5d117cb0dd65c195019dcd321cd4bf9024b426 --- includes/htmlform/HTMLForm.php | 11 +++++++++- .../includes/htmlform/HTMLFormTest.php | 21 +++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 tests/phpunit/includes/htmlform/HTMLFormTest.php diff --git a/includes/htmlform/HTMLForm.php b/includes/htmlform/HTMLForm.php index 8f8caf2b1b..d671029a25 100644 --- a/includes/htmlform/HTMLForm.php +++ b/includes/htmlform/HTMLForm.php @@ -1474,13 +1474,22 @@ class HTMLForm extends ContextSource { * @param string $fieldsetIDPrefix ID prefix for the "
" 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 index 0000000000..b7e0053c72 --- /dev/null +++ b/tests/phpunit/includes/htmlform/HTMLFormTest.php @@ -0,0 +1,21 @@ +setTitle( Title::newFromText( 'Foo' ) ); + $form->prepareForm(); + $html = $form->getHTML( false ); + $this->assertRegExp( '/setTitle( Title::newFromText( 'Foo' ) ); + $form->getHTML( false ); + } +} -- 2.20.1