]> git.mxchange.org Git - friendica.git/commitdiff
Fix test
authorPhilipp <admin@philipp.info>
Tue, 30 Nov 2021 19:53:47 +0000 (20:53 +0100)
committerPhilipp <admin@philipp.info>
Tue, 30 Nov 2021 19:53:47 +0000 (20:53 +0100)
tests/Util/AuthTestConfig.php [new file with mode: 0644]
tests/Util/authtest/authtest.php
tests/legacy/ApiTest.php
tests/src/Module/Api/ApiTest.php
tests/src/Module/Api/Twitter/Media/UploadTest.php [new file with mode: 0644]

diff --git a/tests/Util/AuthTestConfig.php b/tests/Util/AuthTestConfig.php
new file mode 100644 (file)
index 0000000..ad2592d
--- /dev/null
@@ -0,0 +1,11 @@
+<?php
+
+namespace Friendica\Test\Util;
+
+class AuthTestConfig
+{
+       /** @var bool */
+       public static $authenticated = true;
+       /** @var int  */
+       public static $user_id = 42;
+}
index cec0c066ed9938e349f07f13482663b60e42c7ea..7252b6653e5f71026d11194c466ad79e50b8b51d 100644 (file)
@@ -16,6 +16,6 @@ function authtest_install()
 
 function authtest_authenticate($a,&$b)
 {
-       $b['authenticated'] = 1;
-       $b['user_record']   = User::getById(42);
+       $b['authenticated'] = \Friendica\Test\Util\AuthTestConfig::$authenticated;
+       $b['user_record']   = User::getById(\Friendica\Test\Util\AuthTestConfig::$user_id);
 }
index 4801da13a709de415bb9f792cf981b780c48864f..eb5394ec9e0a80aa3bb3d4d9244ac013736e6b0e 100644 (file)
@@ -199,25 +199,6 @@ class ApiTest extends FixtureTest
                // We could probably do more checks here.
        }
 
-       /**
-        * Get the path to a temporary empty PNG image.
-        *
-        * @return string Path
-        */
-       private function getTempImage()
-       {
-               $tmpFile = tempnam(sys_get_temp_dir(), 'tmp_file');
-               file_put_contents(
-                       $tmpFile,
-                       base64_decode(
-                       // Empty 1x1 px PNG image
-                               'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8/5+hHgAHggJ/PchI7wAAAABJRU5ErkJggg=='
-                       )
-               );
-
-               return $tmpFile;
-       }
-
        /**
         * Test the api_user() function.
         *
@@ -999,79 +980,7 @@ class ApiTest extends FixtureTest
                $this->markTestIncomplete();
        }
 
-       /**
-        * Test the \Friendica\Module\Api\Twitter\Media\Upload module.
-        * @runInSeparateProcess
-        * @preserveGlobalState disabled
-        */
-       public function testApiMediaUpload()
-       {
-               $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
-               $_SERVER['REQUEST_METHOD'] = Router::POST;
-               (new Upload(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), $_SERVER))->run();
-       }
-
-       /**
-        * Test the \Friendica\Module\Api\Twitter\Media\Upload module without an authenticated user.
-        *
-        * @return void
-        */
-       public function testApiMediaUploadWithoutAuthenticatedUser()
-       {
-               $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
-               BasicAuth::setCurrentUserID();
-               $_SESSION['authenticated'] = false;
-               $_SERVER['REQUEST_METHOD'] = Router::POST;
-               (new Upload(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), $_SERVER))->run();
-       }
-
-       /**
-        * Test the \Friendica\Module\Api\Twitter\Media\Upload module with an invalid uploaded media.
-        *
-        * @return void
-        */
-       public function testApiMediaUploadWithMedia()
-       {
-               $this->expectException(\Friendica\Network\HTTPException\InternalServerErrorException::class);
-               $_FILES = [
-                       'media' => [
-                               'id'       => 666,
-                               'tmp_name' => 'tmp_name'
-                       ]
-               ];
-               $_SERVER['REQUEST_METHOD'] = Router::POST;
-               (new Upload(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), $_SERVER))->run();
-       }
-
-       /**
-        * Test the \Friendica\Module\Api\Twitter\Media\Upload module with an valid uploaded media.
-        *
-        * @return void
-        */
-       public function testApiMediaUploadWithValidMedia()
-       {
-               $_FILES    = [
-                       'media' => [
-                               'id'       => 666,
-                               'size'     => 666,
-                               'width'    => 666,
-                               'height'   => 666,
-                               'tmp_name' => $this->getTempImage(),
-                               'name'     => 'spacer.png',
-                               'type'     => 'image/png'
-                       ]
-               ];
-
-               $_SERVER['REQUEST_METHOD'] = Router::POST;
-
-               $response = (new Upload(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), $_SERVER))->run();
-               $media = json_decode($response->getBody(), true);
 
-               self::assertEquals('image/png', $media['image']['image_type']);
-               self::assertEquals(1, $media['image']['w']);
-               self::assertEquals(1, $media['image']['h']);
-               self::assertNotEmpty($media['image']['friendica_preview_url']);
-       }
 
        /**
         * Test the api_statuses_repeat() function.
index 98e9b2a03c1992203dd2b9ed4ae68e9b586afd29..154f54a1cf41debd8fb8e331136e5358ea2e523d 100644 (file)
@@ -27,9 +27,11 @@ use Friendica\Core\Hook;
 use Friendica\Database\Database;
 use Friendica\DI;
 use Friendica\Security\Authentication;
+use Friendica\Security\BasicAuth;
 use Friendica\Test\FixtureTest;
 use Friendica\Test\Util\AppDouble;
 use Friendica\Test\Util\AuthenticationDouble;
+use Friendica\Test\Util\AuthTestConfig;
 
 abstract class ApiTest extends FixtureTest
 {
@@ -60,9 +62,19 @@ abstract class ApiTest extends FixtureTest
                // Manual override to bypass API authentication
                DI::app()->setIsLoggedIn(true);
 
+               AuthTestConfig::$authenticated = true;
+               AuthTestConfig::$user_id = 42;
+
                $this->installAuthTest();
        }
 
+       protected function tearDown(): void
+       {
+               BasicAuth::setCurrentUserID();
+
+               parent::tearDown(); // TODO: Change the autogenerated stub
+       }
+
        /**
         * installs auththest.
         *
diff --git a/tests/src/Module/Api/Twitter/Media/UploadTest.php b/tests/src/Module/Api/Twitter/Media/UploadTest.php
new file mode 100644 (file)
index 0000000..abc9d8f
--- /dev/null
@@ -0,0 +1,101 @@
+<?php
+
+namespace Friendica\Test\src\Module\Api\Twitter\Media;
+
+use Friendica\App\Router;
+use Friendica\DI;
+use Friendica\Module\Api\Twitter\Media\Upload;
+use Friendica\Network\HTTPException\BadRequestException;
+use Friendica\Network\HTTPException\InternalServerErrorException;
+use Friendica\Network\HTTPException\UnauthorizedException;
+use Friendica\Test\src\Module\Api\ApiTest;
+use Friendica\Test\Util\AuthTestConfig;
+
+class UploadTest extends ApiTest
+{
+       /**
+        * Test the \Friendica\Module\Api\Twitter\Media\Upload module.
+        */
+       public function testApiMediaUpload()
+       {
+               $this->expectException(BadRequestException::class);
+               $upload = new Upload(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::POST]);
+               $upload->run();
+       }
+
+       /**
+        * Test the \Friendica\Module\Api\Twitter\Media\Upload module without an authenticated user.
+        *
+        * @return void
+        */
+       public function testApiMediaUploadWithoutAuthenticatedUser()
+       {
+               $this->expectException(UnauthorizedException::class);
+               AuthTestConfig::$authenticated = false;
+               (new Upload(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::POST]))->run();
+       }
+
+       /**
+        * Test the \Friendica\Module\Api\Twitter\Media\Upload module with an invalid uploaded media.
+        *
+        * @return void
+        */
+       public function testApiMediaUploadWithMedia()
+       {
+               $this->expectException(InternalServerErrorException::class);
+               $_FILES = [
+                       'media' => [
+                               'id'       => 666,
+                               'tmp_name' => 'tmp_name'
+                       ]
+               ];
+               (new Upload(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::POST]))->run();
+       }
+
+       /**
+        * Test the \Friendica\Module\Api\Twitter\Media\Upload module with an valid uploaded media.
+        *
+        * @return void
+        */
+       public function testApiMediaUploadWithValidMedia()
+       {
+               $_FILES = [
+                       'media' => [
+                               'id'       => 666,
+                               'size'     => 666,
+                               'width'    => 666,
+                               'height'   => 666,
+                               'tmp_name' => $this->getTempImage(),
+                               'name'     => 'spacer.png',
+                               'type'     => 'image/png'
+                       ]
+               ];
+
+               $response = (new Upload(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::POST]))->run();
+               $media    = json_decode($response->getBody(), true);
+
+               self::assertEquals('image/png', $media['image']['image_type']);
+               self::assertEquals(1, $media['image']['w']);
+               self::assertEquals(1, $media['image']['h']);
+               self::assertNotEmpty($media['image']['friendica_preview_url']);
+       }
+
+       /**
+        * Get the path to a temporary empty PNG image.
+        *
+        * @return string Path
+        */
+       private function getTempImage()
+       {
+               $tmpFile = tempnam(sys_get_temp_dir(), 'tmp_file');
+               file_put_contents(
+                       $tmpFile,
+                       base64_decode(
+                       // Empty 1x1 px PNG image
+                               'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8/5+hHgAHggJ/PchI7wAAAABJRU5ErkJggg=='
+                       )
+               );
+
+               return $tmpFile;
+       }
+}