]> git.mxchange.org Git - friendica.git/blob - tests/src/Module/Api/ApiTest.php
spelling: forums
[friendica.git] / tests / src / Module / Api / ApiTest.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, 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\Module\Api;
23
24 use Friendica\App;
25 use Friendica\Capabilities\ICanCreateResponses;
26 use Friendica\Core\Addon;
27 use Friendica\Core\Config\Capability\IManageConfigValues;
28 use Friendica\Core\Hook;
29 use Friendica\Database\Database;
30 use Friendica\DI;
31 use Friendica\Module\Special\HTTPException;
32 use Friendica\Security\Authentication;
33 use Friendica\Security\BasicAuth;
34 use Friendica\Test\FixtureTest;
35 use Friendica\Test\Util\AppDouble;
36 use Friendica\Test\Util\AuthenticationDouble;
37 use Friendica\Test\Util\AuthTestConfig;
38 use Mockery\MockInterface;
39 use Psr\Http\Message\ResponseInterface;
40
41 abstract class ApiTest extends FixtureTest
42 {
43         // User data that the test database is populated with
44         const SELF_USER = [
45                 'id'   => 42,
46                 'name' => 'Self contact',
47                 'nick' => 'selfcontact',
48                 'nurl' => 'http://localhost/profile/selfcontact'
49         ];
50
51         const FRIEND_USER = [
52                 'id'   => 44,
53                 'name' => 'Friend contact',
54                 'nick' => 'friendcontact',
55                 'nurl' => 'http://localhost/profile/friendcontact'
56         ];
57
58         const OTHER_USER = [
59                 'id'   => 43,
60                 'name' => 'othercontact',
61                 'nick' => 'othercontact',
62                 'nurl' => 'http://localhost/profile/othercontact'
63         ];
64
65         /** @var HTTPException */
66         protected $httpExceptionMock;
67
68         // User ID that we know is not in the database
69         const WRONG_USER_ID = 666;
70
71         /**
72          * Assert that the string is XML and contain the root element.
73          *
74          * @param string $result       XML string
75          * @param string $root_element Root element name
76          *
77          * @return void
78          */
79         protected function assertXml(string $result = '', string $root_element = '')
80         {
81                 self::assertStringStartsWith('<?xml version="1.0"?>', $result);
82                 self::assertStringContainsString('<' . $root_element, $result);
83                 // We could probably do more checks here.
84         }
85
86         /**
87          * Assert that an user array contains expected keys.
88          *
89          * @param \stdClass $user User
90          *
91          * @return void
92          */
93         protected function assertSelfUser(\stdClass $user)
94         {
95                 self::assertEquals(self::SELF_USER['id'], $user->uid);
96                 self::assertEquals(self::SELF_USER['id'], $user->cid);
97                 self::assertEquals(1, $user->self);
98                 self::assertEquals('DFRN', $user->location);
99                 self::assertEquals(self::SELF_USER['name'], $user->name);
100                 self::assertEquals(self::SELF_USER['nick'], $user->screen_name);
101                 self::assertEquals('dfrn', $user->network);
102                 self::assertTrue($user->verified);
103         }
104
105         /**
106          * Assert that an user array contains expected keys.
107          *
108          * @param \stdClass $user User
109          *
110          * @return void
111          */
112         protected function assertOtherUser(\stdClass $user)
113         {
114                 self::assertEquals(self::OTHER_USER['id'], $user->id);
115                 self::assertEquals(self::OTHER_USER['id'], $user->id_str);
116                 self::assertEquals(self::OTHER_USER['name'], $user->name);
117                 self::assertEquals(self::OTHER_USER['nick'], $user->screen_name);
118                 self::assertFalse($user->verified);
119         }
120
121         /**
122          * Assert that a status array contains expected keys.
123          *
124          * @param \stdClass $status Status
125          *
126          * @return void
127          */
128         protected function assertStatus(\stdClass $status)
129         {
130                 self::assertIsString($status->text);
131                 self::assertIsInt($status->id);
132                 // We could probably do more checks here.
133         }
134
135         /**
136          * Get the path to a temporary empty PNG image.
137          *
138          * @return string Path
139          */
140         protected function getTempImage()
141         {
142                 $tmpFile = tempnam(sys_get_temp_dir(), 'tmp_file');
143                 file_put_contents(
144                         $tmpFile,
145                         base64_decode(
146                         // Empty 1x1 px PNG image
147                                 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8/5+hHgAHggJ/PchI7wAAAABJRU5ErkJggg=='
148                         )
149                 );
150
151                 return $tmpFile;
152         }
153
154         /**
155          * Transforms a response into a JSON class
156          *
157          * @param ResponseInterface $response
158          *
159          * @return mixed
160          */
161         protected function toJson(ResponseInterface $response)
162         {
163                 self::assertEquals(ICanCreateResponses::TYPE_JSON, $response->getHeaderLine(ICanCreateResponses::X_HEADER));
164
165                 $body = (string)$response->getBody();
166
167                 self::assertJson($body);
168
169                 return json_decode($body);
170         }
171
172         protected function setUp(): void
173         {
174                 parent::setUp(); // TODO: Change the autogenerated stub
175
176                 $this->dice = $this->dice
177                         ->addRule(Authentication::class, ['instanceOf' => AuthenticationDouble::class, 'shared' => true])
178                         ->addRule(App::class, ['instanceOf' => AppDouble::class, 'shared' => true]);
179                 DI::init($this->dice);
180
181                 // Manual override to bypass API authentication
182                 DI::app()->setIsLoggedIn(true);
183
184                 $this->httpExceptionMock = $this->dice->create(HTTPException::class);
185
186                 AuthTestConfig::$authenticated = true;
187                 AuthTestConfig::$user_id       = 42;
188
189                 $this->installAuthTest();
190         }
191
192         protected function tearDown(): void
193         {
194                 BasicAuth::setCurrentUserID();
195
196                 parent::tearDown(); // TODO: Change the autogenerated stub
197         }
198
199         /**
200          * installs auththest.
201          *
202          * @throws \Exception
203          */
204         public function installAuthTest()
205         {
206                 $addon           = 'authtest';
207                 $addon_file_path = __DIR__ . '/../../../Util/authtest/authtest.php';
208                 $t               = @filemtime($addon_file_path);
209
210                 @include_once($addon_file_path);
211                 if (function_exists($addon . '_install')) {
212                         $func = $addon . '_install';
213                         $func(DI::app());
214                 }
215
216                 /** @var $config IManageConfigValues */
217                 $config = $this->dice->create(IManageConfigValues::class);
218
219                 $config->set('addons', $addon, [
220                         'name'         => $addon,
221                         'installed'    => true,
222                         'timestamp'    => $t,
223                         'plugin_admin' => function_exists($addon . '_addon_admin'),
224                 ]);
225
226                 Addon::loadAddons();
227                 Hook::loadHooks();
228         }
229 }