-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathIntercomSettingsPageTest.php
More file actions
237 lines (198 loc) · 7.24 KB
/
Copy pathIntercomSettingsPageTest.php
File metadata and controls
237 lines (198 loc) · 7.24 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
<?php
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\MockObject\MockObject;
class IntercomSettingsPageTest extends TestCase
{
private $settings;
private $intercomSettingsPage;
private $originalGet;
protected function setUp(): void
{
parent::setUp();
// Store original $_GET
$this->originalGet = $_GET;
// Default settings
$this->settings = [
'app_id' => 'test_app_id',
'secret' => 'test_secret',
'identity_verification' => false
];
// Create the IntercomSettingsPage instance
$this->intercomSettingsPage = new IntercomSettingsPage($this->settings);
}
protected function tearDown(): void
{
// Restore original $_GET
$_GET = $this->originalGet;
parent::tearDown();
}
/**
* Test the constructor properly initializes the class
*/
public function testConstructor()
{
$settings = [
'app_id' => 'test_app_id',
'secret' => 'test_secret',
'identity_verification' => true
];
$intercomSettingsPage = new IntercomSettingsPage($settings);
// Use reflection to access private properties
$reflection = new ReflectionClass($intercomSettingsPage);
$settingsProperty = $reflection->getProperty('settings');
$settingsProperty->setAccessible(true);
$stylesProperty = $reflection->getProperty('styles');
$stylesProperty->setAccessible(true);
$this->assertEquals($settings, $settingsProperty->getValue($intercomSettingsPage));
$this->assertNotEmpty($stylesProperty->getValue($intercomSettingsPage));
}
/**
* Test the dismissibleMessage method
*/
public function testDismissibleMessage()
{
$message = "Test message";
$result = $this->intercomSettingsPage->dismissibleMessage($message);
$this->assertStringContainsString('<div id="message" class="updated notice is-dismissible">', $result);
$this->assertStringContainsString('<p>' . $message . '</p>', $result);
$this->assertStringContainsString('<button type="button" class="notice-dismiss">', $result);
}
/**
* Test the getAuthUrl method
*/
public function testGetAuthUrl()
{
// Mock WordPress functions
if (!function_exists('get_site_url')) {
function get_site_url() {
return 'https://example.com';
}
}
if (!function_exists('wp_create_nonce')) {
function wp_create_nonce($action) {
return 'test_nonce';
}
}
$authUrl = $this->intercomSettingsPage->getAuthUrl();
$this->assertStringContainsString('https://wordpress_auth.intercom.io/confirm?state=', $authUrl);
$this->assertStringContainsString('https://example.com', $authUrl);
$this->assertStringContainsString('test_nonce', $authUrl);
}
/**
* Test the htmlUnclosed method with different GET parameters
*/
public function testHtmlUnclosed()
{
// Test with appId parameter
$_GET['appId'] = 'new_app_id';
$html = $this->intercomSettingsPage->htmlUnclosed();
$this->assertStringContainsString('new_app_id', $html);
$this->assertStringContainsString("We've copied your new Intercom app id below", $html);
// Test with saved parameter
$_GET = [];
$_GET['saved'] = true;
$html = $this->intercomSettingsPage->htmlUnclosed();
$this->assertStringContainsString("Your app id has been successfully saved", $html);
// Test with authenticated parameter
$_GET = [];
$_GET['authenticated'] = true;
$html = $this->intercomSettingsPage->htmlUnclosed();
$this->assertStringContainsString("You successfully authenticated with Intercom", $html);
}
/**
* Test the htmlClosed method
*/
public function testHtmlClosed()
{
$html = $this->intercomSettingsPage->htmlClosed();
$this->assertStringContainsString('</form>', $html);
$this->assertStringContainsString('Identity Verification', $html);
$this->assertStringContainsString('https://docs.intercom.com', $html);
}
/**
* Test the html method
*/
public function testHtml()
{
$html = $this->intercomSettingsPage->html();
$this->assertStringContainsString('<div class="wrap">', $html);
$this->assertStringContainsString('</div>', $html);
$this->assertStringContainsString('https://code.jquery.com/jquery-2.2.0.min.js', $html);
}
/**
* Test the setStyles method with different settings
*/
public function testSetStyles()
{
// Test with identity verification enabled
$settings = [
'app_id' => 'test_app_id',
'secret' => 'test_secret',
'identity_verification' => true
];
$intercomSettingsPage = new IntercomSettingsPage($settings);
$reflection = new ReflectionClass($intercomSettingsPage);
$stylesProperty = $reflection->getProperty('styles');
$stylesProperty->setAccessible(true);
$styles = $stylesProperty->getValue($intercomSettingsPage);
$this->assertEquals('checked disabled', $styles['identity_verification_state']);
// Test with app_id but no secret
$settings = [
'app_id' => 'test_app_id',
'secret' => '',
'identity_verification' => false
];
$intercomSettingsPage = new IntercomSettingsPage($settings);
$styles = $stylesProperty->getValue($intercomSettingsPage);
$this->assertEquals('display: none;', $styles['app_secret_row_style']);
$this->assertEquals('', $styles['app_secret_link_style']);
// Test with appId in GET
$_GET['appId'] = 'new_app_id';
$intercomSettingsPage = new IntercomSettingsPage($settings);
$styles = $stylesProperty->getValue($intercomSettingsPage);
$this->assertEquals('readonly', $styles['app_id_state']);
$this->assertEquals('cta__email', $styles['app_id_class']);
$this->assertEquals('', $styles['button_submit_style']);
$this->assertEquals('display: none;', $styles['app_id_copy_hidden']);
// Test with saved in GET
$_GET = [];
$_GET['saved'] = true;
$intercomSettingsPage = new IntercomSettingsPage($settings);
$styles = $stylesProperty->getValue($intercomSettingsPage);
$this->assertEquals('display: none;', $styles['app_id_copy_hidden']);
$this->assertEquals('', $styles['app_id_saved_title']);
// Test with empty app_id
$settings = [
'app_id' => '',
'secret' => 'test_secret',
'identity_verification' => false
];
$intercomSettingsPage = new IntercomSettingsPage($settings);
$styles = $stylesProperty->getValue($intercomSettingsPage);
$this->assertEquals('display: none;', $styles['app_id_row_style']);
$this->assertEquals('', $styles['app_id_link_style']);
}
/**
* Test the getSettings method
*/
public function testGetSettings()
{
$reflection = new ReflectionClass($this->intercomSettingsPage);
$method = $reflection->getMethod('getSettings');
$method->setAccessible(true);
$settings = $method->invoke($this->intercomSettingsPage);
$this->assertEquals($this->settings, $settings);
}
/**
* Test the getStyles method
*/
public function testGetStyles()
{
$reflection = new ReflectionClass($this->intercomSettingsPage);
$method = $reflection->getMethod('getStyles');
$method->setAccessible(true);
$styles = $method->invoke($this->intercomSettingsPage);
$this->assertNotEmpty($styles);
$this->assertIsArray($styles);
}
}