]> git.mxchange.org Git - friendica.git/blob - tests/src/Util/StringsTest.php
spelling: cached
[friendica.git] / tests / src / Util / StringsTest.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Test\src\Util;
23
24 use Friendica\Util\Strings;
25 use PHPUnit\Framework\TestCase;
26
27 /**
28  * Strings utility test class
29  */
30 class StringsTest extends TestCase
31 {
32         /**
33          * randomnames should be random, even length
34          */
35         public function testRandomEven()
36         {
37                 $randomname1 = Strings::getRandomName(10);
38                 $randomname2 = Strings::getRandomName(10);
39
40                 self::assertNotEquals($randomname1, $randomname2);
41         }
42
43         /**
44          * randomnames should be random, odd length
45          */
46         public function testRandomOdd()
47         {
48                 $randomname1 = Strings::getRandomName(9);
49                 $randomname2 = Strings::getRandomName(9);
50
51                 self::assertNotEquals($randomname1, $randomname2);
52         }
53
54         /**
55          * try to fail ramdonnames
56          */
57         public function testRandomNameNoLength()
58         {
59                 $randomname1 = Strings::getRandomName(0);
60                 self::assertEquals(0, strlen($randomname1));
61         }
62
63         /**
64          * try to fail it with invalid input
65          *
66          * @todo What's corect behaviour here? An exception?
67          */
68         public function testRandomNameNegativeLength()
69         {
70                 $randomname1 = Strings::getRandomName(-23);
71                 self::assertEquals(0, strlen($randomname1));
72         }
73
74         /**
75          * test with a length, that may be too short
76          */
77         public function testRandomNameLength1()
78         {
79                 $randomname1 = Strings::getRandomName(1);
80                 self::assertEquals(1, strlen($randomname1));
81
82                 $randomname2 = Strings::getRandomName(1);
83                 self::assertEquals(1, strlen($randomname2));
84         }
85
86         /**
87          * test, that tags are escaped
88          */
89         public function testEscapeHtml()
90         {
91                 $invalidstring='<submit type="button" onclick="alert(\'failed!\');" />';
92
93                 $escapedString = Strings::escapeHtml($invalidstring);
94
95                 self::assertEquals(
96                         "&lt;submit type=&quot;button&quot; onclick=&quot;alert('failed!');&quot; /&gt;",
97                         $escapedString
98                 );
99         }
100
101         public function dataIsHex()
102         {
103                 return [
104                         'validHex' => [
105                                 'input' => '90913473615bf00c122ac78338492980',
106                                 'valid' => true,
107                         ],
108                         'invalidHex' => [
109                                 'input' => '90913473615bf00c122ac7833849293',
110                                 'valid' => false,
111                         ],
112                         'emptyHex' => [
113                                 'input' => '',
114                                 'valid' => false,
115                         ],
116                 ];
117         }
118
119         /**
120          * Tests if the string is a valid hexadecimal value
121          *
122          * @param string $input Input string
123          * @param bool   $valid Whether testing on valid or invalid
124          *
125          * @dataProvider dataIsHex
126          */
127         public function testIsHex(string $input, bool $valid = false)
128         {
129                 self::assertEquals($valid, Strings::isHex($input));
130         }
131
132         /**
133          * Tests that Strings::substringReplace behaves the same as substr_replace with ASCII strings in all the possible
134          * numerical parameter configurations (positive, negative, zero, out of bounds either side, null)
135          */
136         public function testSubstringReplaceASCII()
137         {
138                 for ($start = -10; $start <= 10; $start += 5) {
139                         self::assertEquals(
140                                 substr_replace('string', 'replacement', $start),
141                                 Strings::substringReplace('string', 'replacement', $start)
142                         );
143
144                         for ($length = -10; $length <= 10; $length += 5) {
145                                 self::assertEquals(
146                                         substr_replace('string', 'replacement', $start, $length),
147                                         Strings::substringReplace('string', 'replacement', $start, $length)
148                                 );
149                         }
150                 }
151         }
152
153
154         public function dataSubstringReplaceMultiByte()
155         {
156                 return [
157                         'issue-8470' => [
158                                 'expected' => 'Je n’y pense que maintenant (pask ma sonnette ne fonctionne pas) : mettre un gentil mot avec mes coordonnées sur ma porte est le moyen le plus simple de rester en contact si besoin avec mon voisinage direct ! [url=https://www.instagram.com/p/B-UdH2loee1/?igshid=x4aglyju9kva]instagram.com/p/B-UdH2loee1/…[/url] [rest of the post]',
159                                 'string' => 'Je n’y pense que maintenant (pask ma sonnette ne fonctionne pas) : mettre un gentil mot avec mes coordonnées sur ma porte est le moyen le plus simple de rester en contact si besoin avec mon voisinage direct ! https://t.co/YoBWTHsAAk [rest of the post]',
160                                 'replacement' => '[url=https://www.instagram.com/p/B-UdH2loee1/?igshid=x4aglyju9kva]instagram.com/p/B-UdH2loee1/…[/url]',
161                                 'start' => 209,
162                                 'length' => 23,
163                         ],
164                 ];
165         }
166
167         /**
168          * Tests cases where Strings::substringReplace is needed over substr_replace with multi-byte strings and character
169          * offsets
170          *
171          * @param string   $expected
172          * @param string   $string
173          * @param string   $replacement
174          * @param int      $start
175          * @param int|null $length
176          *
177          * @dataProvider dataSubstringReplaceMultiByte
178          */
179         public function testSubstringReplaceMultiByte(string $expected, string $string, string $replacement, int $start, int $length = null)
180         {
181                 self::assertEquals(
182                         $expected,
183                         Strings::substringReplace(
184                                 $string,
185                                 $replacement,
186                                 $start,
187                                 $length
188                         )
189                 );
190         }
191
192         public function testPerformWithEscapedBlocks()
193         {
194                 $originalText = '[noparse][/noparse][nobb]nobb[/nobb][noparse]noparse[/noparse]';
195
196                 $text = Strings::performWithEscapedBlocks($originalText, '#[(?:noparse|nobb)].*?\[/(?:noparse|nobb)]#is', function ($text) {
197                         return $text;
198                 });
199
200                 self::assertEquals($originalText, $text);
201         }
202
203         public function testPerformWithEscapedBlocksNested()
204         {
205                 $originalText = '[noparse][/noparse][nobb]nobb[/nobb][noparse]noparse[/noparse]';
206
207                 $text = Strings::performWithEscapedBlocks($originalText, '#[nobb].*?\[/nobb]#is', function ($text) {
208                         $text = Strings::performWithEscapedBlocks($text, '#[noparse].*?\[/noparse]#is', function ($text) {
209                                 return $text;
210                         });
211
212                         return $text;
213                 });
214
215                 self::assertEquals($originalText, $text);
216         }
217 }