Skip to content

Commit 2bc68da

Browse files
committed
Add PHPUnit messages for test assertions.
1 parent d5053ac commit 2bc68da

14 files changed

Lines changed: 205 additions & 194 deletions

tests/phpunit/integration/Core/Consent_Mode/Consent_ModeTest.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
88
* @link https://sitekit.withgoogle.com
99
*/
10-
// phpcs:disable PHPCS.PHPUnit.RequireAssertionMessage.MissingAssertionMessage -- Ignoring assertion message rule, messages to be added in #10760
11-
12-
1310
namespace Google\Site_Kit\Tests\Core\Consent_Mode;
1411

1512
use Google\Site_Kit\Context;
@@ -71,7 +68,7 @@ public function test_renders_consent_mode_snippet_when_enabled() {
7168

7269
$output = $this->capture_action( 'wp_head' );
7370

74-
$this->assertStringContainsString( 'Google tag (gtag.js) consent mode dataLayer added by Site Kit', $output );
71+
$this->assertStringContainsString( 'Google tag (gtag.js) consent mode dataLayer added by Site Kit', $output, 'Consent mode snippet should render when enabled.' );
7572
}
7673

7774
public function test_does_not_render_consent_mode_snippet_when_disabled() {
@@ -81,15 +78,15 @@ public function test_does_not_render_consent_mode_snippet_when_disabled() {
8178

8279
$output = $this->capture_action( 'wp_head' );
8380

84-
$this->assertStringNotContainsString( 'Google tag (gtag.js) consent mode dataLayer added by Site Kit', $output );
81+
$this->assertStringNotContainsString( 'Google tag (gtag.js) consent mode dataLayer added by Site Kit', $output, 'Consent mode snippet should not render when disabled.' );
8582
}
8683

8784
public function test_register__googlesitekit_consent_mode_status() {
8885
remove_all_filters( 'googlesitekit_consent_mode_status' );
8986

9087
$this->consent_mode->register();
9188

92-
$this->assertTrue( has_filter( 'googlesitekit_consent_mode_status' ) );
89+
$this->assertTrue( has_filter( 'googlesitekit_consent_mode_status' ), 'Consent mode status filter should be registered.' );
9390
}
9491

9592
public function test_get_feature_metrics() {
@@ -103,7 +100,7 @@ public function test_get_feature_metrics() {
103100
'wp_consent_api' => 'none',
104101
),
105102
$feature_metrics,
106-
'Feature metrics should indicate that consent mode is enabled and WP Consent API is not installed.'
103+
'Feature metrics should report consent enabled without WP Consent API.'
107104
);
108105
}
109106
}

tests/phpunit/integration/Core/Consent_Mode/Consent_Mode_SettingsTest.php

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
88
* @link https://sitekit.withgoogle.com
99
*/
10-
// phpcs:disable PHPCS.PHPUnit.RequireAssertionMessage.MissingAssertionMessage -- Ignoring assertion message rule, messages to be added in #10760
11-
12-
1310
namespace Google\Site_Kit\Tests\Core\Modules;
1411

1512
use Google\Site_Kit\Context;
@@ -54,7 +51,8 @@ public function test_get_default() {
5451
'enabled' => false,
5552
'regions' => Regions::get_regions(),
5653
),
57-
$default_settings
54+
$default_settings,
55+
'Default consent mode settings should include enabled and regions.'
5856
);
5957
}
6058

@@ -150,24 +148,24 @@ public function data_consent_mode_settings() {
150148
*/
151149
public function test_get_sanitize_callback( $input, $expected ) {
152150
$this->settings->set( $input );
153-
$this->assertEqualSetsWithIndex( $expected, $this->settings->get() );
151+
$this->assertEqualSetsWithIndex( $expected, $this->settings->get(), 'Sanitized consent mode settings should match expected values.' );
154152
}
155153

156154
public function test_is_consent_mode_enabled() {
157-
$this->assertFalse( $this->settings->is_consent_mode_enabled() );
155+
$this->assertFalse( $this->settings->is_consent_mode_enabled(), 'Consent mode should be disabled by default.' );
158156

159157
$this->settings->set( array( 'enabled' => true ) );
160-
$this->assertTrue( $this->settings->is_consent_mode_enabled() );
158+
$this->assertTrue( $this->settings->is_consent_mode_enabled(), 'Consent mode should be enabled after setting enabled true.' );
161159

162160
$this->settings->set( array( 'enabled' => false ) );
163-
$this->assertFalse( $this->settings->is_consent_mode_enabled() );
161+
$this->assertFalse( $this->settings->is_consent_mode_enabled(), 'Consent mode should be disabled after setting enabled false.' );
164162
}
165163

166164
public function test_get_regions() {
167-
$this->assertEquals( Regions::get_regions(), $this->settings->get_regions() );
165+
$this->assertEquals( Regions::get_regions(), $this->settings->get_regions(), 'Consent mode regions should default to all regions.' );
168166

169167
$regions = array( 'SG', 'UA-AS' );
170168
$this->settings->set( array( 'regions' => $regions ) );
171-
$this->assertEquals( $regions, $this->settings->get_regions() );
169+
$this->assertEquals( $regions, $this->settings->get_regions(), 'Consent mode regions should match stored regions.' );
172170
}
173171
}

tests/phpunit/integration/Core/Consent_Mode/REST_Consent_Mode_ControllerTest.php

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
88
* @link https://sitekit.withgoogle.com
99
*/
10-
// phpcs:disable PHPCS.PHPUnit.RequireAssertionMessage.MissingAssertionMessage -- Ignoring assertion message rule, messages to be added in #10760
11-
12-
1310
namespace Google\Site_Kit\Tests\Core\Consent_Mode;
1411

1512
use Google\Site_Kit\Context;
@@ -88,8 +85,8 @@ public function test_register() {
8885

8986
$this->controller->register();
9087

91-
$this->assertTrue( has_filter( 'googlesitekit_rest_routes' ) );
92-
$this->assertTrue( has_filter( 'googlesitekit_apifetch_preload_paths' ) );
88+
$this->assertTrue( has_filter( 'googlesitekit_rest_routes' ), 'Consent mode REST routes should be registered.' );
89+
$this->assertTrue( has_filter( 'googlesitekit_apifetch_preload_paths' ), 'Consent mode preload paths should be registered.' );
9390
}
9491

9592
public function test_get_settings() {
@@ -108,7 +105,7 @@ public function test_get_settings() {
108105
$request = new WP_REST_Request( 'GET', '/' . REST_Routes::REST_ROOT . '/core/site/data/consent-mode' );
109106
$response = rest_get_server()->dispatch( $request );
110107

111-
$this->assertEqualSetsWithIndex( $original_settings, $response->get_data() );
108+
$this->assertEqualSetsWithIndex( $original_settings, $response->get_data(), 'Consent mode response should contain stored settings.' );
112109
}
113110

114111
public function test_get_settings__requires_authenticated_admin() {
@@ -127,7 +124,7 @@ public function test_get_settings__requires_authenticated_admin() {
127124

128125
// This admin hasn't authenticated with the Site Kit proxy service yet,
129126
// so they aren't allowed to modify Dashboard Sharing settings.
130-
$this->assertEquals( 'rest_forbidden', $response->get_data()['code'] );
127+
$this->assertEquals( 'rest_forbidden', $response->get_data()['code'], 'Settings response should forbid unauthenticated admins.' );
131128
}
132129

133130
public function test_set_settings() {
@@ -158,7 +155,7 @@ public function test_set_settings() {
158155
);
159156

160157
$response = rest_get_server()->dispatch( $request );
161-
$this->assertEqualSetsWithIndex( $changed_settings, $response->get_data() );
158+
$this->assertEqualSetsWithIndex( $changed_settings, $response->get_data(), 'Consent mode response should contain changed settings.' );
162159
}
163160

164161
public function test_set_settings__requires_authenticated_admin() {
@@ -189,7 +186,7 @@ public function test_set_settings__requires_authenticated_admin() {
189186
$response = rest_get_server()->dispatch( $request );
190187
// This admin hasn't authenticated with the Site Kit proxy service yet,
191188
// so they aren't allowed to modify Dashboard Sharing settings.
192-
$this->assertEquals( 'rest_forbidden', $response->get_data()['code'] );
189+
$this->assertEquals( 'rest_forbidden', $response->get_data()['code'], 'Settings update should forbid unauthenticated admins.' );
193190
}
194191

195192
/**
@@ -208,8 +205,8 @@ public function test_set_settings__wrong_data( $settings ) {
208205
);
209206

210207
$response = rest_get_server()->dispatch( $request );
211-
$this->assertEquals( 400, $response->get_status() );
212-
$this->assertEquals( 'rest_invalid_param', $response->get_data()['code'] );
208+
$this->assertEquals( 400, $response->get_status(), 'Invalid settings response should use bad request status.' );
209+
$this->assertEquals( 'rest_invalid_param', $response->get_data()['code'], 'Invalid settings response should include invalid param code.' );
213210
}
214211

215212
public function provider_wrong_settings_data() {
@@ -246,17 +243,17 @@ public function test_get_api_info() {
246243

247244
$response_data = $response->get_data();
248245

249-
$this->assertFalse( $response_data['hasConsentAPI'] );
250-
$this->assertIsArray( $response_data['wpConsentPlugin'] );
246+
$this->assertFalse( $response_data['hasConsentAPI'], 'Consent API info should report no consent API.' );
247+
$this->assertIsArray( $response_data['wpConsentPlugin'], 'Consent API info should include WP Consent plugin data.' );
251248

252249
$wp_consent_plugin = $response_data['wpConsentPlugin'];
253250

254251
// Plugin not installed (see mock_installed_plugins)
255-
$this->assertFalse( $wp_consent_plugin['installed'] );
252+
$this->assertFalse( $wp_consent_plugin['installed'], 'WP Consent plugin should not be installed.' );
256253
// Plugin is not installed, hence cannot be activated.
257-
$this->assertFalse( $wp_consent_plugin['activateURL'] );
254+
$this->assertFalse( $wp_consent_plugin['activateURL'], 'WP Consent plugin should not have an activate URL.' );
258255

259-
$this->assertStringStartsWith( 'http://example.org/wp-admin/update.php?action=install-plugin&plugin=wp-consent-api&_wpnonce=', $wp_consent_plugin['installURL'] );
256+
$this->assertStringStartsWith( 'http://example.org/wp-admin/update.php?action=install-plugin&plugin=wp-consent-api&_wpnonce=', $wp_consent_plugin['installURL'], 'WP Consent plugin should include an install URL.' );
260257
}
261258

262259
/**
@@ -276,16 +273,16 @@ public function test_get_api_info__multisite() {
276273

277274
$response_data = $response->get_data();
278275

279-
$this->assertFalse( $response_data['hasConsentAPI'] );
280-
$this->assertIsArray( $response_data['wpConsentPlugin'] );
276+
$this->assertFalse( $response_data['hasConsentAPI'], 'Multisite consent API info should report no consent API.' );
277+
$this->assertIsArray( $response_data['wpConsentPlugin'], 'Multisite response should include WP Consent plugin data.' );
281278

282279
$wp_consent_plugin = $response_data['wpConsentPlugin'];
283280

284-
$this->assertFalse( $wp_consent_plugin['installed'] );
281+
$this->assertFalse( $wp_consent_plugin['installed'], 'WP Consent plugin should not be installed on multisite.' );
285282

286283
// We don't expect the ability to install or activate plugins on multisite.
287-
$this->assertFalse( $wp_consent_plugin['activateURL'] );
288-
$this->assertFalse( $wp_consent_plugin['installURL'] );
284+
$this->assertFalse( $wp_consent_plugin['activateURL'], 'WP Consent plugin should not have a multisite activate URL.' );
285+
$this->assertFalse( $wp_consent_plugin['installURL'], 'WP Consent plugin should not have a multisite install URL.' );
289286
}
290287

291288
public function test_get_api_info__requires_authenticated_admin() {
@@ -296,7 +293,7 @@ public function test_get_api_info__requires_authenticated_admin() {
296293

297294
// This admin hasn't authenticated with the Site Kit proxy service yet,
298295
// so they aren't allowed to modify Dashboard Sharing settings.
299-
$this->assertEquals( 'rest_forbidden', $response->get_data()['code'] );
296+
$this->assertEquals( 'rest_forbidden', $response->get_data()['code'], 'Consent API info should forbid unauthenticated admins.' );
300297
}
301298

302299
public function test_get_ads_measurement_status() {
@@ -309,7 +306,7 @@ public function test_get_ads_measurement_status() {
309306

310307
$response_data = $response->get_data();
311308

312-
$this->assertFalse( $response_data['connected'] );
309+
$this->assertFalse( $response_data['connected'], 'Ads measurement status should default to disconnected.' );
313310
}
314311

315312
private function grant_manage_options_permission() {
@@ -341,7 +338,7 @@ public function test_get_ads_measurement_status__requires_authenticated_admin()
341338
$request = new WP_REST_Request( 'GET', '/' . REST_Routes::REST_ROOT . '/core/site/data/ads-measurement-status' );
342339
$response = rest_get_server()->dispatch( $request );
343340

344-
$this->assertEquals( 'rest_forbidden', $response->get_data()['code'] );
341+
$this->assertEquals( 'rest_forbidden', $response->get_data()['code'], 'Ads measurement status should forbid unauthenticated admins.' );
345342
}
346343

347344
public function test_get_ads_measurement_status__early_return_on_first_passing_check() {
@@ -380,10 +377,10 @@ function () use ( &$check_calls ) {
380377

381378
$response_data = $response->get_data();
382379

383-
$this->assertTrue( $response_data['connected'] );
384-
$this->assertEquals( 1, $check_calls['first'] );
385-
$this->assertEquals( 1, $check_calls['second'] );
386-
$this->assertEquals( 0, $check_calls['third'] );
380+
$this->assertTrue( $response_data['connected'], 'Ads measurement status should be connected after passing check.' );
381+
$this->assertEquals( 1, $check_calls['first'], 'First ads measurement check should run once.' );
382+
$this->assertEquals( 1, $check_calls['second'], 'Second ads measurement check should run once.' );
383+
$this->assertEquals( 0, $check_calls['third'], 'Third ads measurement check should not run.' );
387384
}
388385

389386
public function test_get_ads_measurement_status__handles_empty_checks_array() {
@@ -398,7 +395,7 @@ public function test_get_ads_measurement_status__handles_empty_checks_array() {
398395

399396
$response_data = $response->get_data();
400397

401-
$this->assertFalse( $response_data['connected'] );
398+
$this->assertFalse( $response_data['connected'], 'Ads measurement status should be disconnected with no checks.' );
402399
}
403400

404401
public function data_non_callable_checks_provider() {
@@ -429,7 +426,7 @@ function () use ( $non_callable_check ) {
429426

430427
$response_data = $response->get_data();
431428

432-
$this->assertFalse( $response_data['connected'] );
429+
$this->assertFalse( $response_data['connected'], 'Ads measurement status should ignore non-callable checks.' );
433430
}
434431

435432
public function data_non_array_filter_result_provider() {
@@ -460,6 +457,6 @@ function () use ( $non_array_value ) {
460457

461458
$response_data = $response->get_data();
462459

463-
$this->assertFalse( $response_data['connected'] );
460+
$this->assertFalse( $response_data['connected'], 'Ads measurement status should ignore non-array checks.' );
464461
}
465462
}

tests/phpunit/integration/Core/Dashboard_Sharing/View_Only_PointerTest.php

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
88
* @link https://sitekit.withgoogle.com
99
*/
10-
// phpcs:disable PHPCS.PHPUnit.RequireAssertionMessage.MissingAssertionMessage -- Ignoring assertion message rule, messages to be added in #10760
11-
12-
1310
namespace Google\Site_Kit\Tests\Core\Dashboard_Sharing;
1411

1512
use Google\Site_Kit\Context;
@@ -56,21 +53,21 @@ public function set_up() {
5653
}
5754

5855
public function test_register() {
59-
$this->assertTrue( has_filter( 'googlesitekit_admin_pointers' ) );
56+
$this->assertTrue( has_filter( 'googlesitekit_admin_pointers' ), 'View-only pointer should register admin pointers filter.' );
6057

6158
$view_only_pointer = $this->get_registered_pointer();
6259

63-
$this->assertInstanceOf( 'Google\Site_Kit\Core\Admin\Pointer', $view_only_pointer );
64-
$this->assertEquals( View_Only_Pointer::SLUG, $view_only_pointer->get_slug() );
60+
$this->assertInstanceOf( 'Google\Site_Kit\Core\Admin\Pointer', $view_only_pointer, 'Registered view-only pointer should be a pointer instance.' );
61+
$this->assertEquals( View_Only_Pointer::SLUG, $view_only_pointer->get_slug(), 'Registered view-only pointer should use expected slug.' );
6562
}
6663

6764
public function test_active_callback__wrong_hook_suffix() {
6865
$view_only_pointer = $this->get_registered_pointer();
6966

70-
$this->assertIsCallable( array( $view_only_pointer, 'is_active' ) );
67+
$this->assertIsCallable( array( $view_only_pointer, 'is_active' ), 'View-only pointer active callback should be callable.' );
7168

7269
// Should Return false because hook suffix is not index.php
73-
$this->assertFalse( $view_only_pointer->is_active( 'settings.php' ) );
70+
$this->assertFalse( $view_only_pointer->is_active( 'settings.php' ), 'View-only pointer should not be active on wrong hook suffix.' );
7471
}
7572

7673
public function test_active_callback__can_authenticate() {
@@ -81,7 +78,7 @@ public function test_active_callback__can_authenticate() {
8178
$view_only_pointer = $this->get_registered_pointer();
8279

8380
// Should Return false because current user can AUTHENTICATE.
84-
$this->assertFalse( $view_only_pointer->is_active( 'index.php' ) );
81+
$this->assertFalse( $view_only_pointer->is_active( 'index.php' ), 'View-only pointer should not be active for authenticating users.' );
8582
}
8683

8784
public function test_active_callback__can_not_view_splash() {
@@ -92,7 +89,7 @@ public function test_active_callback__can_not_view_splash() {
9289
$view_only_pointer = $this->get_registered_pointer();
9390

9491
// Should Return false because current user can not VIEW_SPLASH.
95-
$this->assertFalse( $view_only_pointer->is_active( 'index.php' ) );
92+
$this->assertFalse( $view_only_pointer->is_active( 'index.php' ), 'View-only pointer should not be active without splash access.' );
9693
}
9794

9895
public function test_active_callback__splash_not_dismissed() {
@@ -105,7 +102,7 @@ public function test_active_callback__splash_not_dismissed() {
105102
$view_only_pointer = $this->get_registered_pointer();
106103

107104
// Should Return true.
108-
$this->assertTrue( $view_only_pointer->is_active( 'index.php' ) );
105+
$this->assertTrue( $view_only_pointer->is_active( 'index.php' ), 'View-only pointer should be active before splash dismissal.' );
109106
}
110107

111108
public function test_active_callback__splash_dismissed() {
@@ -120,7 +117,7 @@ public function test_active_callback__splash_dismissed() {
120117
$this->dismissed_items->add( 'shared_dashboard_splash' );
121118

122119
// Should Return false because current user can no longer VIEW_SPLASH due to splash dismissal.
123-
$this->assertFalse( $view_only_pointer->is_active( 'index.php' ) );
120+
$this->assertFalse( $view_only_pointer->is_active( 'index.php' ), 'View-only pointer should not be active after splash dismissal.' );
124121
}
125122

126123
public function test_active_callback__pointer_dismissed() {
@@ -135,7 +132,7 @@ public function test_active_callback__pointer_dismissed() {
135132
update_user_meta( $user_id, 'dismissed_wp_pointers', View_Only_Pointer::SLUG );
136133

137134
// Should Return false because the pointer has been dismissed.
138-
$this->assertFalse( $view_only_pointer->is_active( 'index.php' ) );
135+
$this->assertFalse( $view_only_pointer->is_active( 'index.php' ), 'View-only pointer should not be active after pointer dismissal.' );
139136
}
140137

141138
public function test_active_callback__dismissed_wp_pointers_meta_is_array() {
@@ -153,7 +150,7 @@ public function test_active_callback__dismissed_wp_pointers_meta_is_array() {
153150

154151
// Should remain active because the array does not contain the pointer slug,
155152
// and must not throw a TypeError when calling explode() on the meta value.
156-
$this->assertTrue( $view_only_pointer->is_active( 'index.php' ) );
153+
$this->assertTrue( $view_only_pointer->is_active( 'index.php' ), 'View-only pointer should remain active with unrelated dismissal data.' );
157154
}
158155

159156
private function get_registered_pointer() {

0 commit comments

Comments
 (0)