-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathGenerateLandmarkLinkTest.php
More file actions
264 lines (215 loc) · 9.27 KB
/
Copy pathGenerateLandmarkLinkTest.php
File metadata and controls
264 lines (215 loc) · 9.27 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
<?php
/**
* Class GenerateLandmarkLinkTest
*
* @package Accessibility_Checker
*/
/**
* Test case for edac_generate_landmark_link function.
*/
class GenerateLandmarkLinkTest extends WP_UnitTestCase {
/**
* Test post ID for testing.
*
* @var int
*/
private $test_post_id;
/**
* Set up test environment.
*/
public function setUp(): void {
parent::setUp();
// Create a test post.
$this->test_post_id = $this->factory->post->create(
[
'post_title' => 'Test Post',
'post_content' => 'Test content',
'post_status' => 'publish',
]
);
}
/**
* Clean up test environment.
*/
public function tearDown(): void {
// Clean up the test post.
wp_delete_post( $this->test_post_id, true );
parent::tearDown();
}
/**
* Tests the edac_generate_landmark_link function with valid landmark and selector.
*/
public function test_edac_generate_landmark_link_with_selector() {
$landmark = 'header';
$landmark_selector = 'header.site-header';
$result = edac_generate_landmark_link( $landmark, $landmark_selector, $this->test_post_id );
// Check that result contains an anchor tag.
$this->assertStringContainsString( '<a href=', $result );
$this->assertStringContainsString( 'class="edac-details-rule-records-record-landmark-link"', $result );
$this->assertStringContainsString( 'target="_blank"', $result );
$this->assertStringContainsString( '>Header</a>', $result );
// Check that the URL contains the expected query parameters.
$this->assertStringContainsString( 'edac_landmark=', $result );
$this->assertStringContainsString( 'edac_nonce=', $result );
// Check aria-label.
$this->assertStringContainsString( 'aria-label="View Header landmark on website, opens a new window"', $result );
// Verify the landmark selector is base64 encoded in the URL.
$encoded_selector = base64_encode( $landmark_selector );
$this->assertStringContainsString( "edac_landmark={$encoded_selector}", $result );
}
/**
* Tests the edac_generate_landmark_link function with only landmark (no selector).
*/
public function test_edac_generate_landmark_link_without_selector() {
$landmark = 'navigation';
$landmark_selector = '';
$result = edac_generate_landmark_link( $landmark, $landmark_selector, $this->test_post_id );
// Should return just the formatted landmark text, not a link.
$this->assertEquals( 'Navigation', $result );
$this->assertStringNotContainsString( '<a href=', $result );
$this->assertStringNotContainsString( 'edac_landmark=', $result );
}
/**
* Tests the edac_generate_landmark_link function with empty landmark.
*/
public function test_edac_generate_landmark_link_with_empty_landmark() {
$landmark = '';
$landmark_selector = 'nav.main-nav';
$result = edac_generate_landmark_link( $landmark, $landmark_selector, $this->test_post_id );
// Should return empty string.
$this->assertEquals( '', $result );
}
/**
* Tests the edac_generate_landmark_link function with custom CSS class.
*/
public function test_edac_generate_landmark_link_with_custom_css_class() {
$landmark = 'main';
$landmark_selector = 'main.content';
$custom_class = 'my-custom-landmark-class';
$result = edac_generate_landmark_link( $landmark, $landmark_selector, $this->test_post_id, $custom_class );
$this->assertStringContainsString( "class=\"{$custom_class}\"", $result );
$this->assertStringNotContainsString( 'class="edac-details-rule-records-record-landmark-link"', $result );
}
/**
* Tests the edac_generate_landmark_link function with target_blank disabled.
*/
public function test_edac_generate_landmark_link_without_target_blank() {
$landmark = 'footer';
$landmark_selector = 'footer.site-footer';
$result = edac_generate_landmark_link( $landmark, $landmark_selector, $this->test_post_id, 'edac-details-rule-records-record-landmark-link', false );
$this->assertStringContainsString( '<a href=', $result );
$this->assertStringNotContainsString( 'target="_blank"', $result );
$this->assertStringContainsString( '>Footer</a>', $result );
}
/**
* Tests the edac_generate_landmark_link function with various landmark types.
*
* @dataProvider landmark_types_data_provider
*
* @param string $landmark The landmark type.
* @param string $expected_display The expected display text.
*/
public function test_edac_generate_landmark_link_landmark_types( $landmark, $expected_display ) {
$landmark_selector = 'div.test';
$result = edac_generate_landmark_link( $landmark, $landmark_selector, $this->test_post_id );
$this->assertStringContainsString( ">{$expected_display}</a>", $result );
$this->assertStringContainsString( "View {$expected_display} landmark on website", $result );
}
/**
* Data provider for landmark types testing.
*
* @return array
*/
public function landmark_types_data_provider() {
return [
'header landmark' => [ 'header', 'Header' ],
'navigation landmark' => [ 'navigation', 'Navigation' ],
'main landmark' => [ 'main', 'Main' ],
'footer landmark' => [ 'footer', 'Footer' ],
'aside landmark' => [ 'aside', 'Aside' ],
'section landmark' => [ 'section', 'Section' ],
'search landmark' => [ 'search', 'Search' ],
'banner landmark' => [ 'banner', 'Banner' ],
'contentinfo landmark' => [ 'contentinfo', 'Contentinfo' ],
'complementary landmark' => [ 'complementary', 'Complementary' ],
'form landmark' => [ 'form', 'Form' ],
'region landmark' => [ 'region', 'Region' ],
];
}
/**
* Tests the edac_generate_landmark_link function with special characters in landmark.
*/
public function test_edac_generate_landmark_link_with_special_characters() {
$landmark = '<script>alert("xss")</script>';
$landmark_selector = 'div.test';
$result = edac_generate_landmark_link( $landmark, $landmark_selector, $this->test_post_id );
// Should be properly escaped.
$this->assertStringNotContainsString( '<script>', $result );
$this->assertStringNotContainsString( 'alert("xss")', $result );
$this->assertStringContainsString( '<Script>Alert("Xss")</Script>', $result );
}
/**
* Tests the edac_generate_landmark_link function with special characters in selector.
*/
public function test_edac_generate_landmark_link_with_special_selector() {
$landmark = 'main';
$landmark_selector = 'div[data-test="value with spaces & symbols"]';
$result = edac_generate_landmark_link( $landmark, $landmark_selector, $this->test_post_id );
// Should contain encoded selector.
$encoded_selector = base64_encode( $landmark_selector );
$this->assertStringContainsString( "edac_landmark={$encoded_selector}", $result );
$this->assertStringContainsString( '>Main</a>', $result );
}
/**
* Tests that the nonce is properly generated.
*/
public function test_edac_generate_landmark_link_nonce_generation() {
$landmark = 'header';
$landmark_selector = 'header.test';
// Generate the link twice.
$result1 = edac_generate_landmark_link( $landmark, $landmark_selector, $this->test_post_id );
$result2 = edac_generate_landmark_link( $landmark, $landmark_selector, $this->test_post_id );
// Both should contain nonce parameters.
$this->assertStringContainsString( 'edac_nonce=', $result1 );
$this->assertStringContainsString( 'edac_nonce=', $result2 );
// Extract nonces (basic check that they're generated).
preg_match( '/edac_nonce=([^&"]+)/', $result1, $matches1 );
preg_match( '/edac_nonce=([^&"]+)/', $result2, $matches2 );
$this->assertNotEmpty( $matches1[1] );
$this->assertNotEmpty( $matches2[1] );
// Verify nonces are valid for the 'edac_highlight' action.
$this->assertTrue( wp_verify_nonce( $matches1[1], 'edac_highlight' ) !== false );
$this->assertTrue( wp_verify_nonce( $matches2[1], 'edac_highlight' ) !== false );
}
/**
* Tests the edac_generate_landmark_link function with null values.
*/
public function test_edac_generate_landmark_link_with_null_values() {
$result = edac_generate_landmark_link( null, null, $this->test_post_id );
$this->assertEquals( '', $result );
$result = edac_generate_landmark_link( 'main', null, $this->test_post_id );
$this->assertEquals( 'Main', $result );
$result = edac_generate_landmark_link( null, 'div.test', $this->test_post_id );
$this->assertEquals( '', $result );
}
/**
* Tests the edac_generate_landmark_link function URL structure.
*/
public function test_edac_generate_landmark_link_url_structure() {
$landmark = 'navigation';
$landmark_selector = 'nav.primary';
$result = edac_generate_landmark_link( $landmark, $landmark_selector, $this->test_post_id );
// Extract the href value.
preg_match( '/href="([^"]+)"/', $result, $matches );
$this->assertNotEmpty( $matches[1] );
$url = $matches[1];
$parsed_url = wp_parse_url( html_entity_decode( $url ) );
// Should contain the post permalink structure.
$this->assertNotEmpty( $parsed_url['query'] );
// Parse query string.
parse_str( $parsed_url['query'], $query_params );
$this->assertArrayHasKey( 'edac_landmark', $query_params );
$this->assertArrayHasKey( 'edac_nonce', $query_params );
$this->assertEquals( base64_encode( $landmark_selector ), $query_params['edac_landmark'] );
}
}