3 * @copyright Copyright (C) 2010-2023, 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\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;
40 abstract class ApiTest extends FixtureTest
42 // User data that the test database is populated with
45 'name' => 'Self contact',
46 'nick' => 'selfcontact',
47 'nurl' => 'http://localhost/profile/selfcontact'
52 'name' => 'Friend contact',
53 'nick' => 'friendcontact',
54 'nurl' => 'http://localhost/profile/friendcontact'
59 'name' => 'othercontact',
60 'nick' => 'othercontact',
61 'nurl' => 'http://localhost/profile/othercontact'
64 /** @var HTTPException */
65 protected $httpExceptionMock;
67 // User ID that we know is not in the database
68 const WRONG_USER_ID = 666;
71 * Assert that the string is XML and contain the root element.
73 * @param string $result XML string
74 * @param string $root_element Root element name
78 protected function assertXml(string $result = '', string $root_element = '')
80 self::assertStringStartsWith('<?xml version="1.0"?>', $result);
81 self::assertStringContainsString('<' . $root_element, $result);
82 // We could probably do more checks here.
86 * Assert that an user array contains expected keys.
88 * @param \stdClass $user User
92 protected function assertSelfUser(\stdClass $user)
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);
105 * Assert that an user array contains expected keys.
107 * @param \stdClass $user User
111 protected function assertOtherUser(\stdClass $user)
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);
121 * Assert that a status array contains expected keys.
123 * @param \stdClass $status Status
127 protected function assertStatus(\stdClass $status)
129 self::assertIsString($status->text);
130 self::assertIsInt($status->id);
131 // We could probably do more checks here.
135 * Get the path to a temporary empty PNG image.
137 * @return string Path
139 protected function getTempImage()
141 $tmpFile = tempnam(sys_get_temp_dir(), 'tmp_file');
145 // Empty 1x1 px PNG image
146 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8/5+hHgAHggJ/PchI7wAAAABJRU5ErkJggg=='
154 * Transforms a response into a JSON class
156 * @param ResponseInterface $response
160 protected function toJson(ResponseInterface $response)
162 self::assertEquals(ICanCreateResponses::TYPE_JSON, $response->getHeaderLine(ICanCreateResponses::X_HEADER));
164 $body = (string)$response->getBody();
166 self::assertJson($body);
168 return json_decode($body);
171 protected function setUp(): void
173 parent::setUp(); // TODO: Change the autogenerated stub
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);
180 // Manual override to bypass API authentication
181 DI::app()->setIsLoggedIn(true);
183 $this->httpExceptionMock = $this->dice->create(HTTPException::class);
185 AuthTestConfig::$authenticated = true;
186 AuthTestConfig::$user_id = 42;
188 $this->installAuthTest();
191 protected function tearDown(): void
193 BasicAuth::setCurrentUserID();
195 parent::tearDown(); // TODO: Change the autogenerated stub
199 * installs auththest.
203 public function installAuthTest()
206 $addon_file_path = __DIR__ . '/../../../Util/authtest/authtest.php';
207 $t = @filemtime($addon_file_path);
209 @include_once($addon_file_path);
210 if (function_exists($addon . '_install')) {
211 $func = $addon . '_install';
215 /** @var Database $dba */
216 $dba = $this->dice->create(Database::class);
218 $dba->insert('addon', [
222 'plugin_admin' => function_exists($addon . '_addon_admin'),
223 'hidden' => file_exists('addon/' . $addon . '/.hidden')