HTMLCheckMatrixTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /**
  3. * Unit tests for the HTMLCheckMatrix
  4. * @covers HTMLCheckMatrix
  5. */
  6. class HTMLCheckMatrixTest extends MediaWikiTestCase {
  7. static private $defaultOptions = [
  8. 'rows' => [ 'r1', 'r2' ],
  9. 'columns' => [ 'c1', 'c2' ],
  10. 'fieldname' => 'test',
  11. ];
  12. public function testPlainInstantiation() {
  13. try {
  14. new HTMLCheckMatrix( [] );
  15. } catch ( MWException $e ) {
  16. $this->assertInstanceOf( HTMLFormFieldRequiredOptionsException::class, $e );
  17. return;
  18. }
  19. $this->fail( 'Expected MWException indicating missing parameters but none was thrown.' );
  20. }
  21. public function testInstantiationWithMinimumRequiredParameters() {
  22. new HTMLCheckMatrix( self::$defaultOptions );
  23. $this->assertTrue( true ); // form instantiation must throw exception on failure
  24. }
  25. public function testValidateCallsUserDefinedValidationCallback() {
  26. $called = false;
  27. $field = new HTMLCheckMatrix( self::$defaultOptions + [
  28. 'validation-callback' => function () use ( &$called ) {
  29. $called = true;
  30. return false;
  31. },
  32. ] );
  33. $this->assertEquals( false, $this->validate( $field, [] ) );
  34. $this->assertTrue( $called );
  35. }
  36. public function testValidateRequiresArrayInput() {
  37. $field = new HTMLCheckMatrix( self::$defaultOptions );
  38. $this->assertEquals( false, $this->validate( $field, null ) );
  39. $this->assertEquals( false, $this->validate( $field, true ) );
  40. $this->assertEquals( false, $this->validate( $field, 'abc' ) );
  41. $this->assertEquals( false, $this->validate( $field, new stdClass ) );
  42. $this->assertEquals( true, $this->validate( $field, [] ) );
  43. }
  44. public function testValidateAllowsOnlyKnownTags() {
  45. $field = new HTMLCheckMatrix( self::$defaultOptions );
  46. $this->assertInstanceOf( Message::class, $this->validate( $field, [ 'foo' ] ) );
  47. }
  48. public function testValidateAcceptsPartialTagList() {
  49. $field = new HTMLCheckMatrix( self::$defaultOptions );
  50. $this->assertTrue( $this->validate( $field, [] ) );
  51. $this->assertTrue( $this->validate( $field, [ 'c1-r1' ] ) );
  52. $this->assertTrue( $this->validate( $field, [ 'c1-r1', 'c1-r2', 'c2-r1', 'c2-r2' ] ) );
  53. }
  54. /**
  55. * This form object actually has no visibility into what happens later on, but essentially
  56. * if the data submitted by the user passes validate the following is run:
  57. * foreach ( $field->filterDataForSubmit( $data ) as $k => $v ) {
  58. * $user->setOption( $k, $v );
  59. * }
  60. */
  61. public function testValuesForcedOnRemainOn() {
  62. $field = new HTMLCheckMatrix( self::$defaultOptions + [
  63. 'force-options-on' => [ 'c2-r1' ],
  64. ] );
  65. $expected = [
  66. 'c1-r1' => false,
  67. 'c1-r2' => false,
  68. 'c2-r1' => true,
  69. 'c2-r2' => false,
  70. ];
  71. $this->assertEquals( $expected, $field->filterDataForSubmit( [] ) );
  72. }
  73. public function testValuesForcedOffRemainOff() {
  74. $field = new HTMLCheckMatrix( self::$defaultOptions + [
  75. 'force-options-off' => [ 'c1-r2', 'c2-r2' ],
  76. ] );
  77. $expected = [
  78. 'c1-r1' => true,
  79. 'c1-r2' => false,
  80. 'c2-r1' => true,
  81. 'c2-r2' => false,
  82. ];
  83. // array_keys on the result simulates submitting all fields checked
  84. $this->assertEquals( $expected, $field->filterDataForSubmit( array_keys( $expected ) ) );
  85. }
  86. protected function validate( HTMLFormField $field, $submitted ) {
  87. return $field->validate(
  88. $submitted,
  89. [ self::$defaultOptions['fieldname'] => $submitted ]
  90. );
  91. }
  92. }