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