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

ShortCodeTest::provideShortCode()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 117
Code Lines 92

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 92
nc 1
nop 0
dl 0
loc 117
rs 8.2857
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * @package wpmautic\tests
4
 */
5
6
/**
7
 * Test shortcode handling
8
 */
9
class ShortCodeTest 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
    public function setUp()
12
    {
13
        parent::setUp();
14
15
        update_option('wpmautic_options', array(
16
            'base_url' => 'http://example.com'
17
        ));
18
    }
19
20
    public function test_shortcode_exists()
21
    {
22
        $this->assertTrue(shortcode_exists('mautic'));
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<ShortCodeTest>.

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...
23
        $this->assertTrue(shortcode_exists('mauticcontent'));
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<ShortCodeTest>.

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...
24
        $this->assertTrue(shortcode_exists('mauticvideo'));
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<ShortCodeTest>.

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...
25
        $this->assertTrue(shortcode_exists('mauticform'));
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<ShortCodeTest>.

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...
26
        $this->assertTrue(shortcode_exists('mautictags'));
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<ShortCodeTest>.

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...
27
        $this->assertTrue(shortcode_exists('mauticfocus'));
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<ShortCodeTest>.

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...
28
    }
29
30
    public function test_form_shortcode_from_empty_atts()
31
    {
32
        $result = wpmautic_form_shortcode(array());
33
        $this->assertFalse($result);
0 ignored issues
show
Bug introduced by
The method assertFalse() does not seem to exist on object<ShortCodeTest>.

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...
34
    }
35
36
    public function provideShortCode()
37
    {
38
        return array(
39
            array(
40
                '[mauticform id="1"]',
41
                '<script type="text/javascript" src="http://example.com/form/generate.js?id=1"></script>'
42
            ),
43
            array(
44
                '[mautic type="form" id="1"]',
45
                '<script type="text/javascript" src="http://example.com/form/generate.js?id=1"></script>'
46
            ),
47
            array(
48
                '[mautictags values="tag,-tag2,tag3"]',
49
                '<img src="http://example.com/mtracking.gif?tags=tag,-tag2,tag3" alt="Mautic Tags" />'
50
            ),
51
            array(
52
                '[mautic type="tags" values="tag,tag2,-tag3"]',
53
                '<img src="http://example.com/mtracking.gif?tags=tag,tag2,-tag3" alt="Mautic Tags" />'
54
            ),
55
            array(
56
                '[mautictags values="tag,-tag2,tag3"]',
57
                '<img src="http://example.com/mtracking.gif?tags=tag,-tag2,tag3" alt="Mautic Tags" />'
58
            ),
59
            array(
60
                '[mautic type="tags" values="tag,tag2,-tag3"]',
61
                '<img src="http://example.com/mtracking.gif?tags=tag,tag2,-tag3" alt="Mautic Tags" />'
62
            ),
63
            array(
64
                '[mauticfocus id="1"]',
65
                '<script type="text/javascript" src="http://example.com/focus/1.js" async="async"></script>'
66
            ),
67
            array(
68
                '[mautic type="focus" id="1"]',
69
                '<script type="text/javascript" src="http://example.com/focus/1.js" async="async"></script>'
70
            ),
71
            array(
72
                '[mauticcontent slot="name"]content[/mauticcontent]',
73
                '<div class="mautic-slot" data-slot-name="name">content</div>'
74
            ),
75
            array(
76
                '[mautic type="content" slot="name"]content[/mautic]',
77
                '<div class="mautic-slot" data-slot-name="name">content</div>'
78
            ),
79
            array(
80
                '[mautic type="video" src="https://www.youtube.com/watch?v=1234" form-id="1" width="148"]',
81
                '<video height="360" width="148" data-form-id="1" data-gate-time="15">' .
82
                    '<source type="video/youtube" src="https://www.youtube.com/watch?v=1234" />' .
83
                '</video>'
84
            ),
85
            array(
86
                '[mauticvideo src="https://www.youtube.com/watch?v=1234" form-id="1" width="148"]',
87
                '<video height="360" width="148" data-form-id="1" data-gate-time="15">' .
88
                    '<source type="video/youtube" src="https://www.youtube.com/watch?v=1234" />' .
89
                '</video>'
90
            ),
91
            array(
92
                '[mautic type="video" src="https://vimeo.com/218680983" form-id="1" height="272"]',
93
                '<video height="272" width="640" data-form-id="1" data-gate-time="15">' .
94
                    '<source type="video/vimeo" src="https://vimeo.com/218680983" />' .
95
                '</video>'
96
            ),
97
            array(
98
                '[mauticvideo src="https://vimeo.com/218680983" form-id="1" height="272"]',
99
                '<video height="272" width="640" data-form-id="1" data-gate-time="15">' .
100
                    '<source type="video/vimeo" src="https://vimeo.com/218680983" />' .
101
                '</video>'
102
            ),
103
            array(
104
                '[mautic type="video" src="https://vimeo.com/218680983" form-id="1" gate-time="25"]',
105
                '<video height="360" width="640" data-form-id="1" data-gate-time="25">' .
106
                    '<source type="video/vimeo" src="https://vimeo.com/218680983" />' .
107
                '</video>'
108
            ),
109
            array(
110
                '[mauticvideo src="https://vimeo.com/218680983" form-id="1" gate-time="25"]',
111
                '<video height="360" width="640" data-form-id="1" data-gate-time="25">' .
112
                    '<source type="video/vimeo" src="https://vimeo.com/218680983" />' .
113
                '</video>'
114
            ),
115
            array(
116
                '[mautic type="video" src="https://vimeo.com/218680983" form-id="1" video-type="mp4"]',
117
                '<video height="360" width="640" data-form-id="1" data-gate-time="15">' .
118
                    '<source type="video/vimeo" src="https://vimeo.com/218680983" />' .
119
                '</video>'
120
            ),
121
            array(
122
                '[mauticvideo src="https://vimeo.com/218680983" form-id="1" video-type="mp4"]',
123
                '<video height="360" width="640" data-form-id="1" data-gate-time="15">' .
124
                    '<source type="video/vimeo" src="https://vimeo.com/218680983" />' .
125
                '</video>'
126
            ),
127
            array(
128
                '[mautic type="video" src="https://example.com/mavideo.mov" form-id="1" video-type="mov"]',
129
                '<video height="360" width="640" data-form-id="1" data-gate-time="15">' .
130
                    '<source type="video/mov" src="https://example.com/mavideo.mov" />' .
131
                '</video>'
132
            ),
133
            array(
134
                '[mauticvideo src="https://example.com/mavideo.mov" form-id="1" video-type="mov"]',
135
                '<video height="360" width="640" data-form-id="1" data-gate-time="15">' .
136
                    '<source type="video/mov" src="https://example.com/mavideo.mov" />' .
137
                '</video>'
138
            ),
139
            array(
140
                '[mautic type="video" src="https://example.com/mavideo.mp4" form-id="1"]',
141
                '<video height="360" width="640" data-form-id="1" data-gate-time="15">' .
142
                    '<source type="video/mp4" src="https://example.com/mavideo.mp4" />' .
143
                '</video>'
144
            ),
145
            array(
146
                '[mauticvideo src="https://example.com/mavideo.mp4" form-id="1"]',
147
                '<video height="360" width="640" data-form-id="1" data-gate-time="15">' .
148
                    '<source type="video/mp4" src="https://example.com/mavideo.mp4" />' .
149
                '</video>'
150
            ),
151
        );
152
    }
153
154
    /**
155
     * @dataProvider provideShortCode
156
     * @param string $content
157
     * @param string $expected
158
     */
159
    public function test_shortcode($content, $expected)
160
    {
161
        $result = do_shortcode($content);
162
        $this->assertEquals($expected, $result);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<ShortCodeTest>.

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...
163
    }
164
165 View Code Duplication
    public function provideShortCodeToBeEmptyWithoutUrl()
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...
166
    {
167
        return array(
168
            array('[mauticform id="1"]'),
169
            array('[mautic type="form" id="1"]'),
170
            array('[mautictags values="tag,-tag2,tag3"]'),
171
            array('[mautic type="tags" values="tag,tag2,-tag3"]'),
172
            array('[mauticfocus id="1"]'),
173
            array('[mautic type="focus" id="1"]'),
174
        );
175
    }
176
177
    /**
178
     * @dataProvider provideShortCodeToBeEmpty
179
     * @param string $content
180
     */
181
    public function test_shortcode_with_empty_url($content)
182
    {
183
        update_option('wpmautic_options', array());
184
185
        $result = do_shortcode($content);
186
        $this->assertEmpty($result);
0 ignored issues
show
Bug introduced by
The method assertEmpty() does not seem to exist on object<ShortCodeTest>.

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...
187
    }
188
189 View Code Duplication
    public function provideShortCodeToBeEmpty()
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...
190
    {
191
        return array(
192
            array('[mauticform]'),
193
            array('[mautic type="form"]'),
194
            array('[mautictags]'),
195
            array('[mautic type="tags"]'),
196
            array('[mauticfocus]'),
197
            array('[mautic type="focus"]'),
198
        );
199
    }
200
201
    /**
202
     * @dataProvider provideShortCodeToBeEmpty
203
     * @param string $content
204
     */
205
    public function test_shortcode_to_be_empty($content)
206
    {
207
        $result = do_shortcode($content);
208
        $this->assertEmpty($result);
0 ignored issues
show
Bug introduced by
The method assertEmpty() does not seem to exist on object<ShortCodeTest>.

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...
209
    }
210
211
    public function test_invalid_shortcode_type()
212
    {
213
        $result = do_shortcode("[mautic type='azerty']");
214
        $this->assertEmpty($result);
0 ignored issues
show
Bug introduced by
The method assertEmpty() does not seem to exist on object<ShortCodeTest>.

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...
215
    }
216
217
    public function test_no_src_on_video_shortcode()
218
    {
219
        $result = do_shortcode("[mautic type='video']");
220
        $this->assertEquals(
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<ShortCodeTest>.

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...
221
            'You must provide a video source. Add a src="URL" attribute to your shortcode. '.
222
            'Replace URL with the source url for your video.',
223
            $result
224
        );
225
    }
226
227
    public function test_no_form_id_on_video_shortcode()
228
    {
229
        $result = do_shortcode("[mautic type='video' src='mavideo.mov']");
230
        $this->assertEquals(
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<ShortCodeTest>.

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...
231
            'You must provide a mautic form id. Add a form-id="#" attribute to your shortcode. '.
232
            'Replace # with the id of the form you want to use.',
233
            $result
234
        );
235
    }
236
237
    public function test_no_video_type_on_video_shortcode()
238
    {
239
        $result = do_shortcode("[mautic type='video' src='mavideo.mov' form-id='1']");
240
        $this->assertEquals(
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<ShortCodeTest>.

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...
241
            'Please define a valid video type with video-type="#".',
242
            $result
243
        );
244
    }
245
}
246