1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\Forms\Tests\HTMLEditor; |
4
|
|
|
|
5
|
|
|
use Silverstripe\Assets\Dev\TestAssetStore; |
6
|
|
|
use SilverStripe\Assets\File; |
7
|
|
|
use SilverStripe\Assets\FileNameFilter; |
8
|
|
|
use SilverStripe\Assets\Filesystem; |
9
|
|
|
use SilverStripe\Assets\Folder; |
10
|
|
|
use SilverStripe\Assets\Image; |
11
|
|
|
use SilverStripe\Core\Config\Config; |
12
|
|
|
use SilverStripe\Dev\CSSContentParser; |
13
|
|
|
use SilverStripe\Dev\FunctionalTest; |
14
|
|
|
use SilverStripe\Forms\HTMLEditor\HTMLEditorField; |
15
|
|
|
use SilverStripe\Forms\HTMLEditor\TinyMCEConfig; |
16
|
|
|
use SilverStripe\Forms\HTMLReadonlyField; |
17
|
|
|
use SilverStripe\Forms\Tests\HTMLEditor\HTMLEditorFieldTest\TestObject; |
18
|
|
|
use SilverStripe\ORM\FieldType\DBHTMLText; |
19
|
|
|
|
20
|
|
|
class HTMLEditorFieldTest extends FunctionalTest |
21
|
|
|
{ |
22
|
|
|
protected static $fixture_file = 'HTMLEditorFieldTest.yml'; |
23
|
|
|
|
24
|
|
|
protected static $extra_dataobjects = [ |
25
|
|
|
TestObject::class, |
26
|
|
|
]; |
27
|
|
|
|
28
|
|
|
protected function setUp() |
29
|
|
|
{ |
30
|
|
|
parent::setUp(); |
31
|
|
|
|
32
|
|
|
// Set backend root to /HTMLEditorFieldTest |
33
|
|
|
TestAssetStore::activate('HTMLEditorFieldTest'); |
34
|
|
|
|
35
|
|
|
// Set the File Name Filter replacements so files have the expected names |
36
|
|
|
Config::modify()->set( |
37
|
|
|
FileNameFilter::class, |
38
|
|
|
'default_replacements', |
39
|
|
|
[ |
40
|
|
|
'/\s/' => '-', // remove whitespace |
41
|
|
|
'/_/' => '-', // underscores to dashes |
42
|
|
|
'/[^A-Za-z0-9+.\-]+/' => '', // remove non-ASCII chars, only allow alphanumeric plus dash and dot |
43
|
|
|
'/[\-]{2,}/' => '-', // remove duplicate dashes |
44
|
|
|
'/^[\.\-_]+/' => '', // Remove all leading dots, dashes or underscores |
45
|
|
|
] |
46
|
|
|
); |
47
|
|
|
|
48
|
|
|
// Create a test files for each of the fixture references |
49
|
|
|
$files = File::get()->exclude('ClassName', Folder::class); |
50
|
|
|
foreach ($files as $file) { |
51
|
|
|
$fromPath = __DIR__ . '/HTMLEditorFieldTest/images/' . $file->Name; |
52
|
|
|
$destPath = TestAssetStore::getLocalPath($file); // Only correct for test asset store |
53
|
|
|
Filesystem::makeFolder(dirname($destPath)); |
54
|
|
|
copy($fromPath, $destPath); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
protected function tearDown() |
59
|
|
|
{ |
60
|
|
|
TestAssetStore::reset(); |
61
|
|
|
parent::tearDown(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function testCasting() |
65
|
|
|
{ |
66
|
|
|
// Shim TinyMCE so silverstripe/admin doesn't have to be installed |
67
|
|
|
TinyMCEConfig::config()->set( |
68
|
|
|
'base_dir', |
69
|
|
|
'silverstripe/framework: tests/php/Forms/HTMLEditor/TinyMCECombinedGeneratorTest/tinymce' |
70
|
|
|
); |
71
|
|
|
HtmlEditorField::config()->set('use_gzip', false); |
72
|
|
|
|
73
|
|
|
// Test special characters |
74
|
|
|
$inputText = "These are some unicodes: ä, ö, & ü"; |
75
|
|
|
$field = new HTMLEditorField("Test", "Test"); |
76
|
|
|
$field->setValue($inputText); |
77
|
|
|
$this->assertContains('These are some unicodes: ä, ö, & ü', $field->Field()); |
78
|
|
|
// Test shortcodes |
79
|
|
|
$inputText = "Shortcode: [file_link id=4]"; |
80
|
|
|
$field = new HTMLEditorField("Test", "Test"); |
81
|
|
|
$field->setValue($inputText); |
82
|
|
|
$this->assertContains('Shortcode: [file_link id=4]', $field->Field()); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function testBasicSaving() |
86
|
|
|
{ |
87
|
|
|
$obj = new TestObject(); |
88
|
|
|
$editor = new HTMLEditorField('Content'); |
89
|
|
|
|
90
|
|
|
$editor->setValue('<p class="foo">Simple Content</p>'); |
91
|
|
|
$editor->saveInto($obj); |
92
|
|
|
$this->assertEquals('<p class="foo">Simple Content</p>', $obj->Content, 'Attributes are preserved.'); |
|
|
|
|
93
|
|
|
|
94
|
|
|
$editor->setValue('<p>Unclosed Tag'); |
95
|
|
|
$editor->saveInto($obj); |
96
|
|
|
$this->assertEquals('<p>Unclosed Tag</p>', $obj->Content, 'Unclosed tags are closed.'); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function testNullSaving() |
100
|
|
|
{ |
101
|
|
|
$obj = new TestObject(); |
102
|
|
|
$editor = new HTMLEditorField('Content'); |
103
|
|
|
|
104
|
|
|
$editor->setValue(null); |
105
|
|
|
$editor->saveInto($obj); |
106
|
|
|
$this->assertEquals('', $obj->Content, "Doesn't choke on empty/null values."); |
|
|
|
|
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function testResizedImageInsertion() |
110
|
|
|
{ |
111
|
|
|
$obj = new TestObject(); |
112
|
|
|
$editor = new HTMLEditorField('Content'); |
113
|
|
|
|
114
|
|
|
$fileID = $this->idFromFixture(Image::class, 'example_image'); |
115
|
|
|
$editor->setValue( |
116
|
|
|
sprintf( |
117
|
|
|
'[image src="assets/example.jpg" width="10" height="20" id="%d"]', |
118
|
|
|
$fileID |
119
|
|
|
) |
120
|
|
|
); |
121
|
|
|
$editor->saveInto($obj); |
122
|
|
|
|
123
|
|
|
$parser = new CSSContentParser($obj->dbObject('Content')->forTemplate()); |
124
|
|
|
$xml = $parser->getByXpath('//img'); |
125
|
|
|
$this->assertEquals( |
126
|
|
|
'example', |
127
|
|
|
(string)$xml[0]['alt'], |
128
|
|
|
'Alt tags are added by default based on filename' |
129
|
|
|
); |
130
|
|
|
$this->assertEquals('', (string)$xml[0]['title'], 'Title tags are added by default.'); |
131
|
|
|
$this->assertEquals(10, (int)$xml[0]['width'], 'Width tag of resized image is set.'); |
132
|
|
|
$this->assertEquals(20, (int)$xml[0]['height'], 'Height tag of resized image is set.'); |
133
|
|
|
|
134
|
|
|
$neededFilename |
135
|
|
|
= '/assets/HTMLEditorFieldTest/f5c7c2f814/example__ResizedImageWzEwLDIwXQ.jpg'; |
136
|
|
|
|
137
|
|
|
$this->assertEquals($neededFilename, (string)$xml[0]['src'], 'Correct URL of resized image is set.'); |
138
|
|
|
$this->assertTrue( |
139
|
|
|
file_exists(PUBLIC_PATH . DIRECTORY_SEPARATOR . $neededFilename), |
140
|
|
|
'File for resized image exists' |
141
|
|
|
); |
142
|
|
|
$this->assertEquals(false, $obj->HasBrokenFile, 'Referenced image file exists.'); |
|
|
|
|
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function testMultiLineSaving() |
146
|
|
|
{ |
147
|
|
|
$obj = $this->objFromFixture(TestObject::class, 'home'); |
148
|
|
|
$editor = new HTMLEditorField('Content'); |
149
|
|
|
$editor->setValue('<p>First Paragraph</p><p>Second Paragraph</p>'); |
150
|
|
|
$editor->saveInto($obj); |
151
|
|
|
$this->assertEquals('<p>First Paragraph</p><p>Second Paragraph</p>', $obj->Content); |
|
|
|
|
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public function testSavingLinksWithoutHref() |
155
|
|
|
{ |
156
|
|
|
$obj = $this->objFromFixture(TestObject::class, 'home'); |
157
|
|
|
$editor = new HTMLEditorField('Content'); |
158
|
|
|
|
159
|
|
|
$editor->setValue('<p><a name="example-anchor"></a></p>'); |
160
|
|
|
$editor->saveInto($obj); |
161
|
|
|
|
162
|
|
|
$this->assertEquals( |
163
|
|
|
'<p><a name="example-anchor"></a></p>', |
164
|
|
|
$obj->Content, |
|
|
|
|
165
|
|
|
'Saving a link without a href attribute works' |
166
|
|
|
); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
public function testReadonlyField() |
170
|
|
|
{ |
171
|
|
|
$editor = new HTMLEditorField('Content'); |
172
|
|
|
$fileID = $this->idFromFixture(Image::class, 'example_image'); |
173
|
|
|
$editor->setValue( |
174
|
|
|
sprintf( |
175
|
|
|
'[image src="assets/example.jpg" width="10" height="20" id="%d"]', |
176
|
|
|
$fileID |
177
|
|
|
) |
178
|
|
|
); |
179
|
|
|
/** @var HTMLReadonlyField $readonly */ |
180
|
|
|
$readonly = $editor->performReadonlyTransformation(); |
181
|
|
|
/** @var DBHTMLText $readonlyContent */ |
182
|
|
|
$readonlyContent = $readonly->Field(); |
183
|
|
|
|
184
|
|
|
// phpcs:disable |
185
|
|
|
$this->assertEquals( |
186
|
|
|
<<<EOS |
187
|
|
|
<span class="readonly typography" id="Content"> |
188
|
|
|
<img src="/assets/HTMLEditorFieldTest/f5c7c2f814/example__ResizedImageWzEwLDIwXQ.jpg" alt="example" width="10" height="20"> |
189
|
|
|
</span> |
190
|
|
|
|
191
|
|
|
|
192
|
|
|
EOS |
193
|
|
|
, |
194
|
|
|
$readonlyContent->getValue() |
195
|
|
|
); |
196
|
|
|
// phpcs:enable |
197
|
|
|
|
198
|
|
|
// Test with include input tag |
199
|
|
|
$readonly = $editor->performReadonlyTransformation() |
200
|
|
|
->setIncludeHiddenField(true); |
201
|
|
|
/** @var DBHTMLText $readonlyContent */ |
202
|
|
|
$readonlyContent = $readonly->Field(); |
203
|
|
|
// phpcs:disable |
204
|
|
|
$this->assertEquals( |
205
|
|
|
<<<EOS |
206
|
|
|
<span class="readonly typography" id="Content"> |
207
|
|
|
<img src="/assets/HTMLEditorFieldTest/f5c7c2f814/example__ResizedImageWzEwLDIwXQ.jpg" alt="example" width="10" height="20"> |
208
|
|
|
</span> |
209
|
|
|
|
210
|
|
|
<input type="hidden" name="Content" value="[image src="/assets/HTMLEditorFieldTest/f5c7c2f814/example.jpg" width="10" height="20" id="{$fileID}"]" /> |
211
|
|
|
|
212
|
|
|
|
213
|
|
|
EOS |
214
|
|
|
, |
215
|
|
|
$readonlyContent->getValue() |
216
|
|
|
); |
217
|
|
|
// phpcs:enable |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|