]> git.mxchange.org Git - friendica.git/blobdiff - tests/include/ApiTest.php
Move redundant System::baseUrl() to DI::baseUrl() calls
[friendica.git] / tests / include / ApiTest.php
index d00aeeb73d0b8d9894848b293a06c9303ac32ff0..6a5dde0c6586d4d01d4f5138482ad836fac1be0a 100644 (file)
@@ -7,12 +7,15 @@ namespace Friendica\Test;
 
 use Dice\Dice;
 use Friendica\App;
-use Friendica\BaseObject;
-use Friendica\Core\Config\Configuration;
-use Friendica\Core\Config\PConfiguration;
+use Friendica\Core\Config\IConfiguration;
+use Friendica\Core\Config\IPConfiguration;
 use Friendica\Core\Protocol;
+use Friendica\Core\Session;
+use Friendica\Core\Session\ISession;
 use Friendica\Core\System;
 use Friendica\Database\Database;
+use Friendica\DI;
+use Friendica\Model\Contact;
 use Friendica\Network\HTTPException;
 use Friendica\Test\Util\Database\StaticDatabase;
 use Monolog\Handler\TestHandler;
@@ -44,7 +47,7 @@ class ApiTest extends DatabaseTest
        /** @var App */
        protected $app;
 
-       /** @var Configuration */
+       /** @var IConfiguration */
        protected $config;
 
        /** @var Dice */
@@ -57,18 +60,34 @@ class ApiTest extends DatabaseTest
        {
                parent::setUp();
 
-               $this->dice = new Dice();
-               $this->dice = $this->dice->addRules(include __DIR__ . '/../../static/dependencies.config.php');
-               $this->dice = $this->dice->addRule(Database::class, ['instanceOf' => StaticDatabase::class, 'shared' => true]);
-               BaseObject::setDependencyInjection($this->dice);
+               $this->dice = (new Dice())
+                       ->addRules(include __DIR__ . '/../../static/dependencies.config.php')
+                       ->addRule(Database::class, ['instanceOf' => StaticDatabase::class, 'shared' => true])
+                       ->addRule(ISession::class, ['instanceOf' => Session\Memory::class, 'shared' => true, 'call' => null]);
+               DI::init($this->dice);
 
                /** @var Database $dba */
                $dba = $this->dice->create(Database::class);
 
+               /** @var IConfiguration $config */
+               $this->config = $this->dice->create(IConfiguration::class);
+
+               $this->config->set('system', 'url', 'http://localhost');
+               $this->config->set('system', 'hostname', 'localhost');
+               $this->config->set('system', 'worker_dont_fork', true);
+
+               // Default config
+               $this->config->set('config', 'hostname', 'localhost');
+               $this->config->set('system', 'throttle_limit_day', 100);
+               $this->config->set('system', 'throttle_limit_week', 100);
+               $this->config->set('system', 'throttle_limit_month', 100);
+               $this->config->set('system', 'theme', 'system_theme');
+
                // Load the API dataset for the whole API
                $this->loadFixture(__DIR__ . '/../datasets/api.fixture.php', $dba);
 
-               $this->app = BaseObject::getApp();
+               /** @var App app */
+               $this->app = DI::app();
 
                $this->app->argc = 1;
                $this->app->argv = ['home'];
@@ -96,6 +115,8 @@ class ApiTest extends DatabaseTest
                // User ID that we know is not in the database
                $this->wrongUserId = 666;
 
+               DI::session()->start();
+
                // Most API require login so we force the session
                $_SESSION = [
                        'allow_api'     => true,
@@ -106,20 +127,6 @@ class ApiTest extends DatabaseTest
                $_POST   = [];
                $_GET    = [];
                $_SERVER = [];
-
-               /** @var Configuration $config */
-               $this->config = $this->dice->create(Configuration::class);
-
-               $this->config->set('system', 'url', 'http://localhost');
-               $this->config->set('system', 'hostname', 'localhost');
-               $this->config->set('system', 'worker_dont_fork', true);
-
-               // Default config
-               $this->config->set('config', 'hostname', 'localhost');
-               $this->config->set('system', 'throttle_limit_day', 100);
-               $this->config->set('system', 'throttle_limit_week', 100);
-               $this->config->set('system', 'throttle_limit_month', 100);
-               $this->config->set('system', 'theme', 'system_theme');
        }
 
        /**
@@ -425,12 +432,14 @@ class ApiTest extends DatabaseTest
                        }
                ];
                $_SERVER['REQUEST_METHOD'] = 'method';
+               $_SERVER['QUERY_STRING'] = 'q=api_path';
                $_GET['callback']          = 'callback_name';
 
-               $this->app->query_string = 'api_path';
+               $args = DI::args()->determine($_SERVER, $_GET);
+
                $this->assertEquals(
                        'callback_name(["some_data"])',
-                       api_call($this->app)
+                       api_call($this->app, $args)
                );
        }
 
@@ -449,9 +458,14 @@ class ApiTest extends DatabaseTest
                                return ['data' => ['some_data']];
                        }
                ];
+
                $_SERVER['REQUEST_METHOD'] = 'method';
-               $this->configset('system', 'profiler', true);
-               $this->configset('rendertime', 'callstack', true);
+               $_SERVER['QUERY_STRING'] = 'q=api_path';
+
+               $args = DI::args()->determine($_SERVER, $_GET);
+
+               $this->config->set('system', 'profiler', true);
+               $this->config->set('rendertime', 'callstack', true);
                $this->app->callstack = [
                        'database'       => ['some_function' => 200],
                        'database_write' => ['some_function' => 200],
@@ -460,10 +474,9 @@ class ApiTest extends DatabaseTest
                        'network'        => ['some_function' => 200]
                ];
 
-               $this->app->query_string = 'api_path';
                $this->assertEquals(
                        '["some_data"]',
-                       api_call($this->app)
+                       api_call($this->app, $args)
                );
        }
 
@@ -483,11 +496,13 @@ class ApiTest extends DatabaseTest
                        }
                ];
                $_SERVER['REQUEST_METHOD'] = 'method';
+               $_SERVER['QUERY_STRING'] = 'q=api_path';
+
+               $args = DI::args()->determine($_SERVER, $_GET);
 
-               $this->app->query_string = 'api_path';
                $this->assertEquals(
                        '{"status":{"error":"Internal Server Error","code":"500 Internal Server Error","request":"api_path"}}',
-                       api_call($this->app)
+                       api_call($this->app, $args)
                );
        }
 
@@ -521,11 +536,13 @@ class ApiTest extends DatabaseTest
                        }
                ];
                $_SERVER['REQUEST_METHOD'] = 'method';
+               $_SERVER['QUERY_STRING'] = 'q=api_path.json';
+
+               $args = DI::args()->determine($_SERVER, $_GET);
 
-               $this->app->query_string = 'api_path.json';
                $this->assertEquals(
                        '["some_data"]',
-                       api_call($this->app)
+                       api_call($this->app, $args)
                );
        }
 
@@ -545,11 +562,13 @@ class ApiTest extends DatabaseTest
                        }
                ];
                $_SERVER['REQUEST_METHOD'] = 'method';
+               $_SERVER['QUERY_STRING'] = 'q=api_path.xml';
+
+               $args = DI::args()->determine($_SERVER, $_GET);
 
-               $this->app->query_string = 'api_path.xml';
                $this->assertEquals(
                        'some_data',
-                       api_call($this->app)
+                       api_call($this->app, $args)
                );
        }
 
@@ -569,12 +588,14 @@ class ApiTest extends DatabaseTest
                        }
                ];
                $_SERVER['REQUEST_METHOD'] = 'method';
+               $_SERVER['QUERY_STRING'] = 'q=api_path.rss';
+
+               $args = DI::args()->determine($_SERVER, $_GET);
 
-               $this->app->query_string = 'api_path.rss';
                $this->assertEquals(
                        '<?xml version="1.0" encoding="UTF-8"?>' . "\n" .
                        'some_data',
-                       api_call($this->app)
+                       api_call($this->app, $args)
                );
        }
 
@@ -594,12 +615,14 @@ class ApiTest extends DatabaseTest
                        }
                ];
                $_SERVER['REQUEST_METHOD'] = 'method';
+               $_SERVER['QUERY_STRING'] = 'q=api_path.atom';
+
+               $args = DI::args()->determine($_SERVER, $_GET);
 
-               $this->app->query_string = 'api_path.atom';
                $this->assertEquals(
                        '<?xml version="1.0" encoding="UTF-8"?>' . "\n" .
                        'some_data',
-                       api_call($this->app)
+                       api_call($this->app, $args)
                );
        }
 
@@ -614,10 +637,13 @@ class ApiTest extends DatabaseTest
                global $API;
                $API['api_path'] = ['method' => 'method'];
 
-               $this->app->query_string = 'api_path';
+               $_SERVER['QUERY_STRING'] = 'q=api_path';
+
+               $args = DI::args()->determine($_SERVER, $_GET);
+
                $this->assertEquals(
                        '{"status":{"error":"Method Not Allowed","code":"405 Method Not Allowed","request":"api_path"}}',
-                       api_call($this->app)
+                       api_call($this->app, $args)
                );
        }
 
@@ -634,13 +660,15 @@ class ApiTest extends DatabaseTest
                        'method' => 'method',
                        'auth'   => true
                ];
-               $_SERVER['REQUEST_METHOD'] = 'method';
                $_SESSION['authenticated'] = false;
+               $_SERVER['REQUEST_METHOD'] = 'method';
+               $_SERVER['QUERY_STRING'] = 'q=api_path';
+
+               $args = DI::args()->determine($_SERVER, $_GET);
 
-               $this->app->query_string = 'api_path';
                $this->assertEquals(
                        '{"status":{"error":"This API requires login","code":"401 Unauthorized","request":"api_path"}}',
-                       api_call($this->app)
+                       api_call($this->app, $args)
                );
        }
 
@@ -654,7 +682,7 @@ class ApiTest extends DatabaseTest
        {
                $this->assertEquals(
                        '{"status":{"error":"error_message","code":"200 OK","request":""}}',
-                       api_error('json', new HTTPException\OKException('error_message'))
+                       api_error('json', new HTTPException\OKException('error_message'), DI::args())
                );
        }
 
@@ -675,7 +703,7 @@ class ApiTest extends DatabaseTest
                        '  <code>200 OK</code>' . "\n" .
                        '  <request/>' . "\n" .
                        '</status>' . "\n",
-                       api_error('xml', new HTTPException\OKException('error_message'))
+                       api_error('xml', new HTTPException\OKException('error_message'), DI::args())
                );
        }
 
@@ -696,7 +724,7 @@ class ApiTest extends DatabaseTest
                        '  <code>200 OK</code>' . "\n" .
                        '  <request/>' . "\n" .
                        '</status>' . "\n",
-                       api_error('rss', new HTTPException\OKException('error_message'))
+                       api_error('rss', new HTTPException\OKException('error_message'), DI::args())
                );
        }
 
@@ -717,7 +745,7 @@ class ApiTest extends DatabaseTest
                        '  <code>200 OK</code>' . "\n" .
                        '  <request/>' . "\n" .
                        '</status>' . "\n",
-                       api_error('atom', new HTTPException\OKException('error_message'))
+                       api_error('atom', new HTTPException\OKException('error_message'), DI::args())
                );
        }
 
@@ -799,7 +827,7 @@ class ApiTest extends DatabaseTest
         */
        public function testApiGetUserWithFrioSchema()
        {
-               $pConfig = $this->dice->create(PConfiguration::class);
+               $pConfig = $this->dice->create(IPConfiguration::class);
                $pConfig->set($this->selfUser['id'], 'frio', 'schema', 'red');
                $user = api_get_user($this->app);
                $this->assertSelfUser($user);
@@ -815,7 +843,7 @@ class ApiTest extends DatabaseTest
         */
        public function testApiGetUserWithCustomFrioSchema()
        {
-               $pConfig = $this->dice->create(PConfiguration::class);
+               $pConfig = $this->dice->create(IPConfiguration::class);
                $pConfig->set($this->selfUser['id'], 'frio', 'schema', '---');
                $pConfig->set($this->selfUser['id'], 'frio', 'nav_bg', '#123456');
                $pConfig->set($this->selfUser['id'], 'frio', 'link_color', '#123456');
@@ -834,7 +862,7 @@ class ApiTest extends DatabaseTest
         */
        public function testApiGetUserWithEmptyFrioSchema()
        {
-               $pConfig = $this->dice->create(PConfiguration::class);
+               $pConfig = $this->dice->create(IPConfiguration::class);
                $pConfig->set($this->selfUser['id'], 'frio', 'schema', '---');
                $user = api_get_user($this->app);
                $this->assertSelfUser($user);
@@ -2390,7 +2418,7 @@ class ApiTest extends DatabaseTest
        public function testApiFormatItemsEmbededImages()
        {
                $this->assertEquals(
-                       'text ' . System::baseUrl() . '/display/item_guid',
+                       'text ' . DI::baseUrl() . '/display/item_guid',
                        api_format_items_embeded_images(['guid' => 'item_guid'], 'text data:image/foo')
                );
        }
@@ -2893,7 +2921,7 @@ class ApiTest extends DatabaseTest
                $result = api_statusnet_config('json');
                $this->assertEquals('localhost', $result['config']['site']['server']);
                $this->assertEquals('default', $result['config']['site']['theme']);
-               $this->assertEquals(System::baseUrl() . '/images/friendica-64.png', $result['config']['site']['logo']);
+               $this->assertEquals(DI::baseUrl() . '/images/friendica-64.png', $result['config']['site']['logo']);
                $this->assertTrue($result['config']['site']['fancy']);
                $this->assertEquals('en', $result['config']['site']['language']);
                $this->assertEquals('UTC', $result['config']['site']['timezone']);
@@ -2921,8 +2949,8 @@ class ApiTest extends DatabaseTest
         */
        public function testApiFfIds()
        {
-               $result = api_ff_ids('json');
-               $this->assertNull($result);
+               $result = api_ff_ids('json', Contact::FOLLOWER);
+               $this->assertEquals(['id' => []], $result);
        }
 
        /**
@@ -2944,7 +2972,7 @@ class ApiTest extends DatabaseTest
        public function testApiFfIdsWithoutAuthenticatedUser()
        {
                $_SESSION['authenticated'] = false;
-               api_ff_ids('json');
+               api_ff_ids('json', Contact::FOLLOWER);
        }
 
        /**
@@ -2955,7 +2983,7 @@ class ApiTest extends DatabaseTest
        public function testApiFriendsIds()
        {
                $result = api_friends_ids('json');
-               $this->assertNull($result);
+               $this->assertEquals(['id' => []], $result);
        }
 
        /**
@@ -2966,7 +2994,7 @@ class ApiTest extends DatabaseTest
        public function testApiFollowersIds()
        {
                $result = api_followers_ids('json');
-               $this->assertNull($result);
+               $this->assertEquals(['id' => []], $result);
        }
 
        /**
@@ -3715,28 +3743,6 @@ class ApiTest extends DatabaseTest
                $this->markTestIncomplete();
        }
 
-       /**
-        * Test the api_get_nick() function.
-        *
-        * @return void
-        */
-       public function testApiGetNick()
-       {
-               $result = api_get_nick($this->otherUser['nurl']);
-               $this->assertEquals('othercontact', $result);
-       }
-
-       /**
-        * Test the api_get_nick() function with a wrong URL.
-        *
-        * @return void
-        */
-       public function testApiGetNickWithWrongUrl()
-       {
-               $result = api_get_nick('wrong_url');
-               $this->assertFalse($result);
-       }
-
        /**
         * Test the api_in_reply_to() function.
         *
@@ -3774,16 +3780,6 @@ class ApiTest extends DatabaseTest
                $this->assertEquals('some_text [url="some_url"]"some_url"[/url]', $result);
        }
 
-       /**
-        * Test the api_clean_attachments() function.
-        *
-        * @return void
-        */
-       public function testApiCleanAttachments()
-       {
-               $this->markTestIncomplete();
-       }
-
        /**
         * Test the api_best_nickname() function.
         *