]> git.mxchange.org Git - friendica.git/blob - tests/src/Util/ACLFormaterTest.php
make ACLFormatter::expand() nullable and return an empty array
[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                         /// @todo should there be an exception? Or array(1, 3)
91                         //  (This should be array(1,3) - mike)
92                         'emptyMatch' => [
93                                 'input' => '<1><><3>', //should be invalid
94                                 'assert' => ['1', '3'],
95                         ],
96                 ];
97         }
98
99         /**
100          * @dataProvider dataExpand
101          */
102         public function testExpand($input, array $assert)
103         {
104                 $this->assertAcl($input, $assert);
105         }
106
107         /**
108          * Test nullable expand (=> no ACL set)
109          */
110         public function testExpandNull()
111         {
112                 $aclFormatter = new ACLFormatter();
113
114                 $allow_people = $aclFormatter->expand();
115                 $allow_groups = $aclFormatter->expand();
116
117                 $this->assertEmpty($aclFormatter->expand(null));
118                 $this->assertEmpty($aclFormatter->expand());
119
120                 $recipients   = array_unique(array_merge($allow_people, $allow_groups));
121                 $this->assertEmpty($recipients);
122         }
123
124         public function dataAclToString()
125         {
126                 return [
127                         'empty'   => [
128                                 'input'  => '',
129                                 'assert' => '',
130                         ],
131                         'string'  => [
132                                 'input'  => '1,2,3,4',
133                                 'assert' => '<1><2><3><4>',
134                         ],
135                         'array'   => [
136                                 'input'  => [1, 2, 3, 4],
137                                 'assert' => '<1><2><3><4>',
138                         ],
139                         'invalid' => [
140                                 'input'  => [1, 'a', 3, 4],
141                                 'assert' => '<1><3><4>',
142                         ],
143                         'invalidString' => [
144                                 'input'  => 'a,bsd23,4',
145                                 'assert' => '<4>',
146                         ],
147                         /** @see https://github.com/friendica/friendica/pull/7787 */
148                         'bug-7778-angle-brackets' => [
149                                 'input' => ["<40195>"],
150                                 'assert' => "<40195>",
151                         ],
152                         Group::FOLLOWERS => [
153                                 'input' => [Group::FOLLOWERS, 1],
154                                 'assert' => '<' . Group::FOLLOWERS . '><1>',
155                         ],
156                         Group::MUTUALS => [
157                                 'input' => [Group::MUTUALS, 1],
158                                 'assert' => '<' . Group::MUTUALS . '><1>',
159                         ],
160                         'wrong-angle-brackets' => [
161                                 'input' => ["<asd>","<123>"],
162                                 'assert' => "<123>",
163                         ],
164                 ];
165         }
166
167         /**
168          * @dataProvider dataAclToString
169          */
170         public function testAclToString($input, string $assert)
171         {
172                 $aclFormatter = new ACLFormatter();
173
174                 $this->assertEquals($assert, $aclFormatter->toString($input));
175         }
176 }