|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Tests for field type get_formatted_output() methods. |
| 4 | + * |
| 5 | + * Regression tests for #178: non-array $value passed to get_formatted_output() |
| 6 | + * should not throw a PHP warning. |
| 7 | + */ |
| 8 | + |
| 9 | +require_once __DIR__ . '/FieldsTestCase.php'; |
| 10 | + |
| 11 | +class FieldTypeFormattedOutputTest extends FieldsTestCase { |
| 12 | + |
| 13 | + /** |
| 14 | + * Build a minimal field stub with dropdown_options meta. |
| 15 | + */ |
| 16 | + private function make_field_stub( array $options ) { |
| 17 | + return new class( $options ) { |
| 18 | + private $options; |
| 19 | + public function __construct( $options ) { $this->options = $options; } |
| 20 | + public function get_meta( $key ) { |
| 21 | + if ( 'dropdown_options' === $key ) { |
| 22 | + return $this->options; |
| 23 | + } |
| 24 | + return array(); |
| 25 | + } |
| 26 | + }; |
| 27 | + } |
| 28 | + |
| 29 | + // ---- Multicheckbox ---- |
| 30 | + |
| 31 | + public function test_multicheckbox_with_array_value_returns_labels() { |
| 32 | + require_once WPUM_PLUGIN_DIR . 'includes/fields/types/class-wpum-field-multicheckbox.php'; |
| 33 | + $field_type = new WPUM_Field_Multicheckbox(); |
| 34 | + $field_stub = $this->make_field_stub( array( |
| 35 | + array( 'value' => 'opt1', 'label' => 'Option 1' ), |
| 36 | + array( 'value' => 'opt2', 'label' => 'Option 2' ), |
| 37 | + ) ); |
| 38 | + |
| 39 | + $result = $field_type->get_formatted_output( $field_stub, array( 'opt1', 'opt2' ) ); |
| 40 | + $this->assertEquals( 'Option 1, Option 2', $result ); |
| 41 | + } |
| 42 | + |
| 43 | + public function test_multicheckbox_with_null_value_returns_empty_string() { |
| 44 | + require_once WPUM_PLUGIN_DIR . 'includes/fields/types/class-wpum-field-multicheckbox.php'; |
| 45 | + $field_type = new WPUM_Field_Multicheckbox(); |
| 46 | + $field_stub = $this->make_field_stub( array( |
| 47 | + array( 'value' => 'opt1', 'label' => 'Option 1' ), |
| 48 | + ) ); |
| 49 | + |
| 50 | + $result = $field_type->get_formatted_output( $field_stub, null ); |
| 51 | + $this->assertEquals( '', $result ); |
| 52 | + } |
| 53 | + |
| 54 | + public function test_multicheckbox_with_empty_string_value_returns_empty_string() { |
| 55 | + require_once WPUM_PLUGIN_DIR . 'includes/fields/types/class-wpum-field-multicheckbox.php'; |
| 56 | + $field_type = new WPUM_Field_Multicheckbox(); |
| 57 | + $field_stub = $this->make_field_stub( array( |
| 58 | + array( 'value' => 'opt1', 'label' => 'Option 1' ), |
| 59 | + ) ); |
| 60 | + |
| 61 | + $result = $field_type->get_formatted_output( $field_stub, '' ); |
| 62 | + $this->assertEquals( '', $result ); |
| 63 | + } |
| 64 | + |
| 65 | + // ---- Multiselect ---- |
| 66 | + |
| 67 | + public function test_multiselect_with_array_value_returns_labels() { |
| 68 | + require_once WPUM_PLUGIN_DIR . 'includes/fields/types/class-wpum-field-multiselect.php'; |
| 69 | + $field_type = new WPUM_Field_Multiselect(); |
| 70 | + $field_stub = $this->make_field_stub( array( |
| 71 | + array( 'value' => 'a', 'label' => 'Apple' ), |
| 72 | + array( 'value' => 'b', 'label' => 'Banana' ), |
| 73 | + ) ); |
| 74 | + |
| 75 | + $result = $field_type->get_formatted_output( $field_stub, array( 'a', 'b' ) ); |
| 76 | + $this->assertEquals( 'Apple, Banana', $result ); |
| 77 | + } |
| 78 | + |
| 79 | + public function test_multiselect_with_null_value_returns_empty_string() { |
| 80 | + require_once WPUM_PLUGIN_DIR . 'includes/fields/types/class-wpum-field-multiselect.php'; |
| 81 | + $field_type = new WPUM_Field_Multiselect(); |
| 82 | + $field_stub = $this->make_field_stub( array( |
| 83 | + array( 'value' => 'a', 'label' => 'Apple' ), |
| 84 | + ) ); |
| 85 | + |
| 86 | + $result = $field_type->get_formatted_output( $field_stub, null ); |
| 87 | + $this->assertEquals( '', $result ); |
| 88 | + } |
| 89 | + |
| 90 | + public function test_multiselect_with_empty_string_value_returns_empty_string() { |
| 91 | + require_once WPUM_PLUGIN_DIR . 'includes/fields/types/class-wpum-field-multiselect.php'; |
| 92 | + $field_type = new WPUM_Field_Multiselect(); |
| 93 | + $field_stub = $this->make_field_stub( array( |
| 94 | + array( 'value' => 'a', 'label' => 'Apple' ), |
| 95 | + ) ); |
| 96 | + |
| 97 | + $result = $field_type->get_formatted_output( $field_stub, '' ); |
| 98 | + $this->assertEquals( '', $result ); |
| 99 | + } |
| 100 | +} |
0 commit comments