Skip to content

Commit 9272a28

Browse files
committed
MC-5723: Flaky MFTF Map Tests - Google Maps Rate Limit Reached
- Ensure API isn’t called if no key is set - Refactor preview logic to use visible as it’s more reliable - Fix console error
1 parent 2c3d275 commit 9272a28

File tree

9 files changed

+65
-35
lines changed

9 files changed

+65
-35
lines changed

app/code/Magento/PageBuilder/Block/GoogleMapsApi.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,19 @@ public function getInvalidApiKeyMessage(): \Magento\Framework\Phrase
6262
$this->_urlBuilder->getUrl('adminhtml/system_config/edit/section/cms', ['_fragment' => 'cms_pagebuilder'])
6363
);
6464
}
65+
66+
/**
67+
* Include the Google Maps library within the admin only if the API key is set
68+
*
69+
* @return bool
70+
*/
71+
public function shouldIncludeGoogleMapsLibrary(): bool
72+
{
73+
try {
74+
return $this->_appState->getAreaCode() !== \Magento\Backend\App\Area\FrontNameResolver::AREA_CODE ||
75+
$this->getApiKey();
76+
} catch (\Magento\Framework\Exception\LocalizedException $e) {
77+
return false;
78+
}
79+
}
6580
}

app/code/Magento/PageBuilder/view/adminhtml/web/css/source/form/element/_map.less

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@
2222
input[type='text'] {
2323
display: none;
2424
}
25-
26-
&.invalid-key .map-field {
27-
display: none;
28-
}
2925
}
3026

3127
.admin__field-map-locations .admin__fieldset {

app/code/Magento/PageBuilder/view/adminhtml/web/js/content-type/map/preview.js

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/code/Magento/PageBuilder/view/adminhtml/web/js/form/element/map.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ define([
99
'Magento_Ui/js/form/element/abstract',
1010
'Magento_PageBuilder/js/utils/map',
1111
'module',
12-
'Magento_PageBuilder/js/events',
13-
'googleMaps'
12+
'Magento_PageBuilder/js/events'
1413
], function (AbstractField, GoogleMap, module, events) {
1514
'use strict';
1615

@@ -49,6 +48,10 @@ define([
4948
mapOptions,
5049
latitudeLongitude;
5150

51+
if (!this.apiKeyValid()) {
52+
return;
53+
}
54+
5255
if (typeof startValue === 'string' && startValue !== '') {
5356
startValue = JSON.parse(startValue);
5457
}
@@ -65,15 +68,9 @@ define([
6568
}.bind(this));
6669

6770
// Create the map
68-
try {
69-
this.mapElement = new GoogleMap(element, [], mapOptions);
70-
} catch (e) {
71-
this.apiKeyValid(false);
72-
73-
return;
74-
}
71+
this.mapElement = new GoogleMap(element, [], mapOptions);
7572

76-
if (!this.mapElement.map) {
73+
if (!this.mapElement || !this.mapElement.map) {
7774
return;
7875
}
7976

@@ -141,6 +138,10 @@ define([
141138
* Callback after an update to map
142139
*/
143140
onUpdate: function () {
141+
if (!this.mapElement) {
142+
return;
143+
}
144+
144145
this._super();
145146
var content = this.value(),
146147
latitudeLongitude;
@@ -153,6 +154,7 @@ define([
153154
}
154155

155156
if (!this.validateCoordinate(content) ||
157+
this.mapElement &&
156158
!this.mapElement.map ||
157159
this.value() === '' ||
158160
this.value() === this.exportValue()) {

app/code/Magento/PageBuilder/view/adminhtml/web/template/content-type/map/default/preview.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
-->
77

88
<div class="pagebuilder-content-type pagebuilder-map" event="{ mouseover: onMouseOver, mouseout: onMouseOut }, mouseoverBubble: false">
9-
<div ifnot="apiKeyValid" class="google-map-auth-failure-placeholder" html="apiKeyErrorMessage"></div>
10-
<div if="apiKeyValid" attr="data.main.attributes" css="data.main.css" ko-style="data.main.style" afterRender="renderMap"></div>
9+
<div visible="!apiKeyValid()" class="google-map-auth-failure-placeholder" html="apiKeyErrorMessage"></div>
10+
<div visible="apiKeyValid" attr="data.main.attributes" css="data.main.css" ko-style="data.main.style" afterRender="renderMap"></div>
1111
<render args="getOptions().template" />
1212
</div>

app/code/Magento/PageBuilder/view/adminhtml/web/template/form/element/map.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
*/
66
-->
77

8-
<div class="admin__field admin__field-map" css="{'invalid-key': !apiKeyValid()}">
9-
<div ifnot="apiKeyValid" class="google-map-auth-failure-placeholder" html="apiKeyErrorMessage"></div>
10-
<div if="apiKeyValid" class="map-field" afterRender="renderMap"></div>
8+
<div class="admin__field admin__field-map">
9+
<div visible="!apiKeyValid()" class="google-map-auth-failure-placeholder" html="apiKeyErrorMessage"></div>
10+
<div visible="apiKeyValid" class="map-field" afterRender="renderMap"></div>
1111
<div attr="id: uid, name: inputName">
1212
<input type="text" ko-value="value().longitude" attr="name: inputName + '.longitude'"/>
1313
<input type="text" ko-value="value().latitude" attr="name: inputName + '.latitude'"/>

app/code/Magento/PageBuilder/view/adminhtml/web/ts/js/content-type/map/preview.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,13 @@ export default class Preview extends BasePreview {
3737
* @returns {void}
3838
*/
3939
public renderMap(element: Element) {
40+
if (!this.apiKeyValid()) {
41+
return;
42+
}
43+
4044
this.generateMap(element);
4145
this.element = element;
42-
if (this.mapElement.map) {
46+
if (this.mapElement && this.mapElement.map) {
4347
this.data.main.attributes.subscribe(() => {
4448
this.updateMap();
4549
});
@@ -67,11 +71,7 @@ export default class Preview extends BasePreview {
6771
options = mapData.options;
6872
}
6973

70-
try {
71-
this.mapElement = new GoogleMap(element, locations, options);
72-
} catch (e) {
73-
this.apiKeyValid(false);
74-
}
74+
this.mapElement = new GoogleMap(element, locations, options);
7575
}
7676

7777
/**

app/code/Magento/PageBuilder/view/base/templates/googlemaps.phtml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,17 @@
2626
}
2727
});
2828
</script>
29+
30+
<?php
31+
// Include the googleMaps dependency only if we have an API key set, this removes unnecessary calls to Google
32+
if ($block->shouldIncludeGoogleMapsLibrary()): ?>
33+
<script>
34+
require.config({
35+
shim: {
36+
'Magento_PageBuilder/js/utils/map': {
37+
deps: ['googleMaps']
38+
}
39+
}
40+
});
41+
</script>
42+
<?php endif; ?>

app/code/Magento/PageBuilder/view/base/web/js/utils/map.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44
*/
55

66
/**
7+
* googleMaps dependency is added within googlemaps.phtml through shim based on API key being set
8+
*
79
* @api
810
*/
911
define([
1012
'underscore',
1113
'module',
12-
'Magento_PageBuilder/js/events',
13-
'googleMaps'
14+
'Magento_PageBuilder/js/events'
1415
], function (_, module, events) {
1516
'use strict';
1617

@@ -41,9 +42,11 @@ define([
4142
var options,
4243
style;
4344

44-
// If we've previously had an API key error, throw our own error instead
45+
// If we've previously had an API key error, throw the error even again
4546
if (gmAuthFailure) {
46-
throw 'InvalidKeyMapError';
47+
events.trigger('googleMaps:authFailure');
48+
49+
return;
4750
}
4851

4952
/**

0 commit comments

Comments
 (0)