]> git.mxchange.org Git - friendica.git/blob - tests/src/Module/Api/ApiTest.php
Simplify json testing
[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         /**
41          * Assert that the string is XML and contain the root element.
42          *
43          * @param string $result       XML string
44          * @param string $root_element Root element name
45          *
46          * @return void
47          */
48         protected function assertXml(string $result = '', string $root_element = '')
49         {
50                 self::assertStringStartsWith('<?xml version="1.0"?>', $result);
51                 self::assertStringContainsString('<' . $root_element, $result);
52                 // We could probably do more checks here.
53         }
54
55         /**
56          * Transforms a response into a JSON class
57          *
58          * @param ResponseInterface $response
59          *
60          * @return mixed
61          */
62         protected function toJson(ResponseInterface $response)
63         {
64                 self::assertEquals(ICanCreateResponses::TYPE_JSON, $response->getHeaderLine(ICanCreateResponses::X_HEADER));
65
66                 $body = (string)$response->getBody();
67
68                 self::assertJson($body);
69
70                 return json_decode($body);
71         }
72
73         protected function setUp(): void
74         {
75                 parent::setUp(); // TODO: Change the autogenerated stub
76
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);
81
82                 // Manual override to bypass API authentication
83                 DI::app()->setIsLoggedIn(true);
84
85                 AuthTestConfig::$authenticated = true;
86                 AuthTestConfig::$user_id       = 42;
87
88                 $this->installAuthTest();
89         }
90
91         protected function tearDown(): void
92         {
93                 BasicAuth::setCurrentUserID();
94
95                 parent::tearDown(); // TODO: Change the autogenerated stub
96         }
97
98         /**
99          * installs auththest.
100          *
101          * @throws \Exception
102          */
103         public function installAuthTest()
104         {
105                 $addon           = 'authtest';
106                 $addon_file_path = __DIR__ . '/../../../Util/authtest/authtest.php';
107                 $t               = @filemtime($addon_file_path);
108
109                 @include_once($addon_file_path);
110                 if (function_exists($addon . '_install')) {
111                         $func = $addon . '_install';
112                         $func(DI::app());
113                 }
114
115                 /** @var Database $dba */
116                 $dba = $this->dice->create(Database::class);
117
118                 $dba->insert('addon', [
119                         'name'         => $addon,
120                         'installed'    => true,
121                         'timestamp'    => $t,
122                         'plugin_admin' => function_exists($addon . '_addon_admin'),
123                         'hidden'       => file_exists('addon/' . $addon . '/.hidden')
124                 ]);
125
126                 Addon::loadAddons();
127                 Hook::loadHooks();
128         }
129 }