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