3 * @copyright Copyright (C) 2010-2021, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Test\src\Util;
24 use Friendica\Util\Strings;
25 use PHPUnit\Framework\TestCase;
28 * Strings utility test class
30 class StringsTest extends TestCase
33 * randomnames should be random, even length
35 public function testRandomEven()
37 $randomname1 = Strings::getRandomName(10);
38 $randomname2 = Strings::getRandomName(10);
40 self::assertNotEquals($randomname1, $randomname2);
44 * randomnames should be random, odd length
46 public function testRandomOdd()
48 $randomname1 = Strings::getRandomName(9);
49 $randomname2 = Strings::getRandomName(9);
51 self::assertNotEquals($randomname1, $randomname2);
55 * try to fail ramdonnames
57 public function testRandomNameNoLength()
59 $randomname1 = Strings::getRandomName(0);
60 self::assertEquals(0, strlen($randomname1));
64 * try to fail it with invalid input
66 * @todo What's corect behaviour here? An exception?
68 public function testRandomNameNegativeLength()
70 $randomname1 = Strings::getRandomName(-23);
71 self::assertEquals(0, strlen($randomname1));
75 * test with a length, that may be too short
77 public function testRandomNameLength1()
79 $randomname1 = Strings::getRandomName(1);
80 self::assertEquals(1, strlen($randomname1));
82 $randomname2 = Strings::getRandomName(1);
83 self::assertEquals(1, strlen($randomname2));
87 * test, that tags are escaped
89 public function testEscapeHtml()
91 $invalidstring='<submit type="button" onclick="alert(\'failed!\');" />';
93 $validstring = Strings::escapeTags($invalidstring);
94 $escapedString = Strings::escapeHtml($invalidstring);
96 self::assertEquals('[submit type="button" onclick="alert(\'failed!\');" /]', $validstring);
98 "<submit type="button" onclick="alert('failed!');" />",
103 public function dataIsHex()
107 'input' => '90913473615bf00c122ac78338492980',
111 'input' => '90913473615bf00c122ac7833849293',
126 * Tests if the string is a valid hexadecimal value
128 * @param string|null $input
131 * @dataProvider dataIsHex
133 public function testIsHex(string $input = null, bool $valid = false)
135 self::assertEquals($valid, Strings::isHex($input));
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)
142 public function testSubstringReplaceASCII()
144 for ($start = -10; $start <= 10; $start += 5) {
146 substr_replace('string', 'replacement', $start),
147 Strings::substringReplace('string', 'replacement', $start)
150 for ($length = -10; $length <= 10; $length += 5) {
152 substr_replace('string', 'replacement', $start, $length),
153 Strings::substringReplace('string', 'replacement', $start, $length)
160 public function dataSubstringReplaceMultiByte()
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]',
174 * Tests cases where Strings::substringReplace is needed over substr_replace with multi-byte strings and character
177 * @param string $expected
178 * @param string $string
179 * @param string $replacement
181 * @param int|null $length
183 * @dataProvider dataSubstringReplaceMultiByte
185 public function testSubstringReplaceMultiByte(string $expected, string $string, string $replacement, int $start, int $length = null)
189 Strings::substringReplace(
198 public function testPerformWithEscapedBlocks()
200 $originalText = '[noparse][/noparse][nobb]nobb[/nobb][noparse]noparse[/noparse]';
202 $text = Strings::performWithEscapedBlocks($originalText, '#[(?:noparse|nobb)].*?\[/(?:noparse|nobb)]#is', function ($text) {
206 self::assertEquals($originalText, $text);
209 public function testPerformWithEscapedBlocksNested()
211 $originalText = '[noparse][/noparse][nobb]nobb[/nobb][noparse]noparse[/noparse]';
213 $text = Strings::performWithEscapedBlocks($originalText, '#[nobb].*?\[/nobb]#is', function ($text) {
214 $text = Strings::performWithEscapedBlocks($text, '#[noparse].*?\[/noparse]#is', function ($text) {
221 self::assertEquals($originalText, $text);