summaryrefslogtreecommitdiff
path: root/tests/phpunit/includes/htmlform
diff options
context:
space:
mode:
authorPierre Schmitz <pierre@archlinux.de>2015-04-01 06:11:44 +0200
committerPierre Schmitz <pierre@archlinux.de>2015-04-01 06:11:44 +0200
commit14f74d141ab5580688bfd46d2f74c026e43ed967 (patch)
tree081b7cbfc4d246ecc42831978d080331267cf57c /tests/phpunit/includes/htmlform
parent4a953b6bfda28604979feb9cfbb58974d13b84bb (diff)
Update to MediaWiki 1.24.2
Diffstat (limited to 'tests/phpunit/includes/htmlform')
-rw-r--r--tests/phpunit/includes/htmlform/HTMLAutoCompleteSelectFieldTest.php68
-rw-r--r--tests/phpunit/includes/htmlform/HTMLCheckMatrixTest.php105
2 files changed, 173 insertions, 0 deletions
diff --git a/tests/phpunit/includes/htmlform/HTMLAutoCompleteSelectFieldTest.php b/tests/phpunit/includes/htmlform/HTMLAutoCompleteSelectFieldTest.php
new file mode 100644
index 00000000..2c7f50c9
--- /dev/null
+++ b/tests/phpunit/includes/htmlform/HTMLAutoCompleteSelectFieldTest.php
@@ -0,0 +1,68 @@
+<?php
+/**
+ * Unit tests for HTMLAutoCompleteSelectField
+ *
+ * @covers HTMLAutoCompleteSelectField
+ */
+class HtmlAutoCompleteSelectFieldTest extends MediaWikiTestCase {
+
+ var $options = array(
+ 'Bulgaria' => 'BGR',
+ 'Burkina Faso' => 'BFA',
+ 'Burundi' => 'BDI',
+ );
+
+ /**
+ * Verify that attempting to instantiate an HTMLAutoCompleteSelectField
+ * without providing any autocomplete options causes an exception to be
+ * thrown.
+ *
+ * @expectedException MWException
+ * @expectedExceptionMessage called without any autocompletions
+ */
+ function testMissingAutocompletions() {
+ new HTMLAutoCompleteSelectField( array( 'fieldname' => 'Test' ) );
+ }
+
+ /**
+ * Verify that the autocomplete options are correctly encoded as
+ * the 'data-autocomplete' attribute of the field.
+ *
+ * @covers HTMLAutoCompleteSelectField::getAttributes
+ */
+ function testGetAttributes() {
+ $field = new HTMLAutoCompleteSelectField( array(
+ 'fieldname' => 'Test',
+ 'autocomplete' => $this->options,
+ ) );
+
+ $attributes = $field->getAttributes( array() );
+ $this->assertEquals( array_keys( $this->options ),
+ FormatJson::decode( $attributes['data-autocomplete'] ),
+ "The 'data-autocomplete' attribute encodes autocomplete option keys as a JSON array."
+ );
+ }
+
+ /**
+ * Test that the optional select dropdown is included or excluded based on
+ * the presence or absence of the 'options' parameter.
+ */
+ function testOptionalSelectElement() {
+ $params = array(
+ 'fieldname' => 'Test',
+ 'autocomplete' => $this->options,
+ 'options' => $this->options,
+ );
+
+ $field = new HTMLAutoCompleteSelectField( $params );
+ $html = $field->getInputHTML( false );
+ $this->assertRegExp( '/select/', $html,
+ "When the 'options' parameter is set, the HTML includes a <select>" );
+
+ unset( $params['options'] );
+ $field = new HTMLAutoCompleteSelectField( $params );
+ $html = $field->getInputHTML( false );
+ $this->assertNotRegExp( '/select/', $html,
+ "When the 'options' parameter is not set, the HTML does not include a <select>" );
+ }
+}
diff --git a/tests/phpunit/includes/htmlform/HTMLCheckMatrixTest.php b/tests/phpunit/includes/htmlform/HTMLCheckMatrixTest.php
new file mode 100644
index 00000000..5a822f53
--- /dev/null
+++ b/tests/phpunit/includes/htmlform/HTMLCheckMatrixTest.php
@@ -0,0 +1,105 @@
+<?php
+
+/**
+ * Unit tests for the HTMLCheckMatrix
+ * @covers HTMLCheckMatrix
+ */
+class HtmlCheckMatrixTest extends MediaWikiTestCase {
+ static private $defaultOptions = array(
+ 'rows' => array( 'r1', 'r2' ),
+ 'columns' => array( 'c1', 'c2' ),
+ 'fieldname' => 'test',
+ );
+
+ public function testPlainInstantiation() {
+ try {
+ new HTMLCheckMatrix( array() );
+ } catch ( MWException $e ) {
+ $this->assertInstanceOf( 'HTMLFormFieldRequiredOptionsException', $e );
+ return;
+ }
+
+ $this->fail( 'Expected MWException indicating missing parameters but none was thrown.' );
+ }
+
+ public function testInstantiationWithMinimumRequiredParameters() {
+ new HTMLCheckMatrix( self::$defaultOptions );
+ $this->assertTrue( true ); // form instantiation must throw exception on failure
+ }
+
+ public function testValidateCallsUserDefinedValidationCallback() {
+ $called = false;
+ $field = new HTMLCheckMatrix( self::$defaultOptions + array(
+ 'validation-callback' => function () use ( &$called ) {
+ $called = true;
+
+ return false;
+ },
+ ) );
+ $this->assertEquals( false, $this->validate( $field, array() ) );
+ $this->assertTrue( $called );
+ }
+
+ public function testValidateRequiresArrayInput() {
+ $field = new HTMLCheckMatrix( self::$defaultOptions );
+ $this->assertEquals( false, $this->validate( $field, null ) );
+ $this->assertEquals( false, $this->validate( $field, true ) );
+ $this->assertEquals( false, $this->validate( $field, 'abc' ) );
+ $this->assertEquals( false, $this->validate( $field, new stdClass ) );
+ $this->assertEquals( true, $this->validate( $field, array() ) );
+ }
+
+ public function testValidateAllowsOnlyKnownTags() {
+ $field = new HTMLCheckMatrix( self::$defaultOptions );
+ $this->assertInternalType( 'string', $this->validate( $field, array( 'foo' ) ) );
+ }
+
+ public function testValidateAcceptsPartialTagList() {
+ $field = new HTMLCheckMatrix( self::$defaultOptions );
+ $this->assertTrue( $this->validate( $field, array() ) );
+ $this->assertTrue( $this->validate( $field, array( 'c1-r1' ) ) );
+ $this->assertTrue( $this->validate( $field, array( 'c1-r1', 'c1-r2', 'c2-r1', 'c2-r2' ) ) );
+ }
+
+ /**
+ * This form object actually has no visibility into what happens later on, but essentially
+ * if the data submitted by the user passes validate the following is run:
+ * foreach ( $field->filterDataForSubmit( $data ) as $k => $v ) {
+ * $user->setOption( $k, $v );
+ * }
+ */
+ public function testValuesForcedOnRemainOn() {
+ $field = new HTMLCheckMatrix( self::$defaultOptions + array(
+ 'force-options-on' => array( 'c2-r1' ),
+ ) );
+ $expected = array(
+ 'c1-r1' => false,
+ 'c1-r2' => false,
+ 'c2-r1' => true,
+ 'c2-r2' => false,
+ );
+ $this->assertEquals( $expected, $field->filterDataForSubmit( array() ) );
+ }
+
+ public function testValuesForcedOffRemainOff() {
+ $field = new HTMLCheckMatrix( self::$defaultOptions + array(
+ 'force-options-off' => array( 'c1-r2', 'c2-r2' ),
+ ) );
+ $expected = array(
+ 'c1-r1' => true,
+ 'c1-r2' => false,
+ 'c2-r1' => true,
+ 'c2-r2' => false,
+ );
+ // array_keys on the result simulates submitting all fields checked
+ $this->assertEquals( $expected, $field->filterDataForSubmit( array_keys( $expected ) ) );
+ }
+
+ protected function validate( HTMLFormField $field, $submitted ) {
+ return $field->validate(
+ $submitted,
+ array( self::$defaultOptions['fieldname'] => $submitted )
+ );
+ }
+
+}