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