-
Notifications
You must be signed in to change notification settings - Fork 341
Expand file tree
/
Copy pathSettingsTest.php
More file actions
174 lines (149 loc) · 5.3 KB
/
SettingsTest.php
File metadata and controls
174 lines (149 loc) · 5.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
/**
* Class Google\Site_Kit\Tests\Modules\Analytics_4\SettingsTest
*
* @package Google\Site_Kit\Tests\Modules\Analytics_4
* @copyright 2021 Google LLC
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
* @link https://sitekit.withgoogle.com
*/
namespace Google\Site_Kit\Tests\Modules\Analytics_4;
use Google\Site_Kit\Context;
use Google\Site_Kit\Core\Permissions\Permissions;
use Google\Site_Kit\Core\Storage\Options;
use Google\Site_Kit\Modules\Analytics_4\Settings;
use Google\Site_Kit\Tests\Modules\SettingsTestCase;
/**
* @group Modules
* @group Analytics
*/
class SettingsTest extends SettingsTestCase {
const VALID_TEST_IDS = array(
'googleTagID' => 'G-XXXX',
'googleTagAccountID' => 12121,
'googleTagContainerID' => 12121,
);
/**
* Settings object.
*
* @var Settings
*/
private $settings;
/**
* Options instance.
*
* @var Options
*/
private $options;
/**
* Admin ID.
*
* @var int
*/
private $user_id;
public function set_up() {
parent::set_up();
$this->options = new Options( new Context( GOOGLESITEKIT_PLUGIN_MAIN_FILE ) );
$this->settings = new Settings( $this->options );
$this->user_id = $this->factory()->user->create( array( 'role' => 'administrator' ) );
wp_set_current_user( $this->user_id );
}
public function test_get_default() {
$this->settings->register();
$this->assertEqualSetsWithIndex(
array(
'accountID' => '',
'adsConversionID' => '',
'propertyID' => '',
'webDataStreamID' => '',
'measurementID' => '',
'trackingDisabled' => array( 'loggedinUsers' ),
'useSnippet' => true,
'ownerID' => 0,
'googleTagID' => '',
'googleTagAccountID' => '',
'googleTagContainerID' => '',
'googleTagContainerDestinationIDs' => null,
'googleTagLastSyncedAtMs' => 0,
'availableCustomDimensions' => null,
'propertyCreateTime' => 0,
'adSenseLinked' => false,
'adSenseLinkedLastSyncedAt' => 0,
'adsLinked' => false,
'adsLinkedLastSyncedAt' => 0,
'detectedEvents' => array(),
'lostConversionEventsLastUpdateAt' => 0,
'newConversionEventsLastUpdateAt' => 0,
),
get_option( Settings::OPTION ),
'Analytics 4 default settings should match expected values.'
);
}
public function data_tag_ids() {
return array(
'googleTagID is valid G-XXXX string' => array( 'googleTagID', 'G-XXXX', 'G-XXXX' ),
'googleTagID is valid GT-XXXX string' => array( 'googleTagID', 'GT-XXXX', 'GT-XXXX' ),
'googleTagID is valid AW-XXXX string' => array( 'googleTagID', 'AW-XXXX', 'AW-XXXX' ),
'googleTagAccountID is valid numeric string' => array( 'googleTagAccountID', 12121, 12121 ),
'googleTagContainerID is valid numeric string' => array( 'googleTagContainerID', 12121, 12121 ),
'googleTagID is invalid string' => array( 'googleTagID', 'xxxx', '' ),
'googleTagID is invalid number' => array( 'googleTagID', 12121, '' ),
'googleTagAccountID is invalid string' => array( 'googleTagAccountID', 'xxxx', '' ),
'googleTagAccountID is invalid number' => array( 'googleTagAccountID', 0, '' ),
'googleTagContainerID is invalid string' => array( 'googleTagContainerID', 'xxxx', '' ),
'googleTagContainerID is invalid number' => array( 'googleTagContainerID', 0, '' ),
);
}
/**
* @dataProvider data_tag_ids
*/
public function test_google_tag_ids( $tag, $id, $expected ) {
$this->settings->register();
$options_key = $this->get_option_name();
delete_option( $options_key );
$options = $this->settings->get();
$options[ $tag ] = $id;
$this->settings->set( $options );
$options = get_option( $options_key );
$this->assertEquals( $expected, $options[ $tag ], 'Sanitized Google Tag field should match expected.' );
}
/**
* @inheritDoc
*/
protected function get_option_name() {
return Settings::OPTION;
}
/**
* @dataProvider data_owned_keys
*/
public function test_owner_id_is_set_in_settings_when_owned_keys_are_changed( $property_name, $property_value ) {
delete_option( $this->get_option_name() );
// Ensure admin user has Permissions::MANAGE_OPTIONS cap regardless of authentication.
add_filter(
'map_meta_cap',
function ( $caps, $cap ) {
if ( Permissions::MANAGE_OPTIONS === $cap ) {
return array( 'manage_options' );
}
return $caps;
},
99,
2
);
$this->settings->register();
$this->settings->merge( array( $property_name => $property_value ) );
$this->assertEquals( $this->user_id, $this->settings->get()['ownerID'], 'ownerID should be set to current admin when owned keys change.' );
}
public function data_owned_keys() {
$tests = array();
$keys = ( new Settings( new Options( new Context( GOOGLESITEKIT_PLUGIN_MAIN_FILE ) ) ) )->get_owned_keys();
foreach ( $keys as $key ) {
$value = '12345';
if ( array_key_exists( $key, self::VALID_TEST_IDS ) ) {
$value = self::VALID_TEST_IDS[ $key ];
}
$tests[ $key ] = array( $key, $value );
}
return $tests;
}
}