From: Philipp <admin@philipp.info>
Date: Fri, 12 Nov 2021 20:08:36 +0000 (+0100)
Subject: Make API testable & move PhotoAlbum tests to new destination
X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=e477cf215dace8f84c856952d865df28a182c83e;p=friendica.git

Make API testable & move PhotoAlbum tests to new destination
---

diff --git a/src/Module/Api/Friendica/Photo/Delete.php b/src/Module/Api/Friendica/Photo/Delete.php
index 13ea7ebe7a..cf287d3d33 100644
--- a/src/Module/Api/Friendica/Photo/Delete.php
+++ b/src/Module/Api/Friendica/Photo/Delete.php
@@ -44,7 +44,7 @@ class Delete extends BaseApi
 
 		// do several checks on input parameters
 		// we do not allow calls without photo id
-		if ($request['photo_id'] == null) {
+		if (empty($request['photo_id'])) {
 			throw new BadRequestException("no photo_id specified");
 		}
 
diff --git a/src/Module/Api/Friendica/Photoalbum/Update.php b/src/Module/Api/Friendica/Photoalbum/Update.php
index 9b75596fa7..369d33e825 100644
--- a/src/Module/Api/Friendica/Photoalbum/Update.php
+++ b/src/Module/Api/Friendica/Photoalbum/Update.php
@@ -43,10 +43,10 @@ class Update extends BaseApi
 		]);
 
 		// we do not allow calls without album string
-		if ($request['album'] == "") {
+		if (empty($request['album'])) {
 			throw new BadRequestException("no albumname specified");
 		}
-		if ($request['album_new'] == "") {
+		if (empty($request['album_new'])) {
 			throw new BadRequestException("no new albumname specified");
 		}
 		// check if album is existing
diff --git a/src/Security/Authentication.php b/src/Security/Authentication.php
index b570af7802..0b2fc9f9cf 100644
--- a/src/Security/Authentication.php
+++ b/src/Security/Authentication.php
@@ -65,6 +65,18 @@ class Authentication
 	/** @var IManagePersonalConfigValues */
 	private $pConfig;
 
+	/**
+	 * Sets the X-Account-Management-Status header
+	 *
+	 * mainly extracted to make it overridable for tests
+	 *
+	 * @param array $user_record
+	 */
+	protected function setXAccMgmtStatusHeader(array $user_record)
+	{
+		header('X-Account-Management-Status: active; name="' . $user_record['username'] . '"; id="' . $user_record['nickname'] . '"');
+	}
+
 	/**
 	 * Authentication constructor.
 	 *
@@ -314,7 +326,7 @@ class Authentication
 			$this->session->set('cid', $contact['id']);
 		}
 
-		header('X-Account-Management-Status: active; name="' . $user_record['username'] . '"; id="' . $user_record['nickname'] . '"');
+		$this->setXAccMgmtStatusHeader($user_record);
 
 		if ($login_initial || $login_refresh) {
 			$this->dba->update('user', ['login_date' => DateTimeFormat::utcNow()], ['uid' => $user_record['uid']]);
diff --git a/tests/FixtureTest.php b/tests/FixtureTest.php
index 17e606a9e6..17760c85d4 100644
--- a/tests/FixtureTest.php
+++ b/tests/FixtureTest.php
@@ -26,7 +26,7 @@ abstract class FixtureTest extends DatabaseTest
 	/**
 	 * Create variables used by tests.
 	 */
-	protected function setUp() : void
+	protected function setUp(): void
 	{
 		parent::setUp();
 
diff --git a/tests/Util/AuthenticationDouble.php b/tests/Util/AuthenticationDouble.php
new file mode 100644
index 0000000000..c2969394f4
--- /dev/null
+++ b/tests/Util/AuthenticationDouble.php
@@ -0,0 +1,13 @@
+<?php
+
+namespace Friendica\Test\Util;
+
+use Friendica\Security\Authentication;
+
+class AuthenticationDouble extends Authentication
+{
+	protected function setXAccMgmtStatusHeader(array $user_record)
+	{
+		// Don't set any header..
+	}
+}
diff --git a/tests/Util/authtest/authtest.php b/tests/Util/authtest/authtest.php
new file mode 100644
index 0000000000..cec0c066ed
--- /dev/null
+++ b/tests/Util/authtest/authtest.php
@@ -0,0 +1,21 @@
+<?php
+/**
+ * Name: TEST-ADDON: Authentication "allow all"
+ * Description: For testing purpose only
+ * Version: 1.0
+ * Author: Philipp Holzer <admin@philipp.info>
+ */
+
+use Friendica\Core\Hook;
+use Friendica\Model\User;
+
+function authtest_install()
+{
+	Hook::register('authenticate', 'tests/Util/authtest/authtest.php', 'authtest_authenticate');
+}
+
+function authtest_authenticate($a,&$b)
+{
+	$b['authenticated'] = 1;
+	$b['user_record']   = User::getById(42);
+}
diff --git a/tests/legacy/ApiTest.php b/tests/legacy/ApiTest.php
index 6bf144463a..645c078a46 100644
--- a/tests/legacy/ApiTest.php
+++ b/tests/legacy/ApiTest.php
@@ -3178,97 +3178,6 @@ class ApiTest extends FixtureTest
 		$this->markTestIncomplete('exit() kills phpunit as well');
 	}
 
-	/**
-	 * Test the api_fr_photoalbum_delete() function.
-	 *
-	 * @return void
-	 */
-	public function testApiFrPhotoalbumDelete()
-	{
-		// $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
-		// api_fr_photoalbum_delete('json');
-	}
-
-	/**
-	 * Test the api_fr_photoalbum_delete() function with an album name.
-	 *
-	 * @return void
-	 */
-	public function testApiFrPhotoalbumDeleteWithAlbum()
-	{
-		// $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
-		// $_REQUEST['album'] = 'album_name';
-		// api_fr_photoalbum_delete('json');
-	}
-
-	/**
-	 * Test the api_fr_photoalbum_delete() function with an album name.
-	 *
-	 * @return void
-	 */
-	public function testApiFrPhotoalbumDeleteWithValidAlbum()
-	{
-		$this->markTestIncomplete('We need to add a dataset for this.');
-	}
-
-	/**
-	 * Test the api_fr_photoalbum_delete() function.
-	 *
-	 * @return void
-	 */
-	public function testApiFrPhotoalbumUpdate()
-	{
-		// $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
-		// api_fr_photoalbum_update('json');
-	}
-
-	/**
-	 * Test the api_fr_photoalbum_delete() function with an album name.
-	 *
-	 * @return void
-	 */
-	public function testApiFrPhotoalbumUpdateWithAlbum()
-	{
-		// $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
-		// $_REQUEST['album'] = 'album_name';
-		// api_fr_photoalbum_update('json');
-	}
-
-	/**
-	 * Test the api_fr_photoalbum_delete() function with an album name.
-	 *
-	 * @return void
-	 */
-	public function testApiFrPhotoalbumUpdateWithAlbumAndNewAlbum()
-	{
-		// $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
-		// $_REQUEST['album']     = 'album_name';
-		// $_REQUEST['album_new'] = 'album_name';
-		// api_fr_photoalbum_update('json');
-	}
-
-	/**
-	 * Test the api_fr_photoalbum_update() function without an authenticated user.
-	 *
-	 * @return void
-	 */
-	public function testApiFrPhotoalbumUpdateWithoutAuthenticatedUser()
-	{
-		// $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
-		// $_SESSION['authenticated'] = false;
-		// api_fr_photoalbum_update('json');
-	}
-
-	/**
-	 * Test the api_fr_photoalbum_delete() function with an album name.
-	 *
-	 * @return void
-	 */
-	public function testApiFrPhotoalbumUpdateWithValidAlbum()
-	{
-		$this->markTestIncomplete('We need to add a dataset for this.');
-	}
-
 	/**
 	 * Test the api_fr_photos_list() function.
 	 *
diff --git a/tests/src/Module/Api/ApiTest.php b/tests/src/Module/Api/ApiTest.php
new file mode 100644
index 0000000000..b2c38c31e6
--- /dev/null
+++ b/tests/src/Module/Api/ApiTest.php
@@ -0,0 +1,58 @@
+<?php
+
+namespace Friendica\Test\src\Module\Api;
+
+use Dice\Dice;
+use Friendica\Core\Addon;
+use Friendica\Core\Hook;
+use Friendica\Database\Database;
+use Friendica\DI;
+use Friendica\Security\Authentication;
+use Friendica\Test\FixtureTest;
+use Friendica\Test\Util\AuthenticationDouble;
+
+class ApiTest extends FixtureTest
+{
+	protected function setUp(): void
+	{
+		parent::setUp(); // TODO: Change the autogenerated stub
+
+		$this->dice = $this->dice
+			->addRule(Authentication::class, ['instanceOf' => AuthenticationDouble::class, 'shared' => true]);
+		DI::init($this->dice);
+
+		$this->installAuthTest();
+	}
+
+	/**
+	 * installs auththest.
+	 *
+	 * @throws \Exception
+	 */
+	public function installAuthTest()
+	{
+		$addon           = 'authtest';
+		$addon_file_path = __DIR__ . '/../../../Util/authtest/authtest.php';
+		$t               = @filemtime($addon_file_path);
+
+		@include_once($addon_file_path);
+		if (function_exists($addon . '_install')) {
+			$func = $addon . '_install';
+			$func(DI::app());
+		}
+
+		/** @var Database $dba */
+		$dba = $this->dice->create(Database::class);
+
+		$dba->insert('addon', [
+			'name'         => $addon,
+			'installed'    => true,
+			'timestamp'    => $t,
+			'plugin_admin' => function_exists($addon . '_addon_admin'),
+			'hidden'       => file_exists('addon/' . $addon . '/.hidden')
+		]);
+
+		Addon::loadAddons();
+		Hook::loadHooks();
+	}
+}
diff --git a/tests/src/Module/Api/Friendica/Photo/DeleteTest.php b/tests/src/Module/Api/Friendica/Photo/DeleteTest.php
new file mode 100644
index 0000000000..69e9dbd314
--- /dev/null
+++ b/tests/src/Module/Api/Friendica/Photo/DeleteTest.php
@@ -0,0 +1,27 @@
+<?php
+
+namespace Friendica\Test\src\Module\Api\Friendica\Photo;
+
+use Friendica\Module\Api\Friendica\Photoalbum\Delete;
+use Friendica\Network\HTTPException\BadRequestException;
+use Friendica\Test\src\Module\Api\ApiTest;
+
+class DeleteTest extends ApiTest
+{
+	public function testEmpty()
+	{
+		self::expectException(BadRequestException::class);
+		Delete::rawContent();
+	}
+
+	public function testWrong()
+	{
+		self::expectException(BadRequestException::class);
+		Delete::rawContent(['album' => 'album_name']);
+	}
+
+	public function testValid()
+	{
+		self::markTestIncomplete('We need to add a dataset for this.');
+	}
+}
diff --git a/tests/src/Module/Api/Friendica/Photo/UpdateTest.php b/tests/src/Module/Api/Friendica/Photo/UpdateTest.php
new file mode 100644
index 0000000000..f233998731
--- /dev/null
+++ b/tests/src/Module/Api/Friendica/Photo/UpdateTest.php
@@ -0,0 +1,38 @@
+<?php
+
+namespace Friendica\Test\src\Module\Api\Friendica\Photo;
+
+use Friendica\Module\Api\Friendica\Photoalbum\Update;
+use Friendica\Network\HTTPException\BadRequestException;
+use Friendica\Test\src\Module\Api\ApiTest;
+
+class UpdateTest extends ApiTest
+{
+	public function testEmpty()
+	{
+		self::expectException(BadRequestException::class);
+		Update::rawContent();
+	}
+
+	public function testTooFewArgs()
+	{
+		self::expectException(BadRequestException::class);
+		Update::rawContent(['album' => 'album_name']);
+	}
+
+	public function testWrongUpdate()
+	{
+		self::expectException(BadRequestException::class);
+		Update::rawContent(['album' => 'album_name', 'album_new' => 'album_name']);
+	}
+
+	public function testUpdateWithoutAuthenticatedUser()
+	{
+		self::markTestIncomplete('Needs BasicAuth as dynamic method for overriding first');
+	}
+
+	public function testValid()
+	{
+		self::markTestIncomplete('We need to add a dataset for this.');
+	}
+}