]> git.mxchange.org Git - friendica.git/blob - tests/src/Util/ACLFormaterTest.php
Fix ACLFormatterTest
[friendica.git] / tests / src / Util / ACLFormaterTest.php
1 <?php
2
3 namespace Friendica\Test\src\Util;
4
5 use Error;
6 use Friendica\Model\Group;
7 use Friendica\Util\ACLFormatter;
8 use PHPUnit\Framework\TestCase;
9
10 /**
11  * @brief ACLFormater utility testing class
12  */
13 class ACLFormaterTest extends TestCase
14 {
15         /**
16          * test expand_acl, perfect input
17          */
18         public function testExpandAclNormal()
19         {
20                 $aclFormatter = new ACLFormatter();
21
22                 $text='<1><2><3><' . Group::FOLLOWERS . '><' . Group::MUTUALS . '>';
23                 $this->assertEquals(array('1', '2', '3', Group::FOLLOWERS, Group::MUTUALS), $aclFormatter->expand($text));
24         }
25
26         /**
27          * test with a big number
28          */
29         public function testExpandAclBigNumber()
30         {
31                 $aclFormatter = new ACLFormatter();
32
33                 $text='<1><' . PHP_INT_MAX . '><15>';
34                 $this->assertEquals(array('1', (string)PHP_INT_MAX, '15'), $aclFormatter->expand($text));
35         }
36
37         /**
38          * test with a string in it.
39          *
40          * @todo is this valid input? Otherwise: should there be an exception?
41          */
42         public function testExpandAclString()
43         {
44                 $aclFormatter = new ACLFormatter();
45
46                 $text="<1><279012><tt>";
47                 $this->assertEquals(array('1', '279012'), $aclFormatter->expand($text));
48         }
49
50         /**
51          * test with a ' ' in it.
52          *
53          * @todo is this valid input? Otherwise: should there be an exception?
54          */
55         public function testExpandAclSpace()
56         {
57                 $aclFormatter = new ACLFormatter();
58
59                 $text="<1><279 012><32>";
60                 $this->assertEquals(array('1', '32'), $aclFormatter->expand($text));
61         }
62
63         /**
64          * test empty input
65          */
66         public function testExpandAclEmpty()
67         {
68                 $aclFormatter = new ACLFormatter();
69
70                 $text="";
71                 $this->assertEquals(array(), $aclFormatter->expand($text));
72         }
73
74         /**
75          * test invalid input, no < at all
76          *
77          * @todo should there be an exception?
78          */
79         public function testExpandAclNoBrackets()
80         {
81                 $aclFormatter = new ACLFormatter();
82
83                 $text="According to documentation, that's invalid. "; //should be invalid
84                 $this->assertEquals(array(), $aclFormatter->expand($text));
85         }
86
87         /**
88          * test invalid input, just open <
89          *
90          * @todo should there be an exception?
91          */
92         public function testExpandAclJustOneBracket1()
93         {
94                 $aclFormatter = new ACLFormatter();
95
96                 $text="<Another invalid string"; //should be invalid
97                 $this->assertEquals(array(), $aclFormatter->expand($text));
98         }
99
100         /**
101          * test invalid input, just close >
102          *
103          * @todo should there be an exception?
104          */
105         public function testExpandAclJustOneBracket2()
106         {
107                 $aclFormatter = new ACLFormatter();
108
109                 $text="Another invalid> string"; //should be invalid
110                 $this->assertEquals(array(), $aclFormatter->expand($text));
111         }
112
113         /**
114          * test invalid input, just close >
115          *
116          * @todo should there be an exception?
117          */
118         public function testExpandAclCloseOnly()
119         {
120                 $aclFormatter = new ACLFormatter();
121
122                 $text="Another> invalid> string>"; //should be invalid
123                 $this->assertEquals(array(), $aclFormatter->expand($text));
124         }
125
126         /**
127          * test invalid input, just open <
128          *
129          * @todo should there be an exception?
130          */
131         public function testExpandAclOpenOnly()
132         {
133                 $aclFormatter = new ACLFormatter();
134
135                 $text="<Another< invalid string<"; //should be invalid
136                 $this->assertEquals(array(), $aclFormatter->expand($text));
137         }
138
139         /**
140          * test invalid input, open and close do not match
141          *
142          * @todo should there be an exception?
143          */
144         public function testExpandAclNoMatching1()
145         {
146                 $aclFormatter = new ACLFormatter();
147
148                 $text="<Another<> invalid <string>"; //should be invalid
149                 $this->assertEquals(array(), $aclFormatter->expand($text));
150         }
151
152         /**
153          * test invalid input, empty <>
154          *
155          * @todo should there be an exception? Or array(1, 3)
156          * (This should be array(1,3) - mike)
157          */
158         public function testExpandAclEmptyMatch()
159         {
160                 $aclFormatter = new ACLFormatter();
161
162                 $text="<1><><3>";
163                 $this->assertEquals(array('1', '3'), $aclFormatter->expand($text));
164         }
165
166         /**
167          * Test nullable expand (=> no ACL set)
168          */
169         public function testExpandNull()
170         {
171                 $aclFormatter = new ACLFormatter();
172
173                 $this->assertNull($aclFormatter->expand(null));
174                 $this->assertNull($aclFormatter->expand());
175         }
176
177         public function dataAclToString()
178         {
179                 return [
180                         'empty'   => [
181                                 'input'  => '',
182                                 'assert' => '',
183                         ],
184                         'string'  => [
185                                 'input'  => '1,2,3,4',
186                                 'assert' => '<1><2><3><4>',
187                         ],
188                         'array'   => [
189                                 'input'  => [1, 2, 3, 4],
190                                 'assert' => '<1><2><3><4>',
191                         ],
192                         'invalid' => [
193                                 'input'  => [1, 'a', 3, 4],
194                                 'assert' => '<1><3><4>',
195                         ],
196                         'invalidString' => [
197                                 'input'  => 'a,bsd23,4',
198                                 'assert' => '<4>',
199                         ],
200                         /** @see https://github.com/friendica/friendica/pull/7787 */
201                         'bug-7778-angle-brackets' => [
202                                 'input' => ["<40195>"],
203                                 'assert' => "<40195>",
204                         ],
205                         Group::FOLLOWERS => [
206                                 'input' => [Group::FOLLOWERS, 1],
207                                 'assert' => '<' . Group::FOLLOWERS . '><1>',
208                         ],
209                         Group::MUTUALS => [
210                                 'input' => [Group::MUTUALS, 1],
211                                 'assert' => '<' . Group::MUTUALS . '><1>',
212                         ],
213                         'wrong-angle-brackets' => [
214                                 'input' => ["<asd>","<123>"],
215                                 'assert' => "<123>",
216                         ],
217                 ];
218         }
219
220         /**
221          * @dataProvider dataAclToString
222          */
223         public function testAclToString($input, string $assert)
224         {
225                 $aclFormatter = new ACLFormatter();
226
227                 $this->assertEquals($assert, $aclFormatter->toString($input));
228         }
229 }