3 * @copyright Copyright (C) 2010-2023, 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\Model\Circle;
25 use Friendica\Util\ACLFormatter;
26 use PHPUnit\Framework\TestCase;
29 * ACLFormatter utility testing class
31 class ACLFormatterTest extends TestCase
33 public function assertAcl($text, array $assert = [])
35 $aclFormatter = new ACLFormatter();
37 $acl = $aclFormatter->expand($text);
39 self::assertEquals($assert, $acl);
41 self::assertMergeable($acl);
44 public function assertMergeable(array $aclOne, array $aclTwo = [])
46 self::assertTrue(is_array($aclOne));
47 self::assertTrue(is_array($aclTwo));
49 $aclMerged = array_unique(array_merge($aclOne, $aclTwo));
50 self::assertTrue(is_array($aclMerged));
55 public function dataExpand()
59 'input' => '<1><2><3><' . Circle::FOLLOWERS . '><' . Circle::MUTUALS . '>',
60 'assert' => ['1', '2', '3', Circle::FOLLOWERS, Circle::MUTUALS],
63 'input' => '<1><' . PHP_INT_MAX . '><15>',
64 'assert' => ['1', (string)PHP_INT_MAX, '15'],
67 'input' => '<1><279012><tt>',
68 'assert' => ['1', '279012'],
71 'input' => '<1><279 012><32>',
72 'assert' => ['1', '32'],
78 /// @todo should there be an exception?
80 'input' => 'According to documentation, that\'s invalid. ', //should be invalid
83 /// @todo should there be an exception?
85 'input' => '<Another invalid string', //should be invalid
88 /// @todo should there be an exception?
89 'justOneBracket2' => [
90 'input' => 'Another invalid> string', //should be invalid
93 /// @todo should there be an exception?
95 'input' => 'Another> invalid> string>', //should be invalid
98 /// @todo should there be an exception?
100 'input' => '<Another< invalid string<', //should be invalid
103 /// @todo should there be an exception?
105 'input' => '<Another<> invalid <string>', //should be invalid
109 'input' => '<1><><3>',
110 'assert' => ['1', '3'],
116 * @dataProvider dataExpand
118 public function testExpand($input, array $assert)
120 self::assertAcl($input, $assert);
124 * Test nullable expand (=> no ACL set)
126 public function testExpandNull()
128 $aclFormatter = new ACLFormatter();
130 $allow_people = $aclFormatter->expand();
131 $allow_circles = $aclFormatter->expand();
133 self::assertEmpty($aclFormatter->expand(null));
134 self::assertEmpty($aclFormatter->expand());
136 $recipients = array_unique(array_merge($allow_people, $allow_circles));
137 self::assertEmpty($recipients);
140 public function dataAclToString()
148 'input' => '1,2,3,4',
149 'assert' => '<1><2><3><4>',
152 'input' => [1, 2, 3, 4],
153 'assert' => '<1><2><3><4>',
156 'input' => [1, 'a', 3, 4],
157 'assert' => '<1><3><4>',
160 'input' => 'a,bsd23,4',
163 /** @see https://github.com/friendica/friendica/pull/7787 */
164 'bug-7778-angle-brackets' => [
165 'input' => ["<40195>"],
166 'assert' => "<40195>",
168 Circle::FOLLOWERS => [
169 'input' => [Circle::FOLLOWERS, 1],
170 'assert' => '<' . Circle::FOLLOWERS . '><1>',
173 'input' => [Circle::MUTUALS, 1],
174 'assert' => '<' . Circle::MUTUALS . '><1>',
176 'wrong-angle-brackets' => [
177 'input' => ["<asd>","<123>"],
184 * @dataProvider dataAclToString
186 public function testAclToString($input, string $assert)
188 $aclFormatter = new ACLFormatter();
190 self::assertEquals($assert, $aclFormatter->toString($input));