From 540ddb92651a44380ad6f6f84ef425a7e540bca4 Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 25 Jul 2021 14:27:13 +0000 Subject: [PATCH] Hopefully making the tests to work --- include/api.php | 40 ++++++++++----------- mod/message.php | 33 +++++++++-------- mod/wall_upload.php | 4 +-- src/App/Arguments.php | 11 ++++++ tests/legacy/ApiTest.php | 77 +++++++++++++++++----------------------- 5 files changed, 82 insertions(+), 83 deletions(-) diff --git a/include/api.php b/include/api.php index 1223e1b2a5..c6f67109a3 100644 --- a/include/api.php +++ b/include/api.php @@ -545,10 +545,10 @@ function api_get_user(App $a, $contact_id = null) } // $called_api is the API path exploded on / and is expected to have at least 2 elements - if (is_null($user) && ($a->argc > (count($called_api) - 1)) && (count($called_api) > 0)) { + if (is_null($user) && (DI::args()->getArgc() > (count($called_api) - 1)) && (count($called_api) > 0)) { $argid = count($called_api); - if (!empty($a->argv[$argid])) { - $data = explode(".", $a->argv[$argid]); + if (!empty(DI::args()->getArgv()[$argid])) { + $data = explode(".", DI::args()->getArgv()[$argid]); if (count($data) > 1) { list($user, $null) = $data; } @@ -1021,7 +1021,7 @@ function api_statuses_mediap($type) } $txt = HTML::toBBCode($txt); - $a->argv[1] = $user_info['screen_name']; //should be set to username? + DI::args()->getArgv()[1] = $user_info['screen_name']; //should be set to username? $picture = wall_upload_post($a, false); @@ -1883,7 +1883,7 @@ function api_statuses_show($type) } // params - $id = intval($a->argv[3] ?? 0); + $id = intval(DI::args()->getArgv()[3] ?? 0); if ($id == 0) { $id = intval($_REQUEST['id'] ?? 0); @@ -1891,7 +1891,7 @@ function api_statuses_show($type) // Hotot workaround if ($id == 0) { - $id = intval($a->argv[4] ?? 0); + $id = intval(DI::args()->getArgv()[4] ?? 0); } Logger::log('API: api_statuses_show: ' . $id); @@ -1962,7 +1962,7 @@ function api_conversation_show($type) } // params - $id = intval($a->argv[3] ?? 0); + $id = intval(DI::args()->getArgv()[3] ?? 0); $since_id = intval($_REQUEST['since_id'] ?? 0); $max_id = intval($_REQUEST['max_id'] ?? 0); $count = intval($_REQUEST['count'] ?? 20); @@ -1976,7 +1976,7 @@ function api_conversation_show($type) // Hotot workaround if ($id == 0) { - $id = intval($a->argv[4] ?? 0); + $id = intval(DI::args()->getArgv()[4] ?? 0); } Logger::info(API_LOG_PREFIX . '{subaction}', ['module' => 'api', 'action' => 'conversation', 'subaction' => 'show', 'id' => $id]); @@ -2045,7 +2045,7 @@ function api_statuses_repeat($type) api_get_user($a); // params - $id = intval($a->argv[3] ?? 0); + $id = intval(DI::args()->getArgv()[3] ?? 0); if ($id == 0) { $id = intval($_REQUEST['id'] ?? 0); @@ -2053,7 +2053,7 @@ function api_statuses_repeat($type) // Hotot workaround if ($id == 0) { - $id = intval($a->argv[4] ?? 0); + $id = intval(DI::args()->getArgv()[4] ?? 0); } Logger::log('API: api_statuses_repeat: '.$id); @@ -2128,7 +2128,7 @@ function api_statuses_destroy($type) api_get_user($a); // params - $id = intval($a->argv[3] ?? 0); + $id = intval(DI::args()->getArgv()[3] ?? 0); if ($id == 0) { $id = intval($_REQUEST['id'] ?? 0); @@ -2136,7 +2136,7 @@ function api_statuses_destroy($type) // Hotot workaround if ($id == 0) { - $id = intval($a->argv[4] ?? 0); + $id = intval(DI::args()->getArgv()[4] ?? 0); } Logger::log('API: api_statuses_destroy: '.$id); @@ -2329,16 +2329,16 @@ function api_favorites_create_destroy($type) // for versioned api. /// @TODO We need a better global soluton $action_argv_id = 2; - if (count($a->argv) > 1 && $a->argv[1] == "1.1") { + if (count(DI::args()->getArgv()) > 1 && DI::args()->getArgv()[1] == "1.1") { $action_argv_id = 3; } - if ($a->argc <= $action_argv_id) { + if (DI::args()->getArgc() <= $action_argv_id) { throw new BadRequestException("Invalid request."); } - $action = str_replace("." . $type, "", $a->argv[$action_argv_id]); - if ($a->argc == $action_argv_id + 2) { - $itemid = intval($a->argv[$action_argv_id + 1] ?? 0); + $action = str_replace("." . $type, "", DI::args()->getArgv()[$action_argv_id]); + if (DI::args()->getArgc() == $action_argv_id + 2) { + $itemid = intval(DI::args()->getArgv()[$action_argv_id + 1] ?? 0); } else { $itemid = intval($_REQUEST['id'] ?? 0); } @@ -5616,7 +5616,7 @@ function api_friendica_activity($type) if (api_user() === false) { throw new ForbiddenException(); } - $verb = strtolower($a->argv[3]); + $verb = strtolower(DI::args()->getArgv()[3]); $verb = preg_replace("|\..*$|", "", $verb); $id = $_REQUEST['id'] ?? 0; @@ -5664,7 +5664,7 @@ function api_friendica_notification($type) if (api_user() === false) { throw new ForbiddenException(); } - if ($a->argc!==3) { + if (DI::args()->getArgc()!==3) { throw new BadRequestException("Invalid argument count"); } @@ -5709,7 +5709,7 @@ function api_friendica_notification_seen($type) if (api_user() === false || $user_info === false) { throw new ForbiddenException(); } - if ($a->argc !== 4) { + if (DI::args()->getArgc() !== 4) { throw new BadRequestException("Invalid argument count"); } diff --git a/mod/message.php b/mod/message.php index ff4998992d..a18a5f9df3 100644 --- a/mod/message.php +++ b/mod/message.php @@ -38,14 +38,14 @@ function message_init(App $a) { $tabs = ''; - if ($a->argc > 1 && is_numeric($a->argv[1])) { + if (DI::args()->getArgc() > 1 && is_numeric(DI::args()->getArgv()[1])) { $tabs = render_messages(get_messages(local_user(), 0, 5), 'mail_list.tpl'); } $new = [ 'label' => DI::l10n()->t('New Message'), 'url' => 'message/new', - 'sel' => $a->argc > 1 && $a->argv[1] == 'new', + 'sel' => DI::args()->getArgc() > 1 && DI::args()->getArgv()[1] == 'new', 'accesskey' => 'm', ]; @@ -96,8 +96,7 @@ function message_post(App $a) // fake it to go back to the input form if no recipient listed if ($norecip) { - $a->argc = 2; - $a->argv[1] = 'new'; + DI::args()->setArgv(['message', 'new']); } else { DI::baseUrl()->redirect(DI::args()->getCommand() . '/' . $ret); } @@ -116,7 +115,7 @@ function message_content(App $a) $myprofile = DI::baseUrl() . '/profile/' . $a->user['nickname']; $tpl = Renderer::getMarkupTemplate('mail_head.tpl'); - if ($a->argc > 1 && $a->argv[1] == 'new') { + if (DI::args()->getArgc() > 1 && DI::args()->getArgv()[1] == 'new') { $button = [ 'label' => DI::l10n()->t('Discard'), 'url' => '/message', @@ -135,20 +134,20 @@ function message_content(App $a) '$button' => $button, ]); - if (($a->argc == 3) && ($a->argv[1] === 'drop' || $a->argv[1] === 'dropconv')) { - if (!intval($a->argv[2])) { + if ((DI::args()->getArgc() == 3) && (DI::args()->getArgv()[1] === 'drop' || DI::args()->getArgv()[1] === 'dropconv')) { + if (!intval(DI::args()->getArgv()[2])) { return; } - $cmd = $a->argv[1]; + $cmd = DI::args()->getArgv()[1]; if ($cmd === 'drop') { - $message = DBA::selectFirst('mail', ['convid'], ['id' => $a->argv[2], 'uid' => local_user()]); + $message = DBA::selectFirst('mail', ['convid'], ['id' => DI::args()->getArgv()[2], 'uid' => local_user()]); if(!DBA::isResult($message)){ notice(DI::l10n()->t('Conversation not found.')); DI::baseUrl()->redirect('message'); } - if (!DBA::delete('mail', ['id' => $a->argv[2], 'uid' => local_user()])) { + if (!DBA::delete('mail', ['id' => DI::args()->getArgv()[2], 'uid' => local_user()])) { notice(DI::l10n()->t('Message was not deleted.')); } @@ -160,7 +159,7 @@ function message_content(App $a) DI::baseUrl()->redirect('message/' . $conversation['id'] ); } else { $r = q("SELECT `parent-uri`,`convid` FROM `mail` WHERE `id` = %d AND `uid` = %d LIMIT 1", - intval($a->argv[2]), + intval(DI::args()->getArgv()[2]), intval(local_user()) ); if (DBA::isResult($r)) { @@ -174,7 +173,7 @@ function message_content(App $a) } } - if (($a->argc > 1) && ($a->argv[1] === 'new')) { + if ((DI::args()->getArgc() > 1) && (DI::args()->getArgv()[1] === 'new')) { $o .= $header; $tpl = Renderer::getMarkupTemplate('msg-header.tpl'); @@ -184,7 +183,7 @@ function message_content(App $a) '$linkurl' => DI::l10n()->t('Please enter a link URL:') ]); - $recipientId = $a->argv[2] ?? null; + $recipientId = DI::args()->getArgv()[2] ?? null; $select = ACL::getMessageContactSelectHTML($recipientId); @@ -210,7 +209,7 @@ function message_content(App $a) $_SESSION['return_path'] = DI::args()->getQueryString(); - if ($a->argc == 1) { + if (DI::args()->getArgc() == 1) { // List messages @@ -241,7 +240,7 @@ function message_content(App $a) return $o; } - if (($a->argc > 1) && (intval($a->argv[1]))) { + if ((DI::args()->getArgc() > 1) && (intval(DI::args()->getArgv()[1]))) { $o .= $header; @@ -252,7 +251,7 @@ function message_content(App $a) WHERE `mail`.`uid` = ? AND `mail`.`id` = ? LIMIT 1", local_user(), - $a->argv[1] + DI::args()->getArgv()[1] ); if (DBA::isResult($message)) { $contact_id = $message['contact-id']; @@ -345,7 +344,7 @@ function message_content(App $a) $tpl = Renderer::getMarkupTemplate('mail_display.tpl'); $o = Renderer::replaceMacros($tpl, [ - '$thread_id' => $a->argv[1], + '$thread_id' => DI::args()->getArgv()[1], '$thread_subject' => $message['title'], '$thread_seen' => $seen, '$delete' => DI::l10n()->t('Delete conversation'), diff --git a/mod/wall_upload.php b/mod/wall_upload.php index c99c43fa0a..7973f1c96f 100644 --- a/mod/wall_upload.php +++ b/mod/wall_upload.php @@ -43,9 +43,9 @@ function wall_upload_post(App $a, $desktopmode = true) $r_json = (!empty($_GET['response']) && $_GET['response'] == 'json'); $album = trim($_GET['album'] ?? ''); - if ($a->argc > 1) { + if (DI::args()->getArgc() > 1) { if (empty($_FILES['media'])) { - $nick = $a->argv[1]; + $nick = DI::args()->getArgv()[1]; $user = DBA::selectFirst('owner-view', ['id', 'uid', 'nickname', 'page-flags'], ['nickname' => $nick, 'blocked' => false]); if (!DBA::isResult($user)) { if ($r_json) { diff --git a/src/App/Arguments.php b/src/App/Arguments.php index a7cb37f289..ae6c64a4f3 100644 --- a/src/App/Arguments.php +++ b/src/App/Arguments.php @@ -87,6 +87,17 @@ class Arguments return $this->argc; } + public function setArgv(array $argv) + { + $this->argv = $argv; + $this->argc = count($argv); + } + + public function setArgc(int $argc) + { + $this->argc = $argc; + } + /** * Returns the value of a argv key * @todo there are a lot of $a->argv usages in combination with ?? which can be replaced with this method diff --git a/tests/legacy/ApiTest.php b/tests/legacy/ApiTest.php index 0f09756e4a..1f761de203 100644 --- a/tests/legacy/ApiTest.php +++ b/tests/legacy/ApiTest.php @@ -76,8 +76,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 = [ @@ -925,7 +924,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 +1197,7 @@ class ApiTest extends FixtureTest */ public function testApiStatusesMediap() { - $this->app->argc = 2; + DI::args()->setArgc(2); $_FILES = [ 'media' => [ @@ -1370,7 +1369,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 +1792,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 +1804,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 +1844,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 +1897,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 +1937,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']); } @@ -2057,8 +2056,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 +2068,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 +2080,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 +2092,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 +2105,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 +2118,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 +2132,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'); } @@ -3719,8 +3711,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); @@ -3733,9 +3724,8 @@ 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'); + DI::args()->setArgv(['api', 'friendica', 'notification']); + $result = api_friendica_notification('xml'); $dateRel = Temporal::getRelativeDate('2020-01-01 12:12:02'); $assertXml=<< @@ -3753,9 +3743,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); }