]> git.mxchange.org Git - friendica.git/blob - tests/src/Console/ServerBlockConsoleTest.php
Merge pull request #12231 from HankG/fix-unfavourite-endpoint
[friendica.git] / tests / src / Console / ServerBlockConsoleTest.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, 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\Console;
23
24 use Friendica\Console\ServerBlock;
25 use Friendica\Core\Config\Capability\IManageConfigValues;
26 use Friendica\Moderation\DomainPatternBlocklist;
27 use Mockery;
28
29 class ServerBlockConsoleTest extends ConsoleTest
30 {
31         protected $defaultBlockList = [
32                 [
33                         'domain' => 'social.nobodyhasthe.biz',
34                         'reason' => 'Illegal content',
35                 ],
36                 [
37                         'domain' => 'pod.ordoevangelistarum.com',
38                         'reason' => 'Illegal content',
39                 ]
40         ];
41         /**
42          * @var DomainPatternBlocklist|Mockery\LegacyMockInterface|Mockery\MockInterface
43          */
44         private $blocklistMock;
45
46         protected function setUp() : void
47         {
48                 parent::setUp();
49
50                 $this->blocklistMock = Mockery::mock(DomainPatternBlocklist::class);
51         }
52
53         /**
54          * Test to list the default blocked servers
55          */
56         public function testBlockedServersList()
57         {
58                 $this->blocklistMock
59                         ->shouldReceive('get')
60                         ->andReturn($this->defaultBlockList)
61                         ->once();
62
63                 $console = new ServerBlock($this->blocklistMock, $this->consoleArgv);
64                 $txt = $this->dumpExecute($console);
65
66                 $php_eol = PHP_EOL;
67
68                 $output = <<<CONS
69 +----------------------------+-----------------+$php_eol| Pattern                    | Reason          |$php_eol+----------------------------+-----------------+$php_eol| social.nobodyhasthe.biz    | Illegal content |$php_eol| pod.ordoevangelistarum.com | Illegal content |$php_eol+----------------------------+-----------------+$php_eol
70
71 CONS;
72
73                 self::assertEquals($output, $txt);
74         }
75
76         /**
77          * Test blockedservers add command
78          */
79         public function testAddBlockedServer()
80         {
81                 $this->blocklistMock
82                         ->shouldReceive('addPattern')
83                         ->with('testme.now', 'I like it!')
84                         ->andReturn(1)
85                         ->once();
86
87                 $console = new ServerBlock($this->blocklistMock, $this->consoleArgv);
88                 $console->setArgument(0, 'add');
89                 $console->setArgument(1, 'testme.now');
90                 $console->setArgument(2, 'I like it!');
91                 $txt = $this->dumpExecute($console);
92
93                 self::assertEquals('The domain pattern \'testme.now\' is now blocked. (Reason: \'I like it!\')' . "\n", $txt);
94         }
95
96         /**
97          * Test blockedservers add command on existed domain
98          */
99         public function testUpdateBlockedServer()
100         {
101                 $this->blocklistMock
102                         ->shouldReceive('addPattern')
103                         ->with('pod.ordoevangelistarum.com', 'Other reason')
104                         ->andReturn(2)
105                         ->once();
106
107                 $console = new ServerBlock($this->blocklistMock, $this->consoleArgv);
108                 $console->setArgument(0, 'add');
109                 $console->setArgument(1, 'pod.ordoevangelistarum.com');
110                 $console->setArgument(2, 'Other reason');
111                 $txt = $this->dumpExecute($console);
112
113                 self::assertEquals('The domain pattern \'pod.ordoevangelistarum.com\' is now updated. (Reason: \'Other reason\')' . "\n", $txt);
114         }
115
116         /**
117          * Test blockedservers remove command
118          */
119         public function testRemoveBlockedServer()
120         {
121                 $this->blocklistMock
122                         ->shouldReceive('removePattern')
123                         ->with('pod.ordoevangelistarum.com')
124                         ->andReturn(2)
125                         ->once();
126
127                 $console = new ServerBlock($this->blocklistMock, $this->consoleArgv);
128                 $console->setArgument(0, 'remove');
129                 $console->setArgument(1, 'pod.ordoevangelistarum.com');
130                 $txt = $this->dumpExecute($console);
131
132                 self::assertEquals('The domain pattern \'pod.ordoevangelistarum.com\' isn\'t blocked anymore' . "\n", $txt);
133         }
134
135         /**
136          * Test blockedservers with a wrong command
137          */
138         public function testBlockedServersWrongCommand()
139         {
140                 $console = new ServerBlock($this->blocklistMock, $this->consoleArgv);
141                 $console->setArgument(0, 'wrongcommand');
142                 $txt = $this->dumpExecute($console);
143
144                 self::assertStringStartsWith('[Warning] Unknown command', $txt);
145         }
146
147         /**
148          * Test blockedservers remove with not existing domain
149          */
150         public function testRemoveBlockedServerNotExist()
151         {
152                 $this->blocklistMock
153                         ->shouldReceive('removePattern')
154                         ->with('not.existing')
155                         ->andReturn(1)
156                         ->once();
157
158                 $console = new ServerBlock($this->blocklistMock, $this->consoleArgv);
159                 $console->setArgument(0, 'remove');
160                 $console->setArgument(1, 'not.existing');
161                 $txt = $this->dumpExecute($console);
162
163                 self::assertEquals('The domain pattern \'not.existing\' wasn\'t blocked.' . "\n", $txt);
164         }
165
166         /**
167          * Test blockedservers add command without argument
168          */
169         public function testAddBlockedServerMissingArgument()
170         {
171                 $console = new ServerBlock($this->blocklistMock, $this->consoleArgv);
172                 $console->setArgument(0, 'add');
173                 $txt = $this->dumpExecute($console);
174
175                 self::assertStringStartsWith('[Warning] Add needs a domain pattern and a reason.', $txt);
176
177                 $console = new ServerBlock($this->blocklistMock, $this->consoleArgv);
178                 $console->setArgument(0, 'add');
179                 $console->setArgument(1, 'testme.now');
180                 $txt = $this->dumpExecute($console);
181
182                 self::assertStringStartsWith('[Warning] Add needs a domain pattern and a reason.', $txt);
183         }
184
185         /**
186          * Test blockedservers add command without save
187          */
188         public function testAddBlockedServerNoSave()
189         {
190                 $this->blocklistMock
191                         ->shouldReceive('addPattern')
192                         ->with('testme.now', 'I like it!')
193                         ->andReturn(0)
194                         ->once();
195
196                 $console = new ServerBlock($this->blocklistMock, $this->consoleArgv);
197                 $console->setArgument(0, 'add');
198                 $console->setArgument(1, 'testme.now');
199                 $console->setArgument(2, 'I like it!');
200                 $txt = $this->dumpExecute($console);
201
202                 self::assertEquals('Couldn\'t save \'testme.now\' as blocked domain pattern' . "\n", $txt);
203         }
204
205         /**
206          * Test blockedservers remove command without save
207          */
208         public function testRemoveBlockedServerNoSave()
209         {
210                 $this->blocklistMock
211                         ->shouldReceive('removePattern')
212                         ->with('pod.ordoevangelistarum.com')
213                         ->andReturn(0)
214                         ->once();
215
216                 $console = new ServerBlock($this->blocklistMock, $this->consoleArgv);
217                 $console->setArgument(0, 'remove');
218                 $console->setArgument(1, 'pod.ordoevangelistarum.com');
219                 $txt = $this->dumpExecute($console);
220
221                 self::assertEquals('Couldn\'t remove \'pod.ordoevangelistarum.com\' from blocked domain patterns' . "\n", $txt);
222         }
223
224         /**
225          * Test blockedservers remove command without argument
226          */
227         public function testRemoveBlockedServerMissingArgument()
228         {
229                 $console = new ServerBlock($this->blocklistMock, $this->consoleArgv);
230                 $console->setArgument(0, 'remove');
231                 $txt = $this->dumpExecute($console);
232
233                 self::assertStringStartsWith('[Warning] Remove needs a second parameter.', $txt);
234         }
235
236         /**
237          * Test the blockedservers help
238          */
239         public function testBlockedServersHelp()
240         {
241                 $console = new ServerBlock($this->blocklistMock, $this->consoleArgv);
242                 $console->setOption('help', true);
243                 $txt = $this->dumpExecute($console);
244
245                 $help = <<<HELP
246 console serverblock - Manage blocked server domain patterns
247 Usage
248     bin/console serverblock [-h|--help|-?] [-v]
249     bin/console serverblock add <pattern> <reason> [-h|--help|-?] [-v]
250     bin/console serverblock remove <pattern> [-h|--help|-?] [-v]
251     bin/console serverblock export <filename>
252     bin/console serverblock import <filename>
253
254 Description
255     With this tool, you can list the current blocked server domain patterns
256     or you can add / remove a blocked server domain pattern from the list.
257     Using the export and import options you can share your server blocklist
258     with other node admins by CSV files.
259
260     Patterns are case-insensitive shell wildcard comprising the following special characters:
261     - * : Any number of characters
262     - ? : Any single character
263     - [<char1><char2>...] : char1 or char2 or...
264
265 Options
266     -h|--help|-? Show help information
267     -v           Show more debug information.
268
269 HELP;
270
271                 self::assertEquals($help, $txt);
272         }
273 }