X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=tests%2FApiTest.php;h=1854c996b0ffbd258e369775744431efca10912b;hb=820afa2284eb603c286921a302d5b827a1b47b9e;hp=f0e27b4af1d25f9d18b993360a70a3282934c9d1;hpb=93daf7883e98d797759fb81dbf12e9e368debf61;p=friendica.git diff --git a/tests/ApiTest.php b/tests/ApiTest.php index f0e27b4af1..1854c996b0 100644 --- a/tests/ApiTest.php +++ b/tests/ApiTest.php @@ -5,12 +5,11 @@ namespace Friendica\Test; -use Friendica\App; use Friendica\Core\Config; use Friendica\Core\PConfig; -use Friendica\Network\BadRequestException; +use Friendica\Core\Protocol; +use Friendica\Core\System; use Friendica\Network\HTTPException; -use Friendica\Render\FriendicaSmarty; /** * Tests for the API functions. @@ -26,9 +25,11 @@ class ApiTest extends DatabaseTest */ protected function setUp() { - global $a; parent::setUp(); + // Reusable App object + $this->app = \Friendica\BaseObject::getApp(); + // User data that the test database is populated with $this->selfUser = [ 'id' => 42, @@ -36,6 +37,12 @@ class ApiTest extends DatabaseTest 'nick' => 'selfcontact', 'nurl' => 'http://localhost/profile/selfcontact' ]; + $this->friendUser = [ + 'id' => 44, + 'name' => 'Friend contact', + 'nick' => 'friendcontact', + 'nurl' => 'http://localhost/profile/friendcontact' + ]; $this->otherUser = [ 'id' => 43, 'name' => 'othercontact', @@ -53,10 +60,6 @@ class ApiTest extends DatabaseTest 'uid' => $this->selfUser['id'] ]; - // Reusable App object - $this->app = new App(__DIR__.'/../'); - $a = $this->app; - // Default config Config::set('config', 'hostname', 'localhost'); Config::set('system', 'throttle_limit_day', 100); @@ -481,7 +484,7 @@ class ApiTest extends DatabaseTest $this->app->query_string = 'api_path.rss'; $this->assertEquals( - ''.PHP_EOL. + ''."\n". 'some_data', api_call($this->app) ); @@ -505,7 +508,7 @@ class ApiTest extends DatabaseTest $this->app->query_string = 'api_path.atom'; $this->assertEquals( - ''.PHP_EOL. + ''."\n". 'some_data', api_call($this->app) ); @@ -571,14 +574,14 @@ class ApiTest extends DatabaseTest public function testApiErrorWithXml() { $this->assertEquals( - ''.PHP_EOL. + ''."\n". ''.PHP_EOL. - ' error_message'.PHP_EOL. - ' 200 Friendica\Network\HTTP'.PHP_EOL. - ' '.PHP_EOL. - ''.PHP_EOL, + 'xmlns:georss="http://www.georss.org/georss">'."\n". + ' error_message'."\n". + ' 200 Friendica\Network\HTTP'."\n". + ' '."\n". + ''."\n", api_error('xml', new HTTPException('error_message')) ); } @@ -591,14 +594,14 @@ class ApiTest extends DatabaseTest public function testApiErrorWithRss() { $this->assertEquals( - ''.PHP_EOL. + ''."\n". ''.PHP_EOL. - ' error_message'.PHP_EOL. - ' 200 Friendica\Network\HTTP'.PHP_EOL. - ' '.PHP_EOL. - ''.PHP_EOL, + 'xmlns:georss="http://www.georss.org/georss">'."\n". + ' error_message'."\n". + ' 200 Friendica\Network\HTTP'."\n". + ' '."\n". + ''."\n", api_error('rss', new HTTPException('error_message')) ); } @@ -611,14 +614,14 @@ class ApiTest extends DatabaseTest public function testApiErrorWithAtom() { $this->assertEquals( - ''.PHP_EOL. + ''."\n". ''.PHP_EOL. - ' error_message'.PHP_EOL. - ' 200 Friendica\Network\HTTP'.PHP_EOL. - ' '.PHP_EOL. - ''.PHP_EOL, + 'xmlns:georss="http://www.georss.org/georss">'."\n". + ' error_message'."\n". + ' 200 Friendica\Network\HTTP'."\n". + ' '."\n". + ''."\n", api_error('atom', new HTTPException('error_message')) ); } @@ -629,7 +632,7 @@ class ApiTest extends DatabaseTest */ public function testApiRssExtra() { - $user_info = ['url' => 'user_url']; + $user_info = ['url' => 'user_url', 'lang' => 'en']; $result = api_rss_extra($this->app, [], $user_info); $this->assertEquals($user_info, $result['$user']); $this->assertEquals($user_info['url'], $result['$rss']['alternate']); @@ -713,10 +716,10 @@ class ApiTest extends DatabaseTest */ public function testApiGetUserWithCustomFrioSchema() { - 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'); + $ret1 = PConfig::set($this->selfUser['id'], 'frio', 'schema', '---'); + $ret2 = PConfig::set($this->selfUser['id'], 'frio', 'nav_bg', '#123456'); + $ret3 = PConfig::set($this->selfUser['id'], 'frio', 'link_color', '#123456'); + $ret4 = 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']); @@ -818,7 +821,7 @@ class ApiTest extends DatabaseTest public function testApiGetUserWithCalledApi() { global $called_api; - $called_api = ['api_path']; + $called_api = ['api', 'api_path']; $this->assertSelfUser(api_get_user($this->app)); } @@ -853,7 +856,6 @@ class ApiTest extends DatabaseTest $this->assertSelfUser(api_get_user($this->app, 0)); } - /** * Test the api_item_get_user() function. * @return void @@ -957,12 +959,12 @@ class ApiTest extends DatabaseTest public function testApiCreateXml() { $this->assertEquals( - ''.PHP_EOL. + ''."\n". ''.PHP_EOL. - ' some_data'.PHP_EOL. - ''.PHP_EOL, + 'xmlns:georss="http://www.georss.org/georss">'."\n". + ' some_data'."\n". + ''."\n", api_create_xml(['data' => ['some_data']], 'root_element') ); } @@ -974,10 +976,10 @@ class ApiTest extends DatabaseTest public function testApiCreateXmlWithoutNamespaces() { $this->assertEquals( - ''.PHP_EOL. - ''.PHP_EOL. - ' some_data'.PHP_EOL. - ''.PHP_EOL, + ''."\n". + ''."\n". + ' some_data'."\n". + ''."\n", api_create_xml(['data' => ['some_data']], 'ok') ); } @@ -999,12 +1001,12 @@ class ApiTest extends DatabaseTest public function testApiFormatDataWithXml() { $this->assertEquals( - ''.PHP_EOL. + ''."\n". ''.PHP_EOL. - ' some_data'.PHP_EOL. - ''.PHP_EOL, + 'xmlns:georss="http://www.georss.org/georss">'."\n". + ' some_data'."\n". + ''."\n", api_format_data('root_element', 'xml', ['data' => ['some_data']]) ); } @@ -1073,6 +1075,7 @@ class ApiTest extends DatabaseTest 'width' => 666, 'height' => 666, 'tmp_name' => $this->getTempImage(), + 'name' => 'spacer.png', 'type' => 'image/png' ] ]; @@ -1110,6 +1113,7 @@ class ApiTest extends DatabaseTest 'width' => 666, 'height' => 666, 'tmp_name' => $this->getTempImage(), + 'name' => 'spacer.png', 'type' => 'image/png' ] ]; @@ -1217,6 +1221,7 @@ class ApiTest extends DatabaseTest 'width' => 666, 'height' => 666, 'tmp_name' => $this->getTempImage(), + 'name' => 'spacer.png', 'type' => 'image/png' ] ]; @@ -1833,6 +1838,8 @@ class ApiTest extends DatabaseTest */ public function testApiFavoritesCreateDestroy() { + $this->app->argv = ['api', '1.1', 'favorites', 'create']; + $this->app->argc = count($this->app->argv); api_favorites_create_destroy('json'); } @@ -1843,9 +1850,8 @@ class ApiTest extends DatabaseTest */ public function testApiFavoritesCreateDestroyWithInvalidId() { - // This triggers a very specific condition ($action_argv_id + 2) - $this->app->argv[1] = '1.1'; - $this->app->argc = 5; + $this->app->argv = ['api', '1.1', 'favorites', 'create', '12.json']; + $this->app->argc = count($this->app->argv); api_favorites_create_destroy('json'); } @@ -1856,8 +1862,8 @@ class ApiTest extends DatabaseTest */ public function testApiFavoritesCreateDestroyWithInvalidAction() { - $this->app->argv[1] = '1.1'; - $this->app->argc = 10; + $this->app->argv = ['api', '1.1', 'favorites', 'change.json']; + $this->app->argc = count($this->app->argv); $_REQUEST['id'] = 1; api_favorites_create_destroy('json'); } @@ -1868,10 +1874,9 @@ class ApiTest extends DatabaseTest */ public function testApiFavoritesCreateDestroyWithCreateAction() { - $this->app->argv[1] = '1.1'; - $this->app->argv[3] = 'create'; - $this->app->argc = 10; - $_REQUEST['id'] = 1; + $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'); $this->assertStatus($result['status']); } @@ -1882,10 +1887,9 @@ class ApiTest extends DatabaseTest */ public function testApiFavoritesCreateDestroyWithCreateActionAndRss() { - $this->app->argv[1] = '1.1'; - $this->app->argv[3] = 'create'; - $this->app->argc = 10; - $_REQUEST['id'] = 1; + $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'); $this->assertXml($result, 'status'); } @@ -1896,10 +1900,9 @@ class ApiTest extends DatabaseTest */ public function testApiFavoritesCreateDestroyWithDestroyAction() { - $this->app->argv[1] = '1.1'; - $this->app->argv[3] = 'destroy'; - $this->app->argc = 10; - $_REQUEST['id'] = 1; + $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'); $this->assertStatus($result['status']); } @@ -1911,6 +1914,8 @@ class ApiTest extends DatabaseTest */ public function testApiFavoritesCreateDestroyWithoutAuthenticatedUser() { + $this->app->argv = ['api', '1.1', 'favorites', 'create.json']; + $this->app->argc = count($this->app->argv); $_SESSION['authenticated'] = false; api_favorites_create_destroy('json'); } @@ -1962,7 +1967,7 @@ class ApiTest extends DatabaseTest ['id' => 2, 'screen_name' => 'recipient_name'], ['id' => 3, 'screen_name' => 'sender_name'] ); - $this->assertEquals('item_title'.PHP_EOL.'item_body', $result['text']); + $this->assertEquals('item_title'."\n".'item_body', $result['text']); $this->assertEquals(1, $result['id']); $this->assertEquals(2, $result['recipient_id']); $this->assertEquals(3, $result['sender_id']); @@ -2014,8 +2019,8 @@ class ApiTest extends DatabaseTest ['id' => 2, 'screen_name' => 'recipient_name'], ['id' => 3, 'screen_name' => 'sender_name'] ); - $this->assertNull($result['sender']); - $this->assertNull($result['recipient']); + $this->assertTrue(!isset($result['sender'])); + $this->assertTrue(!isset($result['recipient'])); } /** @@ -2051,7 +2056,8 @@ class ApiTest extends DatabaseTest 'repellat officia illum quos impedit quam iste esse unde qui '. 'suscipit aut facilis ut inventore omnis exercitationem quo magnam '. 'consequatur maxime aut illum soluta quaerat natus unde aspernatur '. - 'et sed beatae nihil ullam temporibus corporis ratione blanditiis' + 'et sed beatae nihil ullam temporibus corporis ratione blanditiis', + 'plink' => 'item_plink' ] ); $this->assertStringStartsWith('item_title', $result['text']); @@ -2108,7 +2114,7 @@ class ApiTest extends DatabaseTest */ public function testApiGetAttachmentsWithImage() { - $body = '[img]img_url[/img]'; + $body = '[img]http://via.placeholder.com/1x1.png[/img]'; $this->assertInternalType('array', api_get_attachments($body)); } @@ -2119,7 +2125,7 @@ class ApiTest extends DatabaseTest public function testApiGetAttachmentsWithImageAndAndStatus() { $_SERVER['HTTP_USER_AGENT'] = 'AndStatus'; - $body = '[img]img_url[/img]'; + $body = '[img]http://via.placeholder.com/1x1.png[/img]'; $this->assertInternalType('array', api_get_attachments($body)); } @@ -2155,7 +2161,7 @@ class ApiTest extends DatabaseTest public function testApiFormatItemsEmbededImages() { $this->assertEquals( - 'text http://localhost/display/item_guid', + 'text ' . System::baseUrl() . '/display/item_guid', api_format_items_embeded_images(['guid' => 'item_guid'], 'text data:image/foo') ); } @@ -2196,7 +2202,7 @@ class ApiTest extends DatabaseTest */ public function testApiFormatItemsActivities() { - $item = []; + $item = ['uid' => 0, 'uri' => '']; $result = api_format_items_activities($item); $this->assertArrayHasKey('like', $result); $this->assertArrayHasKey('dislike', $result); @@ -2211,7 +2217,7 @@ class ApiTest extends DatabaseTest */ public function testApiFormatItemsActivitiesWithXml() { - $item = []; + $item = ['uid' => 0, 'uri' => '']; $result = api_format_items_activities($item, 'xml'); $this->assertArrayHasKey('friendica:like', $result); $this->assertArrayHasKey('friendica:dislike', $result); @@ -2325,10 +2331,16 @@ class ApiTest extends DatabaseTest [ 'item_network' => 'item_network', 'source' => 'web', - 'coord' => '5 7' + 'coord' => '5 7', + 'body' => '', + 'verb' => '', + 'author-id' => 43, + 'author-network' => Protocol::DFRN, + 'author-link' => 'http://localhost/profile/othercontact', + 'plink' => '', ] ]; - $result = api_format_items($items, [], true); + $result = api_format_items($items, ['id' => 0], true); foreach ($result as $status) { $this->assertStatus($status); } @@ -2342,10 +2354,16 @@ class ApiTest extends DatabaseTest { $items = [ [ - 'coord' => '5 7' + 'coord' => '5 7', + 'body' => '', + 'verb' => '', + 'author-id' => 43, + 'author-network' => Protocol::DFRN, + 'author-link' => 'http://localhost/profile/othercontact', + 'plink' => '', ] ]; - $result = api_format_items($items, [], true, 'xml'); + $result = api_format_items($items, ['id' => 0], true, 'xml'); foreach ($result as $status) { $this->assertStatus($status); } @@ -2389,7 +2407,6 @@ class ApiTest extends DatabaseTest */ public function testApiHelpTestWithXml() { - $this->markTestIncomplete('Triggers this error: "key() expects parameter 1 to be array, string given"'); $result = api_help_test('xml'); $this->assertXml($result, 'ok'); } @@ -2615,7 +2632,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('http://localhost/images/friendica-64.png', $result['config']['site']['logo']); + $this->assertEquals(System::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']); @@ -2725,7 +2742,7 @@ class ApiTest extends DatabaseTest public function testApiDirectMessagesNewWithScreenName() { $_POST['text'] = 'message_text'; - $_POST['screen_name'] = $this->otherUser['nick']; + $_POST['screen_name'] = $this->friendUser['nick']; $result = api_direct_messages_new('json'); $this->assertEquals(1, $result['direct_message']['id']); $this->assertContains('message_text', $result['direct_message']['text']); @@ -2740,7 +2757,7 @@ class ApiTest extends DatabaseTest public function testApiDirectMessagesNewWithTitle() { $_POST['text'] = 'message_text'; - $_POST['screen_name'] = $this->otherUser['nick']; + $_POST['screen_name'] = $this->friendUser['nick']; $_REQUEST['title'] = 'message_title'; $result = api_direct_messages_new('json'); $this->assertEquals(1, $result['direct_message']['id']); @@ -2757,7 +2774,7 @@ class ApiTest extends DatabaseTest public function testApiDirectMessagesNewWithRss() { $_POST['text'] = 'message_text'; - $_POST['screen_name'] = $this->otherUser['nick']; + $_POST['screen_name'] = $this->friendUser['nick']; $result = api_direct_messages_new('rss'); $this->assertXml($result, 'direct-messages'); } @@ -3357,7 +3374,7 @@ class ApiTest extends DatabaseTest */ public function testApiShareAsRetweet() { - $item = []; + $item = ['body' => '']; $result = api_share_as_retweet($item); $this->assertFalse($result); } @@ -3397,7 +3414,7 @@ class ApiTest extends DatabaseTest */ public function testApiInReplyTo() { - $result = api_in_reply_to([]); + $result = api_in_reply_to(['id' => 0, 'parent' => 0, 'uri' => '', 'thr-parent' => '']); $this->assertArrayHasKey('status_id', $result); $this->assertArrayHasKey('user_id', $result); $this->assertArrayHasKey('status_id_str', $result); @@ -3562,7 +3579,8 @@ class ApiTest extends DatabaseTest */ public function testApiFriendicaNotificationWithArgumentCount() { - $this->app->argc = 3; + $this->app->argv = ['api', 'friendica', 'notification']; + $this->app->argc = count($this->app->argv); $result = api_friendica_notification('json'); $this->assertEquals(['note' => false], $result); } @@ -3573,8 +3591,8 @@ class ApiTest extends DatabaseTest */ public function testApiFriendicaNotificationWithXmlResult() { - $this->markTestIncomplete('Fails with "Invalid argument supplied for foreach()".'); - $this->app->argc = 3; + $this->app->argv = ['api', 'friendica', 'notification']; + $this->app->argc = count($this->app->argv); $result = api_friendica_notification('xml'); $this->assertXml($result, 'notes'); }