-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathtest-tracking.php
214 lines (177 loc) · 8.23 KB
/
test-tracking.php
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
<?php
/**
* Test Tracking.
*
* @package Newspack_Newsletters
*/
use Newspack_Newsletters\Tracking\Pixel;
use Newspack_Newsletters\Tracking\Click;
/**
* Newsletters Tracking Test.
*/
class Newsletters_Tracking_Test extends WP_UnitTestCase {
/**
* Test tracking pixel.
*/
public function test_tracking_pixel() {
$post_id = $this->factory->post->create( [ 'post_type' => \Newspack_Newsletters::NEWSPACK_NEWSLETTERS_CPT ] );
$post = \get_post( $post_id );
ob_start();
do_action( 'newspack_newsletters_editor_mjml_body', $post );
$mjml_body = ob_get_clean();
$this->assertMatchesRegularExpression( '/\/np-newsletters.pixel.php\?id=' . $post_id . '/', $mjml_body );
// Fetch the tracking pixel URL from body.
$pattern = '/src="([^"]*np-newsletters-pixel.php[^"]*)"/i';
$matches = [];
preg_match( $pattern, $mjml_body, $matches );
$pixel_url = html_entity_decode( $matches[1] );
$parsed_url = \wp_parse_url( $pixel_url );
$args = \wp_parse_args( $parsed_url['query'] );
$this->assertEquals( $post_id, intval( $args['id'] ) );
$this->assertEquals( get_post_meta( $post_id, 'tracking_id', true ), $args['tid'] );
$this->assertArrayHasKey( 'em', $args );
// Call the tracking pixel.
Pixel::track_seen( $args['id'], $args['tid'], '[email protected]' );
// Assert seen once.
$seen = \get_post_meta( $post_id, 'tracking_pixel_seen', true );
$this->assertEquals( 1, $seen );
// Call the tracking pixel again.
Pixel::track_seen( $args['id'], $args['tid'], '[email protected]' );
// Assert seen twice.
$seen = \get_post_meta( $post_id, 'tracking_pixel_seen', true );
$this->assertEquals( 2, $seen );
}
/**
* Test tracking click.
*/
public function test_tracking_click() {
$content = "<!-- wp:paragraph -->\n<p><a href=\"https://google.com\">Link</a><\/p>\n<!-- \/wp:paragraph -->";
$post_id = $this->factory->post->create(
[
'post_type' => \Newspack_Newsletters::NEWSPACK_NEWSLETTERS_CPT,
'post_title' => 'A newsletter with link.',
'post_content' => $content,
]
);
// Ensure the newspack_email_html meta is set.
update_post_meta( $post_id, 'newspack_email_html', $content );
$post = \get_post( $post_id );
$rendered = Newspack_Newsletters_Renderer::post_to_mjml_components( $post );
// Fetch the link URL from body.
$pattern = '/href="([^"]*)"/i';
$matches = [];
preg_match( $pattern, $rendered, $matches );
$link_url = $matches[1];
$parsed_url = \wp_parse_url( $link_url );
$args = \wp_parse_args( $parsed_url['query'] );
$this->assertEquals( $post_id, intval( $args['id'] ) );
$this->assertArrayHasKey( 'em', $args );
$parsed_destination_url = \wp_parse_url( $args['url'] );
$this->assertEquals( 'https', $parsed_destination_url['scheme'] );
$this->assertEquals( 'google.com', $parsed_destination_url['host'] );
// Trigger the click handled.
$_GET['np_newsletters_click'] = 1;
$_GET['id'] = $post_id;
$_GET['em'] = '[email protected]';
$_GET['url'] = $args['url'];
Click::handle_click( false );
// Assert clicked once.
$clicks = \get_post_meta( $post_id, 'tracking_clicks', true );
$this->assertEquals( 1, $clicks );
// Trigger the click handled again.
Click::handle_click( false );
// Assert clicked twice.
$clicks = \get_post_meta( $post_id, 'tracking_clicks', true );
$this->assertEquals( 2, $clicks );
}
/**
* Test click tracking with a link that was not included in the newsletter.
*/
public function test_tracking_click_not_in_newsletter() {
$post_id = $this->factory->post->create(
[
'post_type' => \Newspack_Newsletters::NEWSPACK_NEWSLETTERS_CPT,
'post_title' => 'A newsletter with link.',
'post_content' => "<!-- wp:paragraph -->\n<p><a href=\"https://google.com\">Link</a><\/p>\n<!-- \/wp:paragraph -->",
]
);
$_GET['np_newsletters_click'] = 1;
$_GET['id'] = $post_id;
$_GET['url'] = 'https://mischievous.com';
try {
Click::handle_click( false );
} catch ( \Throwable $th ) {
$this->assertEquals( 'Invalid URL', $th->getMessage() );
$this->assertEquals( 400, $th->getCode() );
}
}
/**
* Test logs processing.
*/
public function test_process_logs() {
$newsletter_id = $this->factory->post->create( [ 'post_type' => \Newspack_Newsletters::NEWSPACK_NEWSLETTERS_CPT ] );
$tracking_id = 'tracking_id_1';
update_post_meta( $newsletter_id, 'tracking_id', $tracking_id );
// phpcs:disable WordPressVIPMinimum.Functions.RestrictedFunctions
// Create a temporary log file.
$log_file_path = tempnam( sys_get_temp_dir(), 'newspack_newsletters_pixel_log_' );
file_put_contents( $log_file_path, "$newsletter_id|$tracking_id|[email protected]" . PHP_EOL );
file_put_contents( $log_file_path, "$newsletter_id|$tracking_id|[email protected]" . PHP_EOL, FILE_APPEND );
update_option( 'newspack_newsletters_tracking_pixel_log_file', $log_file_path );
Pixel::process_logs();
// Check that the log file has been removed.
$this->assertFileDoesNotExist( $log_file_path );
// Check that a new log file has been created.
$new_log_file_path = get_option( 'newspack_newsletters_tracking_pixel_log_file' );
$this->assertFileExists( $new_log_file_path );
// Check that the log entries have been processed.
$this->assertEquals( 2, get_post_meta( $newsletter_id, 'tracking_pixel_seen', true ) );
// Clean up.
unlink( $new_log_file_path );
// phpcs:enable WordPressVIPMinimum.Functions.RestrictedFunctions
}
/**
* Test logs processing – log file length equals the max lines processed.
*/
public function test_process_logs_max_lines() {
$newsletter_id = $this->factory->post->create( [ 'post_type' => \Newspack_Newsletters::NEWSPACK_NEWSLETTERS_CPT ] );
$tracking_id = 'tracking_id_1';
update_post_meta( $newsletter_id, 'tracking_id', $tracking_id );
// phpcs:disable WordPressVIPMinimum.Functions.RestrictedFunctions
// Create a temporary log file.
$log_file_path = tempnam( sys_get_temp_dir(), 'newspack_newsletters_pixel_log_' );
file_put_contents( $log_file_path, "$newsletter_id|$tracking_id|[email protected]" . PHP_EOL );
file_put_contents( $log_file_path, "$newsletter_id|$tracking_id|[email protected]" . PHP_EOL, FILE_APPEND );
file_put_contents( $log_file_path, "$newsletter_id|$tracking_id|[email protected]" . PHP_EOL, FILE_APPEND );
update_option( 'newspack_newsletters_tracking_pixel_log_file', $log_file_path );
Pixel::process_logs( 3 ); // 3 lines at a time - exactly as many as there are log entries.
// Check that the log entries have been processed.
$this->assertEquals( 3, get_post_meta( $newsletter_id, 'tracking_pixel_seen', true ) );
// Clean up.
unlink( get_option( 'newspack_newsletters_tracking_pixel_log_file' ) );
// phpcs:enable WordPressVIPMinimum.Functions.RestrictedFunctions
}
/**
* Test logs processing – log file length is longer than the max lines processed.
*/
public function test_process_logs_max_lines_more() {
$newsletter_id = $this->factory->post->create( [ 'post_type' => \Newspack_Newsletters::NEWSPACK_NEWSLETTERS_CPT ] );
$tracking_id = 'tracking_id_1';
update_post_meta( $newsletter_id, 'tracking_id', $tracking_id );
// phpcs:disable WordPressVIPMinimum.Functions.RestrictedFunctions
// Create a temporary log file.
$log_file_path = tempnam( sys_get_temp_dir(), 'newspack_newsletters_pixel_log_' );
file_put_contents( $log_file_path, "$newsletter_id|$tracking_id|[email protected]" . PHP_EOL );
file_put_contents( $log_file_path, "$newsletter_id|$tracking_id|[email protected]" . PHP_EOL, FILE_APPEND );
file_put_contents( $log_file_path, "$newsletter_id|$tracking_id|[email protected]" . PHP_EOL, FILE_APPEND );
file_put_contents( $log_file_path, "$newsletter_id|$tracking_id|[email protected]" . PHP_EOL, FILE_APPEND );
file_put_contents( $log_file_path, "$newsletter_id|$tracking_id|[email protected]" . PHP_EOL, FILE_APPEND );
update_option( 'newspack_newsletters_tracking_pixel_log_file', $log_file_path );
Pixel::process_logs( 2 ); // 2 entries at a time – will have to batch the 5 log lines.
// Check that the log entries have been processed.
$this->assertEquals( 5, get_post_meta( $newsletter_id, 'tracking_pixel_seen', true ) );
// Clean up.
unlink( get_option( 'newspack_newsletters_tracking_pixel_log_file' ) );
// phpcs:enable WordPressVIPMinimum.Functions.RestrictedFunctions
}
}