-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathKitForms.php
More file actions
253 lines (225 loc) · 8.22 KB
/
KitForms.php
File metadata and controls
253 lines (225 loc) · 8.22 KB
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
<?php
namespace Tests\Support\Helper;
/**
* Helper methods and actions related to the Kit Plugin's Forms
* functionality, which are then available using $I->{yourFunctionName}.
*
* @since 2.2.0
*/
class KitForms extends \Codeception\Module
{
/**
* Check that expected HTML exists in the DOM of the page we're viewing for
* a Form block or shortcode.
*
* @since 2.5.8
*
* @param EndToEndTester $I Tester.
* @param int $formID Form ID.
* @param bool|string $position Position of the form in the DOM relative to the content.
* @param bool|string $element Element the form should display after.
* @param bool|string $elementIndex Number of elements before the form should display.
*/
public function seeFormOutput($I, $formID, $position = false, $element = false, $elementIndex = 0)
{
// Calculate how many times the Form should be in the DOM.
$count = ( ( $position === 'before_after_content' ) ? 2 : 1 );
// Confirm the Form is in the DOM the expected number of times.
$I->seeNumberOfElementsInDOM('form[data-sv-form="' . $formID . '"]', $count);
// Assert position of form, if required.
if ( ! $position) {
return;
}
// Assert that the first and/or last child element is the Form ID, depending on the position.
switch ($position) {
case 'before_after_content':
$I->assertEquals($formID, $I->grabAttributeFrom('div.entry-content > *:first-child', 'data-sv-form'));
$I->assertEquals($formID, $I->grabAttributeFrom('div.entry-content > *:last-child', 'data-sv-form'));
break;
case 'before_content':
$I->assertEquals($formID, $I->grabAttributeFrom('div.entry-content > *:first-child', 'data-sv-form'));
break;
case 'after_content':
$I->assertEquals($formID, $I->grabAttributeFrom('div.entry-content > *:last-child', 'data-sv-form'));
break;
case 'after_element':
// The block editor automatically adds CSS classes to some elements.
switch ( $element ) {
case 'p':
$I->seeInSource('<' . $element . ' class="wp-block-paragraph">Item #' . $elementIndex . '</' . $element . '><form action="https://app.kit.com/forms/' . $formID . '/subscriptions" ');
break;
case 'img':
$I->seeInSource('<' . $element . ' decoding="async" src="https://placehold.co/600x400" alt="Image #' . $elementIndex . '"><form action="https://app.kit.com/forms/' . $formID . '/subscriptions" ');
break;
// Headings.
default:
$I->seeInSource('<' . $element . ' class="wp-block-heading">Item #' . $elementIndex . '</' . $element . '><form action="https://app.kit.com/forms/' . $formID . '/subscriptions" ');
break;
}
break;
}
}
/**
* Check that expected HTML exists in the DOM of the page we're viewing for
* a Form Trigger block or shortcode, and that the button loads the expected
* Kit Form.
*
* @since 2.2.0
*
* @param EndToEndTester $I Tester.
* @param string $formURL Form URL.
* @param bool|string $text Test if the button text matches the given value.
* @param bool|string $textColor Test if the given text color is applied.
* @param bool|string $backgroundColor Test is the given background color is applied.
* @param bool|string $cssClasses Test if the given CSS classes are applied.
* @param bool|string $styles Test if the given styles are applied.
* @param bool $isBlock Test if this is a form trigger block or shortcode.
*/
public function seeFormTriggerOutput($I, $formURL, $text = false, $textColor = false, $backgroundColor = false, $cssClasses = false, $styles = false, $isBlock = false)
{
// Confirm that the button stylesheet loaded.
$I->seeInSource('<link rel="stylesheet" id="convertkit-frontend-css" href="' . $_ENV['WORDPRESS_URL'] . '/wp-content/plugins/convertkit/resources/frontend/css/frontend.css');
// Confirm that the block button CSS loaded.
if ($isBlock) {
$I->seeInSource('<style id="wp-block-button-inline-css">');
}
// Confirm that the block displays.
$I->seeElementInDOM('a.convertkit-formtrigger.wp-block-button__link');
// Confirm that the button links to the correct form.
$I->assertEquals($formURL, $I->grabAttributeFrom('a.convertkit-formtrigger', 'href'));
// Confirm that the text is as expected.
if ($text !== false) {
$I->see($text);
}
// Confirm that the text color is as expected.
if ($textColor !== false) {
switch ($isBlock) {
case true:
$I->seeElementInDOM('a.convertkit-formtrigger.has-text-color');
break;
default:
$I->assertStringContainsString(
'color:' . $textColor,
$I->grabAttributeFrom('a.convertkit-formtrigger', 'style')
);
break;
}
}
// Confirm that the background color is as expected.
if ($backgroundColor !== false) {
switch ($isBlock) {
case true:
$I->seeElementInDOM('a.convertkit-formtrigger.has-background');
break;
default:
$I->assertStringContainsString(
'background-color:' . $backgroundColor,
$I->grabAttributeFrom('a.convertkit-formtrigger', 'style')
);
break;
}
}
// Confirm that the CSS classes are as expected.
if ($cssClasses !== false) {
$I->assertStringContainsString(
$cssClasses,
$I->grabAttributeFrom('a.convertkit-formtrigger', 'class')
);
}
// Confirm that the styles are as expected.
if ($styles !== false) {
$I->assertStringContainsString(
$styles,
$I->grabAttributeFrom('a.convertkit-formtrigger', 'style')
);
}
// Click the button to confirm that the Kit modal displays.
$I->click('a.convertkit-formtrigger');
$I->waitForElementVisible('div.formkit-overlay');
}
/**
* Check that expected HTML does not exist in the DOM of the page we're viewing for
* a Form Trigger block or shortcode.
*
* @since 2.2.0
*
* @param EndToEndTester $I Tester.
*/
public function dontSeeFormTriggerOutput($I)
{
// Confirm that the block does not display.
$I->dontSeeElementInDOM('div.wp-block-button a.convertkit-formtrigger');
}
/**
* Check that expected HTML exists in the DOM of the page we're viewing for
* a Form Trigger link, and that the link loads the expected
* Kit Form.
*
* @since 2.2.0
*
* @param EndToEndTester $I Tester.
* @param string $formURL Form URL.
* @param bool|string $text Test if the text matches the given value.
*/
public function seeFormTriggerLinkOutput($I, $formURL, $text = false)
{
// Confirm that the link displays.
$I->seeElementInDOM('a.convertkit-form-link');
// Confirm that the button links to the correct form.
$I->assertEquals($formURL, $I->grabAttributeFrom('a.convertkit-form-link', 'href'));
// Confirm that the text is as expected.
if ($text !== false) {
$I->see($text);
}
// Click the link to confirm that the Kit form displays.
$I->click('a.convertkit-form-link');
$I->waitForElementVisible('div.formkit-overlay');
}
/**
* Check that expected HTML does not exist in the DOM of the page we're viewing for
* a Form Trigger link formatter.
*
* @since 2.2.0
*
* @param EndToEndTester $I Tester.
*/
public function dontSeeFormTriggerLinkOutput($I)
{
// Confirm that the link does not display.
$I->dontSeeElementInDOM('a.convertkit-form-link');
}
/**
* Helper method to assert that the expected landing page HTML is output.
*
* @since 2.5.9
*
* @param EndToEndTester $I Tester.
* @param bool $langTag Assert if HTML tag includes lang attribute.
*/
public function seeLandingPageOutput($I, $langTag = false)
{
if ($langTag) {
$I->seeInSource('<html lang="en">');
} else {
$I->seeInSource('<html>');
}
$I->seeInSource('<head>');
$I->seeInSource('</head>');
$I->seeInSource('<body');
$I->seeInSource('</body>');
$I->seeInSource('</html>');
}
/**
* Confirm no extra <html>, <head> or <body> tags are output i.e. injecting the form doesn't result in DOMDocument adding tags.
*
* @since 3.2.1
*
* @param EndToEndTester $I Tester.
*/
public function seeNoExtraHtmlHeadBodyTagsOutput($I)
{
$I->seeNumberOfElementsInDOM('html', 1);
$I->seeNumberOfElementsInDOM('head', 1);
$I->seeNumberOfElementsInDOM('body', 1);
}
}