Test Failed
Branch master (4a30b9)
by John
02:00
created

ScriptInjectionTest::renderRawPage()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 4
nop 2
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * @package wpmautic\tests
4
 */
5
6
/**
7
 * Test Mautic Script injection
8
 */
9
class ScriptInjectionTest extends WP_UnitTestCase
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
10
{
11
    private function renderRawPage($header = true, $footer = true)
12
    {
13
        $this->setOutputCallback(create_function('', ''));
0 ignored issues
show
Bug introduced by
The method setOutputCallback() does not seem to exist on object<ScriptInjectionTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Security Best Practice introduced by
The use of create_function is highly discouraged, better use a closure.

create_function can pose a great security vulnerability as it is similar to eval, and could be used for arbitrary code execution. We highly recommend to use a closure instead.

// Instead of
$function = create_function('$a, $b', 'return $a + $b');

// Better use
$function = function($a, $b) { return $a + $b; }
Loading history...
14
        if (true === $header) {
15
            wp_head();
16
        }
17
        if (true === $footer) {
18
            wp_footer();
19
        }
20
        return $this->getActualOutput();
0 ignored issues
show
Bug introduced by
The method getActualOutput() does not seem to exist on object<ScriptInjectionTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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);
0 ignored issues
show
Bug introduced by
The method assertNotContains() does not seem to exist on object<ScriptInjectionTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
37
        $this->assertNotContains("['MauticTrackingObject']", $output);
0 ignored issues
show
Bug introduced by
The method assertNotContains() does not seem to exist on object<ScriptInjectionTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
38
        $this->assertNotContains("mt('send', 'pageview')", $output);
0 ignored issues
show
Bug introduced by
The method assertNotContains() does not seem to exist on object<ScriptInjectionTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
39
        $this->assertNotContains("/mtracking.gif?d=", $output);
0 ignored issues
show
Bug introduced by
The method assertNotContains() does not seem to exist on object<ScriptInjectionTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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);
0 ignored issues
show
Bug introduced by
The method assertContains() does not seem to exist on object<ScriptInjectionTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
51
        $this->assertContains("['MauticTrackingObject']", $output);
0 ignored issues
show
Bug introduced by
The method assertContains() does not seem to exist on object<ScriptInjectionTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
52
        $this->assertContains("mt('send', 'pageview')", $output);
0 ignored issues
show
Bug introduced by
The method assertContains() does not seem to exist on object<ScriptInjectionTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
53
        $this->assertContains($base_url."/mtracking.gif?d=", $output);
0 ignored issues
show
Bug introduced by
The method assertContains() does not seem to exist on object<ScriptInjectionTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
54
    }
55
56 View Code Duplication
    public function test_script_is_not_injected_in_footer_by_default()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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);
0 ignored issues
show
Bug introduced by
The method assertNotContains() does not seem to exist on object<ScriptInjectionTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
65
        $this->assertNotContains("['MauticTrackingObject']", $output);
0 ignored issues
show
Bug introduced by
The method assertNotContains() does not seem to exist on object<ScriptInjectionTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
66
        $this->assertNotContains("mt('send', 'pageview')", $output);
0 ignored issues
show
Bug introduced by
The method assertNotContains() does not seem to exist on object<ScriptInjectionTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
67
68
        //Fallback activated and <noscript> is always injected in footer !
69
        $this->assertContains(sprintf("%s/mtracking.gif?d=", $base_url), $output);
0 ignored issues
show
Bug introduced by
The method assertContains() does not seem to exist on object<ScriptInjectionTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
70
    }
71
72 View Code Duplication
    public function test_script_is_injected_in_footer_when_requested()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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);
0 ignored issues
show
Bug introduced by
The method assertContains() does not seem to exist on object<ScriptInjectionTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
81
        $this->assertContains("['MauticTrackingObject']", $output);
0 ignored issues
show
Bug introduced by
The method assertContains() does not seem to exist on object<ScriptInjectionTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
82
        $this->assertContains("mt('send', 'pageview')", $output);
0 ignored issues
show
Bug introduced by
The method assertContains() does not seem to exist on object<ScriptInjectionTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
83
        $this->assertContains(sprintf("%s/mtracking.gif?d=", $base_url), $output);
0 ignored issues
show
Bug introduced by
The method assertContains() does not seem to exist on object<ScriptInjectionTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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);
0 ignored issues
show
Bug introduced by
The method assertNotContains() does not seem to exist on object<ScriptInjectionTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
98
    }
99
100
    public function test_tracking_image_payload_is_valid()
101
    {
102
        $base_url = 'http://example.com';
103
        $this->go_to( $base_url );
0 ignored issues
show
Bug introduced by
The method go_to() does not seem to exist on object<ScriptInjectionTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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);
0 ignored issues
show
Bug introduced by
The method assertContains() does not seem to exist on object<ScriptInjectionTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
111
    }
112
}
113