xref: /webtrees/tests/app/Reports/RightToLeftSupportTest.php (revision 062377ee959c5bca8672cc6d68c83a87e2c6abfc)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2021 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Reports;
21
22use Fisharebest\Webtrees\I18N;
23use Fisharebest\Webtrees\TestCase;
24
25/**
26 * Test the RTL functions.  This is very old code, and poorly understood.
27 * These tests exist to capture the existing functionality, and prevent regression during refactoring.
28 */
29class RightToLeftSupportTest extends TestCase
30{
31    /**
32     * @covers \Fisharebest\Webtrees\Reports\RightToLeftSupport
33     *
34     * @return void
35     */
36    public function testEmptyString(): void
37    {
38        I18N::init('en-US', true);
39        $this->assertSame(
40            '',
41            RightToLeftSupport::spanLtrRtl('')
42        );
43
44        I18N::init('he', true);
45        $this->assertSame(
46            '',
47            RightToLeftSupport::spanLtrRtl('')
48        );
49    }
50
51    /**
52     * @covers \Fisharebest\Webtrees\Reports\RightToLeftSupport
53     *
54     * @return void
55     */
56    public function testStripControlCharacters(): void
57    {
58        I18N::init('en-US', true);
59        $this->assertSame(
60            '<span dir="ltr">foobar</span>',
61            RightToLeftSupport::spanLtrRtl('foo&lrm;bar')
62        );
63        $this->assertSame(
64            '<span dir="ltr">foobar</span>',
65            RightToLeftSupport::spanLtrRtl('foo&rlm;bar')
66        );
67        $this->assertSame(
68            '<span dir="ltr">foobar</span>',
69            RightToLeftSupport::spanLtrRtl("foo\xE2\x80\x8Ebar")
70        );
71        $this->assertSame(
72            '<span dir="ltr">foobar</span>',
73            RightToLeftSupport::spanLtrRtl("foo\xE2\x80\x8Fbar")
74        );
75        $this->assertSame(
76            '<span dir="ltr">foobar</span>',
77            RightToLeftSupport::spanLtrRtl("foo\xE2\x80\xADbar")
78        );
79        $this->assertSame(
80            '<span dir="ltr">foobar</span>',
81            RightToLeftSupport::spanLtrRtl("foo\xE2\x80\xAEbar")
82        );
83        $this->assertSame(
84            '<span dir="ltr">foobar</span>',
85            RightToLeftSupport::spanLtrRtl("foo\xE2\x80\xAAbar")
86        );
87        $this->assertSame(
88            '<span dir="ltr">foobar</span>',
89            RightToLeftSupport::spanLtrRtl("foo\xE2\x80\xABbar")
90        );
91        $this->assertSame(
92            '<span dir="ltr">foobar</span>',
93            RightToLeftSupport::spanLtrRtl("foo\xE2\x80\xACbar")
94        );
95
96        I18N::init('he', true);
97        $this->assertSame(
98            '<span dir="ltr">foobar</span>',
99            RightToLeftSupport::spanLtrRtl('foo&lrm;bar')
100        );
101        $this->assertSame(
102            '<span dir="ltr">foobar</span>',
103            RightToLeftSupport::spanLtrRtl('foo&rlm;bar')
104        );
105        $this->assertSame(
106            '<span dir="ltr">foobar</span>',
107            RightToLeftSupport::spanLtrRtl("foo\xE2\x80\x8Ebar")
108        );
109        $this->assertSame(
110            '<span dir="ltr">foobar</span>',
111            RightToLeftSupport::spanLtrRtl("foo\xE2\x80\x8Fbar")
112        );
113        $this->assertSame(
114            '<span dir="ltr">foobar</span>',
115            RightToLeftSupport::spanLtrRtl("foo\xE2\x80\xADbar")
116        );
117        $this->assertSame(
118            '<span dir="ltr">foobar</span>',
119            RightToLeftSupport::spanLtrRtl("foo\xE2\x80\xAEbar")
120        );
121        $this->assertSame(
122            '<span dir="ltr">foobar</span>',
123            RightToLeftSupport::spanLtrRtl("foo\xE2\x80\xAAbar")
124        );
125        $this->assertSame(
126            '<span dir="ltr">foobar</span>',
127            RightToLeftSupport::spanLtrRtl("foo\xE2\x80\xABbar")
128        );
129        $this->assertSame(
130            '<span dir="ltr">foobar</span>',
131            RightToLeftSupport::spanLtrRtl("foo\xE2\x80\xACbar")
132        );
133    }
134
135    /**
136     * @covers \Fisharebest\Webtrees\Reports\RightToLeftSupport
137     *
138     * @return void
139     */
140    public function testNewLinesBecomeHTMLBreaks(): void
141    {
142        I18N::init('en-US', true);
143        $this->assertSame(
144            '<span dir="ltr">foo</span><br><span dir="ltr">bar</span>',
145            RightToLeftSupport::spanLtrRtl("foo\nbar")
146        );
147        $this->assertSame(
148            '<span dir="rtl">אבג</span><br><span dir="rtl">דהו</span>',
149            RightToLeftSupport::spanLtrRtl("אבג\nדהו")
150        );
151
152        I18N::init('he', true);
153        $this->assertSame(
154            '<span dir="ltr">foo</span><br><span dir="ltr">bar</span>',
155            RightToLeftSupport::spanLtrRtl("foo\nbar")
156        );
157        $this->assertSame(
158            '<span dir="rtl">אבג</span><br><span dir="rtl">דהו</span>',
159            RightToLeftSupport::spanLtrRtl("אבג\nדהו")
160        );
161    }
162
163    /**
164     * @covers \Fisharebest\Webtrees\Reports\RightToLeftSupport
165     *
166     * @return void
167     */
168    public function testLineBreaks(): void
169    {
170        I18N::init('en-US', true);
171        $this->assertSame(
172            '<span dir="ltr">foo</span><br><span dir="ltr">bar</span>',
173            RightToLeftSupport::spanLtrRtl("foo<br>bar")
174        );
175        $this->assertSame(
176            '<span dir="rtl">אבג</span><br><span dir="rtl">דהו</span>',
177            RightToLeftSupport::spanLtrRtl("אבג<br>דהו")
178        );
179
180        I18N::init('he', true);
181        $this->assertSame(
182            '<span dir="ltr">foo</span><br><span dir="ltr">bar</span>',
183            RightToLeftSupport::spanLtrRtl("foo<br>bar")
184        );
185        $this->assertSame(
186            '<span dir="rtl">אבג</span><br><span dir="rtl">דהו</span>',
187            RightToLeftSupport::spanLtrRtl("אבג<br>דהו")
188        );
189    }
190
191    /**
192     * @covers \Fisharebest\Webtrees\Reports\RightToLeftSupport
193     *
194     * @return void
195     */
196    public function testHtmlEntities(): void
197    {
198        I18N::init('en-US', true);
199        $this->assertSame(
200            '<span dir="ltr">foo&nbsp;bar</span>',
201            RightToLeftSupport::spanLtrRtl('foo&nbsp;bar')
202        );
203        $this->assertSame(
204            '<span dir="rtl">אבג&nbsp;דהו</span>',
205            RightToLeftSupport::spanLtrRtl('אבג&nbsp;דהו')
206        );
207        $this->assertSame(
208            '<span dir="ltr">foo&bar</span>',
209            RightToLeftSupport::spanLtrRtl('foo&bar')
210        );
211
212        I18N::init('he', true);
213        $this->assertSame(
214            '<span dir="ltr">foo&nbsp;bar</span>',
215            RightToLeftSupport::spanLtrRtl("foo&nbsp;bar")
216        );
217        $this->assertSame(
218            '<span dir="rtl">אבג&nbsp;דהו</span>',
219            RightToLeftSupport::spanLtrRtl("אבג&nbsp;דהו")
220        );
221        $this->assertSame(
222            '<span dir="ltr">foo&bar</span>',
223            RightToLeftSupport::spanLtrRtl('foo&bar')
224        );
225    }
226
227    /**
228     * @covers \Fisharebest\Webtrees\Reports\RightToLeftSupport
229     *
230     * @return void
231     */
232    public function testBraces(): void
233    {
234        I18N::init('en-US', true);
235        $this->assertSame(
236            '<span dir="ltr">foo{{123}}bar</span>',
237            RightToLeftSupport::spanLtrRtl('foo{{123}}bar')
238        );
239        $this->assertSame(
240            '<span dir="ltr">foo{{bar</span>',
241            RightToLeftSupport::spanLtrRtl('foo{{bar')
242        );
243        $this->assertSame(
244            '<span dir="rtl">אבג{{123}}דהו</span>',
245            RightToLeftSupport::spanLtrRtl('אבג{{123}}דהו')
246        );
247
248        I18N::init('he', true);
249        $this->assertSame(
250            '<span dir="ltr">foo{{123}}bar</span>',
251            RightToLeftSupport::spanLtrRtl('foo{{123}}bar')
252        );
253        $this->assertSame(
254            '<span dir="ltr">foo{{bar</span>',
255            RightToLeftSupport::spanLtrRtl('foo{{bar')
256        );
257        $this->assertSame(
258            '<span dir="rtl">אבג{{123}}דהו</span>',
259            RightToLeftSupport::spanLtrRtl('אבג{{123}}דהו')
260        );
261    }
262
263    /**
264     * @covers \Fisharebest\Webtrees\Reports\RightToLeftSupport
265     *
266     * @return void
267     */
268    public function testNumbers(): void
269    {
270        I18N::init('en-US', true);
271        $this->assertSame(
272            '<span dir="ltr">foo 123,456.789 bar</span>',
273            RightToLeftSupport::spanLtrRtl('foo 123,456.789 bar')
274        );
275        $this->assertSame(
276            '<span dir="ltr">foo +123,456.789 bar</span>',
277            RightToLeftSupport::spanLtrRtl('foo +123,456.789 bar')
278        );
279        $this->assertSame(
280            '<span dir="ltr">foo -123,456.789 bar</span>',
281            RightToLeftSupport::spanLtrRtl('foo -123,456.789 bar')
282        );
283        $this->assertSame(
284            '<span dir="rtl">אבג ‪123,456.789‬ דהו</span>',
285            RightToLeftSupport::spanLtrRtl('אבג 123,456.789 דהו')
286        );
287        $this->assertSame(
288            '<span dir="rtl">אבג ‪+123,456.789‬ דהו</span>',
289            RightToLeftSupport::spanLtrRtl('אבג +123,456.789 דהו')
290        );
291        $this->assertSame(
292            '<span dir="rtl">אבג ‪-123,456.789‬ דהו</span>',
293            RightToLeftSupport::spanLtrRtl('אבג -123,456.789 דהו')
294        );
295
296        I18N::init('he', true);
297        $this->assertSame(
298            '<span dir="ltr">foo 123,456.789 bar</span>',
299            RightToLeftSupport::spanLtrRtl('foo 123,456.789 bar')
300        );
301        $this->assertSame(
302            '<span dir="ltr">foo +123,456.789 bar</span>',
303            RightToLeftSupport::spanLtrRtl('foo +123,456.789 bar')
304        );
305        $this->assertSame(
306            '<span dir="ltr">foo -123,456.789 bar</span>',
307            RightToLeftSupport::spanLtrRtl('foo -123,456.789 bar')
308        );
309        $this->assertSame(
310            '<span dir="rtl">אבג ‪123,456.789‬ דהו</span>',
311            RightToLeftSupport::spanLtrRtl('אבג 123,456.789 דהו')
312        );
313        $this->assertSame(
314            '<span dir="rtl">אבג ‪+123,456.789‬ דהו</span>',
315            RightToLeftSupport::spanLtrRtl('אבג +123,456.789 דהו')
316        );
317        $this->assertSame(
318            '<span dir="rtl">אבג ‪-123,456.789‬ דהו</span>',
319            RightToLeftSupport::spanLtrRtl('אבג -123,456.789 דהו')
320        );
321    }
322
323    /**
324     * @covers \Fisharebest\Webtrees\Reports\RightToLeftSupport
325     *
326     * @return void
327     */
328    public function testParentheses(): void
329    {
330        I18N::init('en-US', true);
331        $this->assertSame(
332            '<span dir="ltr">foo (bar)</span>',
333            RightToLeftSupport::spanLtrRtl('foo (bar)')
334        );
335        $this->assertSame(
336            '<span dir="ltr">foo </span><span dir="rtl">(אבג)</span>',
337            RightToLeftSupport::spanLtrRtl('foo (אבג)')
338        );
339        $this->assertSame(
340            '<span dir="rtl">אבג</span><span dir="ltr"> (bar)</span>',
341            RightToLeftSupport::spanLtrRtl('אבג (bar)')
342        );
343        $this->assertSame(
344            '<span dir="rtl">אבג (דהו)</span>',
345            RightToLeftSupport::spanLtrRtl('אבג (דהו)')
346        );
347
348        I18N::init('he', true);
349        $this->assertSame(
350            '<span dir="ltr">foo (bar)</span>',
351            RightToLeftSupport::spanLtrRtl('foo (bar)')
352        );
353        $this->assertSame(
354            '<span dir="ltr">foo </span><span dir="rtl">(אבג)</span>',
355            RightToLeftSupport::spanLtrRtl('foo (אבג)')
356        );
357        $this->assertSame(
358            '<span dir="rtl">אבג </span><span dir="ltr">(bar)</span>',
359            RightToLeftSupport::spanLtrRtl('אבג (bar)')
360        );
361        $this->assertSame(
362            '<span dir="rtl">אבג (דהו)</span>',
363            RightToLeftSupport::spanLtrRtl('אבג (דהו)')
364        );
365    }
366
367    /**
368     * @covers \Fisharebest\Webtrees\Reports\RightToLeftSupport
369     *
370     * @return void
371     */
372    public function testUnescapedHtml(): void
373    {
374        I18N::init('en-US', true);
375        $this->assertSame(
376            '<span dir="ltr">>foo<</span>',
377            RightToLeftSupport::spanLtrRtl(">foo<")
378        );
379        $this->assertSame(
380            '<span dir="ltr">></span><span dir="rtl">אבג<</span>',
381            RightToLeftSupport::spanLtrRtl(">אבג<")
382        );
383
384        I18N::init('he', true);
385        $this->assertSame(
386            '<span dir="rtl">></span><span dir="ltr">foo<</span>',
387            RightToLeftSupport::spanLtrRtl(">foo<")
388        );
389        $this->assertSame(
390            '<span dir="rtl">>אבג<</span>',
391            RightToLeftSupport::spanLtrRtl(">אבג<")
392        );
393    }
394
395    /**
396     * @covers \Fisharebest\Webtrees\Reports\RightToLeftSupport
397     *
398     * @return void
399     */
400    public function testBreakInNumber(): void
401    {
402        I18N::init('en-US', true);
403        $this->assertSame(
404            '<span dir="ltr">123</span><br><span dir="ltr">456</span>',
405            RightToLeftSupport::spanLtrRtl("123<br>456")
406        );
407
408        I18N::init('he', true);
409        $this->assertSame(
410            '<span dir="rtl">‪123‬</span><br><span dir="rtl">‪456‬</span>',
411            RightToLeftSupport::spanLtrRtl("123<br>456")
412        );
413    }
414}
415