Skip to content

Commit f72f33a

Browse files
committed
Refactor validation check
1 parent a4d5eaa commit f72f33a

2 files changed

Lines changed: 41 additions & 40 deletions

File tree

event/listener.php

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -137,28 +137,29 @@ public function add_googleanalytics_configs($event)
137137
*/
138138
public function validate_googleanalytics_id($event)
139139
{
140-
$input = isset($event['cfg_array']['googleanalytics_id']) ? $event['cfg_array']['googleanalytics_id'] : '';
141-
142140
// Check if the validate test is for google_analytics
143-
if ($input !== '' && $event['config_definition']['validate'] === 'googleanalytics_id')
141+
if ($event['config_definition']['validate'] !== 'googleanalytics_id' || empty($event['cfg_array']['googleanalytics_id']))
142+
{
143+
return;
144+
}
145+
146+
// Store the input and error event data
147+
$input = $event['cfg_array']['googleanalytics_id'];
148+
$error = $event['error'];
149+
150+
// Add error message if the input is not a valid Google Analytics ID
151+
if (!preg_match('/^UA-\d{4,9}-\d{1,4}$|^G-[A-Z0-9]{10}$/', $input))
144152
{
145-
// Store the error and input event data
146-
$error = $event['error'];
147-
148-
// Add error message if the input is not a valid Google Analytics ID
149-
if (!preg_match('/^UA-\d{4,9}-\d{1,4}$|^G-[A-Z0-9]{10}$/', $input))
150-
{
151-
$error[] = $this->user->lang('ACP_GOOGLEANALYTICS_ID_INVALID', $input);
152-
}
153-
154-
// Add error message if GTAG is not selected for use with a Measurement ID
155-
if (preg_match('/^G-[A-Z0-9]{10}$/', $input) && (int) $event['cfg_array']['googleanalytics_tag'] === 0)
156-
{
157-
$error[] = $this->user->lang('ACP_GOOGLEANALYTICS_TAG_INVALID', $input);
158-
}
159-
160-
// Update error event data
161-
$event['error'] = $error;
153+
$error[] = $this->user->lang('ACP_GOOGLEANALYTICS_ID_INVALID', $input);
162154
}
155+
156+
// Add error message if GTAG is not selected for use with a Measurement ID
157+
if (preg_match('/^G-[A-Z0-9]{10}$/', $input) && (int) $event['cfg_array']['googleanalytics_tag'] === 0)
158+
{
159+
$error[] = $this->user->lang('ACP_GOOGLEANALYTICS_TAG_INVALID', $input);
160+
}
161+
162+
// Update error event data
163+
$event['error'] = $error;
163164
}
164165
}

tests/event/listener_test.php

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,15 @@ protected function set_listener()
6767
public function test_construct()
6868
{
6969
$this->set_listener();
70-
$this->assertInstanceOf('\Symfony\Component\EventDispatcher\EventSubscriberInterface', $this->listener);
70+
self::assertInstanceOf('\Symfony\Component\EventDispatcher\EventSubscriberInterface', $this->listener);
7171
}
7272

7373
/**
7474
* Test the event listener is subscribing events
7575
*/
7676
public function test_getSubscribedEvents()
7777
{
78-
$this->assertEquals(array(
78+
self::assertEquals(array(
7979
'core.acp_board_config_edit_add',
8080
'core.page_header',
8181
'core.validate_config_variable',
@@ -89,7 +89,7 @@ public function test_load_google_analytics()
8989
{
9090
$this->set_listener();
9191

92-
$this->template->expects($this->once())
92+
$this->template->expects(self::once())
9393
->method('assign_vars')
9494
->with(array(
9595
'GOOGLEANALYTICS_ID' => $this->config['googleanalytics_id'],
@@ -153,13 +153,13 @@ public function test_add_googleanalytics_configs($mode, $display_vars, $expected
153153
$event_data_after = $event->get_data_filtered($event_data);
154154
foreach ($event_data as $expected)
155155
{
156-
$this->assertArrayHasKey($expected, $event_data_after);
156+
self::assertArrayHasKey($expected, $event_data_after);
157157
}
158158
extract($event_data_after);
159159

160160
$keys = array_keys($display_vars['vars']);
161161

162-
$this->assertEquals($expected_keys, $keys);
162+
self::assertEquals($expected_keys, $keys);
163163
}
164164

165165
/**
@@ -172,17 +172,17 @@ public function validate_googleanalytics_id_data()
172172
return array(
173173
array(
174174
// valid code, no error
175-
array('googleanalytics_id' => 'UA-0000-00', 'googleanalytics_tag' => 0),
175+
array('googleanalytics_id' => 'UA-1234-56', 'googleanalytics_tag' => 0),
176176
array(),
177177
),
178178
array(
179179
// valid code, no error
180-
array('googleanalytics_id' => 'UA-0000-00', 'googleanalytics_tag' => 1),
180+
array('googleanalytics_id' => 'UA-1234-56', 'googleanalytics_tag' => 1),
181181
array(),
182182
),
183183
array(
184184
// valid code, no error
185-
array('googleanalytics_id' => 'G-XXXXXXXXXX', 'googleanalytics_tag' => 1),
185+
array('googleanalytics_id' => 'G-A1B2C3D4E5', 'googleanalytics_tag' => 1),
186186
array(),
187187
),
188188
array(
@@ -191,35 +191,35 @@ public function validate_googleanalytics_id_data()
191191
array(),
192192
),
193193
array(
194-
// invalid code, no error
195-
array('googleanalytics_id' => 'G-XXXXXXXXXX', 'googleanalytics_tag' => 0),
194+
// no googleanalytics_id, no error
195+
array('foo' => 'bar'),
196+
array(),
197+
),
198+
array(
199+
// invalid code, error
200+
array('googleanalytics_id' => 'G-A1B2C3D4E5', 'googleanalytics_tag' => 0),
196201
array('ACP_GOOGLEANALYTICS_TAG_INVALID'),
197202
),
198203
array(
199204
// invalid code, error
200-
array('googleanalytics_id' => 'UA-00-00', 'googleanalytics_tag' => 1),
205+
array('googleanalytics_id' => 'UA-12-34', 'googleanalytics_tag' => 1),
201206
array('ACP_GOOGLEANALYTICS_ID_INVALID'),
202207
),
203208
array(
204209
// invalid code, error
205-
array('googleanalytics_id' => 'UA-00000-00000', 'googleanalytics_tag' => 1),
210+
array('googleanalytics_id' => 'UA-01234-56789', 'googleanalytics_tag' => 1),
206211
array('ACP_GOOGLEANALYTICS_ID_INVALID'),
207212
),
208213
array(
209214
// invalid code, error
210-
array('googleanalytics_id' => 'AU-0000-00', 'googleanalytics_tag' => 1),
215+
array('googleanalytics_id' => 'AU-1234-56', 'googleanalytics_tag' => 1),
211216
array('ACP_GOOGLEANALYTICS_ID_INVALID'),
212217
),
213218
array(
214219
// invalid code, error
215220
array('googleanalytics_id' => 'foo-bar-foo', 'googleanalytics_tag' => 1),
216221
array('ACP_GOOGLEANALYTICS_ID_INVALID'),
217222
),
218-
array(
219-
// no googleanalytics_id, no error
220-
array('foo' => 'bar'),
221-
array(),
222-
),
223223
);
224224
}
225225

@@ -246,10 +246,10 @@ public function test_validate_googleanalytics_id($cfg_array, $expected_error)
246246
$event_data_after = $event->get_data_filtered($event_data);
247247
foreach ($event_data as $expected)
248248
{
249-
$this->assertArrayHasKey($expected, $event_data_after);
249+
self::assertArrayHasKey($expected, $event_data_after);
250250
}
251251
extract($event_data_after);
252252

253-
$this->assertEquals($expected_error, $error);
253+
self::assertEquals($expected_error, $error);
254254
}
255255
}

0 commit comments

Comments
 (0)