Skip to content

Fix issues with retrieving google maps API key#217

Merged
nigel-bmlt merged 4 commits intobmlt-enabled:1.1.35-fixesfrom
brotskydotcom:debug-logging-exception-response-not-string
Nov 21, 2025
Merged

Fix issues with retrieving google maps API key#217
nigel-bmlt merged 4 commits intobmlt-enabled:1.1.35-fixesfrom
brotskydotcom:debug-logging-exception-response-not-string

Conversation

@brotskydotcom
Copy link

There are two issues in v1.1.34 with retrieving the Google Maps API key from the BMLT server:

  1. The debug logging that was added was trying to treat the server response as a string. This fixes that to log the body as a string, rather than the full response.
  2. The pattern being used to match the api key in the server's response isn't right for v4.x of the server. (I'm guessing it was right for v3.x.) This fix adds a pattern that matches the v4.x response as a fallback if the v3.x pattern fails.

@nigel-bmlt
Copy link
Collaborator

nice! - thank you for this. I hadn't yet tested against the 4.x version directly however this is a spot that potentially touches the UI code that would have been affected.
As a workaround in the meantime you can manually insert the google maps key into the extension configuration, this was always a clunky fallback to steal it from BMLT if we had no other option.
If you have a chance see if you can add some phpunit test cases for the pattern match - they live under tests/phpunit. You can then run phpunit straight from the root directory to pick it up.
I will create a doc soon to describe how to develop for this extension, up until now it's just been me :)

@brotskydotcom
Copy link
Author

Happy to create tests. Do you use mock servers in the testing? Or can you give me the URLs of live servers? Otherwise I can just mock some data and test against that.

@nigel-bmlt
Copy link
Collaborator

in the phpunit unit tests they are just mocked. there is an integration test suite that uses testcafe + mockoon to emulate the BMLT api, but that might take a bit more to get your head around. just do the unit ones and i can look at the integration tests as I'll create a 4.x endpoint to test against.

@brotskydotcom
Copy link
Author

Will do!

@brotskydotcom
Copy link
Author

I need some help on how to mock this, because I can't seem to mock the internal calls to authenticateRootServerv2 and getv2 but they are always throwing exceptions in the test. Is there some trick to mocking them?

Here's what I have in IntegrationTest.php:

	public function test_getGmapsKey() {
		$mock_body_v3 = '<html>...stuff...\n  "google_api_key":"test-api-key",\n...more stuff...\n</html>,';
		$mock_body_v4 = '<html>...stuff...\n  googleApiKey: \'test-api-key\',\n...more stuff...\n</html>,';
		$mock_body_bad = 'something unexpected';
		Functions\when('get_option')->justReturn('');
		Functions\when('\update_option')->justReturn(true);
		Functions\when('authenticateRootServerv2')->justReturn('ok');
		Functions\when('getv2')->justReturn('ok');

		Functions\when('\wp_remote_retrieve_body')->justReturn($mock_body_bad);
		$integration = new Integration(true, "3.0.0", 'token', time()+2000);
		$result = $integration->getGmapsKey();
		$this->assertEquals('', $result);

		Functions\when('\wp_remote_retrieve_body')->justReturn($mock_body_v3);
		$integration = new Integration(true, "3.0.0", 'token', time()+2000);
		$result = $integration->getGmapsKey();
		$this->assertEquals('test-api-key', $result);

		Functions\when('\wp_remote_retrieve_body')->justReturn($mock_body_v4);
		$integration = new Integration(true, "4.0.0", 'token', time()+2000);
		$result = $integration->getGmapsKey();
		$this->assertEquals('test-api-key', $result);
	}

@nigel-bmlt
Copy link
Collaborator

The issue is you can't mock the internal calls directly, but you can mock the subsequent wordpress calls with Functions\when.

The other issue you have (which is not your fault :) is that there's some smarts being done to mock out the authentication retrieval.

Use something like this to get around it:

		Functions\when('\get_option')->alias(function($value, $default = false) {
			if($value === 'bmltwf_bmlt_password') {
				return(json_decode('{"config":{"size":"MzI=","salt":"\/5ObzNuYZ\/Y5aoYTsr0sZw==","limit_ops":"OA==","limit_mem":"NTM2ODcwOTEy","alg":"Mg==","nonce":"VukDVzDkAaex\/jfB"},"encrypted":"fertj+qRqQrs9tC+Cc32GrXGImHMfiLyAW7sV6Xojw=="}',true));
			} elseif($value === 'bmltwf_google_maps_key' || $value === 'bmltwf_bmlt_google_maps_key') {
				return '';
			} elseif($value === 'bmltwf_bmlt_server_address') {
				return 'https://example.com/';
			}
			return 'true';
		});

The response to the get call is not a string; its body is a string.
Now we look for a pattern that matches both old and new server versions.
@brotskydotcom brotskydotcom force-pushed the debug-logging-exception-response-not-string branch from 3e03f71 to 454b333 Compare November 21, 2025 07:04
@brotskydotcom
Copy link
Author

Thanks, that was the advice I needed! Tests are now added and I think this is ready to merge.

@nigel-bmlt nigel-bmlt changed the base branch from main to 1.1.35-fixes November 21, 2025 21:51
Copy link
Collaborator

@nigel-bmlt nigel-bmlt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

awesome! thank you for this!

@nigel-bmlt nigel-bmlt merged commit eb8d935 into bmlt-enabled:1.1.35-fixes Nov 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants