From 7cb8ecd36b128d07b5086904af48601b9096cdea Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Wed, 27 Dec 2023 13:08:41 +0100 Subject: tests: Enable and fix incomplete tests. Since the tests now have access to the database, there's no longer any need for mocking or stubbing functions that uses the db. As the codebase relies on global state and static class functions (aka classes as namespace), stubbing these are not straight forward and complicates the test code, and reliability. With access to the database, we can now perform these tests by populating the db tables with the content relevant for the test, and perform the actual tests to verify that the code behaves as expected given the db content we've created. In the tests I enabled in this patch, I've explicitly set the expected configuration in the db using the available API's in the code itself. While it would also be possible to add more permanent fixtures to set the db in the expected state, doing it dynamically like this has some advantages. It allows very specific setups for the test that we may not want to be set of every test. The photo factory test to ignore Image Magick is a good example of that. We may want to also test the opposite, that Image Magick is selected if the flag is not set (and the extention is available.) Also, the fixtures themselves are not more informative than the table column names they contain, so comments would be needed to document both the content and how it fits into the test environment. With the more dynamic approach the test code becomes more self documenting. --- tests/unit/Lib/PermissionDescriptionTest.php | 47 ++++++++++++++++++++++++++-- tests/unit/includes/PhotodriverTest.php | 28 ++++------------- 2 files changed, 50 insertions(+), 25 deletions(-) diff --git a/tests/unit/Lib/PermissionDescriptionTest.php b/tests/unit/Lib/PermissionDescriptionTest.php index 96c381d0c..fdd676f61 100644 --- a/tests/unit/Lib/PermissionDescriptionTest.php +++ b/tests/unit/Lib/PermissionDescriptionTest.php @@ -63,11 +63,52 @@ class PermissionDescriptionTest extends UnitTestCase { $this->assertNotNull($permDescSelf); } + /** + * Test fetching permission descriptions for the current channel. + */ public function testFromGlobalPermission() { - //$permDesc = PermissionDescription::fromGlobalPermission('view_profile'); + // Initiate the global App with a channel_id + \App::$channel = array( + 'channel_id' => 42, + ); + + // Make sure the requested permission is set for this channel. + \Zotlabs\Access\PermissionLimits::Set( + \App::$channel['channel_id'], + 'view_profile', + PERMS_NETWORK + ); + + \Zotlabs\Access\PermissionLimits::Set( + \App::$channel['channel_id'], + 'write_storage', + PERMS_SPECIFIC + ); + + // Set an invalid(?) permission + \Zotlabs\Access\PermissionLimits::Set( + \App::$channel['channel_id'], + 'view_wiki', + 1337 + ); + + $permDesc = PermissionDescription::fromGlobalPermission('view_profile'); + $this->assertEquals( + 'Anybody in the Hubzilla network', + $permDesc->get_permission_description() + ); + + $permDesc = PermissionDescription::fromGlobalPermission('write_storage'); + $this->assertEquals( + 'Only connections I specifically allow', + $permDesc->get_permission_description() + ); - $this->markTestIncomplete( - 'The method fromGlobalPermission() is not yet testable ...' + // Permissions we don't know about will get the fallback description. + $permDesc = PermissionDescription::fromGlobalPermission('view_wiki'); + $this->assertEquals( + 'Visible to your default audience', + $permDesc->get_permission_description() ); } diff --git a/tests/unit/includes/PhotodriverTest.php b/tests/unit/includes/PhotodriverTest.php index 6f6ad0ffe..34dc058b7 100644 --- a/tests/unit/includes/PhotodriverTest.php +++ b/tests/unit/includes/PhotodriverTest.php @@ -2,38 +2,22 @@ namespace Zotlabs\Tests\Unit\includes; -//use Zotlabs\Photo\PhotoGd; use Zotlabs\Tests\Unit\UnitTestCase; -//use phpmock\phpunit\PHPMock; /** * @brief Unit Test cases for include/photo/photo_driver.php file. */ class PhotodriverTest extends UnitTestCase { - //use PHPMock; public function testPhotofactoryReturnsNullForUnsupportedType() { - // php-mock can not mock global functions which is called by a global function. - // If the calling function is in a namespace it would work. - //$logger = $this->getFunctionMock(__NAMESPACE__, 'logger'); - //$logger->expects($this->once()); - - //$ph = \photo_factory('', 'image/bmp'); - //$this->assertNull($ph); - - $this->markTestIncomplete('Need to mock logger(), otherwise not unit testable.'); + $photo = \photo_factory('', 'image/bmp'); + $this->assertNull($photo); } public function testPhotofactoryReturnsPhotogdIfConfigIgnore_imagickIsSet() { - // php-mock can not mock global functions which is called by a global function. - // If the calling function is in a namespace it would work. - //$gc = $this->getFunctionMock(__NAMESPACE__, 'get_config'); - // simulate get_config('system', 'ignore_imagick') configured - //$gc->expects($this->once())->willReturn(1) - - //$ph = \photo_factory(file_get_contents('images/hz-16.png'), 'image/png'); - //$this->assertInstanceOf(PhotoGd::class, $ph); + \Zotlabs\Lib\Config::Set('system', 'ignore_imagick', true); - $this->markTestIncomplete('Need to mock get_config(), otherwise not unit testable.'); + $photo = \photo_factory(file_get_contents('images/hz-16.png'), 'image/png'); + $this->assertInstanceOf('Zotlabs\Photo\PhotoGd', $photo); } -} \ No newline at end of file +} -- cgit v1.2.3