]> git.mxchange.org Git - friendica.git/blobdiff - tests/legacy/ApiTest.php
Merge remote-tracking branch 'upstream/develop' into error-handling
[friendica.git] / tests / legacy / ApiTest.php
index 423294e5972f418be210f386e7d1c40a017727c7..e800f68c80e2c76e1ab2993f9694100f1b38b978 100644 (file)
@@ -6,12 +6,13 @@
 namespace Friendica\Test\legacy;
 
 use Friendica\App;
-use Friendica\Core\Config\IConfig;
-use Friendica\Core\PConfig\IPConfig;
+use Friendica\Core\Config\Capability\IManageConfigValues;
+use Friendica\Core\PConfig\Capability\IManagePersonalConfigValues;
 use Friendica\Core\Protocol;
 use Friendica\DI;
 use Friendica\Network\HTTPException;
 use Friendica\Test\FixtureTest;
+use Friendica\Util\DateTimeFormat;
 use Friendica\Util\Temporal;
 use Monolog\Handler\TestHandler;
 
@@ -44,7 +45,7 @@ class ApiTest extends FixtureTest
        /** @var App */
        protected $app;
 
-       /** @var IConfig */
+       /** @var IManageConfigValues */
        protected $config;
 
        /**
@@ -58,8 +59,8 @@ class ApiTest extends FixtureTest
 
                parent::setUp();
 
-               /** @var IConfig $config */
-               $this->config = $this->dice->create(IConfig::class);
+               /** @var IManageConfigValues $config */
+               $this->config = $this->dice->create(IManageConfigValues::class);
 
                $this->config->set('system', 'url', 'http://localhost');
                $this->config->set('system', 'hostname', 'localhost');
@@ -76,8 +77,7 @@ class ApiTest extends FixtureTest
                /** @var App app */
                $this->app = DI::app();
 
-               $this->app->argc = 1;
-               $this->app->argv = [''];
+               DI::args()->setArgc(1);
 
                // User data that the test database is populated with
                $this->selfUser   = [
@@ -157,8 +157,8 @@ class ApiTest extends FixtureTest
         */
        private function assertStatus(array $status = [])
        {
-               self::assertInternalType('string', $status['text'] ?? '');
-               self::assertInternalType('int', $status['id'] ?? '');
+               self::assertIsString($status['text'] ?? '');
+               self::assertIsInt($status['id'] ?? '');
                // We could probably do more checks here.
        }
 
@@ -171,9 +171,9 @@ class ApiTest extends FixtureTest
         */
        private function assertList(array $list = [])
        {
-               self::assertInternalType('string', $list['name']);
-               self::assertInternalType('int', $list['id']);
-               self::assertInternalType('string', $list['id_str']);
+               self::assertIsString($list['name']);
+               self::assertIsInt($list['id']);
+               self::assertIsString('string', $list['id_str']);
                self::assertContains($list['mode'], ['public', 'private']);
                // We could probably do more checks here.
        }
@@ -762,7 +762,7 @@ class ApiTest extends FixtureTest
        public function testApiRssExtraWithoutUserInfo()
        {
                $result = api_rss_extra($this->app, [], null);
-               self::assertInternalType('array', $result['$user']);
+               self::assertIsArray($result['$user']);
                self::assertArrayHasKey('alternate', $result['$rss']);
                self::assertArrayHasKey('self', $result['$rss']);
                self::assertArrayHasKey('base', $result['$rss']);
@@ -813,7 +813,7 @@ class ApiTest extends FixtureTest
         */
        public function testApiGetUserWithFrioSchema()
        {
-               $pConfig = $this->dice->create(IPConfig::class);
+               $pConfig = $this->dice->create(IManagePersonalConfigValues::class);
                $pConfig->set($this->selfUser['id'], 'frio', 'schema', 'red');
                $user = api_get_user($this->app);
                self::assertSelfUser($user);
@@ -829,7 +829,7 @@ class ApiTest extends FixtureTest
         */
        public function testApiGetUserWithEmptyFrioSchema()
        {
-               $pConfig = $this->dice->create(IPConfig::class);
+               $pConfig = $this->dice->create(IManagePersonalConfigValues::class);
                $pConfig->set($this->selfUser['id'], 'frio', 'schema', '---');
                $user = api_get_user($this->app);
                self::assertSelfUser($user);
@@ -845,7 +845,7 @@ class ApiTest extends FixtureTest
         */
        public function testApiGetUserWithCustomFrioSchema()
        {
-               $pConfig = $this->dice->create(IPConfig::class);
+               $pConfig = $this->dice->create(IManagePersonalConfigValues::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');
@@ -925,7 +925,7 @@ class ApiTest extends FixtureTest
        {
                global $called_api;
                $called_api         = ['api_path'];
-               $this->app->argv[1] = $this->otherUser['id'] . '.json';
+               DI::args()->setArgv(['', $this->otherUser['id'] . '.json']);
                self::assertOtherUser(api_get_user($this->app));
        }
 
@@ -1198,7 +1198,7 @@ class ApiTest extends FixtureTest
         */
        public function testApiStatusesMediap()
        {
-               $this->app->argc = 2;
+               DI::args()->setArgc(2);
 
                $_FILES         = [
                        'media' => [
@@ -1370,7 +1370,7 @@ class ApiTest extends FixtureTest
                        ]
                ];
                $app       = DI::app();
-               $app->argc = 2;
+               DI::args()->setArgc(2);
 
                $result = api_media_upload();
                self::assertEquals('image/png', $result['media']['image']['image_type']);
@@ -1793,8 +1793,8 @@ class ApiTest extends FixtureTest
         */
        public function testApiStatusesShowWithId()
        {
-               $this->app->argv[3] = 1;
-               $result             = api_statuses_show('json');
+               DI::args()->setArgv(['', '', '', 1]);
+               $result = api_statuses_show('json');
                self::assertStatus($result['status']);
        }
 
@@ -1805,7 +1805,7 @@ class ApiTest extends FixtureTest
         */
        public function testApiStatusesShowWithConversation()
        {
-               $this->app->argv[3]       = 1;
+               DI::args()->setArgv(['', '', '', 1]);
                $_REQUEST['conversation'] = 1;
                $result                   = api_statuses_show('json');
                self::assertNotEmpty($result['status']);
@@ -1845,7 +1845,7 @@ class ApiTest extends FixtureTest
         */
        public function testApiConversationShowWithId()
        {
-               $this->app->argv[3] = 1;
+               DI::args()->setArgv(['', '', '', 1]);
                $_REQUEST['max_id'] = 10;
                $_REQUEST['page']   = -2;
                $result             = api_conversation_show('json');
@@ -1898,13 +1898,13 @@ class ApiTest extends FixtureTest
         */
        public function testApiStatusesRepeatWithId()
        {
-               $this->app->argv[3] = 1;
-               $result             = api_statuses_repeat('json');
+               DI::args()->setArgv(['', '', '', 1]);
+               $result = api_statuses_repeat('json');
                self::assertStatus($result['status']);
 
                // Also test with a shared status
-               $this->app->argv[3] = 5;
-               $result             = api_statuses_repeat('json');
+               DI::args()->setArgv(['', '', '', 5]);
+               $result = api_statuses_repeat('json');
                self::assertStatus($result['status']);
        }
 
@@ -1938,8 +1938,8 @@ class ApiTest extends FixtureTest
         */
        public function testApiStatusesDestroyWithId()
        {
-               $this->app->argv[3] = 1;
-               $result             = api_statuses_destroy('json');
+               DI::args()->setArgv(['', '', '', 1]);
+               $result = api_statuses_destroy('json');
                self::assertStatus($result['status']);
        }
 
@@ -1950,7 +1950,7 @@ class ApiTest extends FixtureTest
         */
        public function testApiStatusesMentions()
        {
-               $this->app->user    = ['nickname' => $this->selfUser['nick']];
+               $this->app->setLoggedInUserNickname($this->selfUser['nick']);
                $_REQUEST['max_id'] = 10;
                $result             = api_statuses_mentions('json');
                self::assertEmpty($result['status']);
@@ -2057,8 +2057,7 @@ class ApiTest extends FixtureTest
        public function testApiFavoritesCreateDestroy()
        {
                $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
-               $this->app->argv = ['api', '1.1', 'favorites', 'create'];
-               $this->app->argc = count($this->app->argv);
+               DI::args()->setArgv(['api', '1.1', 'favorites', 'create']);
                api_favorites_create_destroy('json');
        }
 
@@ -2070,8 +2069,7 @@ class ApiTest extends FixtureTest
        public function testApiFavoritesCreateDestroyWithInvalidId()
        {
                $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
-               $this->app->argv = ['api', '1.1', 'favorites', 'create', '12.json'];
-               $this->app->argc = count($this->app->argv);
+               DI::args()->setArgv(['api', '1.1', 'favorites', 'create', '12.json']);
                api_favorites_create_destroy('json');
        }
 
@@ -2083,9 +2081,8 @@ class ApiTest extends FixtureTest
        public function testApiFavoritesCreateDestroyWithInvalidAction()
        {
                $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
-               $this->app->argv = ['api', '1.1', 'favorites', 'change.json'];
-               $this->app->argc = count($this->app->argv);
-               $_REQUEST['id']  = 1;
+               DI::args()->setArgv(['api', '1.1', 'favorites', 'change.json']);
+               $_REQUEST['id'] = 1;
                api_favorites_create_destroy('json');
        }
 
@@ -2096,10 +2093,9 @@ class ApiTest extends FixtureTest
         */
        public function testApiFavoritesCreateDestroyWithCreateAction()
        {
-               $this->app->argv = ['api', '1.1', 'favorites', 'create.json'];
-               $this->app->argc = count($this->app->argv);
-               $_REQUEST['id']  = 3;
-               $result          = api_favorites_create_destroy('json');
+               DI::args()->setArgv(['api', '1.1', 'favorites', 'create.json']);
+               $_REQUEST['id'] = 3;
+               $result         = api_favorites_create_destroy('json');
                self::assertStatus($result['status']);
        }
 
@@ -2110,10 +2106,9 @@ class ApiTest extends FixtureTest
         */
        public function testApiFavoritesCreateDestroyWithCreateActionAndRss()
        {
-               $this->app->argv = ['api', '1.1', 'favorites', 'create.rss'];
-               $this->app->argc = count($this->app->argv);
-               $_REQUEST['id']  = 3;
-               $result          = api_favorites_create_destroy('rss');
+               DI::args()->setArgv(['api', '1.1', 'favorites', 'create.rss']);
+               $_REQUEST['id'] = 3;
+               $result         = api_favorites_create_destroy('rss');
                self::assertXml($result, 'status');
        }
 
@@ -2124,10 +2119,9 @@ class ApiTest extends FixtureTest
         */
        public function testApiFavoritesCreateDestroyWithDestroyAction()
        {
-               $this->app->argv = ['api', '1.1', 'favorites', 'destroy.json'];
-               $this->app->argc = count($this->app->argv);
-               $_REQUEST['id']  = 3;
-               $result          = api_favorites_create_destroy('json');
+               DI::args()->setArgv(['api', '1.1', 'favorites', 'destroy.json']);
+               $_REQUEST['id'] = 3;
+               $result         = api_favorites_create_destroy('json');
                self::assertStatus($result['status']);
        }
 
@@ -2139,8 +2133,7 @@ class ApiTest extends FixtureTest
        public function testApiFavoritesCreateDestroyWithoutAuthenticatedUser()
        {
                $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
-               $this->app->argv           = ['api', '1.1', 'favorites', 'create.json'];
-               $this->app->argc           = count($this->app->argv);
+               DI::args()->setArgv(['api', '1.1', 'favorites', 'create.json']);
                $_SESSION['authenticated'] = false;
                api_favorites_create_destroy('json');
        }
@@ -2192,9 +2185,9 @@ class ApiTest extends FixtureTest
        public function testApiFormatMessages()
        {
                $result = api_format_messages(
-                       ['id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
-                       ['id' => 2, 'screen_name' => 'recipient_name'],
-                       ['id' => 3, 'screen_name' => 'sender_name']
+                       ['id' => 1, 'uri-id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
+                       ['id' => 2, 'uri-id' => 2, 'screen_name' => 'recipient_name'],
+                       ['id' => 3, 'uri-id' => 2, 'screen_name' => 'sender_name']
                );
                self::assertEquals('item_title' . "\n" . 'item_body', $result['text']);
                self::assertEquals(1, $result['id']);
@@ -2213,9 +2206,9 @@ class ApiTest extends FixtureTest
        {
                $_GET['getText'] = 'html';
                $result          = api_format_messages(
-                       ['id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
-                       ['id' => 2, 'screen_name' => 'recipient_name'],
-                       ['id' => 3, 'screen_name' => 'sender_name']
+                       ['id' => 1, 'uri-id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
+                       ['id' => 2, 'uri-id' => 2, 'screen_name' => 'recipient_name'],
+                       ['id' => 3, 'uri-id' => 3, 'screen_name' => 'sender_name']
                );
                self::assertEquals('item_title', $result['title']);
                self::assertEquals('<strong>item_body</strong>', $result['text']);
@@ -2230,9 +2223,9 @@ class ApiTest extends FixtureTest
        {
                $_GET['getText'] = 'plain';
                $result          = api_format_messages(
-                       ['id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
-                       ['id' => 2, 'screen_name' => 'recipient_name'],
-                       ['id' => 3, 'screen_name' => 'sender_name']
+                       ['id' => 1, 'uri-id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
+                       ['id' => 2, 'uri-id' => 2, 'screen_name' => 'recipient_name'],
+                       ['id' => 3, 'uri-id' => 3, 'screen_name' => 'sender_name']
                );
                self::assertEquals('item_title', $result['title']);
                self::assertEquals('item_body', $result['text']);
@@ -2247,9 +2240,9 @@ class ApiTest extends FixtureTest
        {
                $_GET['getUserObjects'] = 'false';
                $result                 = api_format_messages(
-                       ['id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
-                       ['id' => 2, 'screen_name' => 'recipient_name'],
-                       ['id' => 3, 'screen_name' => 'sender_name']
+                       ['id' => 1, 'uri-id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
+                       ['id' => 2, 'uri-id' => 2, 'screen_name' => 'recipient_name'],
+                       ['id' => 3, 'uri-id' => 3, 'screen_name' => 'sender_name']
                );
                self::assertTrue(!isset($result['sender']));
                self::assertTrue(!isset($result['recipient']));
@@ -2344,7 +2337,7 @@ class ApiTest extends FixtureTest
        public function testApiGetAttachments()
        {
                $body = 'body';
-               self::assertEmpty(api_get_attachments($body));
+               self::assertEmpty(api_get_attachments($body, 0));
        }
 
        /**
@@ -2355,7 +2348,7 @@ class ApiTest extends FixtureTest
        public function testApiGetAttachmentsWithImage()
        {
                $body = '[img]http://via.placeholder.com/1x1.png[/img]';
-               self::assertInternalType('array', api_get_attachments($body));
+               self::assertIsArray(api_get_attachments($body, 0));
        }
 
        /**
@@ -2367,7 +2360,7 @@ class ApiTest extends FixtureTest
        {
                $_SERVER['HTTP_USER_AGENT'] = 'AndStatus';
                $body                       = '[img]http://via.placeholder.com/1x1.png[/img]';
-               self::assertInternalType('array', api_get_attachments($body));
+               self::assertIsArray(api_get_attachments($body, 0));
        }
 
        /**
@@ -2378,7 +2371,7 @@ class ApiTest extends FixtureTest
        public function testApiGetEntitities()
        {
                $text = 'text';
-               self::assertInternalType('array', api_get_entitities($text, 'bbcode'));
+               self::assertIsArray(api_get_entitities($text, 'bbcode', 0));
        }
 
        /**
@@ -2390,11 +2383,11 @@ class ApiTest extends FixtureTest
        {
                $_REQUEST['include_entities'] = 'true';
                $text                         = 'text';
-               $result                       = api_get_entitities($text, 'bbcode');
-               self::assertInternalType('array', $result['hashtags']);
-               self::assertInternalType('array', $result['symbols']);
-               self::assertInternalType('array', $result['urls']);
-               self::assertInternalType('array', $result['user_mentions']);
+               $result                       = api_get_entitities($text, 'bbcode', 0);
+               self::assertIsArray($result['hashtags']);
+               self::assertIsArray($result['symbols']);
+               self::assertIsArray($result['urls']);
+               self::assertIsArray($result['user_mentions']);
        }
 
        /**
@@ -2532,7 +2525,7 @@ class ApiTest extends FixtureTest
                $result = api_account_rate_limit_status('json');
                self::assertEquals(150, $result['hash']['remaining_hits']);
                self::assertEquals(150, $result['hash']['hourly_limit']);
-               self::assertInternalType('int', $result['hash']['reset_time_in_seconds']);
+               self::assertIsInt($result['hash']['reset_time_in_seconds']);
        }
 
        /**
@@ -2873,7 +2866,7 @@ class ApiTest extends FixtureTest
         */
        public function testApiDirectMessagesNewWithScreenName()
        {
-               $this->app->user    = ['nickname' => $this->selfUser['nick']];
+               $this->app->setLoggedInUserNickname($this->selfUser['nick']);
                $_POST['text']        = 'message_text';
                $_POST['screen_name'] = $this->friendUser['nick'];
                $result               = api_direct_messages_new('json');
@@ -2889,7 +2882,7 @@ class ApiTest extends FixtureTest
         */
        public function testApiDirectMessagesNewWithTitle()
        {
-               $this->app->user    = ['nickname' => $this->selfUser['nick']];
+               $this->app->setLoggedInUserNickname($this->selfUser['nick']);
                $_POST['text']        = 'message_text';
                $_POST['screen_name'] = $this->friendUser['nick'];
                $_REQUEST['title']    = 'message_title';
@@ -2907,7 +2900,7 @@ class ApiTest extends FixtureTest
         */
        public function testApiDirectMessagesNewWithRss()
        {
-               $this->app->user    = ['nickname' => $this->selfUser['nick']];
+               $this->app->setLoggedInUserNickname($this->selfUser['nick']);
                $_POST['text']        = 'message_text';
                $_POST['screen_name'] = $this->friendUser['nick'];
                $result               = api_direct_messages_new('rss');
@@ -3518,55 +3511,6 @@ class ApiTest extends FixtureTest
                $this->markTestIncomplete();
        }
 
-       /**
-        * Test the api_friendica_remoteauth() function.
-        *
-        * @return void
-        */
-       public function testApiFriendicaRemoteauth()
-       {
-               $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
-               api_friendica_remoteauth();
-       }
-
-       /**
-        * Test the api_friendica_remoteauth() function with an URL.
-        *
-        * @return void
-        */
-       public function testApiFriendicaRemoteauthWithUrl()
-       {
-               $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
-               $_GET['url']   = 'url';
-               $_GET['c_url'] = 'url';
-               api_friendica_remoteauth();
-       }
-
-       /**
-        * Test the api_friendica_remoteauth() function with a correct URL.
-        *
-        * @return void
-        */
-       public function testApiFriendicaRemoteauthWithCorrectUrl()
-       {
-               $this->markTestIncomplete("We can't use an assertion here because of App->redirect().");
-               $_GET['url']   = 'url';
-               $_GET['c_url'] = $this->selfUser['nurl'];
-               api_friendica_remoteauth();
-       }
-
-       /**
-        * Test the api_share_as_retweet() function.
-        *
-        * @return void
-        */
-       public function testApiShareAsRetweet()
-       {
-               $item   = ['body' => '', 'author-id' => 1, 'owner-id' => 1];
-               $result = api_share_as_retweet($item);
-               self::assertFalse($result);
-       }
-
        /**
         * Test the api_share_as_retweet() function with a valid item.
         *
@@ -3756,8 +3700,7 @@ class ApiTest extends FixtureTest
         */
        public function testApiFriendicaNotificationWithEmptyResult()
        {
-               $this->app->argv = ['api', 'friendica', 'notification'];
-               $this->app->argc = count($this->app->argv);
+               DI::args()->setArgv(['api', 'friendica', 'notification']);
                $_SESSION['uid'] = 41;
                $result          = api_friendica_notification('json');
                self::assertEquals(['note' => false], $result);
@@ -3770,14 +3713,15 @@ class ApiTest extends FixtureTest
         */
        public function testApiFriendicaNotificationWithXmlResult()
        {
-               $this->app->argv = ['api', 'friendica', 'notification'];
-               $this->app->argc = count($this->app->argv);
-               $result          = api_friendica_notification('xml');
-               $dateRel = Temporal::getRelativeDate('2020-01-01 12:12:02');
+               DI::args()->setArgv(['api', 'friendica', 'notification']);
+               $result  = api_friendica_notification('xml');
+               $date = DateTimeFormat::local('2020-01-01 12:12:02');
+               $dateRel = Temporal::getRelativeDate('2020-01-01 07:12:02');
+
                $assertXml=<<<XML
 <?xml version="1.0"?>
 <notes>
-  <note id="1" hash="" type="8" name="Reply to" url="http://localhost/display/1" photo="http://localhost/" date="2020-01-01 12:12:02" msg="A test reply from an item" uid="42" uri-id="" link="http://localhost/notification/1" iid="4" parent="" parent-uri-id="" seen="0" verb="" otype="item" name_cache="Reply to" msg_cache="A test reply from an item" timestamp="1577880722" date_rel="{$dateRel}" msg_html="A test reply from an item" msg_plain="A test reply from an item"/>
+  <note date="$date" date_rel="$dateRel" id="1" iid="4" link="http://localhost/notification/1" msg="A test reply from an item" msg_cache="A test reply from an item" msg_html="A test reply from an item" msg_plain="A test reply from an item" name="Reply to" name_cache="Reply to" otype="item" parent="" photo="http://localhost/" seen="false" timestamp="1577880722" type="8" uid="42" url="http://localhost/display/1" verb="http://activitystrea.ms/schema/1.0/post"/>
 </notes>
 XML;
                self::assertXmlStringEqualsXmlString($assertXml, $result);
@@ -3790,9 +3734,8 @@ XML;
         */
        public function testApiFriendicaNotificationWithJsonResult()
        {
-               $this->app->argv = ['api', 'friendica', 'notification'];
-               $this->app->argc = count($this->app->argv);
-               $result          = json_encode(api_friendica_notification('json'));
+               DI::args()->setArgv(['api', 'friendica', 'notification']);
+               $result = json_encode(api_friendica_notification('json'));
                self::assertJson($result);
        }