Skip to content

Commit 070ce14

Browse files
committed
Added tests for individual rules. Updated placeholder text for excluded queries field.
1 parent 6a45575 commit 070ce14

File tree

7 files changed

+534
-1
lines changed

7 files changed

+534
-1
lines changed

plugins/wpgraphql-logging/src/Admin/Settings/Fields/Tab/BasicConfigurationTab.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public function get_fields(): array {
9797
__( 'Exclude Queries', 'wpgraphql-logging' ),
9898
'',
9999
__( 'Comma-separated list of GraphQL query names to exclude from logging.', 'wpgraphql-logging' ),
100-
__( 'e.g., __schema,SeedNode,__typename', 'wpgraphql-logging' )
100+
__( 'e.g., __schema,GetSeedNode', 'wpgraphql-logging' )
101101
);
102102

103103
$fields[ self::ADMIN_USER_LOGGING ] = new CheckboxField(
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
6+
namespace WPGraphQL\Logging\Tests\Logging\Rules;
7+
8+
use lucatume\WPBrowser\TestCase\WPTestCase;
9+
use WPGraphQL\Logging\Admin\Settings\Fields\Tab\BasicConfigurationTab;
10+
use WPGraphQL\Logging\Logger\Rules\AdminUserRule;
11+
12+
13+
/**
14+
* Test class for AdminUserRule.
15+
*
16+
* @package WPGraphQL\Logging
17+
*
18+
* @since 0.0.1
19+
*/
20+
class AdminUserRuleTest extends WPTestCase {
21+
22+
private AdminUserRule $rule;
23+
24+
public function setUp(): void {
25+
parent::setUp();
26+
$this->rule = new AdminUserRule();
27+
}
28+
29+
public function set_admin_user() {
30+
$user_id = $this->factory()->user->create(['role' => 'administrator']);
31+
wp_set_current_user($user_id);
32+
}
33+
34+
public function test_get_name_returns_correct_name(): void {
35+
$this->assertEquals('admin_user_rule', $this->rule->get_name());
36+
}
37+
38+
public function test_passes_when_admin_user_logging_disabled(): void {
39+
$config = [
40+
BasicConfigurationTab::ADMIN_USER_LOGGING => false,
41+
];
42+
43+
$this->assertTrue($this->rule->passes($config));
44+
}
45+
46+
public function test_passes_when_admin_user_logging_config_missing(): void {
47+
$config = [];
48+
49+
$this->assertTrue($this->rule->passes($config));
50+
}
51+
52+
public function test_passes_when_admin_user_logging_enabled_and_user_can_manage_options(): void {
53+
$this->set_admin_user();
54+
55+
$config = [
56+
BasicConfigurationTab::ADMIN_USER_LOGGING => true,
57+
];
58+
59+
$this->assertTrue($this->rule->passes($config));
60+
61+
62+
$config = [
63+
BasicConfigurationTab::ADMIN_USER_LOGGING => false,
64+
];
65+
66+
$this->assertTrue($this->rule->passes($config));
67+
}
68+
69+
public function test_fails_when_admin_user_logging_enabled_and_user_cannot_manage_options(): void {
70+
$user_id = $this->factory()->user->create(['role' => 'subscriber']);
71+
wp_set_current_user($user_id);
72+
73+
$config = [
74+
BasicConfigurationTab::ADMIN_USER_LOGGING => true,
75+
];
76+
77+
$this->assertFalse($this->rule->passes($config));
78+
}
79+
80+
public function test_fails_when_admin_user_logging_enabled_and_no_user_logged_in(): void {
81+
wp_set_current_user(0);
82+
83+
$config = [
84+
BasicConfigurationTab::ADMIN_USER_LOGGING => true,
85+
];
86+
87+
$this->assertFalse($this->rule->passes($config));
88+
}
89+
90+
public function test_passes_with_query_string_parameter(): void {
91+
$config = [
92+
BasicConfigurationTab::ADMIN_USER_LOGGING => false,
93+
];
94+
95+
$this->assertTrue($this->rule->passes($config, 'query { posts { id } }'));
96+
}
97+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace WPGraphQL\Logging\Tests\Logging\Rules;
6+
7+
use lucatume\WPBrowser\TestCase\WPTestCase;
8+
use WPGraphQL\Logging\Logger\Rules\EnabledRule;
9+
use WPGraphQL\Logging\Admin\Settings\Fields\Tab\BasicConfigurationTab;
10+
11+
/**
12+
* Test cases for the EnabledRule
13+
*
14+
* @package WPGraphQL\Logging
15+
*
16+
* @since 0.0.1
17+
*/
18+
class EnabledRuleTest extends WPTestCase {
19+
20+
private EnabledRule $rule;
21+
22+
public function setUp(): void {
23+
parent::setUp();
24+
$this->rule = new EnabledRule();
25+
}
26+
27+
public function test_get_name_returns_correct_name(): void {
28+
$name = $this->rule->get_name();
29+
30+
$this->assertSame('enabled_rule', $name);
31+
}
32+
33+
public function test_passes_when_enabled_is_true(): void {
34+
$config = [
35+
BasicConfigurationTab::ENABLED => true,
36+
];
37+
38+
$result = $this->rule->passes($config);
39+
40+
$this->assertTrue($result);
41+
}
42+
43+
public function test_passes_when_enabled_is_false(): void {
44+
$config = [
45+
BasicConfigurationTab::ENABLED => false,
46+
];
47+
48+
$result = $this->rule->passes($config);
49+
50+
$this->assertFalse($result);
51+
}
52+
53+
public function test_passes_when_enabled_key_missing(): void {
54+
$config = [];
55+
56+
$result = $this->rule->passes($config);
57+
58+
$this->assertFalse($result);
59+
}
60+
61+
public function test_passes_with_truthy_values(): void {
62+
$config = [
63+
BasicConfigurationTab::ENABLED => 1,
64+
];
65+
66+
$result = $this->rule->passes($config);
67+
68+
$this->assertTrue($result);
69+
}
70+
71+
public function test_passes_with_falsy_values(): void {
72+
$config = [
73+
BasicConfigurationTab::ENABLED => 0,
74+
];
75+
76+
$result = $this->rule->passes($config);
77+
78+
$this->assertFalse($result);
79+
}
80+
81+
public function test_passes_ignores_query_string(): void {
82+
$config = [
83+
BasicConfigurationTab::ENABLED => true,
84+
];
85+
86+
$result = $this->rule->passes($config, 'query { posts { id } }');
87+
88+
$this->assertTrue($result);
89+
}
90+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace WPGraphQL\Logging\Tests\Logging\Rules;
6+
7+
use lucatume\WPBrowser\TestCase\WPTestCase;
8+
use WPGraphQL\Logging\Admin\Settings\Fields\Tab\BasicConfigurationTab;
9+
use WPGraphQL\Logging\Logger\Rules\ExcludeQueryRule;
10+
11+
12+
/**
13+
* Test cases for the ExcludeQueryRule
14+
*
15+
* @package WPGraphQL\Logging
16+
*
17+
* @since 0.0.1
18+
*/
19+
class ExcludeQueryRuleTest extends WPTestCase {
20+
21+
private ExcludeQueryRule $rule;
22+
23+
public function setUp(): void {
24+
parent::setUp();
25+
$this->rule = new ExcludeQueryRule();
26+
}
27+
28+
public function test_passes_when_no_excluded_queries_configured(): void {
29+
$config = [];
30+
$query_string = 'query { posts { nodes { id title } } }';
31+
32+
$this->assertTrue($this->rule->passes($config, $query_string));
33+
}
34+
35+
public function test_passes_when_excluded_queries_is_empty_string(): void {
36+
$config = [BasicConfigurationTab::EXCLUDE_QUERY => ''];
37+
$query_string = 'query { posts { nodes { id title } } }';
38+
39+
$this->assertTrue($this->rule->passes($config, $query_string));
40+
}
41+
42+
public function test_passes_when_query_string_is_null(): void {
43+
$config = [BasicConfigurationTab::EXCLUDE_QUERY => 'introspection'];
44+
45+
$this->assertTrue($this->rule->passes($config, null));
46+
}
47+
48+
public function test_fails_when_query_contains_excluded_term(): void {
49+
$config = [BasicConfigurationTab::EXCLUDE_QUERY => 'introspection'];
50+
$query_string = 'query IntrospectionQuery { __schema { types { name } } }';
51+
52+
$this->assertFalse($this->rule->passes($config, $query_string));
53+
}
54+
55+
public function test_fails_with_case_insensitive_matching(): void {
56+
$config = [BasicConfigurationTab::EXCLUDE_QUERY => 'INTROSPECTION'];
57+
$query_string = 'query introspectionQuery { __schema { types { name } } }';
58+
59+
$this->assertFalse($this->rule->passes($config, $query_string));
60+
}
61+
62+
public function test_handles_multiple_excluded_queries(): void {
63+
$config = [BasicConfigurationTab::EXCLUDE_QUERY => 'introspection, __schema, GetSeedNode'];
64+
65+
$this->assertFalse($this->rule->passes($config, 'query { __schema { types } }'));
66+
$this->assertTrue($this->rule->passes($config, 'query { posts { nodes { id } } }'));
67+
$this->assertFalse($this->rule->passes($config, 'query GetSeedNode { node(id: "1") { id } }'));
68+
}
69+
70+
public function test_passes_when_query_does_not_match_excluded_terms(): void {
71+
$config = [BasicConfigurationTab::EXCLUDE_QUERY => 'introspection, debug'];
72+
$query_string = 'query { posts { nodes { id title content } } }';
73+
74+
$this->assertTrue($this->rule->passes($config, $query_string));
75+
}
76+
77+
public function test_get_name_returns_correct_name(): void {
78+
$this->assertEquals('exclude_query_rule', $this->rule->get_name());
79+
}
80+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace WPGraphQL\Logging\Tests\Logging\Rules;
6+
7+
use lucatume\WPBrowser\TestCase\WPTestCase;
8+
use WPGraphQL\Logging\Logger\Rules\IpRestrictionsRule;
9+
use WPGraphQL\Logging\Admin\Settings\Fields\Tab\BasicConfigurationTab;
10+
11+
/**
12+
* Test cases for the IpRestrictionsRule
13+
*
14+
* @package WPGraphQL\Logging
15+
*
16+
* @since 0.0.1
17+
*/
18+
class IpRestrictionsRuleTest extends WPTestCase {
19+
20+
private IpRestrictionsRule $rule;
21+
22+
public function setUp(): void {
23+
parent::setUp();
24+
$this->rule = new IpRestrictionsRule();
25+
}
26+
27+
public function tearDown(): void {
28+
unset($_SERVER['REMOTE_ADDR']);
29+
parent::tearDown();
30+
}
31+
32+
public function test_passes_when_no_ip_restrictions_configured(): void {
33+
$config = [];
34+
$this->assertTrue($this->rule->passes($config));
35+
}
36+
37+
public function test_passes_when_empty_ip_restrictions_configured(): void {
38+
$config = [BasicConfigurationTab::IP_RESTRICTIONS => ''];
39+
$this->assertTrue($this->rule->passes($config));
40+
}
41+
42+
public function test_fails_when_remote_addr_not_set(): void {
43+
$config = [BasicConfigurationTab::IP_RESTRICTIONS => '192.168.1.1'];
44+
unset($_SERVER['REMOTE_ADDR']);
45+
$this->assertFalse($this->rule->passes($config));
46+
}
47+
48+
public function test_fails_when_invalid_ip_in_remote_addr(): void {
49+
$config = [BasicConfigurationTab::IP_RESTRICTIONS => '192.168.1.1'];
50+
$_SERVER['REMOTE_ADDR'] = 'invalid-ip';
51+
$this->assertFalse($this->rule->passes($config));
52+
}
53+
54+
public function test_passes_when_ip_matches_single_allowed_ip(): void {
55+
$config = [BasicConfigurationTab::IP_RESTRICTIONS => '192.168.1.1'];
56+
$_SERVER['REMOTE_ADDR'] = '192.168.1.1';
57+
$this->assertTrue($this->rule->passes($config));
58+
}
59+
60+
public function test_fails_when_ip_does_not_match_single_allowed_ip(): void {
61+
$config = [BasicConfigurationTab::IP_RESTRICTIONS => '192.168.1.1'];
62+
$_SERVER['REMOTE_ADDR'] = '192.168.1.2';
63+
$this->assertFalse($this->rule->passes($config));
64+
}
65+
66+
public function test_passes_when_ip_matches_one_of_multiple_allowed_ips(): void {
67+
$config = [BasicConfigurationTab::IP_RESTRICTIONS => '192.168.1.1,10.0.0.1,172.16.0.1'];
68+
$_SERVER['REMOTE_ADDR'] = '10.0.0.1';
69+
$this->assertTrue($this->rule->passes($config));
70+
}
71+
72+
public function test_fails_when_ip_does_not_match_any_allowed_ips(): void {
73+
$config = [BasicConfigurationTab::IP_RESTRICTIONS => '192.168.1.1,10.0.0.1,172.16.0.1'];
74+
$_SERVER['REMOTE_ADDR'] = '203.0.113.1';
75+
$this->assertFalse($this->rule->passes($config));
76+
}
77+
78+
public function test_handles_whitespace_in_ip_list(): void {
79+
$config = [BasicConfigurationTab::IP_RESTRICTIONS => ' 192.168.1.1 , 10.0.0.1 , 172.16.0.1 '];
80+
$_SERVER['REMOTE_ADDR'] = '10.0.0.1';
81+
$this->assertTrue($this->rule->passes($config));
82+
}
83+
84+
public function test_works_with_ipv6_addresses(): void {
85+
$config = [BasicConfigurationTab::IP_RESTRICTIONS => '::1,2001:db8::1'];
86+
$_SERVER['REMOTE_ADDR'] = '::1';
87+
$this->assertTrue($this->rule->passes($config));
88+
}
89+
90+
public function test_get_name_returns_correct_identifier(): void {
91+
$this->assertEquals('ip_restrictions', $this->rule->get_name());
92+
}
93+
}

0 commit comments

Comments
 (0)