]> git.mxchange.org Git - friendica.git/blob - tests/src/Util/StringsTest.php
Merge pull request #8751 from annando/notice
[friendica.git] / tests / src / Util / StringsTest.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
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                 $this->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                 $this->assertNotEquals($randomname1, $randomname2);
52         }
53
54         /**
55          * try to fail ramdonnames
56          */
57         public function testRandomNameNoLength()
58         {
59                 $randomname1 = Strings::getRandomName(0);
60                 $this->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                 $this->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                 $this->assertEquals(1, strlen($randomname1));
81
82                 $randomname2 = Strings::getRandomName(1);
83                 $this->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                 $validstring = Strings::escapeTags($invalidstring);
94                 $escapedString = Strings::escapeHtml($invalidstring);
95
96                 $this->assertEquals('[submit type="button" onclick="alert(\'failed!\');" /]', $validstring);
97                 $this->assertEquals(
98                         "&lt;submit type=&quot;button&quot; onclick=&quot;alert('failed!');&quot; /&gt;",
99                         $escapedString
100                 );
101         }
102
103         public function dataIsHex()
104         {
105                 return [
106                         'validHex' => [
107                                 'input' => '90913473615bf00c122ac78338492980',
108                                 'valid' => true,
109                         ],
110                         'invalidHex' => [
111                                 'input' => '90913473615bf00c122ac7833849293',
112                                 'valid' => false,
113                         ],
114                         'emptyHex' => [
115                                 'input' => '',
116                                 'valid' => false,
117                         ],
118                         'nullHex' => [
119                                 'input' => null,
120                                 'valid' => false,
121                         ],
122                 ];
123         }
124
125         /**
126          * Tests if the string is a valid hexadecimal value
127          *
128          * @param string $input
129          * @param bool $valid
130          *
131          * @dataProvider dataIsHex
132          */
133         public function testIsHex($input, $valid)
134         {
135                 $this->assertEquals($valid, Strings::isHex($input));
136         }
137
138         /**
139          * Tests that Strings::substringReplace behaves the same as substr_replace with ASCII strings in all the possible
140          * numerical parameter configurations (positive, negative, zero, out of bounds either side, null)
141          */
142         public function testSubstringReplaceASCII()
143         {
144                 for ($start = -10; $start <= 10; $start += 5) {
145                         $this->assertEquals(
146                                 substr_replace('string', 'replacement', $start),
147                                 Strings::substringReplace('string', 'replacement', $start)
148                         );
149
150                         for ($length = -10; $length <= 10; $length += 5) {
151                                 $this->assertEquals(
152                                         substr_replace('string', 'replacement', $start, $length),
153                                         Strings::substringReplace('string', 'replacement', $start, $length)
154                                 );
155                         }
156                 }
157         }
158
159
160         public function dataSubstringReplaceMultiByte()
161         {
162                 return [
163                         'issue-8470' => [
164                                 '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]',
165                                 '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]',
166                                 'replacement' => '[url=https://www.instagram.com/p/B-UdH2loee1/?igshid=x4aglyju9kva]instagram.com/p/B-UdH2loee1/…[/url]',
167                                 'start' => 209,
168                                 'length' => 23,
169                         ],
170                 ];
171         }
172
173         /**
174          * Tests cases where Strings::substringReplace is needed over substr_replace with multi-byte strings and character
175          * offsets
176          *
177          * @param string   $expected
178          * @param string   $string
179          * @param string   $replacement
180          * @param int      $start
181          * @param int|null $length
182          *
183          * @dataProvider dataSubstringReplaceMultiByte
184          */
185         public function testSubstringReplaceMultiByte(string $expected, string $string, string $replacement, int $start, int $length = null)
186         {
187                 $this->assertEquals(
188                         $expected,
189                         Strings::substringReplace(
190                                 $string,
191                                 $replacement,
192                                 $start,
193                                 $length
194                         )
195                 );
196         }
197
198         public function testPerformWithEscapedBlocks()
199         {
200                 $originalText = '[noparse][/noparse][nobb]nobb[/nobb][noparse]noparse[/noparse]';
201
202                 $text = Strings::performWithEscapedBlocks($originalText, '#[(?:noparse|nobb)].*?\[/(?:noparse|nobb)]#is', function ($text) {
203                         return $text;
204                 });
205
206                 $this->assertEquals($originalText, $text);
207         }
208
209         public function testPerformWithEscapedBlocksNested()
210         {
211                 $originalText = '[noparse][/noparse][nobb]nobb[/nobb][noparse]noparse[/noparse]';
212
213                 $text = Strings::performWithEscapedBlocks($originalText, '#[nobb].*?\[/nobb]#is', function ($text) {
214                         $text = Strings::performWithEscapedBlocks($text, '#[noparse].*?\[/noparse]#is', function ($text) {
215                                 return $text;
216                         });
217
218                         return $text;
219                 });
220
221                 $this->assertEquals($originalText, $text);
222         }
223 }