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