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