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\Config\Capability\IManageConfigValues;
28 use Friendica\Core\Hook;
29 use Friendica\Database\Database;
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;
41 abstract class ApiTest extends FixtureTest
43 // User data that the test database is populated with
46 'name' => 'Self contact',
47 'nick' => 'selfcontact',
48 'nurl' => 'http://localhost/profile/selfcontact'
53 'name' => 'Friend contact',
54 'nick' => 'friendcontact',
55 'nurl' => 'http://localhost/profile/friendcontact'
60 'name' => 'othercontact',
61 'nick' => 'othercontact',
62 'nurl' => 'http://localhost/profile/othercontact'
65 /** @var HTTPException */
66 protected $httpExceptionMock;
68 // User ID that we know is not in the database
69 const WRONG_USER_ID = 666;
72 * Assert that the string is XML and contain the root element.
74 * @param string $result XML string
75 * @param string $root_element Root element name
79 protected function assertXml(string $result = '', string $root_element = '')
81 self::assertStringStartsWith('<?xml version="1.0"?>', $result);
82 self::assertStringContainsString('<' . $root_element, $result);
83 // We could probably do more checks here.
87 * Assert that an user array contains expected keys.
89 * @param \stdClass $user User
93 protected function assertSelfUser(\stdClass $user)
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);
106 * Assert that an user array contains expected keys.
108 * @param \stdClass $user User
112 protected function assertOtherUser(\stdClass $user)
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);
122 * Assert that a status array contains expected keys.
124 * @param \stdClass $status Status
128 protected function assertStatus(\stdClass $status)
130 self::assertIsString($status->text);
131 self::assertIsInt($status->id);
132 // We could probably do more checks here.
136 * Get the path to a temporary empty PNG image.
138 * @return string Path
140 protected function getTempImage()
142 $tmpFile = tempnam(sys_get_temp_dir(), 'tmp_file');
146 // Empty 1x1 px PNG image
147 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8/5+hHgAHggJ/PchI7wAAAABJRU5ErkJggg=='
155 * Transforms a response into a JSON class
157 * @param ResponseInterface $response
161 protected function toJson(ResponseInterface $response)
163 self::assertEquals(ICanCreateResponses::TYPE_JSON, $response->getHeaderLine(ICanCreateResponses::X_HEADER));
165 $body = (string)$response->getBody();
167 self::assertJson($body);
169 return json_decode($body);
172 protected function setUp(): void
174 parent::setUp(); // TODO: Change the autogenerated stub
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);
181 // Manual override to bypass API authentication
182 DI::app()->setIsLoggedIn(true);
184 $this->httpExceptionMock = $this->dice->create(HTTPException::class);
186 AuthTestConfig::$authenticated = true;
187 AuthTestConfig::$user_id = 42;
189 $this->installAuthTest();
192 protected function tearDown(): void
194 BasicAuth::setCurrentUserID();
196 parent::tearDown(); // TODO: Change the autogenerated stub
200 * installs auththest.
204 public function installAuthTest()
207 $addon_file_path = __DIR__ . '/../../../Util/authtest/authtest.php';
208 $t = @filemtime($addon_file_path);
210 @include_once($addon_file_path);
211 if (function_exists($addon . '_install')) {
212 $func = $addon . '_install';
216 /** @var $config IManageConfigValues */
217 $config = $this->dice->create(IManageConfigValues::class);
219 $config->set('addons', $addon, [
223 'plugin_admin' => function_exists($addon . '_addon_admin'),