xref: /webtrees/tests/app/Reports/RightToLeftSupportTest.php (revision 5183874f79e6c96ed7a5a66bc6a021feaad4d6f6)
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            '',
42            RightToLeftSupport::spanLtrRtl('')
43        );
44
45        I18N::init('he', true);
46        $this->assertSame(
47            '',
48            RightToLeftSupport::spanLtrRtl('')
49        );
50    }
51
52    /**
53     * @covers \Fisharebest\Webtrees\Reports\RightToLeftSupport
54     *
55     * @return void
56     */
57    public function testStripControlCharacters(): void
58    {
59        I18N::init('en-US', true);
60        $this->assertSame(
61            '<span dir="ltr">foobar</span>',
62            RightToLeftSupport::spanLtrRtl('foo&lrm;bar')
63        );
64        $this->assertSame(
65            '<span dir="ltr">foobar</span>',
66            RightToLeftSupport::spanLtrRtl('foo&rlm;bar')
67        );
68        $this->assertSame(
69            '<span dir="ltr">foobar</span>',
70            RightToLeftSupport::spanLtrRtl("foo\xE2\x80\x8Ebar")
71        );
72        $this->assertSame(
73            '<span dir="ltr">foobar</span>',
74            RightToLeftSupport::spanLtrRtl("foo\xE2\x80\x8Fbar")
75        );
76        $this->assertSame(
77            '<span dir="ltr">foobar</span>',
78            RightToLeftSupport::spanLtrRtl("foo\xE2\x80\xADbar")
79        );
80        $this->assertSame(
81            '<span dir="ltr">foobar</span>',
82            RightToLeftSupport::spanLtrRtl("foo\xE2\x80\xAEbar")
83        );
84        $this->assertSame(
85            '<span dir="ltr">foobar</span>',
86            RightToLeftSupport::spanLtrRtl("foo\xE2\x80\xAAbar")
87        );
88        $this->assertSame(
89            '<span dir="ltr">foobar</span>',
90            RightToLeftSupport::spanLtrRtl("foo\xE2\x80\xABbar")
91        );
92        $this->assertSame(
93            '<span dir="ltr">foobar</span>',
94            RightToLeftSupport::spanLtrRtl("foo\xE2\x80\xACbar")
95        );
96
97        I18N::init('he', true);
98        $this->assertSame(
99            '<span dir="ltr">foobar</span>',
100            RightToLeftSupport::spanLtrRtl('foo&lrm;bar')
101        );
102        $this->assertSame(
103            '<span dir="ltr">foobar</span>',
104            RightToLeftSupport::spanLtrRtl('foo&rlm;bar')
105        );
106        $this->assertSame(
107            '<span dir="ltr">foobar</span>',
108            RightToLeftSupport::spanLtrRtl("foo\xE2\x80\x8Ebar")
109        );
110        $this->assertSame(
111            '<span dir="ltr">foobar</span>',
112            RightToLeftSupport::spanLtrRtl("foo\xE2\x80\x8Fbar")
113        );
114        $this->assertSame(
115            '<span dir="ltr">foobar</span>',
116            RightToLeftSupport::spanLtrRtl("foo\xE2\x80\xADbar")
117        );
118        $this->assertSame(
119            '<span dir="ltr">foobar</span>',
120            RightToLeftSupport::spanLtrRtl("foo\xE2\x80\xAEbar")
121        );
122        $this->assertSame(
123            '<span dir="ltr">foobar</span>',
124            RightToLeftSupport::spanLtrRtl("foo\xE2\x80\xAAbar")
125        );
126        $this->assertSame(
127            '<span dir="ltr">foobar</span>',
128            RightToLeftSupport::spanLtrRtl("foo\xE2\x80\xABbar")
129        );
130        $this->assertSame(
131            '<span dir="ltr">foobar</span>',
132            RightToLeftSupport::spanLtrRtl("foo\xE2\x80\xACbar")
133        );
134    }
135
136    /**
137     * @covers \Fisharebest\Webtrees\Reports\RightToLeftSupport
138     *
139     * @return void
140     */
141    public function testNewLinesBecomeHTMLBreaks(): void
142    {
143        I18N::init('en-US', true);
144        $this->assertSame(
145            '<span dir="ltr">foo</span><br><span dir="ltr">bar</span>',
146            RightToLeftSupport::spanLtrRtl("foo\nbar")
147        );
148        $this->assertSame(
149            '<span dir="rtl">אבג</span><br><span dir="rtl">דהו</span>',
150            RightToLeftSupport::spanLtrRtl("אבג\nדהו")
151        );
152
153        I18N::init('he', true);
154        $this->assertSame(
155            '<span dir="ltr">foo</span><br><span dir="ltr">bar</span>',
156            RightToLeftSupport::spanLtrRtl("foo\nbar")
157        );
158        $this->assertSame(
159            '<span dir="rtl">אבג</span><br><span dir="rtl">דהו</span>',
160            RightToLeftSupport::spanLtrRtl("אבג\nדהו")
161        );
162    }
163
164    /**
165     * @covers \Fisharebest\Webtrees\Reports\RightToLeftSupport
166     *
167     * @return void
168     */
169    public function testLineBreaks(): void
170    {
171        I18N::init('en-US', true);
172        $this->assertSame(
173            '<span dir="ltr">foo</span><br><span dir="ltr">bar</span>',
174            RightToLeftSupport::spanLtrRtl("foo<br>bar")
175        );
176        $this->assertSame(
177            '<span dir="rtl">אבג</span><br><span dir="rtl">דהו</span>',
178            RightToLeftSupport::spanLtrRtl("אבג<br>דהו")
179        );
180
181        I18N::init('he', true);
182        $this->assertSame(
183            '<span dir="ltr">foo</span><br><span dir="ltr">bar</span>',
184            RightToLeftSupport::spanLtrRtl("foo<br>bar")
185        );
186        $this->assertSame(
187            '<span dir="rtl">אבג</span><br><span dir="rtl">דהו</span>',
188            RightToLeftSupport::spanLtrRtl("אבג<br>דהו")
189        );
190    }
191
192    /**
193     * @covers \Fisharebest\Webtrees\Reports\RightToLeftSupport
194     *
195     * @return void
196     */
197    public function testHtmlEntities(): void
198    {
199        I18N::init('en-US', true);
200        $this->assertSame(
201            '<span dir="ltr">foo&nbsp;bar</span>',
202            RightToLeftSupport::spanLtrRtl("foo&nbsp;bar")
203        );
204        $this->assertSame(
205            '<span dir="rtl">אבג&nbsp;דהו</span>',
206            RightToLeftSupport::spanLtrRtl("אבג&nbsp;דהו")
207        );
208
209        I18N::init('he', true);
210        $this->assertSame(
211            '<span dir="ltr">foo&nbsp;bar</span>',
212            RightToLeftSupport::spanLtrRtl("foo&nbsp;bar")
213        );
214        $this->assertSame(
215            '<span dir="rtl">אבג&nbsp;דהו</span>',
216            RightToLeftSupport::spanLtrRtl("אבג&nbsp;דהו")
217        );
218    }
219
220    /**
221     * @covers \Fisharebest\Webtrees\Reports\RightToLeftSupport
222     *
223     * @return void
224     */
225    public function xtestLeftToRight(): void
226    {
227        $this->assertSame('<span dir="ltr">foo&eacutebar</span>', RightToLeftSupport::spanLtrRtl('foo&eacutebar'));
228        // Number
229        $this->assertSame('<span dir="ltr">foo 123,456.78 bar</span>', RightToLeftSupport::spanLtrRtl('foo 123,456.78 bar'));
230        $this->assertSame('<span dir="ltr">foo -123,456.78 bar</span>', RightToLeftSupport::spanLtrRtl('foo -123,456.78 bar'));
231        $this->assertSame('<span dir="ltr">foo +123,456.78 bar</span>', RightToLeftSupport::spanLtrRtl('foo +123,456.78 bar'));
232        $this->assertSame('<span dir="rtl">אבג</span><span dir="ltr"> ‪123,456.78‬ bar</span>', RightToLeftSupport::spanLtrRtl('אבג 123,456.78 bar'));
233        $this->assertSame('<span dir="rtl">אבג</span><span dir="ltr"> ‪-123,456.78‬ bar</span>', RightToLeftSupport::spanLtrRtl('אבג -123,456.78 bar'));
234        $this->assertSame('<span dir="rtl">אבג</span><span dir="ltr"> ‪+123,456.78‬ bar</span>', RightToLeftSupport::spanLtrRtl('אבג +123,456.78 bar'));
235        // TCPDF directive
236        $this->assertSame('<span dir="ltr">{{FOO BAR}}</span>', RightToLeftSupport::spanLtrRtl('{{FOO BAR}}'));
237        // Broken TCPDF directive
238        $this->assertSame('<span dir="ltr">{{FOO BAR</span>', RightToLeftSupport::spanLtrRtl('{{FOO BAR'));
239        // Starred name.
240        $this->assertSame('<span dir="ltr">John&nbsp;<u>Paul</u>&nbsp;Sartre</span>', RightToLeftSupport::spanLtrRtl('John <span class="starredname">Paul</span> Sartre'));
241        // Unclosed HTML tag
242        $this->assertSame('<span dir="ltr"><foo</span>', RightToLeftSupport::spanLtrRtl('<foo'));
243        // All LTR/RTL
244        $this->assertSame('<span dir="ltr">foo</span>', RightToLeftSupport::spanLtrRtl('foo'));
245        $this->assertSame('<span dir="rtl">אבג</span>', RightToLeftSupport::spanLtrRtl('אבג'));
246        // Leading/trailing spaces
247        $this->assertSame('<span dir="ltr">   foo   </span>', RightToLeftSupport::spanLtrRtl('   foo   '));
248        $this->assertSame('<span dir="ltr">   </span><span dir="rtl">אבג</span><span dir="ltr">   </span>', RightToLeftSupport::spanLtrRtl('   אבג   '));
249        $this->assertSame('<span dir="ltr">&nbsp;foo&nbsp;</span>', RightToLeftSupport::spanLtrRtl('&nbsp;foo&nbsp;'));
250        $this->assertSame('<span dir="ltr">&nbsp;</span><span dir="rtl">אבג</span><span dir="ltr"> </span>', RightToLeftSupport::spanLtrRtl('&nbsp;אבג&nbsp;'));
251        // Spaces stick to the LTR text
252        $this->assertSame('<span dir="ltr">foo </span><span dir="rtl">אבג</span>', RightToLeftSupport::spanLtrRtl('foo אבג'));
253        $this->assertSame('<span dir="rtl">אבג</span><span dir="ltr"> foo</span>', RightToLeftSupport::spanLtrRtl('אבג foo'));
254        // Line breaks
255        $this->assertSame('<span dir="ltr">foo</span><br><span dir="rtl">אבג</span><br><span dir="ltr">bar</span>', RightToLeftSupport::spanLtrRtl('foo<br>אבג<br>bar'));
256    }
257}
258