]> git.mxchange.org Git - friendica.git/blob - tests/src/Module/Api/ApiTest.php
Merge pull request #11043 from annando/api-fixes
[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\Core\Addon;
26 use Friendica\Core\Hook;
27 use Friendica\Database\Database;
28 use Friendica\DI;
29 use Friendica\Security\Authentication;
30 use Friendica\Security\BasicAuth;
31 use Friendica\Test\FixtureTest;
32 use Friendica\Test\Util\AppDouble;
33 use Friendica\Test\Util\AuthenticationDouble;
34 use Friendica\Test\Util\AuthTestConfig;
35
36 abstract class ApiTest extends FixtureTest
37 {
38         /**
39          * Assert that the string is XML and contain the root element.
40          *
41          * @param string $result       XML string
42          * @param string $root_element Root element name
43          *
44          * @return void
45          */
46         protected function assertXml(string $result = '', string $root_element = '')
47         {
48                 self::assertStringStartsWith('<?xml version="1.0"?>', $result);
49                 self::assertStringContainsString('<' . $root_element, $result);
50                 // We could probably do more checks here.
51         }
52
53         protected function setUp(): void
54         {
55                 parent::setUp(); // TODO: Change the autogenerated stub
56
57                 $this->dice = $this->dice
58                         ->addRule(Authentication::class, ['instanceOf' => AuthenticationDouble::class, 'shared' => true])
59                         ->addRule(App::class, ['instanceOf' => AppDouble::class, 'shared' => true]);
60                 DI::init($this->dice);
61
62                 // Manual override to bypass API authentication
63                 DI::app()->setIsLoggedIn(true);
64
65                 AuthTestConfig::$authenticated = true;
66                 AuthTestConfig::$user_id = 42;
67
68                 $this->installAuthTest();
69         }
70
71         protected function tearDown(): void
72         {
73                 BasicAuth::setCurrentUserID();
74
75                 parent::tearDown(); // TODO: Change the autogenerated stub
76         }
77
78         /**
79          * installs auththest.
80          *
81          * @throws \Exception
82          */
83         public function installAuthTest()
84         {
85                 $addon           = 'authtest';
86                 $addon_file_path = __DIR__ . '/../../../Util/authtest/authtest.php';
87                 $t               = @filemtime($addon_file_path);
88
89                 @include_once($addon_file_path);
90                 if (function_exists($addon . '_install')) {
91                         $func = $addon . '_install';
92                         $func(DI::app());
93                 }
94
95                 /** @var Database $dba */
96                 $dba = $this->dice->create(Database::class);
97
98                 $dba->insert('addon', [
99                         'name'         => $addon,
100                         'installed'    => true,
101                         'timestamp'    => $t,
102                         'plugin_admin' => function_exists($addon . '_addon_admin'),
103                         'hidden'       => file_exists('addon/' . $addon . '/.hidden')
104                 ]);
105
106                 Addon::loadAddons();
107                 Hook::loadHooks();
108         }
109 }