X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=tests%2Finclude%2FApiTest.php;h=ceeaac6f6fdcd4c3e8dca4c68561a5f6c254c5b5;hb=31c55a6fa45f0acd8ef9ff1ef86224778d614819;hp=3d6bdc0c16816c8adb8fd862441e3bb36678e385;hpb=f617e2a287b53790bee1ebfdf0d02740f22d343e;p=friendica.git diff --git a/tests/include/ApiTest.php b/tests/include/ApiTest.php index 3d6bdc0c16..ceeaac6f6f 100644 --- a/tests/include/ApiTest.php +++ b/tests/include/ApiTest.php @@ -5,17 +5,13 @@ 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\IConfig; +use Friendica\Core\PConfig\IPConfig; use Friendica\Core\Protocol; -use Friendica\Core\System; -use Friendica\Database\Database; -use Friendica\Model\Contact; +use Friendica\DI; use Friendica\Network\HTTPException; -use Friendica\Test\Util\Database\StaticDatabase; +use Friendica\Util\Temporal; use Monolog\Handler\TestHandler; require_once __DIR__ . '/../../include/api.php'; @@ -26,7 +22,7 @@ require_once __DIR__ . '/../../include/api.php'; * Functions that use header() need to be tested in a separate process. * @see https://phpunit.de/manual/5.7/en/appendixes.annotations.html#appendixes.annotations.runTestsInSeparateProcesses */ -class ApiTest extends DatabaseTest +class ApiTest extends FixtureTest { /** * @var TestHandler Can handle log-outputs @@ -45,12 +41,9 @@ class ApiTest extends DatabaseTest /** @var App */ protected $app; - /** @var Configuration */ + /** @var IConfig */ protected $config; - /** @var Dice */ - protected $dice; - /** * Create variables used by tests. */ @@ -58,16 +51,8 @@ class ApiTest extends DatabaseTest { parent::setUp(); - $this->dice = (new Dice()) - ->addRules(include __DIR__ . '/../../static/dependencies.config.php') - ->addRule(Database::class, ['instanceOf' => StaticDatabase::class, 'shared' => true]); - BaseObject::setDependencyInjection($this->dice); - - /** @var Database $dba */ - $dba = $this->dice->create(Database::class); - - /** @var Configuration $config */ - $this->config = $this->dice->create(Configuration::class); + /** @var IConfig $config */ + $this->config = $this->dice->create(IConfig::class); $this->config->set('system', 'url', 'http://localhost'); $this->config->set('system', 'hostname', 'localhost'); @@ -80,11 +65,9 @@ class ApiTest extends DatabaseTest $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); /** @var App app */ - $this->app = BaseObject::getApp(); + $this->app = DI::app(); $this->app->argc = 1; $this->app->argv = ['home']; @@ -112,6 +95,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, @@ -427,12 +412,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) ); } @@ -451,7 +438,12 @@ class ApiTest extends DatabaseTest return ['data' => ['some_data']]; } ]; + $_SERVER['REQUEST_METHOD'] = 'method'; + $_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 = [ @@ -462,10 +454,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) ); } @@ -485,11 +476,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) ); } @@ -523,11 +516,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) ); } @@ -547,11 +542,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) ); } @@ -571,12 +568,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( '' . "\n" . 'some_data', - api_call($this->app) + api_call($this->app, $args) ); } @@ -596,12 +595,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( '' . "\n" . 'some_data', - api_call($this->app) + api_call($this->app, $args) ); } @@ -616,10 +617,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) ); } @@ -636,13 +640,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) ); } @@ -656,7 +662,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()) ); } @@ -677,7 +683,7 @@ class ApiTest extends DatabaseTest ' 200 OK' . "\n" . ' ' . "\n" . '' . "\n", - api_error('xml', new HTTPException\OKException('error_message')) + api_error('xml', new HTTPException\OKException('error_message'), DI::args()) ); } @@ -698,7 +704,7 @@ class ApiTest extends DatabaseTest ' 200 OK' . "\n" . ' ' . "\n" . '' . "\n", - api_error('rss', new HTTPException\OKException('error_message')) + api_error('rss', new HTTPException\OKException('error_message'), DI::args()) ); } @@ -719,7 +725,7 @@ class ApiTest extends DatabaseTest ' 200 OK' . "\n" . ' ' . "\n" . '' . "\n", - api_error('atom', new HTTPException\OKException('error_message')) + api_error('atom', new HTTPException\OKException('error_message'), DI::args()) ); } @@ -801,7 +807,7 @@ class ApiTest extends DatabaseTest */ public function testApiGetUserWithFrioSchema() { - $pConfig = $this->dice->create(PConfiguration::class); + $pConfig = $this->dice->create(IPConfig::class); $pConfig->set($this->selfUser['id'], 'frio', 'schema', 'red'); $user = api_get_user($this->app); $this->assertSelfUser($user); @@ -811,38 +817,38 @@ class ApiTest extends DatabaseTest } /** - * Test the api_get_user() function with a custom Frio schema. + * Test the api_get_user() function with an empty Frio schema. * * @return void */ - public function testApiGetUserWithCustomFrioSchema() + public function testApiGetUserWithEmptyFrioSchema() { - $pConfig = $this->dice->create(PConfiguration::class); + $pConfig = $this->dice->create(IPConfig::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'); - $pConfig->set($this->selfUser['id'], 'frio', 'background_color', '#123456'); $user = api_get_user($this->app); $this->assertSelfUser($user); - $this->assertEquals('123456', $user['profile_sidebar_fill_color']); - $this->assertEquals('123456', $user['profile_link_color']); - $this->assertEquals('123456', $user['profile_background_color']); + $this->assertEquals('708fa0', $user['profile_sidebar_fill_color']); + $this->assertEquals('6fdbe8', $user['profile_link_color']); + $this->assertEquals('ededed', $user['profile_background_color']); } /** - * Test the api_get_user() function with an empty Frio schema. + * Test the api_get_user() function with a custom Frio schema. * * @return void */ - public function testApiGetUserWithEmptyFrioSchema() + public function testApiGetUserWithCustomFrioSchema() { - $pConfig = $this->dice->create(PConfiguration::class); + $pConfig = $this->dice->create(IPConfig::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'); + $pConfig->set($this->selfUser['id'], 'frio', 'background_color', '#123456'); $user = api_get_user($this->app); $this->assertSelfUser($user); - $this->assertEquals('708fa0', $user['profile_sidebar_fill_color']); - $this->assertEquals('6fdbe8', $user['profile_link_color']); - $this->assertEquals('ededed', $user['profile_background_color']); + $this->assertEquals('123456', $user['profile_sidebar_fill_color']); + $this->assertEquals('123456', $user['profile_link_color']); + $this->assertEquals('123456', $user['profile_background_color']); } /** @@ -1357,7 +1363,7 @@ class ApiTest extends DatabaseTest 'type' => 'image/png' ] ]; - $app = \get_app(); + $app = DI::app(); $app->argc = 2; $result = api_media_upload(); @@ -2392,7 +2398,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') ); } @@ -2461,102 +2467,6 @@ class ApiTest extends DatabaseTest $this->assertArrayHasKey('friendica:attendmaybe', $result); } - /** - * Test the api_format_items_profiles() function. - * - * @return void - */ - public function testApiFormatItemsProfiles() - { - $profile_row = [ - 'id' => 'profile_id', - 'profile-name' => 'profile_name', - 'is-default' => true, - 'hide-friends' => true, - 'photo' => 'profile_photo', - 'thumb' => 'profile_thumb', - 'publish' => true, - 'net-publish' => true, - 'pdesc' => 'description', - 'dob' => 'date_of_birth', - 'address' => 'address', - 'locality' => 'city', - 'region' => 'region', - 'postal-code' => 'postal_code', - 'country-name' => 'country', - 'hometown' => 'hometown', - 'gender' => 'gender', - 'marital' => 'marital', - 'with' => 'marital_with', - 'howlong' => 'marital_since', - 'sexual' => 'sexual', - 'politic' => 'politic', - 'religion' => 'religion', - 'pub_keywords' => 'public_keywords', - 'prv_keywords' => 'private_keywords', - - 'likes' => 'likes', - 'dislikes' => 'dislikes', - 'about' => 'about', - 'music' => 'music', - 'book' => 'book', - 'tv' => 'tv', - 'film' => 'film', - 'interest' => 'interest', - 'romance' => 'romance', - 'work' => 'work', - 'education' => 'education', - 'contact' => 'social_networks', - 'homepage' => 'homepage' - ]; - $result = api_format_items_profiles($profile_row); - $this->assertEquals( - [ - 'profile_id' => 'profile_id', - 'profile_name' => 'profile_name', - 'is_default' => true, - 'hide_friends' => true, - 'profile_photo' => 'profile_photo', - 'profile_thumb' => 'profile_thumb', - 'publish' => true, - 'net_publish' => true, - 'description' => 'description', - 'date_of_birth' => 'date_of_birth', - 'address' => 'address', - 'city' => 'city', - 'region' => 'region', - 'postal_code' => 'postal_code', - 'country' => 'country', - 'hometown' => 'hometown', - 'gender' => 'gender', - 'marital' => 'marital', - 'marital_with' => 'marital_with', - 'marital_since' => 'marital_since', - 'sexual' => 'sexual', - 'politic' => 'politic', - 'religion' => 'religion', - 'public_keywords' => 'public_keywords', - 'private_keywords' => 'private_keywords', - - 'likes' => 'likes', - 'dislikes' => 'dislikes', - 'about' => 'about', - 'music' => 'music', - 'book' => 'book', - 'tv' => 'tv', - 'film' => 'film', - 'interest' => 'interest', - 'romance' => 'romance', - 'work' => 'work', - 'education' => 'education', - 'social_networks' => 'social_networks', - 'homepage' => 'homepage', - 'users' => null - ], - $result - ); - } - /** * Test the api_format_items() function. * @@ -2895,7 +2805,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']); @@ -2916,61 +2826,6 @@ class ApiTest extends DatabaseTest $this->assertEquals('0.9.7', $result['version']); } - /** - * Test the api_ff_ids() function. - * - * @return void - */ - public function testApiFfIds() - { - $result = api_ff_ids('json', Contact::FOLLOWER); - $this->assertEquals(['id' => []], $result); - } - - /** - * Test the api_ff_ids() function with a result. - * - * @return void - */ - public function testApiFfIdsWithResult() - { - $this->markTestIncomplete(); - } - - /** - * Test the api_ff_ids() function without an authenticated user. - * - * @return void - * @expectedException Friendica\Network\HTTPException\ForbiddenException - */ - public function testApiFfIdsWithoutAuthenticatedUser() - { - $_SESSION['authenticated'] = false; - api_ff_ids('json', Contact::FOLLOWER); - } - - /** - * Test the api_friends_ids() function. - * - * @return void - */ - public function testApiFriendsIds() - { - $result = api_friends_ids('json'); - $this->assertEquals(['id' => []], $result); - } - - /** - * Test the api_followers_ids() function. - * - * @return void - */ - public function testApiFollowersIds() - { - $result = api_followers_ids('json'); - $this->assertEquals(['id' => []], $result); - } - /** * Test the api_direct_messages_new() function. * @@ -3014,6 +2869,7 @@ class ApiTest extends DatabaseTest */ public function testApiDirectMessagesNewWithScreenName() { + $this->app->user = ['nickname' => $this->selfUser['nick']]; $_POST['text'] = 'message_text'; $_POST['screen_name'] = $this->friendUser['nick']; $result = api_direct_messages_new('json'); @@ -3029,6 +2885,7 @@ class ApiTest extends DatabaseTest */ public function testApiDirectMessagesNewWithTitle() { + $this->app->user = ['nickname' => $this->selfUser['nick']]; $_POST['text'] = 'message_text'; $_POST['screen_name'] = $this->friendUser['nick']; $_REQUEST['title'] = 'message_title'; @@ -3046,6 +2903,7 @@ class ApiTest extends DatabaseTest */ public function testApiDirectMessagesNewWithRss() { + $this->app->user = ['nickname' => $this->selfUser['nick']]; $_POST['text'] = 'message_text'; $_POST['screen_name'] = $this->friendUser['nick']; $result = api_direct_messages_new('rss'); @@ -3279,7 +3137,7 @@ class ApiTest extends DatabaseTest */ public function testApiOauthRequestToken() { - $this->markTestIncomplete('killme() kills phpunit as well'); + $this->markTestIncomplete('exit() kills phpunit as well'); } /** @@ -3289,7 +3147,7 @@ class ApiTest extends DatabaseTest */ public function testApiOauthAccessToken() { - $this->markTestIncomplete('killme() kills phpunit as well'); + $this->markTestIncomplete('exit() kills phpunit as well'); } /** @@ -3890,14 +3748,15 @@ class ApiTest extends DatabaseTest } /** - * Test the api_friendica_notification() function with an argument count. + * Test the api_friendica_notification() function with empty result * * @return void */ - public function testApiFriendicaNotificationWithArgumentCount() + public function testApiFriendicaNotificationWithEmptyResult() { $this->app->argv = ['api', 'friendica', 'notification']; $this->app->argc = count($this->app->argv); + $_SESSION['uid'] = 41; $result = api_friendica_notification('json'); $this->assertEquals(['note' => false], $result); } @@ -3912,89 +3771,57 @@ class ApiTest extends DatabaseTest $this->app->argv = ['api', 'friendica', 'notification']; $this->app->argc = count($this->app->argv); $result = api_friendica_notification('xml'); - $this->assertXml($result, 'notes'); + $dateRel = Temporal::getRelativeDate('2020-01-01 12:12:02'); + $assertXml=<< + + + +XML; + $this->assertXmlStringEqualsXmlString($assertXml, $result); } /** - * Test the api_friendica_notification_seen() function. + * Test the api_friendica_notification() function with an JSON result. * * @return void */ - public function testApiFriendicaNotificationSeen() + public function testApiFriendicaNotificationWithJsonResult() { - $this->markTestIncomplete(); + $this->app->argv = ['api', 'friendica', 'notification']; + $this->app->argc = count($this->app->argv); + $result = json_encode(api_friendica_notification('json')); + $this->assertJson($result); } /** - * Test the api_friendica_direct_messages_setseen() function. + * Test the api_friendica_notification_seen() function. * * @return void */ - public function testApiFriendicaDirectMessagesSetseen() + public function testApiFriendicaNotificationSeen() { $this->markTestIncomplete(); } /** - * Test the api_friendica_direct_messages_search() function. + * Test the api_friendica_direct_messages_setseen() function. * * @return void */ - public function testApiFriendicaDirectMessagesSearch() + public function testApiFriendicaDirectMessagesSetseen() { $this->markTestIncomplete(); } /** - * Test the api_friendica_profile_show() function. - * - * @return void - */ - public function testApiFriendicaProfileShow() - { - $result = api_friendica_profile_show('json'); - // We can't use assertSelfUser() here because the user object is missing some properties. - $this->assertEquals($this->selfUser['id'], $result['$result']['friendica_owner']['cid']); - $this->assertEquals('DFRN', $result['$result']['friendica_owner']['location']); - $this->assertEquals($this->selfUser['name'], $result['$result']['friendica_owner']['name']); - $this->assertEquals($this->selfUser['nick'], $result['$result']['friendica_owner']['screen_name']); - $this->assertEquals('dfrn', $result['$result']['friendica_owner']['network']); - $this->assertTrue($result['$result']['friendica_owner']['verified']); - $this->assertFalse($result['$result']['multi_profiles']); - } - - /** - * Test the api_friendica_profile_show() function with a profile ID. - * - * @return void - */ - public function testApiFriendicaProfileShowWithProfileId() - { - $this->markTestIncomplete('We need to add a dataset for this.'); - } - - /** - * Test the api_friendica_profile_show() function with a wrong profile ID. - * - * @return void - * @expectedException Friendica\Network\HTTPException\BadRequestException - */ - public function testApiFriendicaProfileShowWithWrongProfileId() - { - $_REQUEST['profile_id'] = 666; - api_friendica_profile_show('json'); - } - - /** - * Test the api_friendica_profile_show() function without an authenticated user. + * Test the api_friendica_direct_messages_search() function. * * @return void - * @expectedException Friendica\Network\HTTPException\ForbiddenException */ - public function testApiFriendicaProfileShowWithoutAuthenticatedUser() + public function testApiFriendicaDirectMessagesSearch() { - $_SESSION['authenticated'] = false; - api_friendica_profile_show('json'); + $this->markTestIncomplete(); } /**