-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathtest-tracking.php
96 lines (80 loc) · 3.12 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
<?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=1&id=' . $post_id . '/', $mjml_body );
// Fetch the tracking pixel URL from body.
$pattern = '/src="([^"]*np_newsletters_pixel[^"]*)"/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() {
$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 -->",
]
);
$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'] );
// Manually track the click.
Click::track_click( $args['id'], '[email protected]', $args['url'] );
// Assert clicked once.
$clicks = \get_post_meta( $post_id, 'tracking_clicks', true );
$this->assertEquals( 1, $clicks );
// Manually track the click again.
Click::track_click( $args['id'], '[email protected]', $args['url'] );
// Assert clicked twice.
$clicks = \get_post_meta( $post_id, 'tracking_clicks', true );
$this->assertEquals( 2, $clicks );
}
}