@@ -67,7 +67,7 @@ public function testGetConstituencyByName() {
6767 'getConstituency ' ,
6868 'name ' ,
6969 'Amber Valley ' ,
70- '{"name":"Amber Valley"} '
70+ '{"demographics":{"population":72000,"age_groups":{"18-35":25,"36-55":40,"56+":35}},"description":"A parliamentary constituency in Derbyshire"," name":"Amber Valley"} '
7171 );
7272 }
7373
@@ -140,6 +140,68 @@ public function testGetMlasLookup() {
140140 );
141141 }
142142
143+ public function testGetMPsInfoJsonEscaping () {
144+ // Test that JSON stored in personinfo is not double-encoded when requesting JSON output
145+ $ page = $ this ->fetch_page ('getMPsInfo ' , [
146+ 'key ' => 'test_key ' ,
147+ 'id ' => '2,3 ' ,
148+ 'output ' => 'json ' ,
149+ ]);
150+
151+ $ response = json_decode ($ page , true );
152+ $ this ->assertNotNull ($ response , 'Response should be valid JSON ' );
153+
154+ // Check that the JSON field is properly parsed as an object, not escaped as a string
155+ $ this ->assertIsArray ($ response ['2 ' ]['test_json_field ' ], 'JSON field should be parsed as array/object, not string ' );
156+ $ this ->assertEquals ('Test Person ' , $ response ['2 ' ]['test_json_field ' ]['name ' ]);
157+ $ this ->assertEquals ('London ' , $ response ['2 ' ]['test_json_field ' ]['location ' ]);
158+ $ this ->assertIsArray ($ response ['2 ' ]['test_json_field ' ]['interests ' ]);
159+
160+ // Check that plain text fields remain as strings
161+ $ this ->assertIsString ($ response ['2 ' ]['test_plain_field ' ]);
162+ $ this ->assertEquals ('Simple text value ' , $ response ['2 ' ]['test_plain_field ' ]);
163+
164+ // Check that JSON arrays are properly parsed
165+ $ this ->assertIsArray ($ response ['3 ' ]['test_json_array ' ]);
166+ $ this ->assertEquals (1 , $ response ['3 ' ]['test_json_array ' ][0 ]['id ' ]);
167+ $ this ->assertEquals ('Item 1 ' , $ response ['3 ' ]['test_json_array ' ][0 ]['name ' ]);
168+ }
169+
170+ public function testGetMPsInfoXmlOutput () {
171+ // Test that XML output still works with JSON content (should be escaped in XML)
172+ $ page = $ this ->fetch_page ('getMPsInfo ' , [
173+ 'key ' => 'test_key ' ,
174+ 'id ' => '2 ' ,
175+ 'output ' => 'xml ' ,
176+ ]);
177+
178+ $ this ->assertStringContainsString ('<test_json_field> ' , $ page );
179+ // In XML, JSON content should be escaped/as string with HTML entities
180+ $ this ->assertStringContainsString ('"name": "Test Person" ' , $ page );
181+ }
182+
183+ public function testGetConstituencyInfoJsonEscaping () {
184+ // Test that JSON stored in consinfo is not double-encoded when requesting JSON output
185+ $ page = $ this ->fetch_page ('getConstituency ' , [
186+ 'key ' => 'test_key ' ,
187+ 'name ' => 'Amber Valley ' ,
188+ 'output ' => 'json ' ,
189+ ]);
190+
191+ $ response = json_decode ($ page , true );
192+ $ this ->assertNotNull ($ response , 'Response should be valid JSON ' );
193+
194+ // Check that the JSON field is properly parsed as an object, not escaped as a string
195+ $ this ->assertIsArray ($ response ['demographics ' ], 'JSON field should be parsed as array/object, not string ' );
196+ $ this ->assertEquals (72000 , $ response ['demographics ' ]['population ' ]);
197+ $ this ->assertIsArray ($ response ['demographics ' ]['age_groups ' ]);
198+ $ this ->assertEquals (25 , $ response ['demographics ' ]['age_groups ' ]['18-35 ' ]);
199+
200+ // Check that plain text fields remain as strings
201+ $ this ->assertIsString ($ response ['description ' ]);
202+ $ this ->assertEquals ('A parliamentary constituency in Derbyshire ' , $ response ['description ' ]);
203+ }
204+
143205 public function testApiKeySignup () {
144206 $ page = $ this ->post_page ('key ' );
145207 $ this ->assertStringContainsString ('Subscribe to a plan ' , $ page );
0 commit comments