Update copyright
[friendica.git/.git] / tests / src / Util / ACLFormaterTest.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, 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\Model\Group;
25 use Friendica\Util\ACLFormatter;
26 use PHPUnit\Framework\TestCase;
27
28 /**
29  * ACLFormater utility testing class
30  */
31 class ACLFormaterTest extends TestCase
32 {
33         public function assertAcl($text, array $assert = [])
34         {
35                 $aclFormatter = new ACLFormatter();
36
37                 $acl = $aclFormatter->expand($text);
38
39                 self::assertEquals($assert, $acl);
40
41                 self::assertMergable($acl);
42         }
43
44         public function assertMergable(array $aclOne, array $aclTwo = [])
45         {
46                 self::assertTrue(is_array($aclOne));
47                 self::assertTrue(is_array($aclTwo));
48
49                 $aclMerged = array_unique(array_merge($aclOne, $aclTwo));
50                 self::assertTrue(is_array($aclMerged));
51
52                 return $aclMerged;
53         }
54
55         public function dataExpand()
56         {
57                 return [
58                         'normal' => [
59                                 'input' => '<1><2><3><' . Group::FOLLOWERS . '><' . Group::MUTUALS . '>',
60                                 'assert' => ['1', '2', '3', Group::FOLLOWERS, Group::MUTUALS],
61                         ],
62                         'nigNumber' => [
63                                 'input' => '<1><' . PHP_INT_MAX . '><15>',
64                                 'assert' => ['1', (string)PHP_INT_MAX, '15'],
65                         ],
66                         'string' => [
67                                 'input' => '<1><279012><tt>',
68                                 'assert' => ['1', '279012'],
69                         ],
70                         'space' => [
71                                 'input' => '<1><279 012><32>',
72                                 'assert' => ['1', '32'],
73                         ],
74                         'empty' => [
75                                 'input' => '',
76                                 'assert' => [],
77                         ],
78                         /// @todo should there be an exception?
79                         'noBrackets' => [
80                                 'input' => 'According to documentation, that\'s invalid. ', //should be invalid
81                                 'assert' => [],
82                         ],
83                         /// @todo should there be an exception?
84                         'justOneBracket' => [
85                                 'input' => '<Another invalid string', //should be invalid
86                                 'assert' => [],
87                         ],
88                         /// @todo should there be an exception?
89                         'justOneBracket2' => [
90                                 'input' => 'Another invalid> string', //should be invalid
91                                 'assert' => [],
92                         ],
93                         /// @todo should there be an exception?
94                         'closeOnly' => [
95                                 'input' => 'Another> invalid> string>', //should be invalid
96                                 'assert' => [],
97                         ],
98                         /// @todo should there be an exception?
99                         'openOnly' => [
100                                 'input' => '<Another< invalid string<', //should be invalid
101                                 'assert' => [],
102                         ],
103                         /// @todo should there be an exception?
104                         'noMatching1' => [
105                                 'input' => '<Another<> invalid <string>', //should be invalid
106                                 'assert' => [],
107                         ],
108                         'emptyMatch' => [
109                                 'input' => '<1><><3>',
110                                 'assert' => ['1', '3'],
111                         ],
112                 ];
113         }
114
115         /**
116          * @dataProvider dataExpand
117          */
118         public function testExpand($input, array $assert)
119         {
120                 self::assertAcl($input, $assert);
121         }
122
123         /**
124          * Test nullable expand (=> no ACL set)
125          */
126         public function testExpandNull()
127         {
128                 $aclFormatter = new ACLFormatter();
129
130                 $allow_people = $aclFormatter->expand();
131                 $allow_groups = $aclFormatter->expand();
132
133                 self::assertEmpty($aclFormatter->expand(null));
134                 self::assertEmpty($aclFormatter->expand());
135
136                 $recipients   = array_unique(array_merge($allow_people, $allow_groups));
137                 self::assertEmpty($recipients);
138         }
139
140         public function dataAclToString()
141         {
142                 return [
143                         'empty'   => [
144                                 'input'  => '',
145                                 'assert' => '',
146                         ],
147                         'string'  => [
148                                 'input'  => '1,2,3,4',
149                                 'assert' => '<1><2><3><4>',
150                         ],
151                         'array'   => [
152                                 'input'  => [1, 2, 3, 4],
153                                 'assert' => '<1><2><3><4>',
154                         ],
155                         'invalid' => [
156                                 'input'  => [1, 'a', 3, 4],
157                                 'assert' => '<1><3><4>',
158                         ],
159                         'invalidString' => [
160                                 'input'  => 'a,bsd23,4',
161                                 'assert' => '<4>',
162                         ],
163                         /** @see https://github.com/friendica/friendica/pull/7787 */
164                         'bug-7778-angle-brackets' => [
165                                 'input' => ["<40195>"],
166                                 'assert' => "<40195>",
167                         ],
168                         Group::FOLLOWERS => [
169                                 'input' => [Group::FOLLOWERS, 1],
170                                 'assert' => '<' . Group::FOLLOWERS . '><1>',
171                         ],
172                         Group::MUTUALS => [
173                                 'input' => [Group::MUTUALS, 1],
174                                 'assert' => '<' . Group::MUTUALS . '><1>',
175                         ],
176                         'wrong-angle-brackets' => [
177                                 'input' => ["<asd>","<123>"],
178                                 'assert' => "<123>",
179                         ],
180                 ];
181         }
182
183         /**
184          * @dataProvider dataAclToString
185          */
186         public function testAclToString($input, string $assert)
187         {
188                 $aclFormatter = new ACLFormatter();
189
190                 self::assertEquals($assert, $aclFormatter->toString($input));
191         }
192 }