Skip to content

Commit 74d9f57

Browse files
Fix 624 - updated the custom hook to use argument and fix minor issues (#629)
1 parent 7dc9249 commit 74d9f57

File tree

4 files changed

+105
-22
lines changed

4 files changed

+105
-22
lines changed

apigee_edge.api.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,12 @@ function hook_apigee_edge_app_listing_page_title_alter(\Drupal\Core\StringTransl
3737
}
3838

3939
/**
40-
* Alters the user agent string.
40+
* Extends the default user-agent information sent by the API client.
41+
*
42+
* @param array $user_agent_parts
43+
* The user agent string.
4144
*/
42-
function hook_user_agent_string_alter() {
45+
function hook_apigee_edge_user_agent_string_alter(array &$user_agent_parts): void {
4346
}
4447

4548
/**

apigee_edge.module

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@ use Drupal\Core\Breadcrumb\Breadcrumb;
4646
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
4747
use Drupal\Core\Entity\EntityInterface;
4848
use Drupal\Core\Entity\EntityTypeInterface;
49-
use Drupal\Core\Extension\InfoParser;
50-
use Drupal\Core\Extension\InfoParserInterface;
5149
use Drupal\Core\Field\BaseFieldDefinition;
5250
use Drupal\Core\Form\FormStateInterface;
5351
use Drupal\Core\Link;
@@ -271,21 +269,6 @@ function apigee_edge_system_breadcrumb_alter(Breadcrumb &$breadcrumb, RouteMatch
271269
}
272270
}
273271

274-
/**
275-
* Implements hook_apigee_edge_user_agent_string_alter().
276-
*/
277-
function apigee_edge_user_agent_string_alter() {
278-
$infoParser = new InfoParser();
279-
$edge_module_info = $infoParser->parse(\Drupal::service('module_handler')->getModule('apigee_edge')->getPathname());
280-
281-
if (!isset($edge_module_info['version'])) {
282-
$edge_module_info['version'] = '2.x-dev';
283-
}
284-
$edge_module_info = $edge_module_info['name'] . '/' . $edge_module_info['version'];
285-
286-
return $edge_module_info;
287-
}
288-
289272
/**
290273
* Implements hook_entity_base_field_info().
291274
*/

src/SDKConnector.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -250,10 +250,19 @@ private function buildCredentials(KeyInterface $key): CredentialsInterface {
250250
*/
251251
protected function userAgentPrefix(): string {
252252
if (NULL === self::$userAgentPrefix) {
253-
$userAgent = \Drupal::moduleHandler()->invokeAll('user_agent_string_alter');
254-
$userAgent = implode('; ', $userAgent);
253+
// apigee_edge module info.
254+
$edge_module_info = $this->infoParser->parse($this->moduleHandler->getModule('apigee_edge')->getPathname());
255+
if (!isset($edge_module_info['version'])) {
256+
$edge_module_info['version'] = '2.x-dev';
257+
}
258+
$user_agent_parts[] = $edge_module_info['name'] . '/' . $edge_module_info['version'];
259+
$user_agent_parts[] = 'Drupal/' . \Drupal::VERSION;
260+
261+
// Get info from other modules.
262+
$userAgent = $this->moduleHandler->invokeAll('apigee_edge_user_agent_string_alter', [&$user_agent_parts]);
263+
$userAgent = !empty($userAgent) ? implode('; ', $userAgent) : implode('; ', $user_agent_parts);
255264

256-
self::$userAgentPrefix = $userAgent . '; Drupal/' . \Drupal::VERSION;
265+
self::$userAgentPrefix = $userAgent;
257266
}
258267

259268
return self::$userAgentPrefix;

tests/src/Kernel/UserAgentTest.php

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
3+
/*
4+
* Copyright 2022 Google Inc.
5+
*
6+
* This program is free software; you can redistribute it and/or modify it under
7+
* the terms of the GNU General Public License version 2 as published by the
8+
* Free Software Foundation.
9+
*
10+
* This program is distributed in the hope that it will be useful, but WITHOUT
11+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12+
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
13+
* License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License along
16+
* with this program; if not, write to the Free Software Foundation, Inc., 51
17+
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18+
*/
19+
20+
namespace Drupal\Tests\apigee_edge\Kernel;
21+
22+
use Drupal\KernelTests\KernelTestBase;
23+
use Drupal\Core\Extension\InfoParser;
24+
use Drupal\Core\Extension\InfoParserInterface;
25+
26+
/**
27+
* Apigee Useragent tests.
28+
*
29+
* @group apigee_edge
30+
* @group apigee_edge_kernel
31+
*/
32+
class UserAgentTest extends KernelTestBase {
33+
34+
/**
35+
* Apigee Edge module info.
36+
*
37+
* @var string
38+
*/
39+
protected $edgeModuleInfo;
40+
41+
/**
42+
* The info parser.
43+
*
44+
* @var \Drupal\Core\Extension\InfoParserInterface
45+
*/
46+
protected $infoParser;
47+
48+
/**
49+
* {@inheritdoc}
50+
*/
51+
protected static $modules = [
52+
'file',
53+
'system',
54+
];
55+
56+
/**
57+
* {@inheritdoc}
58+
*/
59+
protected function setUp() {
60+
parent::setUp();
61+
62+
// Install the apigee edge module.
63+
\Drupal::service('module_installer')->install(['apigee_edge']);
64+
}
65+
66+
/**
67+
* Test the user agent data without monetization module.
68+
*
69+
* @throws \Exception
70+
*/
71+
public function testUserAgentWithoutMonetization() {
72+
// apigee_edge module info.
73+
$infoParser = new InfoParser();
74+
$this->edgeModuleInfo = $infoParser->parse(\Drupal::service('module_handler')->getModule('apigee_edge')->getPathname());
75+
if (!isset($this->edgeModuleInfo['version'])) {
76+
$this->edgeModuleInfo['version'] = '2.x-dev';
77+
}
78+
79+
$user_agent_parts[] = $this->edgeModuleInfo['name'] . '/' . $this->edgeModuleInfo['version'];
80+
$user_agent_parts[] = 'Drupal/' . \Drupal::VERSION;
81+
82+
\Drupal::moduleHandler()->invokeAll('apigee_edge_user_agent_string_alter', [&$user_agent_parts]);
83+
$userAgentPrefix = implode('; ', $user_agent_parts);
84+
85+
$this->assertSame($userAgentPrefix, 'Apigee Edge/2.x-dev;' . ' Drupal/' . \Drupal::VERSION);
86+
}
87+
88+
}

0 commit comments

Comments
 (0)