]> git.mxchange.org Git - friendica.git/blobdiff - tests/include/ApiTest.php
BugFix: forgot configCache in ConfigFactory
[friendica.git] / tests / include / ApiTest.php
index a539eb944777bb9e7a558b32e1ffc2668e25abc8..94557ac69c234b46b78a1bb48f8608d9c94632f1 100644 (file)
@@ -5,12 +5,17 @@
 
 namespace Friendica\Test;
 
-use Friendica\BaseObject;
+use Friendica\App;
 use Friendica\Core\Config;
 use Friendica\Core\PConfig;
 use Friendica\Core\Protocol;
 use Friendica\Core\System;
+use Friendica\Factory;
 use Friendica\Network\HTTPException;
+use Friendica\Util\BasePath;
+use Friendica\Util\BaseURL;
+use Friendica\Util\Config\ConfigFileLoader;
+use Monolog\Handler\TestHandler;
 
 require_once __DIR__ . '/../../include/api.php';
 
@@ -22,14 +27,42 @@ require_once __DIR__ . '/../../include/api.php';
  */
 class ApiTest extends DatabaseTest
 {
+       /**
+        * @var TestHandler Can handle log-outputs
+        */
+       protected $logOutput;
+
+       /** @var App */
+       protected $app;
+
+       /** @var array */
+       protected $selfUser;
+       /** @var array */
+       protected $friendUser;
+       /** @var array */
+       protected $otherUser;
+
+       protected $wrongUserId;
+
        /**
         * Create variables used by tests.
         */
        public function setUp()
        {
-               parent::setUp();
+               $basePath = BasePath::create(dirname(__DIR__) . '/../');
+               $mode = new App\Mode($basePath);
+               $router = new App\Router();
+               $configLoader = new ConfigFileLoader($basePath, $mode);
+               $configCache = Factory\ConfigFactory::createCache($configLoader);
+               $profiler = Factory\ProfilerFactory::create($configCache);
+               $database = Factory\DBFactory::init($configCache, $profiler, $_SERVER);
+               $config = Factory\ConfigFactory::createConfig($configCache);
+               Factory\ConfigFactory::createPConfig($configCache, new Config\Cache\PConfigCache());
+               $logger = Factory\LoggerFactory::create('test', $database, $config, $profiler);
+               $baseUrl = new BaseURL($config, $_SERVER);
+               $this->app = new App($database, $config, $mode, $router, $baseUrl, $logger, $profiler, false);
 
-               $this->app = BaseObject::getApp();
+               parent::setUp();
 
                // User data that the test database is populated with
                $this->selfUser = [
@@ -94,7 +127,7 @@ class ApiTest extends DatabaseTest
                $this->assertEquals($this->selfUser['id'], $user['uid']);
                $this->assertEquals($this->selfUser['id'], $user['cid']);
                $this->assertEquals(1, $user['self']);
-               $this->assertEquals('Friendica', $user['location']);
+               $this->assertEquals('DFRN', $user['location']);
                $this->assertEquals($this->selfUser['name'], $user['name']);
                $this->assertEquals($this->selfUser['nick'], $user['screen_name']);
                $this->assertEquals('dfrn', $user['network']);
@@ -577,8 +610,8 @@ class ApiTest extends DatabaseTest
        public function testApiErrorWithJson()
        {
                $this->assertEquals(
-                       '{"status":{"error":"error_message","code":"200 Friendica\\\\Network\\\\HTTP","request":""}}',
-                       api_error('json', new HTTPException('error_message'))
+                       '{"status":{"error":"error_message","code":"200 OK","request":""}}',
+                       api_error('json', new HTTPException\OKException('error_message'))
                );
        }
 
@@ -595,10 +628,10 @@ class ApiTest extends DatabaseTest
                                'xmlns:friendica="http://friendi.ca/schema/api/1/" '.
                                'xmlns:georss="http://www.georss.org/georss">'."\n".
                        '  <error>error_message</error>'."\n".
-                       '  <code>200 Friendica\Network\HTTP</code>'."\n".
+                       '  <code>200 OK</code>'."\n".
                        '  <request/>'."\n".
                        '</status>'."\n",
-                       api_error('xml', new HTTPException('error_message'))
+                       api_error('xml', new HTTPException\OKException('error_message'))
                );
        }
 
@@ -615,10 +648,10 @@ class ApiTest extends DatabaseTest
                                'xmlns:friendica="http://friendi.ca/schema/api/1/" '.
                                'xmlns:georss="http://www.georss.org/georss">'."\n".
                        '  <error>error_message</error>'."\n".
-                       '  <code>200 Friendica\Network\HTTP</code>'."\n".
+                       '  <code>200 OK</code>'."\n".
                        '  <request/>'."\n".
                        '</status>'."\n",
-                       api_error('rss', new HTTPException('error_message'))
+                       api_error('rss', new HTTPException\OKException('error_message'))
                );
        }
 
@@ -635,10 +668,10 @@ class ApiTest extends DatabaseTest
                                'xmlns:friendica="http://friendi.ca/schema/api/1/" '.
                                'xmlns:georss="http://www.georss.org/georss">'."\n".
                        '  <error>error_message</error>'."\n".
-                       '  <code>200 Friendica\Network\HTTP</code>'."\n".
+                       '  <code>200 OK</code>'."\n".
                        '  <request/>'."\n".
                        '</status>'."\n",
-                       api_error('atom', new HTTPException('error_message'))
+                       api_error('atom', new HTTPException\OKException('error_message'))
                );
        }
 
@@ -1118,7 +1151,7 @@ class ApiTest extends DatabaseTest
         */
        public function testApiStatusesUpdate()
        {
-               $_GET['status'] = 'Status content';
+               $_GET['status'] = 'Status content #friendica';
                $_GET['in_reply_to_status_id'] = -1;
                $_GET['lat'] = 48;
                $_GET['long'] = 7;
@@ -1242,42 +1275,42 @@ class ApiTest extends DatabaseTest
                                'type' => 'image/png'
                        ]
                ];
-               $app = get_app();
+               $app = \get_app();
                $app->argc = 2;
 
                $result = api_media_upload();
                $this->assertEquals('image/png', $result['media']['image']['image_type']);
                $this->assertEquals(1, $result['media']['image']['w']);
                $this->assertEquals(1, $result['media']['image']['h']);
+               $this->assertNotEmpty($result['media']['image']['friendica_preview_url']);
        }
 
        /**
         * Test the api_status_show() function.
-        * @return void
         */
-       public function testApiStatusShow()
+       public function testApiStatusShowWithJson()
        {
-               $result = api_status_show('json');
+               $result = api_status_show('json', 1);
                $this->assertStatus($result['status']);
        }
 
        /**
         * Test the api_status_show() function with an XML result.
-        * @return void
         */
        public function testApiStatusShowWithXml()
        {
-               $result = api_status_show('xml');
+               $result = api_status_show('xml', 1);
                $this->assertXml($result, 'statuses');
        }
 
        /**
-        * Test the api_status_show() function with a raw result.
-        * @return void
+        * Test the api_get_last_status() function
         */
-       public function testApiStatusShowWithRaw()
+       public function testApiGetLastStatus()
        {
-               $this->assertStatus(api_status_show('raw'));
+               $item = api_get_last_status($this->selfUser['id'], $this->selfUser['id']);
+
+               $this->assertNotNull($item);
        }
 
        /**
@@ -1289,7 +1322,7 @@ class ApiTest extends DatabaseTest
                $result = api_users_show('json');
                // We can't use assertSelfUser() here because the user object is missing some properties.
                $this->assertEquals($this->selfUser['id'], $result['user']['cid']);
-               $this->assertEquals('Friendica', $result['user']['location']);
+               $this->assertEquals('DFRN', $result['user']['location']);
                $this->assertEquals($this->selfUser['name'], $result['user']['name']);
                $this->assertEquals($this->selfUser['nick'], $result['user']['screen_name']);
                $this->assertEquals('dfrn', $result['user']['network']);
@@ -1404,6 +1437,34 @@ class ApiTest extends DatabaseTest
                }
        }
 
+       /**
+        * Test the api_search() function with an q parameter contains hashtag.
+        * @return void
+        */
+       public function testApiSearchWithHashtag()
+       {
+               $_REQUEST['q'] = '%23friendica';
+               $result = api_search('json');
+               foreach ($result['status'] as $status) {
+                       $this->assertStatus($status);
+                       $this->assertContains('#friendica', $status['text'], null, true);
+               }
+       }
+
+       /**
+        * Test the api_search() function with an exclude_replies parameter.
+        * @return void
+        */
+       public function testApiSearchWithExcludeReplies()
+       {
+               $_REQUEST['max_id'] = 10;
+               $_REQUEST['exclude_replies'] = true;
+               $_REQUEST['q'] = 'friendica';
+               $result = api_search('json');
+               foreach ($result['status'] as $status) {
+                       $this->assertStatus($status);
+               }
+       }
 
        /**
         * Test the api_search() function without an authenticated user.
@@ -3296,7 +3357,7 @@ class ApiTest extends DatabaseTest
                $result = api_account_update_profile('json');
                // We can't use assertSelfUser() here because the user object is missing some properties.
                $this->assertEquals($this->selfUser['id'], $result['user']['cid']);
-               $this->assertEquals('Friendica', $result['user']['location']);
+               $this->assertEquals('DFRN', $result['user']['location']);
                $this->assertEquals($this->selfUser['nick'], $result['user']['screen_name']);
                $this->assertEquals('dfrn', $result['user']['network']);
                $this->assertEquals('new_name', $result['user']['name']);
@@ -3650,7 +3711,7 @@ class ApiTest extends DatabaseTest
                $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('Friendica', $result['$result']['friendica_owner']['location']);
+               $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']);