/
ComponentsTest.php
237 lines (193 loc) · 7.89 KB
/
ComponentsTest.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
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
<?php
/**
* @copyright Copyright (c) PutYourLightsOn
*/
namespace putyourlightson\sprigcoretests\unit\services;
use Codeception\Test\Unit;
use Craft;
use craft\elements\Entry;
use craft\web\Request;
use putyourlightson\sprig\errors\InvalidVariableException;
use putyourlightson\sprig\Sprig;
use UnitTester;
use yii\base\Model;
use yii\web\BadRequestHttpException;
/**
* @author PutYourLightsOn
* @package Sprig
* @since 1.0.0
*/
class ComponentsTest extends Unit
{
/**
* @var UnitTester
*/
protected $tester;
protected function _before()
{
parent::_before();
// Bootstrap the module
Sprig::bootstrap();
Craft::$app->getView()->setTemplatesPath(Craft::getAlias('@templates'));
}
public function testCreate()
{
$markup = Sprig::$core->components->create(
'_component',
['number' => 15],
['id' => 'abc', 's-trigger' => 'load', 's-vars' => 'limit:1', 's-push-url' => 'new-url']
);
$html = (string)$markup;
$this->assertStringContainsString('id="abc"', $html);
$this->assertStringContainsString('data-hx-include="#abc *"', $html);
$this->assertStringContainsString('data-hx-trigger="load"', $html);
$this->assertStringContainsString('sprig:template', $html);
$this->assertStringContainsString('limit:1', $html);
$this->assertStringContainsString('data-hx-push-url="new-url"', $html);
$this->assertStringContainsString('xyz 15', $html);
}
public function testCreateEmptyComponent()
{
$markup = Sprig::$core->components->create('_empty');
$html = (string)$markup;
$this->assertStringContainsString('data-hx-get', $html);
}
public function testCreateNoComponent()
{
$this->expectException(BadRequestHttpException::class);
Sprig::$core->components->create('_no-component');
}
public function testCreateObjectFromComponent()
{
// Require the class since it is not autoloaded.
require CRAFT_TESTS_PATH.'/_craft/sprig/components/TestComponent.php';
$object = Sprig::$core->components->createObject(
'TestComponent',
['number' => 15]
);
$html = $object->render();
$this->assertStringContainsString('xyz 15', $html);
}
public function testCreateObjectFromNoComponent()
{
$object = Sprig::$core->components->createObject('NoComponent');
$this->assertNull($object);
}
public function testCreateInvalidVariableEntry()
{
$this->_testCreateInvalidVariable(['number' => '', 'entry' => new Entry()]);
}
public function testCreateInvalidVariableModel()
{
$this->_testCreateInvalidVariable(['number' => '', 'model' => new Model()]);
}
public function testCreateInvalidVariableObject()
{
$this->_testCreateInvalidVariable(['number' => '', 'model' => (object)[]]);
}
public function testCreateInvalidVariableArray()
{
$this->_testCreateInvalidVariable(['number' => '', 'array' => [new Entry()]]);
}
public function testGetParsedTagAttributes()
{
$html = '<div sprig s-method="post" s-action="a/b/c" s-vals=\'{"limit":1}\'></div>';
$html = Sprig::$core->components->parse($html);
$this->assertStringContainsString('data-hx-post=', $html);
$this->assertStringContainsString('&sprig:action=', $html);
$this->assertStringContainsString('data-hx-headers="{"'.Request::CSRF_HEADER.'"', $html);
$this->assertStringContainsString('data-hx-vals="{"limit":1}', $html);
}
public function testGetParsedTagAttributesWithData()
{
$html = '<div data-sprig></div>';
$html = Sprig::$core->components->parse($html);
$this->assertStringContainsString('data-hx-get=', $html);
}
public function testGetParsedTagAttributesWithSpaces()
{
$html = '<div s-target = "#id"></div>';
$html = Sprig::$core->components->parse($html);
$this->assertStringContainsString('data-hx-target="#id"', $html);
$html = '<div s-target = \'#id\'></div>';
$html = Sprig::$core->components->parse($html);
$this->assertStringContainsString('data-hx-target="#id"', $html);
$html = '<div s-target = #id></div>';
$html = Sprig::$core->components->parse($html);
$this->assertStringContainsString('data-hx-target="#id"', $html);
}
public function testGetParsedTagAttributesVals()
{
$html = '<div s-val:x-y-z="a" s-vals=\'{"limit":1}\'></div>';
$html = Sprig::$core->components->parse($html);
$this->assertStringContainsString('data-hx-vals="{"xYZ":"a","limit":1}"', $html);
}
public function testGetParsedTagAttributesValsWithEmpty()
{
$html = '<div s-val:x-y-z="" s-vals=\'{"limit":1}\'></div>';
$html = Sprig::$core->components->parse($html);
$this->assertStringContainsString('data-hx-vals="{"xYZ":"","limit":1}"', $html);
}
public function testGetParsedTagAttributesValsWithEncoded()
{
$html = '<div s-val:x-y-z="a" s-vals=\'{"limit":1}\'></div>';
$html = Sprig::$core->components->parse($html);
$this->assertStringContainsString('data-hx-vals="{"xYZ":"a","limit":1}"', $html);
}
public function testGetParsedTagAttributesValsWithBrackets()
{
$html = '<div s-val:fields[x-y-z]="a"></div>';
$html = Sprig::$core->components->parse($html);
$this->assertStringContainsString('data-hx-vals="{"fields[xYZ]":"a"}"', $html);
}
public function testGetParsedTagAttributesValsEncodedAndSanitized()
{
$html = '<div s-val:x="alert(\'xss\')" s-val:z=\'alert("xss")\' s-vals=\'{"limit":1}\'></div>';
$html = Sprig::$core->components->parse($html);
$this->assertStringContainsString('data-hx-vals="{"x":"alert(\u0027xss\u0027)","z":"alert(\u0022xss\u0022)","limit":1}"', $html);
}
public function testGetParsedTagAttributesEmpty()
{
$html = '';
$result = Sprig::$core->components->parse($html);
$this->assertEquals($html, $result);
}
public function testGetParsedTagAttributesHtml()
{
$html = '<div><p><span><template><h1>Hello</h1></template></span></p></div>';
$result = Sprig::$core->components->parse($html);
$this->assertEquals($html, $result);
}
public function testGetParsedTagAttributesDuplicateIds()
{
$html = '<div id="my-id"><p id="my-id"><span id="my-id"></span></p></div>';
$result = Sprig::$core->components->parse($html);
$this->assertEquals($html, $result);
}
public function testGetParsedTagAttributesComment()
{
$html = '<!-- Comment mentioning sprig -->';
$result = Sprig::$core->components->parse($html);
$this->assertEquals($html, $result);
}
public function testGetParsedTagAttributesScript()
{
$html = '<script>if(i < 1) let sprig=1</script>';
$result = Sprig::$core->components->parse($html);
$this->assertEquals($html, $result);
}
public function testGetParsedTagAttributesUtfEncoding()
{
$placeholder = 'ÆØÅäöü';
$html = '<div sprig placeholder="'.$placeholder.'"></div>';
$result = Sprig::$core->components->parse($html);
$this->assertStringContainsString($placeholder, $result);
}
private function _testCreateInvalidVariable(array $variables)
{
$this->tester->mockCraftMethods('view', ['doesTemplateExist' => true]);
Craft::$app->getView()->setTemplatesPath(Craft::getAlias('@templates'));
$this->expectException(InvalidVariableException::class);
Sprig::$core->components->create('_component', $variables);
}
}