]> git.mxchange.org Git - friendica.git/blobdiff - tests/src/Module/Api/ApiTest.php
Reenable Twitter/Show test
[friendica.git] / tests / src / Module / Api / ApiTest.php
index 154f54a1cf41debd8fb8e331136e5358ea2e523d..09fbb7bbe95e2a82ccbec2e5db480b5e804d75bb 100644 (file)
@@ -22,6 +22,7 @@
 namespace Friendica\Test\src\Module\Api;
 
 use Friendica\App;
+use Friendica\Capabilities\ICanCreateResponses;
 use Friendica\Core\Addon;
 use Friendica\Core\Hook;
 use Friendica\Database\Database;
@@ -32,9 +33,35 @@ use Friendica\Test\FixtureTest;
 use Friendica\Test\Util\AppDouble;
 use Friendica\Test\Util\AuthenticationDouble;
 use Friendica\Test\Util\AuthTestConfig;
+use Psr\Http\Message\ResponseInterface;
 
 abstract class ApiTest extends FixtureTest
 {
+       // User data that the test database is populated with
+       const SELF_USER = [
+               'id'   => 42,
+               'name' => 'Self contact',
+               'nick' => 'selfcontact',
+               'nurl' => 'http://localhost/profile/selfcontact'
+       ];
+
+       const FRIEND_USER = [
+               'id'   => 44,
+               'name' => 'Friend contact',
+               'nick' => 'friendcontact',
+               'nurl' => 'http://localhost/profile/friendcontact'
+       ];
+
+       const OTHER_USER = [
+               'id'   => 43,
+               'name' => 'othercontact',
+               'nick' => 'othercontact',
+               'nurl' => 'http://localhost/profile/othercontact'
+       ];
+
+       // User ID that we know is not in the database
+       const WRONG_USER_ID = 666;
+
        /**
         * Assert that the string is XML and contain the root element.
         *
@@ -50,6 +77,73 @@ abstract class ApiTest extends FixtureTest
                // We could probably do more checks here.
        }
 
+       /**
+        * Assert that an user array contains expected keys.
+        *
+        * @param \stdClass $user User
+        *
+        * @return void
+        */
+       protected function assertSelfUser(\stdClass $user)
+       {
+               self::assertEquals(self::SELF_USER['id'], $user->uid);
+               self::assertEquals(self::SELF_USER['id'], $user->cid);
+               self::assertEquals(1, $user->self);
+               self::assertEquals('DFRN', $user->location);
+               self::assertEquals(self::SELF_USER['name'], $user->name);
+               self::assertEquals(self::SELF_USER['nick'], $user->screen_name);
+               self::assertEquals('dfrn', $user->network);
+               self::assertTrue($user->verified);
+       }
+
+       /**
+        * Assert that an user array contains expected keys.
+        *
+        * @param \stdClass $user User
+        *
+        * @return void
+        */
+       protected function assertOtherUser(\stdClass $user)
+       {
+               self::assertEquals(self::OTHER_USER['id'], $user->id);
+               self::assertEquals(self::OTHER_USER['id'], $user->id_str);
+               self::assertEquals(self::OTHER_USER['name'], $user->name);
+               self::assertEquals(self::OTHER_USER['nick'], $user->screen_name);
+               self::assertFalse($user->verified);
+       }
+
+       /**
+        * Assert that a status array contains expected keys.
+        *
+        * @param array $status Status array
+        *
+        * @return void
+        */
+       protected function assertStatus(array $status = [])
+       {
+               self::assertIsString($status['text'] ?? '');
+               self::assertIsInt($status['id'] ?? '');
+               // We could probably do more checks here.
+       }
+
+       /**
+        * Transforms a response into a JSON class
+        *
+        * @param ResponseInterface $response
+        *
+        * @return mixed
+        */
+       protected function toJson(ResponseInterface $response)
+       {
+               self::assertEquals(ICanCreateResponses::TYPE_JSON, $response->getHeaderLine(ICanCreateResponses::X_HEADER));
+
+               $body = (string)$response->getBody();
+
+               self::assertJson($body);
+
+               return json_decode($body);
+       }
+
        protected function setUp(): void
        {
                parent::setUp(); // TODO: Change the autogenerated stub
@@ -63,7 +157,7 @@ abstract class ApiTest extends FixtureTest
                DI::app()->setIsLoggedIn(true);
 
                AuthTestConfig::$authenticated = true;
-               AuthTestConfig::$user_id = 42;
+               AuthTestConfig::$user_id       = 42;
 
                $this->installAuthTest();
        }