1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package wpmautic\tests |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Test Mautic Script injection |
8
|
|
|
*/ |
9
|
|
|
class ScriptInjectionTest extends WP_UnitTestCase |
|
|
|
|
10
|
|
|
{ |
11
|
|
|
private function renderRawPage($header = true, $footer = true) |
12
|
|
|
{ |
13
|
|
|
$this->setOutputCallback(create_function('', '')); |
|
|
|
|
14
|
|
|
if (true === $header) { |
15
|
|
|
wp_head(); |
16
|
|
|
} |
17
|
|
|
if (true === $footer) { |
18
|
|
|
wp_footer(); |
19
|
|
|
} |
20
|
|
|
return $this->getActualOutput(); |
|
|
|
|
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function setUp() |
24
|
|
|
{ |
25
|
|
|
parent::setUp(); |
26
|
|
|
|
27
|
|
|
remove_action('wp_head', 'wpmautic_inject_script'); |
28
|
|
|
remove_action('wp_footer', 'wpmautic_inject_script'); |
29
|
|
|
remove_action('wp_footer', 'wpmautic_inject_noscript'); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function test_script_is_not_injected_when_base_url_is_empty() |
33
|
|
|
{ |
34
|
|
|
$output = $this->renderRawPage(); |
35
|
|
|
|
36
|
|
|
$this->assertNotContains(sprintf("(window,document,'script','/mtc.js','mt')"), $output); |
|
|
|
|
37
|
|
|
$this->assertNotContains("['MauticTrackingObject']", $output); |
|
|
|
|
38
|
|
|
$this->assertNotContains("mt('send', 'pageview')", $output); |
|
|
|
|
39
|
|
|
$this->assertNotContains("/mtracking.gif?d=", $output); |
|
|
|
|
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function test_script_is_injected_with_valid_base_url() |
43
|
|
|
{ |
44
|
|
|
$base_url = 'http://example.com'; |
45
|
|
|
update_option('wpmautic_options', array('base_url' => $base_url)); |
46
|
|
|
do_action('plugins_loaded'); |
47
|
|
|
|
48
|
|
|
$output = $this->renderRawPage(); |
49
|
|
|
|
50
|
|
|
$this->assertContains(sprintf("(window,document,'script','{$base_url}/mtc.js','mt')"), $output); |
|
|
|
|
51
|
|
|
$this->assertContains("['MauticTrackingObject']", $output); |
|
|
|
|
52
|
|
|
$this->assertContains("mt('send', 'pageview')", $output); |
|
|
|
|
53
|
|
|
$this->assertContains($base_url."/mtracking.gif?d=", $output); |
|
|
|
|
54
|
|
|
} |
55
|
|
|
|
56
|
|
View Code Duplication |
public function test_script_is_not_injected_in_footer_by_default() |
|
|
|
|
57
|
|
|
{ |
58
|
|
|
$base_url = 'http://example.com'; |
59
|
|
|
update_option('wpmautic_options', array('base_url' => $base_url)); |
60
|
|
|
do_action('plugins_loaded'); |
61
|
|
|
|
62
|
|
|
$output = $this->renderRawPage(false, true); |
63
|
|
|
|
64
|
|
|
$this->assertNotContains(sprintf("(window,document,'script','{$base_url}/mtc.js','mt')"), $output); |
|
|
|
|
65
|
|
|
$this->assertNotContains("['MauticTrackingObject']", $output); |
|
|
|
|
66
|
|
|
$this->assertNotContains("mt('send', 'pageview')", $output); |
|
|
|
|
67
|
|
|
|
68
|
|
|
//Fallback activated and <noscript> is always injected in footer ! |
69
|
|
|
$this->assertContains(sprintf("%s/mtracking.gif?d=", $base_url), $output); |
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
|
View Code Duplication |
public function test_script_is_injected_in_footer_when_requested() |
|
|
|
|
73
|
|
|
{ |
74
|
|
|
$base_url = 'http://example.com'; |
75
|
|
|
update_option('wpmautic_options', array('base_url' => $base_url, 'script_location' => 'footer')); |
76
|
|
|
do_action('plugins_loaded'); |
77
|
|
|
|
78
|
|
|
$output = $this->renderRawPage(false, true); |
79
|
|
|
|
80
|
|
|
$this->assertContains(sprintf("(window,document,'script','{$base_url}/mtc.js','mt')"), $output); |
|
|
|
|
81
|
|
|
$this->assertContains("['MauticTrackingObject']", $output); |
|
|
|
|
82
|
|
|
$this->assertContains("mt('send', 'pageview')", $output); |
|
|
|
|
83
|
|
|
$this->assertContains(sprintf("%s/mtracking.gif?d=", $base_url), $output); |
|
|
|
|
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function test_tracking_image_is_not_injected_when_disabled() |
87
|
|
|
{ |
88
|
|
|
$base_url = 'http://example.com'; |
89
|
|
|
update_option('wpmautic_options', array( |
90
|
|
|
'base_url' => $base_url, |
91
|
|
|
'fallback_activated' => false |
92
|
|
|
)); |
93
|
|
|
do_action('plugins_loaded'); |
94
|
|
|
|
95
|
|
|
$output = $this->renderRawPage(); |
96
|
|
|
|
97
|
|
|
$this->assertNotContains("/mtracking.gif?d=", $output); |
|
|
|
|
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function test_tracking_image_payload_is_valid() |
101
|
|
|
{ |
102
|
|
|
$base_url = 'http://example.com'; |
103
|
|
|
$this->go_to( $base_url ); |
|
|
|
|
104
|
|
|
update_option('wpmautic_options', array('base_url' => $base_url)); |
105
|
|
|
do_action('plugins_loaded'); |
106
|
|
|
|
107
|
|
|
$output = $this->renderRawPage(); |
108
|
|
|
|
109
|
|
|
$payload = rawurlencode( base64_encode( serialize( wpmautic_get_url_query() ) ) ); |
110
|
|
|
$this->assertContains(sprintf("%s/mtracking.gif?d=%s", $base_url, $payload), $output); |
|
|
|
|
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.