From fe5f1e4d67d999ed3c6ef78dc4d49f5dd1a93056 Mon Sep 17 00:00:00 2001 From: Klaus Weidenbach Date: Tue, 24 Oct 2017 16:33:58 +0200 Subject: :white_check_mark: Some unit test for include/language.php Unfortunately not so much unit testable, but added in preparation to update LanguageDetect and Intl library. --- tests/unit/includes/LanguageTest.php | 165 +++++++++++++++++++++++++++++++++++ 1 file changed, 165 insertions(+) create mode 100644 tests/unit/includes/LanguageTest.php (limited to 'tests/unit') diff --git a/tests/unit/includes/LanguageTest.php b/tests/unit/includes/LanguageTest.php new file mode 100644 index 000000000..789dbe80b --- /dev/null +++ b/tests/unit/includes/LanguageTest.php @@ -0,0 +1,165 @@ +getFunctionMock(__NAMESPACE__, 'get_config'); + //$gc->expects($this->once())->willReturn(10) + //$cg = $this->getFunctionMock('Zotlabs\Lib\Config', 'Get'); + //$cg->expects($this->once())->willReturn(10); + //$this->assertEquals($langCode, detect_language($text)); + + + // Can not unit test detect_language(), therefore test the used library + // only for now to find regressions on library updates. + require_once('library/langdet/Text/LanguageDetect.php'); + $l = new Text_LanguageDetect; + // return 2-letter ISO 639-1 (en) language code + $l->setNameMode(2); + $lng = $l->detectConfidence($text); + + $this->assertEquals($langCode, $lng['language']); + $this->assertEquals($confidence, round($lng['confidence'], 6)); + } + + public function languageExamplesProvider() { + return [ + 'empty text' => [ + '', + '', + null + ], + 'English' => [ + 'English is a West Germanic language that was first spoken in early medieval England and is now a global lingua franca.[4][5] Named after the Angles, one of the Germanic tribes that migrated to England, it ultimately derives its name from the Anglia (Angeln) peninsula in the Baltic Sea. It is closely related to the Frisian languages, but its vocabulary has been significantly influenced by other Germanic languages, particularly Norse (a North Germanic language), as well as by Latin and Romance languages, especially French.', + 'da', // nearly impossible to find a wikipedia article which is detected as english + 0.000367 // minimum confidence is checked in detect_language(), but that is not yet unit testable + ], + 'German' => [ + 'Deutschland ist ein Bundesstaat in Mitteleuropa. Er besteht aus 16 Ländern und ist als freiheitlich-demokratischer und sozialer Rechtsstaat verfasst. Die Bundesrepublik Deutschland stellt die jüngste Ausprägung des deutschen Nationalstaates dar. Mit rund 82,8 Millionen Einwohnern (31. Dezember 2016) zählt Deutschland zu den dicht besiedelten Flächenstaaten.', + 'de', + 0.134339 + ], + 'Norwegian' => [ + 'Kongeriket Norge er et nordisk, europeisk land og en selvstendig stat vest på Den skandinaviske halvøy. Landet er langt og smalt, og kysten strekker seg langs Nord-Atlanteren, hvor også Norges kjente fjorder befinner seg. Totalt dekker det relativt tynt befolkede landet 385 000 kvadratkilometer med litt over fem millioner innbyggere (2016).', + 'no', + 0.007076 + ] + ]; + } + + + /** + * @covers ::get_language_name + * @dataProvider getLanguageNameProvider + */ + public function testGetLanguageName($lang, $name, $trans) { + $this->assertEquals($name, get_language_name($lang)); + foreach ($trans as $k => $v) { + //echo "$k -> $v"; + $this->assertEquals($v, get_language_name($lang, $k)); + } + } + + public function getLanguageNameProvider() { + return [ + 'empty language code' => [ + '', + '', + ['de' => ''] + ], + 'invalid language code' => [ + 'zz', + 'zz', + ['de' => 'zz'] + ], + 'de' => [ + 'de', + 'German', + [ + 'de' => 'Deutsch', + 'nb' => 'tysk' + ] + ], + 'de-de' => [ + 'de-de', + 'German', + [ + 'de-de' => 'Deutsch', + 'nb' => 'Deutsch' // should be tysk, seems to be a bug upstream + ] + ], + 'en' => [ + 'en', + 'English', + [ + 'de' => 'Englisch', + 'nb' => 'engelsk' + ] + ], + 'en-gb' => [ + 'en-gb', + 'British English', + [ + 'de' => 'Britisches Englisch', + 'nb' => 'britisk engelsk' + ] + ], + 'en-au' => [ + 'en-au', + 'Australian English', + [ + 'de' => 'Australisches Englisch', + 'nb' => 'australsk engelsk' + ] + ], + 'nb' => [ + 'nb', + 'Norwegian Bokmål', + [ + 'de' => 'Norwegisch Bokmål', + 'nb' => 'norsk bokmål' + ] + ] + ]; + } +} \ No newline at end of file -- cgit v1.2.3 From 8e4c5db766ce23d05b8507991b04fece743147de Mon Sep 17 00:00:00 2001 From: Klaus Weidenbach Date: Wed, 25 Oct 2017 01:57:18 +0200 Subject: :arrow_up: Update Text_LanguageDetect. Update from v0.3.0 (2012) to v1.0.0 (2017) which should remove some warnings and improve PHP7 support. Using composer to handle this PEAR library now. Fix a problem in FeedutilsTest. --- tests/unit/includes/FeedutilsTest.php | 60 +++++++++++++++++++++++++++++++++++ tests/unit/includes/FeedutilsText.php | 55 -------------------------------- tests/unit/includes/LanguageTest.php | 5 ++- 3 files changed, 62 insertions(+), 58 deletions(-) create mode 100644 tests/unit/includes/FeedutilsTest.php delete mode 100644 tests/unit/includes/FeedutilsText.php (limited to 'tests/unit') diff --git a/tests/unit/includes/FeedutilsTest.php b/tests/unit/includes/FeedutilsTest.php new file mode 100644 index 000000000..d27df4939 --- /dev/null +++ b/tests/unit/includes/FeedutilsTest.php @@ -0,0 +1,60 @@ +assertEquals('id', normalise_id('id')); + $this->assertEquals('id', normalise_id('X-ZOT:id')); + $this->assertEquals('id id2', normalise_id('X-ZOT:id X-ZOT:id2')); + $this->assertEmpty(normalise_id('')); + } + + public function test_encode_rel_links() { + // invalid params return empty array + $this->assertEquals([], encode_rel_links('string')); + $this->assertEquals([], encode_rel_links([])); + + $b = ['attribs' => ['' => [ + 'rel' => 'rel_value', + 'type' => 'type_value', + 'href' => 'href_value', + 'length' => 'length_value', + 'title' => 'title_value' + ]]]; + $blink1 = ['link1' => $b]; + $bresult[] = $b['attribs']['']; + $this->assertEquals($bresult, encode_rel_links($blink1)); + } + +/* public function test_encode_rel_links_fail() { + $a = [ 'key' => 'value']; + $this->assertFalse(encode_rel_links($a)); + //Illegal string offset 'attribs' + }*/ + + /** + * @uses ::xmlify + */ + public function test_atom_author() { + $this->assertEquals('', atom_author('', 'nick', 'name', 'uri', 72, 72, 'png', 'photourl')); + + $a = ' + uri + nick + uri + + + nick + name +'; + + $this->assertXmlStringEqualsXmlString($a, atom_author('tag', 'nick', 'name', 'uri', 72, 72, 'png', 'http://photourl')); + } +} diff --git a/tests/unit/includes/FeedutilsText.php b/tests/unit/includes/FeedutilsText.php deleted file mode 100644 index 932a1b3a1..000000000 --- a/tests/unit/includes/FeedutilsText.php +++ /dev/null @@ -1,55 +0,0 @@ -assertEquals('id', normalise_id('id')); - $this->assertEquals('id', normalise_id('X-ZOT:id')); - $this->assertEquals('id id2', normalise_id('X-ZOT:id X-ZOT:id2')); - $this->assertEmpty(normalise_id('')); - } - - public function test_encode_rel_links() { - // invalid params return empty array - $this->assertEquals([], encode_rel_links('string')); - $this->assertEquals([], encode_rel_links([])); - - $b = ['attribs' => ['' => [ - 'rel' => 'rel_value', - 'type' => 'type_value', - 'href' => 'href_value' - ]]]; - $blink1 = ['link1' => $b]; - $bresult[] = $b['attribs']['']; - $this->assertEquals($bresult, encode_rel_links($blink1)); - } - -/* public function test_encode_rel_links_fail() { - $a = [ 'key' => 'value']; - $this->assertFalse(encode_rel_links($a)); - //Illegal string offset 'attribs' - }*/ - - public function test_atom_author() { - $this->assertEquals('', atom_author('', 'nick', 'name', 'uri', 72, 72, 'png', 'photourl')); - - $a = ' - uri - nick - uri - - - nick - name -'; - - $this->assertXmlStringEqualsXmlString($a, atom_author('tag', 'nick', 'name', 'uri', 72, 72, 'png', 'http://photourl')); - } -} diff --git a/tests/unit/includes/LanguageTest.php b/tests/unit/includes/LanguageTest.php index 789dbe80b..8bf9ca766 100644 --- a/tests/unit/includes/LanguageTest.php +++ b/tests/unit/includes/LanguageTest.php @@ -52,7 +52,6 @@ class LanguageTest extends UnitTestCase { // Can not unit test detect_language(), therefore test the used library // only for now to find regressions on library updates. - require_once('library/langdet/Text/LanguageDetect.php'); $l = new Text_LanguageDetect; // return 2-letter ISO 639-1 (en) language code $l->setNameMode(2); @@ -71,8 +70,8 @@ class LanguageTest extends UnitTestCase { ], 'English' => [ 'English is a West Germanic language that was first spoken in early medieval England and is now a global lingua franca.[4][5] Named after the Angles, one of the Germanic tribes that migrated to England, it ultimately derives its name from the Anglia (Angeln) peninsula in the Baltic Sea. It is closely related to the Frisian languages, but its vocabulary has been significantly influenced by other Germanic languages, particularly Norse (a North Germanic language), as well as by Latin and Romance languages, especially French.', - 'da', // nearly impossible to find a wikipedia article which is detected as english - 0.000367 // minimum confidence is checked in detect_language(), but that is not yet unit testable + 'en', + 0.078422 ], 'German' => [ 'Deutschland ist ein Bundesstaat in Mitteleuropa. Er besteht aus 16 Ländern und ist als freiheitlich-demokratischer und sozialer Rechtsstaat verfasst. Die Bundesrepublik Deutschland stellt die jüngste Ausprägung des deutschen Nationalstaates dar. Mit rund 82,8 Millionen Einwohnern (31. Dezember 2016) zählt Deutschland zu den dicht besiedelten Flächenstaaten.', -- cgit v1.2.3 From 66832c41e9fff481c20ca219b3cc0a4e53b8b551 Mon Sep 17 00:00:00 2001 From: Klaus Weidenbach Date: Wed, 25 Oct 2017 23:21:07 +0200 Subject: :arrow_up: Update intl library. Update intl library from v0.4? (2014) to v0.7.4 (2016). Use global composer autoloader now. --- tests/unit/includes/LanguageTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'tests/unit') diff --git a/tests/unit/includes/LanguageTest.php b/tests/unit/includes/LanguageTest.php index 8bf9ca766..dd71115c4 100644 --- a/tests/unit/includes/LanguageTest.php +++ b/tests/unit/includes/LanguageTest.php @@ -140,7 +140,7 @@ class LanguageTest extends UnitTestCase { 'British English', [ 'de' => 'Britisches Englisch', - 'nb' => 'britisk engelsk' + 'nb' => 'engelsk (Storbritannia)' ] ], 'en-au' => [ @@ -148,7 +148,7 @@ class LanguageTest extends UnitTestCase { 'Australian English', [ 'de' => 'Australisches Englisch', - 'nb' => 'australsk engelsk' + 'nb' => 'engelsk (Australia)' ] ], 'nb' => [ -- cgit v1.2.3 From 1be98d7b7fea30a929d27e7aa0b1412da3276b2c Mon Sep 17 00:00:00 2001 From: Klaus Weidenbach Date: Sat, 28 Oct 2017 01:22:59 +0200 Subject: :white_check_mark: Some more work on unit tests. Some small improvements for coverage report. Add more functions from include/text.php Update composer dev libraries for phpunit. --- tests/unit/Access/AccessListTest.php | 4 +++- tests/unit/Access/PermissionsTest.php | 4 ++-- tests/unit/includes/FeedutilsTest.php | 29 +++++++++++++++++++++++++ tests/unit/includes/TextTest.php | 40 ++++++++++++++++++++++++++++++++++- 4 files changed, 73 insertions(+), 4 deletions(-) (limited to 'tests/unit') diff --git a/tests/unit/Access/AccessListTest.php b/tests/unit/Access/AccessListTest.php index 3dbe5cd65..dbc19fabb 100644 --- a/tests/unit/Access/AccessListTest.php +++ b/tests/unit/Access/AccessListTest.php @@ -121,7 +121,9 @@ class AccessListTest extends UnitTestCase { /** * set_from_array() calls some other functions, too which are not yet unit tested. - * @uses ::perms2str() + * @uses ::perms2str + * @uses ::sanitise_acl + * @uses ::notags */ public function testSetFromArray() { // array diff --git a/tests/unit/Access/PermissionsTest.php b/tests/unit/Access/PermissionsTest.php index 93c641fb1..73d0e7827 100644 --- a/tests/unit/Access/PermissionsTest.php +++ b/tests/unit/Access/PermissionsTest.php @@ -57,7 +57,7 @@ class PermissionsTest extends UnitTestCase { [], ['perm1' => 0, 'perm2' => 0] ], - 'valild' => [ + 'valid' => [ [['perm1' => 1]], ['perm1' => 1, 'perm2' => 0] ] @@ -145,4 +145,4 @@ class PermissionsTest extends UnitTestCase { ] ]; } -} \ No newline at end of file +} diff --git a/tests/unit/includes/FeedutilsTest.php b/tests/unit/includes/FeedutilsTest.php index d27df4939..e9826a73d 100644 --- a/tests/unit/includes/FeedutilsTest.php +++ b/tests/unit/includes/FeedutilsTest.php @@ -57,4 +57,33 @@ class FeedutilsTest extends UnitTestCase { $this->assertXmlStringEqualsXmlString($a, atom_author('tag', 'nick', 'name', 'uri', 72, 72, 'png', 'http://photourl')); } + + /** + * @uses ::xmlify + */ + public function test_atom_render_author() { + $xchan = [ + 'xchan_addr' => 'chan@hub', + 'xchan_url' => 'http://hub', + 'xchan_name' => 'Chan', + 'xchan_photo_l' => 'http://hub/img', + 'xchan_photo_mimetype' => 'mimetype' + ]; + // There is no input validation in atom_render_author + //$this->assertEquals('', atom_render_author('', $xchan)); + + $a = ' + http://activitystrea.ms/schema/1.0/person + http://hub + chan + http://hub + + + + chan + Chan +'; + + $this->assertXmlStringEqualsXmlString($a, atom_render_author('tag', $xchan)); + } } diff --git a/tests/unit/includes/TextTest.php b/tests/unit/includes/TextTest.php index acc490001..97fa64895 100644 --- a/tests/unit/includes/TextTest.php +++ b/tests/unit/includes/TextTest.php @@ -5,7 +5,7 @@ namespace Zotlabs\Tests\Unit\includes; use Zotlabs\Tests\Unit\UnitTestCase; /** - * @brief Unit Test case for include/texter.php file. + * @brief Unit Test case for include/text.php file. * * @author ken restivo */ @@ -79,4 +79,42 @@ empty line above'; $this->assertEquals('
invalid position removed
', purify_html('
invalid position removed
', true)); } + /** + * @dataProvider notagsProvider + */ + public function testNotags($string, $expected) { + $this->assertEquals($expected, notags($string)); + } + public function notagsProvider() { + return [ + 'empty string' => ['', ''], + 'simple tag' => ['', '[value]'], + 'tag pair' => ['text', '[b]text[/b]'], + 'double angle bracket' => ['< ['>', '>'] + ]; + } + + /** + * @dataProvider sanitise_aclProvider + */ + public function testSanitise_acl($string, $expected) { + sanitise_acl($string); + $this->assertEquals($expected, $string); + } + public function sanitise_aclProvider() { + return [ + 'text' => ['value', ''], + 'text with angle bracket' => ['', '<[value]>'], + 'comma separated acls' => ['value1,value2', ''] + ]; + } + + public function testUnsetSanitise_acl() { + $empty = ''; + sanitise_acl($empty); + $this->assertTrue(isset($empty)); // unset() not working? Would expect false + $this->assertEmpty($empty); + } + } -- cgit v1.2.3 From c571ca40d7cb6642ce7243c3f6cc09b9c7a29ad8 Mon Sep 17 00:00:00 2001 From: Klaus Weidenbach Date: Tue, 31 Oct 2017 21:23:42 +0100 Subject: :white_check_mark: unit tests for Zotlabs\Access classes. --- tests/unit/Access/PermissionLimitsTest.php | 78 +++++++++ tests/unit/Access/PermissionRolesTest.php | 100 ++++++++++++ tests/unit/Access/PermissionsTest.php | 244 ++++++++++++++++++++++++----- 3 files changed, 387 insertions(+), 35 deletions(-) create mode 100644 tests/unit/Access/PermissionLimitsTest.php create mode 100644 tests/unit/Access/PermissionRolesTest.php (limited to 'tests/unit') diff --git a/tests/unit/Access/PermissionLimitsTest.php b/tests/unit/Access/PermissionLimitsTest.php new file mode 100644 index 000000000..58595111a --- /dev/null +++ b/tests/unit/Access/PermissionLimitsTest.php @@ -0,0 +1,78 @@ +getFunctionMock('Zotlabs\Access', 't'); + $t->expects($this->exactly($permsCount)); + + $stdlimits = PermissionLimits::Std_Limits(); + $this->assertCount($permsCount, $stdlimits, "There should be $permsCount permissions."); + + $this->assertEquals(PERMS_PUBLIC, $stdlimits['view_stream']); + $this->assertEquals(PERMS_SPECIFIC, $stdlimits['send_stream']); + $this->assertEquals(PERMS_PUBLIC, $stdlimits['view_profile']); + $this->assertEquals(PERMS_PUBLIC, $stdlimits['view_contacts']); + $this->assertEquals(PERMS_PUBLIC, $stdlimits['view_storage']); + $this->assertEquals(PERMS_SPECIFIC, $stdlimits['write_storage']); + $this->assertEquals(PERMS_PUBLIC, $stdlimits['view_pages']); + $this->assertEquals(PERMS_PUBLIC, $stdlimits['view_wiki']); + $this->assertEquals(PERMS_SPECIFIC, $stdlimits['write_pages']); + $this->assertEquals(PERMS_SPECIFIC, $stdlimits['write_wiki']); + $this->assertEquals(PERMS_SPECIFIC, $stdlimits['post_wall']); + $this->assertEquals(PERMS_PUBLIC, $stdlimits['post_comments']); + $this->assertEquals(PERMS_SPECIFIC, $stdlimits['post_mail']); + $this->assertEquals(PERMS_SPECIFIC, $stdlimits['post_like']); + $this->assertEquals(PERMS_SPECIFIC, $stdlimits['tag_deliver']); + $this->assertEquals(PERMS_SPECIFIC, $stdlimits['chat']); + $this->assertEquals(PERMS_SPECIFIC, $stdlimits['republish']); + $this->assertEquals(PERMS_SPECIFIC, $stdlimits['delegate']); + } + +} \ No newline at end of file diff --git a/tests/unit/Access/PermissionRolesTest.php b/tests/unit/Access/PermissionRolesTest.php new file mode 100644 index 000000000..5e64e773a --- /dev/null +++ b/tests/unit/Access/PermissionRolesTest.php @@ -0,0 +1,100 @@ +assertEquals($expectedVersion, PermissionRoles::version()); + + $pr = new PermissionRoles(); + $this->assertEquals($expectedVersion, $pr->version()); + } + + + public function testRoles() { + // Create a stub for global function t() with expectation + $t = $this->getFunctionMock('Zotlabs\Access', 't'); + $t->expects($this->atLeastOnce())->willReturnCallback( + function ($string) { + return $string; + } + ); + + $roles = PermissionRoles::roles(); + $r = new PermissionRoles(); + $this->assertEquals($roles, $r->roles()); + + $socialNetworking = [ + 'social' => 'Social - Mostly Public', + 'social_restricted' => 'Social - Restricted', + 'social_private' => 'Social - Private' + ]; + + $this->assertArraySubset(['Social Networking' => $socialNetworking], $roles); + $this->assertEquals($socialNetworking, $roles['Social Networking']); + + $this->assertCount(5, $roles, 'There should be 5 permission groups.'); + + $this->assertCount(1, $roles['Other'], "In the 'Other' group should be just one permission role"); + } + + + /** + * @uses ::call_hooks + * @uses Zotlabs\Access\PermissionLimits::Std_Limits + * @uses Zotlabs\Access\Permissions::Perms + */ + public function testRole_perms() { + // Create a stub for global function t() + $t = $this->getFunctionMock('Zotlabs\Access', 't'); + $t = $this->getFunctionMock('Zotlabs\Access', 'get_config'); + + $rp_social = PermissionRoles::role_perms('social'); + $this->assertEquals('social', $rp_social['role']); + + + $rp_custom = PermissionRoles::role_perms('custom'); + $this->assertEquals(['role' => 'custom'], $rp_custom); + + $rp_nonexistent = PermissionRoles::role_perms('nonexistent'); + $this->assertEquals(['role' => 'nonexistent'], $rp_nonexistent); + } + +} \ No newline at end of file diff --git a/tests/unit/Access/PermissionsTest.php b/tests/unit/Access/PermissionsTest.php index 73d0e7827..40724fff8 100644 --- a/tests/unit/Access/PermissionsTest.php +++ b/tests/unit/Access/PermissionsTest.php @@ -23,6 +23,7 @@ namespace Zotlabs\Tests\Unit\Access; +use phpmock\phpunit\PHPMock; use Zotlabs\Tests\Unit\UnitTestCase; use Zotlabs\Access\Permissions; @@ -33,54 +34,228 @@ use Zotlabs\Access\Permissions; */ class PermissionsTest extends UnitTestCase { + use PHPMock; + + public function testVersion() { + $expectedVersion = 2; + + // static call + $this->assertEquals($expectedVersion, Permissions::version()); + + // instance call + $p = new Permissions(); + $this->assertEquals($expectedVersion, $p->version()); + } + + /** + * @coversNothing + */ + public function testVersionEqualsPermissionRoles() { + $p = new Permissions(); + $pr = new \Zotlabs\Access\PermissionRoles(); + $this->assertEquals($p->version(), $pr->version()); + } + + /** + * @uses ::call_hooks + */ + public function testPerms() { + // There are 18 default perms + $permsCount = 18; + + // Create a stub for global function t() with expectation + $t = $this->getFunctionMock('Zotlabs\Access', 't'); + $t->expects($this->exactly(2*$permsCount))->willReturnCallback( + function ($string) { + return $string; + } + ); + + // static method Perms() + $perms = Permissions::Perms(); + + $p = new Permissions(); + $this->assertEquals($perms, $p->Perms()); + + $this->assertEquals($permsCount, count($perms), "There should be $permsCount permissions."); + + $this->assertEquals('Can view my channel stream and posts', $perms['view_stream']); + + // non existent perm should not be set + $this->assertFalse(isset($perms['invalid_perm'])); + } + + /** + * filter parmeter is only used in hook \b permissions_list. So the result + * in this test should be the same as if there was no filter parameter. + * + * @todo Stub call_hooks() function and also test filter + * + * @uses ::call_hooks + */ + public function testPermsFilter() { + // There are 18 default perms + $permsCount = 18; + + // Create a stub for global function t() with expectation + $t = $this->getFunctionMock('Zotlabs\Access', 't'); + $t->expects($this->exactly(2*$permsCount))->willReturnCallback( + function ($string) { + return $string; + } + ); + + $perms = Permissions::Perms('view_'); + $this->assertEquals($permsCount, count($perms)); + + $this->assertEquals('Can view my channel stream and posts', $perms['view_stream']); + + $perms = Permissions::Perms('invalid_perm'); + $this->assertEquals($permsCount, count($perms)); + } + /** + * Better should mock Permissions::Perms, but not possible with static methods. + * + * @uses ::call_hooks + * * @dataProvider FilledPermsProvider + * + * @param array $permarr An indexed permissions array to pass + * @param array $expected The expected result perms array */ public function testFilledPerms($permarr, $expected) { - $this->markTestIncomplete( - 'Need to mock static function Permissions::Perms() ...' - ); - //$this->assertEquals($expected, Permissions::FilledPerms($permarr)); - -/* $perms = $this->getMockBuilder(Permissions::class) - ->setMethods(['Perms']) - ->getMock(); - $perms->expects($this->once()) - ->method('Perms'); - // still calls the static self::Perms() - $perms->FilledPerms($permarr); -*/ + // Create a stub for global function t() + $t = $this->getFunctionMock('Zotlabs\Access', 't'); + + $this->assertEquals($expected, Permissions::FilledPerms($permarr)); } + /** + * @return array An associative array with test values for FilledPerms() + * * \e array Indexed array which is passed as parameter to FilledPerms() + * * \e array Expected associative result array with filled perms + */ public function FilledPermsProvider() { return [ - 'empty' => [ + 'Empty param array' => [ [], - ['perm1' => 0, 'perm2' => 0] + [ + 'view_stream' => 0, + 'send_stream' => 0, + 'view_profile' => 0, + 'view_contacts' => 0, + 'view_storage' => 0, + 'write_storage' => 0, + 'view_pages' => 0, + 'view_wiki' => 0, + 'write_pages' => 0, + 'write_wiki' => 0, + 'post_wall' => 0, + 'post_comments' => 0, + 'post_mail' => 0, + 'post_like' => 0, + 'tag_deliver' => 0, + 'chat' => 0, + 'republish' => 0, + 'delegate' => 0 + ] ], - 'valid' => [ - [['perm1' => 1]], - ['perm1' => 1, 'perm2' => 0] + 'provide view_stream and view_pages as param' => [ + ['view_stream', 'view_pages'], + [ + 'view_stream' => 1, + 'send_stream' => 0, + 'view_profile' => 0, + 'view_contacts' => 0, + 'view_storage' => 0, + 'write_storage' => 0, + 'view_pages' => 1, + 'view_wiki' => 0, + 'write_pages' => 0, + 'write_wiki' => 0, + 'post_wall' => 0, + 'post_comments' => 0, + 'post_mail' => 0, + 'post_like' => 0, + 'tag_deliver' => 0, + 'chat' => 0, + 'republish' => 0, + 'delegate' => 0 + ] + ], + 'provide an unknown param' => [ + ['view_stream', 'unknown_perm'], + [ + 'view_stream' => 1, + 'send_stream' => 0, + 'view_profile' => 0, + 'view_contacts' => 0, + 'view_storage' => 0, + 'write_storage' => 0, + 'view_pages' => 0, + 'view_wiki' => 0, + 'write_pages' => 0, + 'write_wiki' => 0, + 'post_wall' => 0, + 'post_comments' => 0, + 'post_mail' => 0, + 'post_like' => 0, + 'tag_deliver' => 0, + 'chat' => 0, + 'republish' => 0, + 'delegate' => 0 + ] ] ]; } -/* public function testFilledPermsNull() { - // need to mock global function btlogger(); - Permissions::FilledPerms(null); + /** + * @uses ::call_hooks + */ + public function testFilledPermsNull() { + // Create a stub for global function t() with expectation + $t = $this->getFunctionMock('Zotlabs\Access', 't'); + $t->expects($this->atLeastOnce()); + // Create a stub for global function bt() with expectations + $bt = $this->getFunctionMock('Zotlabs\Access', 'btlogger'); + $bt->expects($this->once())->with($this->equalTo('FilledPerms: null')); + + $result = [ + 'view_stream' => 0, + 'send_stream' => 0, + 'view_profile' => 0, + 'view_contacts' => 0, + 'view_storage' => 0, + 'write_storage' => 0, + 'view_pages' => 0, + 'view_wiki' => 0, + 'write_pages' => 0, + 'write_wiki' => 0, + 'post_wall' => 0, + 'post_comments' => 0, + 'post_mail' => 0, + 'post_like' => 0, + 'tag_deliver' => 0, + 'chat' => 0, + 'republish' => 0, + 'delegate' => 0 + ]; + + $this->assertEquals($result, Permissions::FilledPerms(null)); } -*/ + /** * @dataProvider OPermsProvider * - * @param array $permarr - * @param array $expected + * @param array $permarr The params to pass to the OPerms method + * @param array $expected The expected result */ public function testOPerms($permarr, $expected) { $this->assertEquals($expected, Permissions::OPerms($permarr)); } /** - * @return Associative array with test values for OPerms() - * * \e array Array to test - * * \e array Expect array + * @return array An associative array with test values for OPerms() + * * \e array Array with perms to test + * * \e array Expected result array */ public function OPermsProvider() { return [ @@ -99,22 +274,21 @@ class PermissionsTest extends UnitTestCase { ]; } - /** * @dataProvider permsCompareProvider * - * @param array $p1 - * @param array $p2 - * @param boolean $expectedresult + * @param array $p1 The first permission + * @param array $p2 The second permission + * @param boolean $expectedresult The expected result of the tested method */ public function testPermsCompare($p1, $p2, $expectedresult) { $this->assertEquals($expectedresult, Permissions::PermsCompare($p1, $p2)); } /** - * @return Associative array with test values for PermsCompare() - * * \e array 1st array - * * \e array 2nd array - * * \e boolean expected result for the test + * @return array An associative array with test values for PermsCompare() + * * \e array 1st array with perms + * * \e array 2nd array with perms + * * \e boolean expected result for the perms comparison */ public function permsCompareProvider() { return [ -- cgit v1.2.3 From 063654373d4dd560c8cc4f5ca9b9e74fc06332fd Mon Sep 17 00:00:00 2001 From: Klaus Weidenbach Date: Sat, 25 Nov 2017 19:01:53 +0100 Subject: Fix a PHP error in a unit test for PHP7.2. --- tests/unit/AutonameTest.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'tests/unit') diff --git a/tests/unit/AutonameTest.php b/tests/unit/AutonameTest.php index 9f92f736f..33e237bb9 100644 --- a/tests/unit/AutonameTest.php +++ b/tests/unit/AutonameTest.php @@ -57,7 +57,7 @@ class AutonameTest extends TestCase { // public function testAutonameMaxLength() { // $autoname2=autoname(PHP_INT_MAX); - // $this->assertEquals(PHP_INT_MAX, count($autoname2)); + // $this->assertEquals(PHP_INT_MAX, strlen($autoname2)); // } /** @@ -65,14 +65,14 @@ class AutonameTest extends TestCase { */ public function testAutonameLength1() { $autoname1=autoname(1); - $this->assertEquals(1, count($autoname1)); + $this->assertEquals(1, strlen($autoname1)); $autoname2=autoname(1); - $this->assertEquals(1, count($autoname2)); + $this->assertEquals(1, strlen($autoname2)); // The following test is problematic, with only 26 possibilities // generating the same thing twice happens often aka // birthday paradox // $this->assertFalse($autoname1==$autoname2); } -} \ No newline at end of file +} -- cgit v1.2.3