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