|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Tests for Address field type issues. |
| 4 | + * |
| 5 | + * @package WPGraphQL\GF\Tests\WPUnit |
| 6 | + */ |
| 7 | + |
| 8 | +use Tests\WPGraphQL\GF\TestCase\GFGraphQLTestCase; |
| 9 | + |
| 10 | +/** |
| 11 | + * Class AddressFieldTypesTest |
| 12 | + */ |
| 13 | +class AddressFieldTypesTest extends GFGraphQLTestCase { |
| 14 | + /** |
| 15 | + * The ID of the form created for testing. |
| 16 | + * |
| 17 | + * @var int |
| 18 | + */ |
| 19 | + private $form_id; |
| 20 | + |
| 21 | + /** |
| 22 | + * {@inheritDoc} |
| 23 | + */ |
| 24 | + public function setUp(): void { |
| 25 | + parent::setUp(); |
| 26 | + |
| 27 | + wp_set_current_user( $this->admin->ID ); |
| 28 | + $this->clearSchema(); |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * {@inheritDoc} |
| 33 | + */ |
| 34 | + public function tearDown(): void { |
| 35 | + $this->factory->form->delete( $this->form_id ); |
| 36 | + |
| 37 | + parent::tearDown(); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Helper method to create a form with an address field with the given properties. |
| 42 | + * |
| 43 | + * @param array $props The properties for the address field. |
| 44 | + * |
| 45 | + * @return int The ID of the created form. |
| 46 | + */ |
| 47 | + private function create_address_form( array $props ): int { |
| 48 | + $field_helper = $this->tester->getPropertyHelper( 'AddressField', $props ); |
| 49 | + $field = $this->factory->field->create( $field_helper->values ); |
| 50 | + |
| 51 | + return $this->factory->form->create( |
| 52 | + array_merge( |
| 53 | + [ 'fields' => [ $field ] ], |
| 54 | + $this->tester->getFormDefaultArgs() |
| 55 | + ) |
| 56 | + ); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Test that the US address type returns the correct address type and default state, and that default province is null. |
| 61 | + */ |
| 62 | + public function testUsAddressType(): void { |
| 63 | + $this->form_id = $this->create_address_form( |
| 64 | + [ |
| 65 | + 'addressType' => 'us', |
| 66 | + 'defaultState' => 'Michigan', |
| 67 | + 'defaultProvince' => 'Ontario', |
| 68 | + ] |
| 69 | + ); |
| 70 | + |
| 71 | + $query = ' |
| 72 | + query ($id: ID!) { |
| 73 | + gfForm(id: $id, idType: DATABASE_ID) { |
| 74 | + formFields { |
| 75 | + nodes { |
| 76 | + ... on AddressField { |
| 77 | + addressType |
| 78 | + defaultState |
| 79 | + defaultProvince |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + '; |
| 86 | + |
| 87 | + $variables = [ 'id' => $this->form_id ]; |
| 88 | + $response = $this->graphql( compact( 'query', 'variables' ) ); |
| 89 | + |
| 90 | + $this->assertArrayNotHasKey( 'errors', $response ); |
| 91 | + $this->assertEquals( 'US', $response['data']['gfForm']['formFields']['nodes'][0]['addressType'] ); |
| 92 | + $this->assertNotNull( $response['data']['gfForm']['formFields']['nodes'][0]['defaultState'] ); |
| 93 | + $this->assertEquals( 'MICHIGAN', $response['data']['gfForm']['formFields']['nodes'][0]['defaultState'] ); |
| 94 | + $this->assertNull( $response['data']['gfForm']['formFields']['nodes'][0]['defaultProvince'] ); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Test that the Canadian address type returns the correct address type and default province, and that default state is null. |
| 99 | + */ |
| 100 | + public function testCanadianAddressTypeWithProvince(): void { |
| 101 | + $this->form_id = $this->create_address_form( |
| 102 | + [ |
| 103 | + 'addressType' => 'canadian', |
| 104 | + 'defaultProvince' => 'Ontario', |
| 105 | + 'defaultState' => 'Michigan', |
| 106 | + ] |
| 107 | + ); |
| 108 | + |
| 109 | + $query = ' |
| 110 | + query ($id: ID!) { |
| 111 | + gfForm(id: $id, idType: DATABASE_ID) { |
| 112 | + formFields { |
| 113 | + nodes { |
| 114 | + ... on AddressField { |
| 115 | + addressType |
| 116 | + defaultState |
| 117 | + defaultProvince |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + '; |
| 124 | + |
| 125 | + $variables = [ 'id' => $this->form_id ]; |
| 126 | + $response = $this->graphql( compact( 'query', 'variables' ) ); |
| 127 | + |
| 128 | + $this->assertArrayNotHasKey( 'errors', $response ); |
| 129 | + $this->assertEquals( 'CANADA', $response['data']['gfForm']['formFields']['nodes'][0]['addressType'] ); |
| 130 | + $this->assertNotNull( $response['data']['gfForm']['formFields']['nodes'][0]['defaultProvince'] ); |
| 131 | + $this->assertEquals( 'ONTARIO', $response['data']['gfForm']['formFields']['nodes'][0]['defaultProvince'] ); |
| 132 | + $this->assertNull( $response['data']['gfForm']['formFields']['nodes'][0]['defaultState'] ); |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Test that the Canadian address type returns the correct address type and default province, |
| 137 | + * and that default state is null, when default province is set but default state is not set. |
| 138 | + */ |
| 139 | + public function testCanadianAddressTypeWithState(): void { |
| 140 | + $this->form_id = $this->create_address_form( |
| 141 | + [ |
| 142 | + 'addressType' => 'canadian', |
| 143 | + 'defaultProvince' => null, |
| 144 | + 'defaultState' => 'Ontario', |
| 145 | + ] |
| 146 | + ); |
| 147 | + |
| 148 | + $query = ' |
| 149 | + query ($id: ID!) { |
| 150 | + gfForm(id: $id, idType: DATABASE_ID) { |
| 151 | + formFields { |
| 152 | + nodes { |
| 153 | + ... on AddressField { |
| 154 | + addressType |
| 155 | + defaultState |
| 156 | + defaultProvince |
| 157 | + } |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | + '; |
| 163 | + |
| 164 | + $variables = [ 'id' => $this->form_id ]; |
| 165 | + $response = $this->graphql( compact( 'query', 'variables' ) ); |
| 166 | + |
| 167 | + $this->assertArrayNotHasKey( 'errors', $response ); |
| 168 | + $this->assertEquals( 'CANADA', $response['data']['gfForm']['formFields']['nodes'][0]['addressType'] ); |
| 169 | + $this->assertNotNull( $response['data']['gfForm']['formFields']['nodes'][0]['defaultProvince'] ); |
| 170 | + $this->assertEquals( 'ONTARIO', $response['data']['gfForm']['formFields']['nodes'][0]['defaultProvince'] ); |
| 171 | + $this->assertNull( $response['data']['gfForm']['formFields']['nodes'][0]['defaultState'] ); |
| 172 | + } |
| 173 | + |
| 174 | + /** |
| 175 | + * Test that the international address type returns the correct address type and that default state and default province are null. |
| 176 | + */ |
| 177 | + public function testInternationalAddressType(): void { |
| 178 | + $this->form_id = $this->create_address_form( |
| 179 | + [ |
| 180 | + 'addressType' => 'international', |
| 181 | + 'defaultState' => 'Michigan', |
| 182 | + 'defaultProvince' => 'Ontario', |
| 183 | + ] |
| 184 | + ); |
| 185 | + |
| 186 | + $query = ' |
| 187 | + query ($id: ID!) { |
| 188 | + gfForm(id: $id, idType: DATABASE_ID) { |
| 189 | + formFields { |
| 190 | + nodes { |
| 191 | + ... on AddressField { |
| 192 | + addressType |
| 193 | + defaultState |
| 194 | + defaultProvince |
| 195 | + } |
| 196 | + } |
| 197 | + } |
| 198 | + } |
| 199 | + } |
| 200 | + '; |
| 201 | + |
| 202 | + $variables = [ 'id' => $this->form_id ]; |
| 203 | + $response = $this->graphql( compact( 'query', 'variables' ) ); |
| 204 | + |
| 205 | + $this->assertArrayNotHasKey( 'errors', $response ); |
| 206 | + $this->assertEquals( 'INTERNATIONAL', $response['data']['gfForm']['formFields']['nodes'][0]['addressType'] ); |
| 207 | + $this->assertNull( $response['data']['gfForm']['formFields']['nodes'][0]['defaultState'] ); |
| 208 | + $this->assertNull( $response['data']['gfForm']['formFields']['nodes'][0]['defaultProvince'] ); |
| 209 | + } |
| 210 | +} |
0 commit comments