3 * @copyright Copyright (C) 2010-2021, 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
41 * Assert that the string is XML and contain the root element.
43 * @param string $result XML string
44 * @param string $root_element Root element name
48 protected function assertXml(string $result = '', string $root_element = '')
50 self::assertStringStartsWith('<?xml version="1.0"?>', $result);
51 self::assertStringContainsString('<' . $root_element, $result);
52 // We could probably do more checks here.
56 * Transforms a response into a JSON class
58 * @param ResponseInterface $response
62 protected function toJson(ResponseInterface $response)
64 self::assertEquals(ICanCreateResponses::TYPE_JSON, $response->getHeaderLine(ICanCreateResponses::X_HEADER));
66 $body = (string)$response->getBody();
68 self::assertJson($body);
70 return json_decode($body);
73 protected function setUp(): void
75 parent::setUp(); // TODO: Change the autogenerated stub
77 $this->dice = $this->dice
78 ->addRule(Authentication::class, ['instanceOf' => AuthenticationDouble::class, 'shared' => true])
79 ->addRule(App::class, ['instanceOf' => AppDouble::class, 'shared' => true]);
80 DI::init($this->dice);
82 // Manual override to bypass API authentication
83 DI::app()->setIsLoggedIn(true);
85 AuthTestConfig::$authenticated = true;
86 AuthTestConfig::$user_id = 42;
88 $this->installAuthTest();
91 protected function tearDown(): void
93 BasicAuth::setCurrentUserID();
95 parent::tearDown(); // TODO: Change the autogenerated stub
103 public function installAuthTest()
106 $addon_file_path = __DIR__ . '/../../../Util/authtest/authtest.php';
107 $t = @filemtime($addon_file_path);
109 @include_once($addon_file_path);
110 if (function_exists($addon . '_install')) {
111 $func = $addon . '_install';
115 /** @var Database $dba */
116 $dba = $this->dice->create(Database::class);
118 $dba->insert('addon', [
122 'plugin_admin' => function_exists($addon . '_addon_admin'),
123 'hidden' => file_exists('addon/' . $addon . '/.hidden')