3 * @copyright Copyright (C) 2010-2022, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Test\src\Module\Api;
25 use Friendica\Capabilities\ICanCreateResponses;
26 use Friendica\Core\Addon;
27 use Friendica\Core\Hook;
28 use Friendica\Database\Database;
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;
38 abstract class ApiTest extends FixtureTest
40 // User data that the test database is populated with
43 'name' => 'Self contact',
44 'nick' => 'selfcontact',
45 'nurl' => 'http://localhost/profile/selfcontact'
50 'name' => 'Friend contact',
51 'nick' => 'friendcontact',
52 'nurl' => 'http://localhost/profile/friendcontact'
57 'name' => 'othercontact',
58 'nick' => 'othercontact',
59 'nurl' => 'http://localhost/profile/othercontact'
62 // User ID that we know is not in the database
63 const WRONG_USER_ID = 666;
66 * Assert that the string is XML and contain the root element.
68 * @param string $result XML string
69 * @param string $root_element Root element name
73 protected function assertXml(string $result = '', string $root_element = '')
75 self::assertStringStartsWith('<?xml version="1.0"?>', $result);
76 self::assertStringContainsString('<' . $root_element, $result);
77 // We could probably do more checks here.
81 * Assert that an user array contains expected keys.
83 * @param \stdClass $user User
87 protected function assertSelfUser(\stdClass $user)
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);
100 * Assert that an user array contains expected keys.
102 * @param \stdClass $user User
106 protected function assertOtherUser(\stdClass $user)
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);
116 * Assert that a status array contains expected keys.
118 * @param \stdClass $status Status
122 protected function assertStatus(\stdClass $status)
124 self::assertIsString($status->text);
125 self::assertIsInt($status->id);
126 // We could probably do more checks here.
130 * Get the path to a temporary empty PNG image.
132 * @return string Path
134 protected function getTempImage()
136 $tmpFile = tempnam(sys_get_temp_dir(), 'tmp_file');
140 // Empty 1x1 px PNG image
141 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8/5+hHgAHggJ/PchI7wAAAABJRU5ErkJggg=='
149 * Transforms a response into a JSON class
151 * @param ResponseInterface $response
155 protected function toJson(ResponseInterface $response)
157 self::assertEquals(ICanCreateResponses::TYPE_JSON, $response->getHeaderLine(ICanCreateResponses::X_HEADER));
159 $body = (string)$response->getBody();
161 self::assertJson($body);
163 return json_decode($body);
166 protected function setUp(): void
168 parent::setUp(); // TODO: Change the autogenerated stub
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);
175 // Manual override to bypass API authentication
176 DI::app()->setIsLoggedIn(true);
178 AuthTestConfig::$authenticated = true;
179 AuthTestConfig::$user_id = 42;
181 $this->installAuthTest();
184 protected function tearDown(): void
186 BasicAuth::setCurrentUserID();
188 parent::tearDown(); // TODO: Change the autogenerated stub
192 * installs auththest.
196 public function installAuthTest()
199 $addon_file_path = __DIR__ . '/../../../Util/authtest/authtest.php';
200 $t = @filemtime($addon_file_path);
202 @include_once($addon_file_path);
203 if (function_exists($addon . '_install')) {
204 $func = $addon . '_install';
208 /** @var Database $dba */
209 $dba = $this->dice->create(Database::class);
211 $dba->insert('addon', [
215 'plugin_admin' => function_exists($addon . '_addon_admin'),
216 'hidden' => file_exists('addon/' . $addon . '/.hidden')