]> git.mxchange.org Git - friendica.git/blob - tests/src/Module/Api/ApiTest.php
Reenable Twitter/Lookup tests & add standard assertions
[friendica.git] / tests / src / Module / Api / ApiTest.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, 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 array $status Status array
119          *
120          * @return void
121          */
122         protected function assertStatus(array $status = [])
123         {
124                 self::assertIsString($status['text'] ?? '');
125                 self::assertIsInt($status['id'] ?? '');
126                 // We could probably do more checks here.
127         }
128
129         /**
130          * Transforms a response into a JSON class
131          *
132          * @param ResponseInterface $response
133          *
134          * @return mixed
135          */
136         protected function toJson(ResponseInterface $response)
137         {
138                 self::assertEquals(ICanCreateResponses::TYPE_JSON, $response->getHeaderLine(ICanCreateResponses::X_HEADER));
139
140                 $body = (string)$response->getBody();
141
142                 self::assertJson($body);
143
144                 return json_decode($body);
145         }
146
147         protected function setUp(): void
148         {
149                 parent::setUp(); // TODO: Change the autogenerated stub
150
151                 $this->dice = $this->dice
152                         ->addRule(Authentication::class, ['instanceOf' => AuthenticationDouble::class, 'shared' => true])
153                         ->addRule(App::class, ['instanceOf' => AppDouble::class, 'shared' => true]);
154                 DI::init($this->dice);
155
156                 // Manual override to bypass API authentication
157                 DI::app()->setIsLoggedIn(true);
158
159                 AuthTestConfig::$authenticated = true;
160                 AuthTestConfig::$user_id       = 42;
161
162                 $this->installAuthTest();
163         }
164
165         protected function tearDown(): void
166         {
167                 BasicAuth::setCurrentUserID();
168
169                 parent::tearDown(); // TODO: Change the autogenerated stub
170         }
171
172         /**
173          * installs auththest.
174          *
175          * @throws \Exception
176          */
177         public function installAuthTest()
178         {
179                 $addon           = 'authtest';
180                 $addon_file_path = __DIR__ . '/../../../Util/authtest/authtest.php';
181                 $t               = @filemtime($addon_file_path);
182
183                 @include_once($addon_file_path);
184                 if (function_exists($addon . '_install')) {
185                         $func = $addon . '_install';
186                         $func(DI::app());
187                 }
188
189                 /** @var Database $dba */
190                 $dba = $this->dice->create(Database::class);
191
192                 $dba->insert('addon', [
193                         'name'         => $addon,
194                         'installed'    => true,
195                         'timestamp'    => $t,
196                         'plugin_admin' => function_exists($addon . '_addon_admin'),
197                         'hidden'       => file_exists('addon/' . $addon . '/.hidden')
198                 ]);
199
200                 Addon::loadAddons();
201                 Hook::loadHooks();
202         }
203 }