-
Notifications
You must be signed in to change notification settings - Fork 1
/
FrontendHyphenatorTest.php
220 lines (192 loc) · 12.1 KB
/
FrontendHyphenatorTest.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
<?php
/*
* Copyright (c) 2021 Heimrich & Hannot GmbH
*
* @license LGPL-3.0-or-later
*/
namespace HeimrichHannot\HyphenatorBundle\Tests\Hyphenator;
use Contao\CoreBundle\Config\ResourceFinder;
use Contao\TestCase\ContaoTestCase;
use HeimrichHannot\HyphenatorBundle\Hyphenator\FrontendHyphenator;
use HeimrichHannot\UtilsBundle\Model\ModelUtil;
use Symfony\Component\DependencyInjection\ContainerInterface;
class FrontendHyphenatorTest extends ContaoTestCase
{
/**
* @var ContainerInterface
*/
private $container;
/**
* {@inheritdoc}
*/
protected function setUp()
{
parent::setUp();
if (!\defined('TL_ROOT')) {
\define('TL_ROOT', $this->getFixturesDir());
}
$this->container = $this->mockContainer();
}
/**
* Tests the object instantiation.
*/
public function testCanBeInstantiated()
{
$listener = new FrontendHyphenator($this->container);
$this->assertInstanceOf(FrontendHyphenator::class, $listener);
}
/**
* Test hyphenate without page context.
*/
public function testHyphenateWithoutPageContext()
{
$listener = new FrontendHyphenator($this->container);
$this->assertSame('test', $listener->hyphenate('test'));
}
/**
* Test hyphenate without page context.
*
* @dataProvider hyphenationProvider
*/
public function testHyphenate($buffer, array $pageData, array $config, $expected)
{
$this->container->set('contao.resource_finder', new ResourceFinder([$this->getFixturesDir().'/vendor/contao/core-bundle/src/Resources/contao']));
$this->container->setParameter('kernel.cache_dir', $this->getFixturesDir().'/var/cache');
$modelUtil = $this->createMock(ModelUtil::class);
$this->container->set('huh.utils.model', $modelUtil);
$this->container->setParameter('huh_hyphenator', ['skip_tags' => ['script', 'img']]);
$listener = new FrontendHyphenator($this->container);
global $objPage;
$objPage = (object) $pageData;
foreach ($config as $key => $value) {
$GLOBALS['TL_CONFIG'][$key] = $value;
}
$this->assertSame($expected, $listener->hyphenate($buffer));
}
public function hyphenationProvider()
{
$GLOBALS['TL_CONFIG']['hyphenator_tags'] = 'h1, h1> a, h2, h2 > a, h3, h3 > a, h4, h4 > a, h5, h5 > a, h6, h6 > a, p';
$GLOBALS['TL_CONFIG']['hyphenator_wordMin'] = 10;
$GLOBALS['TL_CONFIG']['hyphenator_hyphen'] = '­';
$GLOBALS['TL_CONFIG']['hyphenator_skipPages'] = [];
$GLOBALS['TL_CONFIG']['hyphenator_enableCache'] = true;
$GLOBALS['TL_CONFIG']['hyphenator_hyphenedLeftMin'] = 6;
$GLOBALS['TL_CONFIG']['hyphenator_hyphenedRightMin'] = 6;
return [
[
'<p>We have some really long words in german like sauerstofffeldflasche.</p>',
$this->getPage(),
$this->getConfig(['hyphenator_skipPages' => [2]]),
'<p>We have some really long words in german like sauerstofffeldflasche.</p>',
],
[
'<p>We have some really long words in german like sauerstofffeldflasche.</p>',
$this->getPage(),
$this->getConfig(),
'<p>We have some really long words in german like sau­er­stoff­feld­fla­sche.</p>',
],
[
'<p>Kromě toho můžete závodit na cyklotrenažerech, řešit hádanky, vyrábět a stavět z Lega.</p>',
$this->getPage(['customLineBreakExceptions' => true, 'lineBreakExceptions' => [[], ['search' => '(\s\w{1})(\s)', 'replace' => '$1 ']]]),
$this->getConfig(['hyphenator_locale_language_mapping' => ['cz' => 'cs']]),
'<p>Kromě toho můžete závodit na cy­klot­ren­aže­rech, řešit hádanky, vyrábět<span class="text-nowrap"> a </span>stavět<span class="text-nowrap"> z </span>Lega.</p>',
],
[
'<p>Die Musterfirma AG hat den Kredit in Höhe von 126 Mio. € bewilligt und somit die Realisierung der ersten 81.000 m² finanziert.</p>',
$this->getPage(['customLineBreakExceptions' => true, 'lineBreakExceptions' => [['search' => 'Musterfirma AG', 'replace' => ''], ['search' => '(\d|€)(\s)(\w)', 'replace' => '$1[nbsp]$3']]]),
$this->getConfig(['hyphenator_locale_language_mapping' => ['cz' => 'cs']]),
'<p>Die <span class="text-nowrap">Mus­ter­fir­ma AG</span> hat den Kredit in Höhe von 12<span class="text-nowrap">6 M</span>io. € bewilligt und somit die Rea­li­sie­rung der ersten 81.00<span class="text-nowrap">0 m</span>² fi­nan­ziert.</p>',
],
[
'<p>Die Musterfirma AG wurde unter dem Namen Musterfirmas gegründet.</p>',
$this->getPage(['customLineBreakExceptions' => true, 'lineBreakExceptions' => [['search' => 'Musterfirma(?:\sAG)|Musterfirma(?:s)', 'replace' => '']]]),
$this->getConfig(['hyphenator_locale_language_mapping' => ['cz' => 'cs']]),
'<p>Die <span class="text-nowrap">Mus­ter­fir­ma AG</span> wurde unter dem Namen <span class="text-nowrap">Mus­ter­fir­mas</span> gegründet.</p>',
],
[
'<p>We have some really long words in german like <a href="http://sauerstofffeldflasche.de" title="sauerstofffeldflasche">sauerstofffeldflasche.</a></p>',
$this->getPage(),
$this->getConfig(),
'<p>We have some really long words in german like <a href="http://sauerstofffeldflasche.de" title="sauerstofffeldflasche">sau­er­stoff­feld­fla­sche.</a></p>',
],
[
'<!DOCTYPE html><html><head></head><body class="index <esi:include src="/_fragment?_path=insertTag={{ua::class}}&_format=html&_locale=de&_controller=contao.controller.insert_tags:renderAction&clientCache=0&pageId=4&request=de/&_hash=zZYbGzqkVE2ZqKSMgbxxQT6iXorr3OSFLJVqBiou0HE=" onerror="continue"/>"><p>We have some really long words in german like sauerstofffeldflasche.</p></body></html>',
$this->getPage(),
$this->getConfig(),
"<!DOCTYPE html>\n<html><head></head><body class=\"index <esi:include src=\"/_fragment?_path=insertTag={{ua::class}}&_format=html&_locale=de&_controller=contao.controller.insert_tags:renderAction&clientCache=0&pageId=4&request=de/&_hash=zZYbGzqkVE2ZqKSMgbxxQT6iXorr3OSFLJVqBiou0HE=\" onerror=\"continue\"/>\"><p>We have some really long words in german like sau­er­stoff­feld­fla­sche.</p></body></html>\n",
],
[
'<p>Familienunternehmen</p>',
$this->getPage(),
$this->getConfig(),
'<p>Fa­mi­li­en­un­ter­neh­men</p>',
],
[
'<p>Wasserwirtschaft</p>',
$this->getPage(),
$this->getConfig(),
'<p>Was­ser­wirt­schaft</p>',
],
[
'<h1><a href="http://sauerstofffeldflasche.de" title="sauerstofffeldflasche">We have some really long words in german like sauerstofffeldflasche.</a></h1>',
$this->getPage(),
$this->getConfig(),
'<h1><a href="http://sauerstofffeldflasche.de" title="sauerstofffeldflasche">We have some really long words in german like sau­er­stoff­feld­fla­sche.</a></h1>',
],
[
'<p><br> Tel: +49 40 123 45 67<br> Fax: +49 40 123 45 68 <br> {{email::test@test.de} <br> <br><a class="more" href="mailto:{{email_url::test@test.de}}">Kontaktieren Sie uns</a></p>',
$this->getPage(),
$this->getConfig(),
'<p><br> Tel: +49 40 123 45 67<br> Fax: +49 40 123 45 68 <br> {{email::test@test.de} <br> <br><a class="more" href="mailto:%7B%7Bemail_url::test@test.de%7D%7D">Kon­tak­tie­ren Sie uns</a></p>',
],
[
'<h1><a href="mailto:test@test.com" title="email obfuscation test for test@test.com">test@test.com</a></h1>',
$this->getPage(),
$this->getConfig(),
'<h1><a href="mailto:test@test.com" title="email obfuscation test for test@test.com">test@test.com</a></h1>',
],
[
'<picture><!--[if IE 9]><video style="display: none;"><![endif]--><source srcset="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkqAcAAIUAgUW0RjgAAAAASUVORK5CYII=" media="(min-width: 853px)"><!--[if IE 9]></video><![endif]--><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkqAcAAIUAgUW0RjgAAAAASUVORK5CYII=" data-wrapper="#image-wrapper-1368074709" class="image" alt=""></picture>',
$this->getPage(),
$this->getConfig(),
'<picture><!--[if IE 9]><video style="display: none;"><![endif]--><source srcset="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkqAcAAIUAgUW0RjgAAAAASUVORK5CYII=" media="(min-width: 853px)"><!--[if IE 9]></video><![endif]--><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkqAcAAIUAgUW0RjgAAAAASUVORK5CYII=" data-wrapper="#image-wrapper-1368074709" class="image" alt=""></picture>',
],
[
'<a><script>document.querySelectorAll("#id").addEventListener("click", e => {console.log(e.target})</script></a>',
$this->getPage(),
$this->getConfig(),
'<a><script>document.querySelectorAll("#id").addEventListener("click", e => {console.log(e.target})</script></a>',
],
[
'<a><picture><!--[if IE 9]><video style="display: none;"><![endif]--><source srcset="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkqAcAAIUAgUW0RjgAAAAASUVORK5CYII=" media="(min-width: 853px)"><!--[if IE 9]></video><![endif]--><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkqAcAAIUAgUW0RjgAAAAASUVORK5CYII=" data-wrapper="#image-wrapper-1368074709" class="image" alt=""></picture></a>',
$this->getPage(),
$this->getConfig(),
'<a><picture><!--[if IE 9]><video style="display: none;"><![endif]--><source srcset="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkqAcAAIUAgUW0RjgAAAAASUVORK5CYII=" media="(min-width: 853px)"><!--[if IE 9]></video><![endif]--><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkqAcAAIUAgUW0RjgAAAAASUVORK5CYII=" data-wrapper="#image-wrapper-1368074709" class="image" alt=""></picture></a>',
],
];
}
public function getPage(array $data = [])
{
$page['id'] = 2;
$page['language'] = 'de';
$page['customLineBreakExceptions'] = false;
$page['rootId'] = 1;
$page['pid'] = 1;
$page['hyphenation'] = 'active';
$data = array_merge($page, $data);
$GLOBALS['TL_LANGUAGE'] = $data['language'];
return $data;
}
public function getConfig(array $data = [])
{
$config['hyphenator_tags'] = 'h1, h1> a, h2, h2 > a, h3, h3 > a, h4, h4 > a, h5, h5 > a, h6, h6 > a, p';
$config['hyphenator_wordMin'] = 10;
$config['hyphenator_hyphen'] = '­';
$config['hyphenator_skipPages'] = [];
return array_merge($config, $data);
}
protected function getFixturesDir(): string
{
return __DIR__.\DIRECTORY_SEPARATOR.'..'.\DIRECTORY_SEPARATOR.'Fixtures';
}
}