3 * @copyright Copyright (C) 2020, Friendica
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;
25 use Friendica\Model\Group;
26 use Friendica\Util\ACLFormatter;
27 use PHPUnit\Framework\TestCase;
30 * ACLFormater utility testing class
32 class ACLFormaterTest extends TestCase
34 public function assertAcl($text, array $assert = [])
36 $aclFormatter = new ACLFormatter();
38 $acl = $aclFormatter->expand($text);
40 $this->assertEquals($assert, $acl);
42 $this->assertMergable($acl);
45 public function assertMergable(array $aclOne, array $aclTwo = [])
47 $this->assertTrue(is_array($aclOne));
48 $this->assertTrue(is_array($aclTwo));
50 $aclMerged = array_unique(array_merge($aclOne, $aclTwo));
51 $this->assertTrue(is_array($aclMerged));
56 public function dataExpand()
60 'input' => '<1><2><3><' . Group::FOLLOWERS . '><' . Group::MUTUALS . '>',
61 'assert' => ['1', '2', '3', Group::FOLLOWERS, Group::MUTUALS],
64 'input' => '<1><' . PHP_INT_MAX . '><15>',
65 'assert' => ['1', (string)PHP_INT_MAX, '15'],
68 'input' => '<1><279012><tt>',
69 'assert' => ['1', '279012'],
72 'input' => '<1><279 012><32>',
73 'assert' => ['1', '32'],
79 /// @todo should there be an exception?
81 'input' => 'According to documentation, that\'s invalid. ', //should be invalid
84 /// @todo should there be an exception?
86 'input' => '<Another invalid string', //should be invalid
89 /// @todo should there be an exception?
90 'justOneBracket2' => [
91 'input' => 'Another invalid> string', //should be invalid
94 /// @todo should there be an exception?
96 'input' => 'Another> invalid> string>', //should be invalid
99 /// @todo should there be an exception?
101 'input' => '<Another< invalid string<', //should be invalid
104 /// @todo should there be an exception?
106 'input' => '<Another<> invalid <string>', //should be invalid
110 'input' => '<1><><3>',
111 'assert' => ['1', '3'],
117 * @dataProvider dataExpand
119 public function testExpand($input, array $assert)
121 $this->assertAcl($input, $assert);
125 * Test nullable expand (=> no ACL set)
127 public function testExpandNull()
129 $aclFormatter = new ACLFormatter();
131 $allow_people = $aclFormatter->expand();
132 $allow_groups = $aclFormatter->expand();
134 $this->assertEmpty($aclFormatter->expand(null));
135 $this->assertEmpty($aclFormatter->expand());
137 $recipients = array_unique(array_merge($allow_people, $allow_groups));
138 $this->assertEmpty($recipients);
141 public function dataAclToString()
149 'input' => '1,2,3,4',
150 'assert' => '<1><2><3><4>',
153 'input' => [1, 2, 3, 4],
154 'assert' => '<1><2><3><4>',
157 'input' => [1, 'a', 3, 4],
158 'assert' => '<1><3><4>',
161 'input' => 'a,bsd23,4',
164 /** @see https://github.com/friendica/friendica/pull/7787 */
165 'bug-7778-angle-brackets' => [
166 'input' => ["<40195>"],
167 'assert' => "<40195>",
169 Group::FOLLOWERS => [
170 'input' => [Group::FOLLOWERS, 1],
171 'assert' => '<' . Group::FOLLOWERS . '><1>',
174 'input' => [Group::MUTUALS, 1],
175 'assert' => '<' . Group::MUTUALS . '><1>',
177 'wrong-angle-brackets' => [
178 'input' => ["<asd>","<123>"],
185 * @dataProvider dataAclToString
187 public function testAclToString($input, string $assert)
189 $aclFormatter = new ACLFormatter();
191 $this->assertEquals($assert, $aclFormatter->toString($input));