xref: /webtrees/tests/app/Reports/RightToLeftSupportTest.php (revision 67497a53f705a1c2998fed00c3bba0fd1c7a86ed)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2023 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\Report\RightToLeftSupport;
24use Fisharebest\Webtrees\TestCase;
25
26/**
27 * Test the RTL functions.  This is very old code, and poorly understood.
28 * These tests exist to capture the existing functionality, and prevent regression during refactoring.
29 */
30class RightToLeftSupportTest extends TestCase
31{
32    /**
33     * @covers \Fisharebest\Webtrees\Report\RightToLeftSupport
34     */
35    public function testEmptyString(): void
36    {
37        I18N::init('en-US', true);
38        static::assertSame(
39            '',
40            RightToLeftSupport::spanLtrRtl('')
41        );
42
43        I18N::init('he', true);
44        static::assertSame(
45            '',
46            RightToLeftSupport::spanLtrRtl('')
47        );
48    }
49
50    /**
51     * @covers \Fisharebest\Webtrees\Report\RightToLeftSupport
52     */
53    public function testStripControlCharacters(): void
54    {
55        I18N::init('en-US', true);
56        static::assertSame(
57            '<span dir="ltr">foobar</span>',
58            RightToLeftSupport::spanLtrRtl('foo&lrm;bar')
59        );
60        static::assertSame(
61            '<span dir="ltr">foobar</span>',
62            RightToLeftSupport::spanLtrRtl('foo&rlm;bar')
63        );
64        static::assertSame(
65            '<span dir="ltr">foobar</span>',
66            RightToLeftSupport::spanLtrRtl("foo\xE2\x80\x8Ebar")
67        );
68        static::assertSame(
69            '<span dir="ltr">foobar</span>',
70            RightToLeftSupport::spanLtrRtl("foo\xE2\x80\x8Fbar")
71        );
72        static::assertSame(
73            '<span dir="ltr">foobar</span>',
74            RightToLeftSupport::spanLtrRtl("foo\xE2\x80\xADbar")
75        );
76        static::assertSame(
77            '<span dir="ltr">foobar</span>',
78            RightToLeftSupport::spanLtrRtl("foo\xE2\x80\xAEbar")
79        );
80        static::assertSame(
81            '<span dir="ltr">foobar</span>',
82            RightToLeftSupport::spanLtrRtl("foo\xE2\x80\xAAbar")
83        );
84        static::assertSame(
85            '<span dir="ltr">foobar</span>',
86            RightToLeftSupport::spanLtrRtl("foo\xE2\x80\xABbar")
87        );
88        static::assertSame(
89            '<span dir="ltr">foobar</span>',
90            RightToLeftSupport::spanLtrRtl("foo\xE2\x80\xACbar")
91        );
92
93        I18N::init('he', true);
94        static::assertSame(
95            '<span dir="ltr">foobar</span>',
96            RightToLeftSupport::spanLtrRtl('foo&lrm;bar')
97        );
98        static::assertSame(
99            '<span dir="ltr">foobar</span>',
100            RightToLeftSupport::spanLtrRtl('foo&rlm;bar')
101        );
102        static::assertSame(
103            '<span dir="ltr">foobar</span>',
104            RightToLeftSupport::spanLtrRtl("foo\xE2\x80\x8Ebar")
105        );
106        static::assertSame(
107            '<span dir="ltr">foobar</span>',
108            RightToLeftSupport::spanLtrRtl("foo\xE2\x80\x8Fbar")
109        );
110        static::assertSame(
111            '<span dir="ltr">foobar</span>',
112            RightToLeftSupport::spanLtrRtl("foo\xE2\x80\xADbar")
113        );
114        static::assertSame(
115            '<span dir="ltr">foobar</span>',
116            RightToLeftSupport::spanLtrRtl("foo\xE2\x80\xAEbar")
117        );
118        static::assertSame(
119            '<span dir="ltr">foobar</span>',
120            RightToLeftSupport::spanLtrRtl("foo\xE2\x80\xAAbar")
121        );
122        static::assertSame(
123            '<span dir="ltr">foobar</span>',
124            RightToLeftSupport::spanLtrRtl("foo\xE2\x80\xABbar")
125        );
126        static::assertSame(
127            '<span dir="ltr">foobar</span>',
128            RightToLeftSupport::spanLtrRtl("foo\xE2\x80\xACbar")
129        );
130    }
131
132    /**
133     * @covers \Fisharebest\Webtrees\Report\RightToLeftSupport
134     */
135    public function testNewLinesBecomeHTMLBreaks(): void
136    {
137        I18N::init('en-US', true);
138        static::assertSame(
139            '<span dir="ltr">foo</span><br><span dir="ltr">bar</span>',
140            RightToLeftSupport::spanLtrRtl("foo\nbar")
141        );
142        static::assertSame(
143            '<span dir="rtl">אבג</span><br><span dir="rtl">דהו</span>',
144            RightToLeftSupport::spanLtrRtl("אבג\nדהו")
145        );
146
147        I18N::init('he', true);
148        static::assertSame(
149            '<span dir="ltr">foo</span><br><span dir="ltr">bar</span>',
150            RightToLeftSupport::spanLtrRtl("foo\nbar")
151        );
152        static::assertSame(
153            '<span dir="rtl">אבג</span><br><span dir="rtl">דהו</span>',
154            RightToLeftSupport::spanLtrRtl("אבג\nדהו")
155        );
156    }
157
158    /**
159     * @covers \Fisharebest\Webtrees\Report\RightToLeftSupport
160     */
161    public function testLineBreaks(): void
162    {
163        I18N::init('en-US', true);
164        static::assertSame(
165            '<span dir="ltr">foo</span><br><span dir="ltr">bar</span>',
166            RightToLeftSupport::spanLtrRtl('foo<br>bar')
167        );
168        static::assertSame(
169            '<span dir="rtl">אבג</span><br><span dir="rtl">דהו</span>',
170            RightToLeftSupport::spanLtrRtl('אבג<br>דהו')
171        );
172
173        I18N::init('he', true);
174        static::assertSame(
175            '<span dir="ltr">foo</span><br><span dir="ltr">bar</span>',
176            RightToLeftSupport::spanLtrRtl('foo<br>bar')
177        );
178        static::assertSame(
179            '<span dir="rtl">אבג</span><br><span dir="rtl">דהו</span>',
180            RightToLeftSupport::spanLtrRtl('אבג<br>דהו')
181        );
182    }
183
184    /**
185     * @covers \Fisharebest\Webtrees\Report\RightToLeftSupport
186     */
187    public function testHtmlEntities(): void
188    {
189        I18N::init('en-US', true);
190        static::assertSame(
191            '<span dir="ltr">foo&nbsp;bar</span>',
192            RightToLeftSupport::spanLtrRtl('foo&nbsp;bar')
193        );
194        static::assertSame(
195            '<span dir="rtl">אבג&nbsp;דהו</span>',
196            RightToLeftSupport::spanLtrRtl('אבג&nbsp;דהו')
197        );
198        static::assertSame(
199            '<span dir="ltr">foo&bar</span>',
200            RightToLeftSupport::spanLtrRtl('foo&bar')
201        );
202
203        I18N::init('he', true);
204        static::assertSame(
205            '<span dir="ltr">foo&nbsp;bar</span>',
206            RightToLeftSupport::spanLtrRtl('foo&nbsp;bar')
207        );
208        static::assertSame(
209            '<span dir="rtl">אבג&nbsp;דהו</span>',
210            RightToLeftSupport::spanLtrRtl('אבג&nbsp;דהו')
211        );
212        static::assertSame(
213            '<span dir="ltr">foo&bar</span>',
214            RightToLeftSupport::spanLtrRtl('foo&bar')
215        );
216    }
217
218    /**
219     * @covers \Fisharebest\Webtrees\Report\RightToLeftSupport
220     */
221    public function testBraces(): void
222    {
223        I18N::init('en-US', true);
224        static::assertSame(
225            '<span dir="ltr">foo{{123}}bar</span>',
226            RightToLeftSupport::spanLtrRtl('foo{{123}}bar')
227        );
228        static::assertSame(
229            '<span dir="ltr">foo{{bar</span>',
230            RightToLeftSupport::spanLtrRtl('foo{{bar')
231        );
232        static::assertSame(
233            '<span dir="rtl">אבג{{123}}דהו</span>',
234            RightToLeftSupport::spanLtrRtl('אבג{{123}}דהו')
235        );
236
237        I18N::init('he', true);
238        static::assertSame(
239            '<span dir="ltr">foo{{123}}bar</span>',
240            RightToLeftSupport::spanLtrRtl('foo{{123}}bar')
241        );
242        static::assertSame(
243            '<span dir="ltr">foo{{bar</span>',
244            RightToLeftSupport::spanLtrRtl('foo{{bar')
245        );
246        static::assertSame(
247            '<span dir="rtl">אבג{{123}}דהו</span>',
248            RightToLeftSupport::spanLtrRtl('אבג{{123}}דהו')
249        );
250    }
251
252    /**
253     * @covers \Fisharebest\Webtrees\Report\RightToLeftSupport
254     */
255    public function testNumbers(): void
256    {
257        I18N::init('en-US', true);
258        static::assertSame(
259            '<span dir="ltr">foo 123,456.789 bar</span>',
260            RightToLeftSupport::spanLtrRtl('foo 123,456.789 bar')
261        );
262        static::assertSame(
263            '<span dir="ltr">foo +123,456.789 bar</span>',
264            RightToLeftSupport::spanLtrRtl('foo +123,456.789 bar')
265        );
266        static::assertSame(
267            '<span dir="ltr">foo -123,456.789 bar</span>',
268            RightToLeftSupport::spanLtrRtl('foo -123,456.789 bar')
269        );
270        static::assertSame(
271            '<span dir="rtl">אבג ‪123,456.789‬ דהו</span>',
272            RightToLeftSupport::spanLtrRtl('אבג 123,456.789 דהו')
273        );
274        static::assertSame(
275            '<span dir="rtl">אבג ‪+123,456.789‬ דהו</span>',
276            RightToLeftSupport::spanLtrRtl('אבג +123,456.789 דהו')
277        );
278        static::assertSame(
279            '<span dir="rtl">אבג ‪-123,456.789‬ דהו</span>',
280            RightToLeftSupport::spanLtrRtl('אבג -123,456.789 דהו')
281        );
282
283        I18N::init('he', true);
284        static::assertSame(
285            '<span dir="ltr">foo 123,456.789 bar</span>',
286            RightToLeftSupport::spanLtrRtl('foo 123,456.789 bar')
287        );
288        static::assertSame(
289            '<span dir="ltr">foo +123,456.789 bar</span>',
290            RightToLeftSupport::spanLtrRtl('foo +123,456.789 bar')
291        );
292        static::assertSame(
293            '<span dir="ltr">foo -123,456.789 bar</span>',
294            RightToLeftSupport::spanLtrRtl('foo -123,456.789 bar')
295        );
296        static::assertSame(
297            '<span dir="rtl">אבג ‪123,456.789‬ דהו</span>',
298            RightToLeftSupport::spanLtrRtl('אבג 123,456.789 דהו')
299        );
300        static::assertSame(
301            '<span dir="rtl">אבג ‪+123,456.789‬ דהו</span>',
302            RightToLeftSupport::spanLtrRtl('אבג +123,456.789 דהו')
303        );
304        static::assertSame(
305            '<span dir="rtl">אבג ‪-123,456.789‬ דהו</span>',
306            RightToLeftSupport::spanLtrRtl('אבג -123,456.789 דהו')
307        );
308    }
309
310    /**
311     * @covers \Fisharebest\Webtrees\Report\RightToLeftSupport
312     */
313    public function testParentheses(): void
314    {
315        I18N::init('en-US', true);
316        static::assertSame(
317            '<span dir="ltr">foo (bar)</span>',
318            RightToLeftSupport::spanLtrRtl('foo (bar)')
319        );
320        static::assertSame(
321            '<span dir="ltr">foo </span><span dir="rtl">(אבג)</span>',
322            RightToLeftSupport::spanLtrRtl('foo (אבג)')
323        );
324        static::assertSame(
325            '<span dir="rtl">אבג</span><span dir="ltr"> (bar)</span>',
326            RightToLeftSupport::spanLtrRtl('אבג (bar)')
327        );
328        static::assertSame(
329            '<span dir="rtl">אבג (דהו)</span>',
330            RightToLeftSupport::spanLtrRtl('אבג (דהו)')
331        );
332
333        I18N::init('he', true);
334        static::assertSame(
335            '<span dir="ltr">foo (bar)</span>',
336            RightToLeftSupport::spanLtrRtl('foo (bar)')
337        );
338        static::assertSame(
339            '<span dir="ltr">foo </span><span dir="rtl">(אבג)</span>',
340            RightToLeftSupport::spanLtrRtl('foo (אבג)')
341        );
342        static::assertSame(
343            '<span dir="rtl">אבג </span><span dir="ltr">(bar)</span>',
344            RightToLeftSupport::spanLtrRtl('אבג (bar)')
345        );
346        static::assertSame(
347            '<span dir="rtl">אבג (דהו)</span>',
348            RightToLeftSupport::spanLtrRtl('אבג (דהו)')
349        );
350    }
351
352    /**
353     * @covers \Fisharebest\Webtrees\Report\RightToLeftSupport
354     */
355    public function testUnescapedHtml(): void
356    {
357        I18N::init('en-US', true);
358        static::assertSame(
359            '<span dir="ltr">>foo<</span>',
360            RightToLeftSupport::spanLtrRtl('>foo<')
361        );
362        static::assertSame(
363            '<span dir="ltr">></span><span dir="rtl">אבג<</span>',
364            RightToLeftSupport::spanLtrRtl('>אבג<')
365        );
366
367        I18N::init('he', true);
368        static::assertSame(
369            '<span dir="rtl">></span><span dir="ltr">foo<</span>',
370            RightToLeftSupport::spanLtrRtl('>foo<')
371        );
372        static::assertSame(
373            '<span dir="rtl">>אבג<</span>',
374            RightToLeftSupport::spanLtrRtl('>אבג<')
375        );
376    }
377
378    /**
379     * @covers \Fisharebest\Webtrees\Report\RightToLeftSupport
380     */
381    public function testBreakInNumber(): void
382    {
383        I18N::init('en-US', true);
384        static::assertSame(
385            '<span dir="ltr">123</span><br><span dir="ltr">456</span>',
386            RightToLeftSupport::spanLtrRtl('123<br>456')
387        );
388
389        I18N::init('he', true);
390        static::assertSame(
391            '<span dir="rtl">‪123‬</span><br><span dir="rtl">‪456‬</span>',
392            RightToLeftSupport::spanLtrRtl('123<br>456')
393        );
394    }
395}
396