]> git.mxchange.org Git - friendica.git/commitdiff
Moved API\Notification tests
authorPhilipp <admin@philipp.info>
Fri, 12 Nov 2021 20:35:21 +0000 (21:35 +0100)
committerPhilipp <admin@philipp.info>
Fri, 12 Nov 2021 20:35:21 +0000 (21:35 +0100)
src/Module/Api/ApiResponse.php
tests/Util/ApiResponseDouble.php [new file with mode: 0644]
tests/legacy/ApiTest.php
tests/src/Module/Api/ApiTest.php
tests/src/Module/Api/Friendica/NotificationTest.php [new file with mode: 0644]
tests/src/Module/Api/Friendica/Photo/DeleteTest.php
tests/src/Module/Api/Friendica/Photoalbum/DeleteTest.php
tests/src/Module/Api/Friendica/Photoalbum/UpdateTest.php

index 82801f2e60488637c23575dfe7b811ae00e6505a..d0d03da9943a84d20b02682e3f04321f622f183b 100644 (file)
@@ -35,6 +35,29 @@ class ApiResponse
                $this->logger = $logger;
        }
 
+       /**
+        * Sets header directly
+        * mainly used to override it for tests
+        *
+        * @param string $header
+        */
+       protected function setHeader(string $header)
+       {
+               header($header);
+       }
+
+       /**
+        * Prints output directly to the caller
+        * mainly used to override it for tests
+        *
+        * @param string $output
+        */
+       protected function printOutput(string $output)
+       {
+               echo $output;
+               exit;
+       }
+
        /**
         * Creates the XML from a JSON style array
         *
@@ -143,7 +166,7 @@ class ApiResponse
                        'request' => $this->args->getQueryString()
                ];
 
-               header(($_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.1') . ' ' . $code . ' ' . $description);
+               $this->setHeader(($_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.1') . ' ' . $code . ' ' . $description);
 
                $this->exit('status', ['status' => $error], $format);
        }
@@ -165,10 +188,10 @@ class ApiResponse
 
                switch ($format) {
                        case 'xml':
-                               header('Content-Type: text/xml');
+                               $this->setHeader('Content-Type: text/xml');
                                break;
                        case 'json':
-                               header('Content-Type: application/json');
+                               $this->setHeader('Content-Type: application/json');
                                if (!empty($return)) {
                                        $json = json_encode(end($return));
                                        if (!empty($_GET['callback'])) {
@@ -178,17 +201,16 @@ class ApiResponse
                                }
                                break;
                        case 'rss':
-                               header('Content-Type: application/rss+xml');
+                               $this->setHeader('Content-Type: application/rss+xml');
                                $return = '<?xml version="1.0" encoding="UTF-8"?>' . "\n" . $return;
                                break;
                        case 'atom':
-                               header('Content-Type: application/atom+xml');
+                               $this->setHeader('Content-Type: application/atom+xml');
                                $return = '<?xml version="1.0" encoding="UTF-8"?>' . "\n" . $return;
                                break;
                }
 
-               echo $return;
-               exit;
+               $this->printOutput($return);
        }
 
        /**
diff --git a/tests/Util/ApiResponseDouble.php b/tests/Util/ApiResponseDouble.php
new file mode 100644 (file)
index 0000000..c931f2b
--- /dev/null
@@ -0,0 +1,54 @@
+<?php
+
+namespace Friendica\Test\Util;
+
+use Friendica\Module\Api\ApiResponse;
+
+class ApiResponseDouble extends ApiResponse
+{
+       /**
+        * The header list
+        *
+        * @var string[]
+        */
+       protected static $header = [];
+
+       /**
+        * The printed output
+        *
+        * @var string
+        */
+       protected static $output = '';
+
+       /**
+        * @return string[]
+        */
+       public static function getHeader(): array
+       {
+               return static::$header;
+       }
+
+       /**
+        * @return string
+        */
+       public static function getOutput(): string
+       {
+               return static::$output;
+       }
+
+       public static function reset()
+       {
+               self::$output = '';
+               self::$header = [];
+       }
+
+       protected function setHeader(string $header)
+       {
+               static::$header[] = $header;
+       }
+
+       protected function printOutput(string $output)
+       {
+               static::$output .= $output;
+       }
+}
index 1a88e6403f7cea2076be26be66e3665e921de728..0c1af085ca3ad57826332b869b78515b49a83a32 100644 (file)
@@ -3565,77 +3565,6 @@ class ApiTest extends FixtureTest
                $this->markTestIncomplete();
        }
 
-       /**
-        * Test the api_friendica_notification() function.
-        *
-        * @return void
-        */
-       public function testApiFriendicaNotification()
-       {
-               // $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
-               // api_friendica_notification('json');
-       }
-
-       /**
-        * Test the api_friendica_notification() function without an authenticated user.
-        *
-        * @return void
-        */
-       public function testApiFriendicaNotificationWithoutAuthenticatedUser()
-       {
-               // $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
-               // $_SESSION['authenticated'] = false;
-               // api_friendica_notification('json');
-       }
-
-       /**
-        * Test the api_friendica_notification() function with empty result
-        *
-        * @return void
-        */
-       public function testApiFriendicaNotificationWithEmptyResult()
-       {
-               // DI::args()->setArgv(['api', 'friendica', 'notification']);
-               // $_SESSION['uid'] = 41;
-               // $result          = api_friendica_notification('json');
-               // self::assertEquals(['note' => false], $result);
-       }
-
-       /**
-        * Test the api_friendica_notification() function with an XML result.
-        *
-        * @return void
-        */
-       public function testApiFriendicaNotificationWithXmlResult()
-       {
-               /*
-               DI::args()->setArgv(['api', 'friendica', 'notification']);
-               $result  = api_friendica_notification('xml');
-               $date = DateTimeFormat::local('2020-01-01 12:12:02');
-               $dateRel = Temporal::getRelativeDate('2020-01-01 07:12:02');
-
-               $assertXml=<<<XML
-<?xml version="1.0"?>
-<notes>
-  <note date="$date" date_rel="$dateRel" id="1" iid="4" link="http://localhost/notification/1" msg="A test reply from an item" msg_cache="A test reply from an item" msg_html="A test reply from an item" msg_plain="A test reply from an item" name="Reply to" name_cache="Reply to" otype="item" parent="" photo="http://localhost/" seen="false" timestamp="1577880722" type="8" uid="42" url="http://localhost/display/1" verb="http://activitystrea.ms/schema/1.0/post"/>
-</notes>
-XML;
-               self::assertXmlStringEqualsXmlString($assertXml, $result);
-               */
-       }
-
-       /**
-        * Test the api_friendica_notification() function with an JSON result.
-        *
-        * @return void
-        */
-       public function testApiFriendicaNotificationWithJsonResult()
-       {
-               // DI::args()->setArgv(['api', 'friendica', 'notification']);
-               // $result = json_encode(api_friendica_notification('json'));
-               // self::assertJson($result);
-       }
-
        /**
         * Test the api_friendica_notification_seen() function.
         *
index b2c38c31e6a5a4cedea24c816d9a9c07b24d5145..95d04041f983f037b077a1f596b309a8dfb8550a 100644 (file)
@@ -7,8 +7,10 @@ use Friendica\Core\Addon;
 use Friendica\Core\Hook;
 use Friendica\Database\Database;
 use Friendica\DI;
+use Friendica\Module\Api\ApiResponse;
 use Friendica\Security\Authentication;
 use Friendica\Test\FixtureTest;
+use Friendica\Test\Util\ApiResponseDouble;
 use Friendica\Test\Util\AuthenticationDouble;
 
 class ApiTest extends FixtureTest
@@ -18,12 +20,20 @@ class ApiTest extends FixtureTest
                parent::setUp(); // TODO: Change the autogenerated stub
 
                $this->dice = $this->dice
-                       ->addRule(Authentication::class, ['instanceOf' => AuthenticationDouble::class, 'shared' => true]);
+                       ->addRule(Authentication::class, ['instanceOf' => AuthenticationDouble::class, 'shared' => true])
+                       ->addRule(ApiResponse::class, ['instanceOf' => ApiResponseDouble::class, 'shared' => true]);
                DI::init($this->dice);
 
                $this->installAuthTest();
        }
 
+       protected function tearDown(): void
+       {
+               ApiResponseDouble::reset();
+
+               parent::tearDown();
+       }
+
        /**
         * installs auththest.
         *
diff --git a/tests/src/Module/Api/Friendica/NotificationTest.php b/tests/src/Module/Api/Friendica/NotificationTest.php
new file mode 100644 (file)
index 0000000..d9451d0
--- /dev/null
@@ -0,0 +1,60 @@
+<?php
+
+namespace Friendica\Test\src\Module\Api\Friendica;
+
+use Friendica\DI;
+use Friendica\Module\Api\Friendica\Notification;
+use Friendica\Network\HTTPException\BadRequestException;
+use Friendica\Test\src\Module\Api\ApiTest;
+use Friendica\Test\Util\ApiResponseDouble;
+use Friendica\Util\DateTimeFormat;
+use Friendica\Util\Temporal;
+
+class NotificationTest extends ApiTest
+{
+       public function testEmpty()
+       {
+               self::markTestIncomplete('Needs BasicAuth as dynamic method for overriding first');
+
+               $this->expectException(BadRequestException::class);
+               DI::session()->set('uid', '');
+
+               Notification::rawContent();
+       }
+
+       public function testWithoutAuthenticatedUser()
+       {
+               self::markTestIncomplete('Needs BasicAuth as dynamic method for overriding first');
+
+               $this->expectException(BadRequestException::class);
+               DI::session()->set('uid', 41);
+
+               Notification::rawContent();
+       }
+
+       public function testWithXmlResult()
+       {
+               $date    = DateTimeFormat::local('2020-01-01 12:12:02');
+               $dateRel = Temporal::getRelativeDate('2020-01-01 07:12:02');
+
+               $assertXml = <<<XML
+<?xml version="1.0"?>
+<notes>
+  <note date="$date" date_rel="$dateRel" id="1" iid="4" link="http://localhost/notification/1" msg="A test reply from an item" msg_cache="A test reply from an item" msg_html="A test reply from an item" msg_plain="A test reply from an item" name="Reply to" name_cache="Reply to" otype="item" parent="" photo="http://localhost/" seen="false" timestamp="1577880722" type="8" uid="42" url="http://localhost/display/1" verb="http://activitystrea.ms/schema/1.0/post"/>
+</notes>
+XML;
+
+               Notification::rawContent(['extension' => 'xml']);
+
+               self::assertXmlStringEqualsXmlString($assertXml, ApiResponseDouble::getOutput());
+       }
+
+       public function testWithJsonResult()
+       {
+               Notification::rawContent(['parameter' => 'json']);
+
+               $result = json_encode(ApiResponseDouble::getOutput());
+
+               self::assertJson($result);
+       }
+}
index a47c87e98ad14bbb70c950141656bf583a8d492d..5023cd6c16a3d37b613b2e04b45ff462ea6ae523 100644 (file)
@@ -10,7 +10,7 @@ class DeleteTest extends ApiTest
 {
        public function testEmpty()
        {
-               self::expectException(BadRequestException::class);
+               $this->expectException(BadRequestException::class);
                Delete::rawContent();
        }
 
@@ -21,7 +21,7 @@ class DeleteTest extends ApiTest
 
        public function testWrong()
        {
-               self::expectException(BadRequestException::class);
+               $this->expectException(BadRequestException::class);
                Delete::rawContent(['photo_id' => 1]);
        }
 
index ed9cd11578d8b680003165e6647e19541c761913..a8f07d5aa1141e6a2597c5158db88dc78990c601 100644 (file)
@@ -10,13 +10,13 @@ class DeleteTest extends ApiTest
 {
        public function testEmpty()
        {
-               self::expectException(BadRequestException::class);
+               $this->expectException(BadRequestException::class);
                Delete::rawContent();
        }
 
        public function testWrong()
        {
-               self::expectException(BadRequestException::class);
+               $this->expectException(BadRequestException::class);
                Delete::rawContent(['album' => 'album_name']);
        }
 
index 93c1cdc8661aa4b9d1ddee926a4c87a0eeb4b827..2e4de7a798124e435a75700d186ae819448070f6 100644 (file)
@@ -10,19 +10,19 @@ class UpdateTest extends ApiTest
 {
        public function testEmpty()
        {
-               self::expectException(BadRequestException::class);
+               $this->expectException(BadRequestException::class);
                Update::rawContent();
        }
 
        public function testTooFewArgs()
        {
-               self::expectException(BadRequestException::class);
+               $this->expectException(BadRequestException::class);
                Update::rawContent(['album' => 'album_name']);
        }
 
        public function testWrongUpdate()
        {
-               self::expectException(BadRequestException::class);
+               $this->expectException(BadRequestException::class);
                Update::rawContent(['album' => 'album_name', 'album_new' => 'album_name']);
        }