|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace RESTAPI\Tests; |
| 4 | + |
| 5 | +require_once 'RESTAPI/autoloader.inc'; |
| 6 | + |
| 7 | +use RESTAPI\Core\TestCase; |
| 8 | +use RESTAPI\Fields\InterfaceAddressField; |
| 9 | +use RESTAPI\Models\NetworkInterface; |
| 10 | +use RESTAPI\Models\RESTAPISettings; |
| 11 | + |
| 12 | +/** |
| 13 | + * Tests represented interface address references and literal values. |
| 14 | + */ |
| 15 | +class APIFieldsInterfaceAddressFieldTestCase extends TestCase { |
| 16 | + /** |
| 17 | + * Checks choices, validation, round-trip conversion, and relations in every representation mode. |
| 18 | + */ |
| 19 | + public function test_interface_representations(): void { |
| 20 | + $settings = new RESTAPISettings(); |
| 21 | + foreach (['id', 'if', 'descr'] as $mode) { |
| 22 | + $settings->represent_interfaces_as->value = $mode; |
| 23 | + $settings->update(); |
| 24 | + |
| 25 | + foreach (['wan', 'lan', 'opt1'] as $id) { |
| 26 | + $interface = NetworkInterface::query(query_params: ['id' => $id])->first(); |
| 27 | + $represented = $mode === 'id' ? $id : $interface->$mode->value; |
| 28 | + foreach (['_ipv4', '_ipv6'] as $suffix) { |
| 29 | + $field = $this->make_field(); |
| 30 | + $field->value = $represented . $suffix; |
| 31 | + $this->assert_is_true(in_array($field->value, $field->choices, true)); |
| 32 | + $this->assert_is_true($field->validate()); |
| 33 | + $this->assert_equals($field->to_internal(), $id . $suffix); |
| 34 | + $field->from_internal($id . $suffix); |
| 35 | + $this->assert_equals($field->value, $represented . $suffix); |
| 36 | + $this->assert_equals($field->get_related_model()->id, $id); |
| 37 | + $this->assert_is_empty($field->get_related_models()->model_objects); |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Checks that literal values are preserved and never identify related interfaces. |
| 45 | + */ |
| 46 | + public function test_static_choices(): void { |
| 47 | + $settings = new RESTAPISettings(); |
| 48 | + foreach (['id', 'if', 'descr'] as $mode) { |
| 49 | + $settings->represent_interfaces_as->value = $mode; |
| 50 | + $settings->update(); |
| 51 | + $keywords = ['custom', 'any_ipv4', 'any_ipv6', 'localhost_ipv4', 'localhost_ipv6', 'WAN_ipv4']; |
| 52 | + $field = $this->make_field(allow_custom: $keywords); |
| 53 | + foreach ($keywords as $keyword) { |
| 54 | + $field->value = $keyword; |
| 55 | + $this->assert_is_true($field->validate()); |
| 56 | + $this->assert_equals($field->to_internal(), $keyword); |
| 57 | + $field->from_internal($keyword); |
| 58 | + $this->assert_equals($field->value, $keyword); |
| 59 | + $this->assert_equals($field->get_related_model(), null); |
| 60 | + $this->assert_is_false(in_array($keyword . '_ipv4', $field->choices, true)); |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Checks that bare, unknown, and malformed interface references are rejected. |
| 67 | + */ |
| 68 | + public function test_invalid_references(): void { |
| 69 | + $settings = new RESTAPISettings(); |
| 70 | + $settings->represent_interfaces_as->value = 'descr'; |
| 71 | + $settings->update(); |
| 72 | + $field = $this->make_field(); |
| 73 | + |
| 74 | + foreach (['WAN', 'wan_ipv4', 'missing_ipv6', 'WAN_ipv5', 'WAN:ip', 'WAN_ipv4_extra', 'any_ipv4'] as $value) { |
| 75 | + $field->value = $value; |
| 76 | + $this->assert_throws_response( |
| 77 | + response_id: 'FIELD_INVALID_CHOICE', |
| 78 | + code: 400, |
| 79 | + callable: fn() => $field->validate(), |
| 80 | + ); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Checks that interface queries constrain both address families and related models. |
| 86 | + */ |
| 87 | + public function test_interface_query(): void { |
| 88 | + $settings = new RESTAPISettings(); |
| 89 | + $settings->represent_interfaces_as->value = 'id'; |
| 90 | + $settings->update(); |
| 91 | + $field = $this->make_field(interface_query: ['id' => 'lan']); |
| 92 | + $this->assert_equals(array_keys($field->get_interface_choices()), ['lan_ipv4', 'lan_ipv6']); |
| 93 | + $field->value = 'wan_ipv4'; |
| 94 | + $this->assert_equals($field->get_related_model(), null); |
| 95 | + $this->assert_throws_response( |
| 96 | + response_id: 'FIELD_INVALID_CHOICE', |
| 97 | + code: 400, |
| 98 | + callable: fn() => $field->validate(), |
| 99 | + ); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Checks many-valued conversion and HATEOAS discovery without treating static values as interfaces. |
| 104 | + */ |
| 105 | + public function test_many_values(): void { |
| 106 | + $settings = new RESTAPISettings(); |
| 107 | + $settings->represent_interfaces_as->value = 'descr'; |
| 108 | + $settings->update(); |
| 109 | + |
| 110 | + foreach ([',', null] as $delimiter) { |
| 111 | + $field = $this->make_field(many: true, delimiter: $delimiter, allow_custom: ['any_ipv4']); |
| 112 | + $field->value = ['WAN_ipv4', 'LAN_ipv6', 'any_ipv4']; |
| 113 | + $internal = ['wan_ipv4', 'lan_ipv6', 'any_ipv4']; |
| 114 | + $internal = $delimiter === null ? $internal : implode($delimiter, $internal); |
| 115 | + $this->assert_is_true($field->validate()); |
| 116 | + $this->assert_equals($field->to_internal(), $internal); |
| 117 | + $field->from_internal($internal); |
| 118 | + $this->assert_equals($field->value, ['WAN_ipv4', 'LAN_ipv6', 'any_ipv4']); |
| 119 | + $this->assert_equals($field->get_related_model(), null); |
| 120 | + $related = $field->get_related_models()->model_objects; |
| 121 | + $this->assert_equals(count($related), 2); |
| 122 | + $this->assert_equals($related[0]->id, 'wan'); |
| 123 | + $this->assert_equals($related[1]->id, 'lan'); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Checks that optional null and empty values do not create interface references. |
| 129 | + */ |
| 130 | + public function test_optional_values(): void { |
| 131 | + $field = $this->make_field(allow_null: true, allow_empty: true); |
| 132 | + foreach ([null, ''] as $value) { |
| 133 | + $field->value = $value; |
| 134 | + $this->assert_is_true($field->validate()); |
| 135 | + $this->assert_equals($field->to_internal(), $value); |
| 136 | + $field->from_internal($value); |
| 137 | + $this->assert_equals($field->value, $value); |
| 138 | + $this->assert_equals($field->get_related_model(), null); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Constructs a field with the context and dynamic choices required for validation. |
| 144 | + * @param mixed ...$options Additional field options. |
| 145 | + * @return InterfaceAddressField The field to test. |
| 146 | + */ |
| 147 | + private function make_field(mixed ...$options): InterfaceAddressField { |
| 148 | + $field = new InterfaceAddressField(...$options); |
| 149 | + $field->name = 'extaddr'; |
| 150 | + $field->context = new RESTAPISettings(); |
| 151 | + $field->set_choices_from_callable(); |
| 152 | + return $field; |
| 153 | + } |
| 154 | +} |
0 commit comments