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