From b0b0973e2dce0f430639aaf73b0fc538e1df44ab Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Sun, 2 Jul 2023 19:12:09 +0200 Subject: unit tests: Cosmetic fixes in AntiXSSTest.php - Start comments with capital letter - Fix indentation - Add missing doc comments --- tests/unit/AntiXSSTest.php | 54 +++++++++++++++++++++++++--------------------- 1 file changed, 29 insertions(+), 25 deletions(-) (limited to 'tests') diff --git a/tests/unit/AntiXSSTest.php b/tests/unit/AntiXSSTest.php index 09642726f..f4ee984bc 100644 --- a/tests/unit/AntiXSSTest.php +++ b/tests/unit/AntiXSSTest.php @@ -1,6 +1,6 @@ '; @@ -24,28 +24,30 @@ class AntiXSSTest extends TestCase { $this->assertEquals("<submit type="button" onclick="alert('failed!');" />", $escapedString); } - /** - * @dataProvider urlTestProvider - */ - public function testEscapeURL($url, $expected) : void { - $this->assertEquals($expected, escape_url($url)); - } - - public function urlTestProvider() : array { - return [ - [ - "https://example.com/settings/calendar/?f=&rpath=https://example.com/cdav/calendar'>", - "https://example.com/settings/calendar/?f=&rpath=https://example.com/cdav/calendar'><script>alert('boom')</script>" - ], - [ - "settings/calendar/?f=&rpath=https://example.com'+accesskey=x+onclick=alert(/boom/);a='", - "settings/calendar/?f=&rpath=https://example.com'+accesskey=x+onclick=alert(/boom/);a='" - ], - ]; - } + /** + * Test escaping URL's to make them safe for use in html and attributes. + * + * @dataProvider urlTestProvider + */ + public function testEscapeURL($url, $expected) : void { + $this->assertEquals($expected, escape_url($url)); + } + + public function urlTestProvider() : array { + return [ + [ + "https://example.com/settings/calendar/?f=&rpath=https://example.com/cdav/calendar'>", + "https://example.com/settings/calendar/?f=&rpath=https://example.com/cdav/calendar'><script>alert('boom')</script>" + ], + [ + "settings/calendar/?f=&rpath=https://example.com'+accesskey=x+onclick=alert(/boom/);a='", + "settings/calendar/?f=&rpath=https://example.com'+accesskey=x+onclick=alert(/boom/);a='" + ], + ]; + } /** - *xmlify and unxmlify + * Test xmlify and unxmlify */ public function testXmlify() { $text="I want to break\n this!11!"; @@ -56,7 +58,7 @@ class AntiXSSTest extends TestCase { } /** - * xmlify and put in a document + * Test xmlify and put in a document */ public function testXmlifyDocument() { $tag="I want to break"; @@ -65,7 +67,9 @@ class AntiXSSTest extends TestCase { $xml_parser=xml_parser_create(); //should be possible to parse it - $values=array(); $index=array(); + $values=array(); + $index=array(); + $this->assertEquals(1, xml_parse_into_struct($xml_parser, $text, $values, $index)); $this->assertEquals(array('TEXT'=>array(0)), @@ -77,7 +81,7 @@ class AntiXSSTest extends TestCase { } /** - * test hex2bin and reverse + * Test hex2bin and reverse */ public function testHex2Bin() { $this->assertEquals(-3, hex2bin(bin2hex(-3))); -- cgit v1.2.3 From 7bd2de996750830aca191853345e0b798d68f16a Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Sun, 2 Jul 2023 19:33:23 +0200 Subject: unit-tests: There's no need to test builtin func hex2bin --- tests/unit/AntiXSSTest.php | 10 ---------- 1 file changed, 10 deletions(-) (limited to 'tests') diff --git a/tests/unit/AntiXSSTest.php b/tests/unit/AntiXSSTest.php index f4ee984bc..6b24cc9e3 100644 --- a/tests/unit/AntiXSSTest.php +++ b/tests/unit/AntiXSSTest.php @@ -80,16 +80,6 @@ class AntiXSSTest extends TestCase { xml_parser_free($xml_parser); } - /** - * Test hex2bin and reverse - */ - public function testHex2Bin() { - $this->assertEquals(-3, hex2bin(bin2hex(-3))); - $this->assertEquals(0, hex2bin(bin2hex(0))); - $this->assertEquals(12, hex2bin(bin2hex(12))); - $this->assertEquals(PHP_INT_MAX, hex2bin(bin2hex(PHP_INT_MAX))); - } - //function qp, quick and dirty?? //get_mentions //get_contact_block, bis Zeile 538 -- cgit v1.2.3 From 6f13cabfba7fb3b64ccc9647f1c58ad4a8ec5286 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Wed, 5 Jul 2023 20:44:49 +0200 Subject: tests: Don't explicitly require CUT in AntiXSSTest. Rely on PHPUnit config to load the app and the code under test. --- tests/unit/AntiXSSTest.php | 2 -- 1 file changed, 2 deletions(-) (limited to 'tests') diff --git a/tests/unit/AntiXSSTest.php b/tests/unit/AntiXSSTest.php index 6b24cc9e3..1de9d54cd 100644 --- a/tests/unit/AntiXSSTest.php +++ b/tests/unit/AntiXSSTest.php @@ -7,8 +7,6 @@ use PHPUnit\Framework\TestCase; -require_once('include/text.php'); - class AntiXSSTest extends TestCase { /** -- cgit v1.2.3 From 9beceb9b020363f1d85a69177c1cb1a484a6cec5 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Wed, 5 Jul 2023 20:46:40 +0200 Subject: tests: Cleanup Autoname test. Not sure if this test is actually useful, but leave it for now. --- tests/unit/AutonameTest.php | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) (limited to 'tests') diff --git a/tests/unit/AutonameTest.php b/tests/unit/AutonameTest.php index 566fe6149..b79a600c5 100644 --- a/tests/unit/AutonameTest.php +++ b/tests/unit/AutonameTest.php @@ -1,24 +1,21 @@ Date: Wed, 5 Jul 2023 20:47:45 +0200 Subject: tests: Cleanup ContainsAttribute test. Mostly cosmetic to satisfy phpcs + remove explicit require. --- tests/unit/ContainsAttributeTest.php | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) (limited to 'tests') diff --git a/tests/unit/ContainsAttributeTest.php b/tests/unit/ContainsAttributeTest.php index 0930d9837..db9618826 100644 --- a/tests/unit/ContainsAttributeTest.php +++ b/tests/unit/ContainsAttributeTest.php @@ -1,24 +1,21 @@ assertFalse(attribute_contains($testAttr, "class2")); } -} \ No newline at end of file +} -- cgit v1.2.3 From e80d3653c9e2aac2b114eb1c77b1e717dc90efb9 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Wed, 5 Jul 2023 20:48:39 +0200 Subject: test: Enable and cleanup GetTagsTest. These tests actually tests both the get_tags() function, and the handle_tags() function. The latter don't work, and should probably be split into it's own test cases. There's also some stubs here that we may want to deal with in another way. Also removed meaningless "test" of intval() builtin function. It behaves as documented, no need for us to test it. --- tests/unit/GetTagsTest.php | 281 ++++++++++++++++++++++++++++++++++++++ tests/unit/get_tags_test.php | 317 ------------------------------------------- 2 files changed, 281 insertions(+), 317 deletions(-) create mode 100644 tests/unit/GetTagsTest.php delete mode 100644 tests/unit/get_tags_test.php (limited to 'tests') diff --git a/tests/unit/GetTagsTest.php b/tests/unit/GetTagsTest.php new file mode 100644 index 000000000..418d32c47 --- /dev/null +++ b/tests/unit/GetTagsTest.php @@ -0,0 +1,281 @@ +15, + 'attag'=>'', 'network'=>'dfrn', + 'name'=>'Mike Lastname', 'alias'=>'Mike', + 'nick'=>'Mike', 'url'=>"http://justatest.de")); + + $args=func_get_args(); + + //last parameter is always (in this test) uid, so, it should be 11 + if($args[count($args)-1]!=11) { + return; + } + + + if(3==count($args)) { + //first call in handle_body, id only + if($result[0]['id']==$args[1]) { + return $result; + } + //second call in handle_body, name + if($result[0]['name']===$args[1]) { + return $result; + } + } + //third call in handle_body, nick or attag + if($result[0]['nick']===$args[2] || $result[0]['attag']===$args[1]) { + return $result; + } +} + +/** + * Replacement for dbesc. + * I don't want to test dbesc here, so + * I just return the input. It won't be a problem, because + * the test does not use a real database. + * + * DON'T USE HAT FUNCTION OUTSIDE A TEST! + * + * @param string $str + * + * @return input + */ +function dbesc($str) { + return $str; +} + +/** + * TestCase for tag handling. + * + * @author alexander + * @package test.util + */ +class GetTagsTest extends Zotlabs\Tests\Unit\UnitTestCase { + /** + * The mock to use as app + */ + private $a; + + /** + * Initialize the test. That's a phpUnit function, + * don't change its name. + */ + public function setUp() : void { + $this->a = new MockApp(); + } + + /** + * Test with one Person tag + */ + public function testGetTagsShortPerson() { + $text="hi @Mike"; + + $tags=get_tags($text); + + $str_tags=''; + foreach($tags as $tag) { + handle_tag($text, $str_tags, 11, $tag); + } + + //correct tags found? + $this->assertEquals(1, count($tags)); + $this->assertTrue(in_array("@Mike", $tags)); + + //correct output from handle_tag? + //$this->assertEquals("@[url=http://justatest.de]Mike Lastname[/url]", $str_tags); + //$this->assertEquals("hi @[url=http://justatest.de]Mike Lastname[/url]", $text); + } + + /** + * Test with one Person tag. + * There's a minor spelling mistake... + */ + public function testGetTagsShortPersonSpelling() { + $text="hi @Mike.because"; + + $tags=get_tags($text); + + //correct tags found? + $this->assertEquals(1, count($tags)); + $this->assertTrue(in_array("@Mike.because", $tags)); + + $str_tags=''; + handle_tag($text, $str_tags, 11, $tags[0]); + + // (mike) - This is a tricky case. + // we support mentions as in @mike@example.com - which contains a period. + // This shouldn't match anything unless you have a contact named "Mike.because". + // We may need another test for "@Mike. because" - which should return the contact + // as we ignore trailing periods in tags. + +// $this->assertEquals("cid:15", $inform); +// $this->assertEquals("@[url=http://justatest.de]Mike Lastname[/url]", $str_tags); +// $this->assertEquals("hi @[url=http://justatest.de]Mike Lastname[/url].because", $text); + + $this->assertEquals("", $str_tags); + + } + + /** + * Test with one hash tag. + */ + public function testGetTagsShortTag() { + $text="This is a #test_case"; + + $tags=get_tags($text); + + $this->assertEquals(1, count($tags)); + $this->assertTrue(in_array("#test_case", $tags)); + } + + /** + * Test with a person and a hash tag + */ + public function testGetTagsShortTagAndPerson() { + $text="hi @Mike This is a #test_case"; + + $tags=get_tags($text); + + $this->assertEquals(2, count($tags)); + $this->assertTrue(in_array("@Mike", $tags)); + $this->assertTrue(in_array("#test_case", $tags)); + + $str_tags=''; + foreach($tags as $tag) { + handle_tag($text, $str_tags, 11, $tag); + } + + //$this->assertEquals("@[url=http://justatest.de]Mike Lastname[/url],#[url=baseurl/search?tag=test%20case]test case[/url]", $str_tags); + //$this->assertEquals("hi @[url=http://justatest.de]Mike Lastname[/url] This is a #[url=baseurl/search?tag=test%20case]test case[/url]", $text); + + } + + /** + * Test with a person, a hash tag and some special chars. + */ + public function testGetTagsShortTagAndPersonSpecialChars() { + $text="hi @Mike, This is a #test_case."; + + $tags=get_tags($text); + + $this->assertEquals(2, count($tags)); + $this->assertTrue(in_array("@Mike", $tags)); + $this->assertTrue(in_array("#test_case", $tags)); + } + + /** + * Test with a person tag and text behind it. + */ + public function testGetTagsPersonOnly() { + $text="@Test I saw the Theme Dev group was created."; + + $tags=get_tags($text); + + $this->assertEquals(1, count($tags)); + $this->assertTrue(in_array("@Test", $tags)); + } + + /** + * Test a tag with an id in it + */ + public function testIdTag() { + $text="Test with @mike+15 id tag"; + + $tags=get_tags($text); + + $this->assertEquals(1, count($tags)); + $this->assertTrue(in_array("@mike+15", $tags)); + + $str_tags=''; + foreach($tags as $tag) { + handle_tag($text, $str_tags, 11, $tag); + } + + //$this->assertEquals("Test with @[url=http://justatest.de]Mike Lastname[/url] id tag", $text); + //$this->assertEquals("@[url=http://justatest.de]Mike Lastname[/url]", $str_tags); + } + + /** + * Test with two persons and one special tag. + */ + public function testGetTags2Persons1TagSpecialChars() { + $text="hi @Mike, I'm just writing #test_cases, so" + . " so @somebody@friendica.com may change #things."; + + $tags=get_tags($text); + + $this->assertEquals(4, count($tags)); + $this->assertTrue(in_array("@Mike", $tags)); + $this->assertTrue(in_array("#test_cases", $tags)); + $this->assertTrue(in_array("@somebody@friendica.com", $tags)); + $this->assertTrue(in_array("#things", $tags)); + } + + /** + * Test with a long text. + */ + public function testGetTags() { + $text="hi @Mike, I'm just writing #test_cases, " + . " so @somebody@friendica.com may change #things. Of course I " + . "look for a lot of #pitfalls, like #tags at the end of a sentence " + . "@comment. I hope noone forgets about @fullstops.because that might" + . " break #things. @Mike@campino@friendica.eu is also #nice, isn't it? " + . "Now, add a @first_last tag. "; + + $tags=get_tags($text); + + $this->assertTrue(in_array("@Mike", $tags)); + $this->assertTrue(in_array("#test_cases", $tags)); + $this->assertTrue(in_array("@somebody@friendica.com", $tags)); + $this->assertTrue(in_array("#things", $tags)); + $this->assertTrue(in_array("#pitfalls", $tags)); + $this->assertTrue(in_array("#tags", $tags)); + $this->assertTrue(in_array("@comment", $tags)); + $this->assertTrue(in_array("@fullstops.because", $tags)); + $this->assertTrue(in_array("#things", $tags)); + $this->assertTrue(in_array("@Mike", $tags)); + $this->assertTrue(in_array("#nice", $tags)); + $this->assertTrue(in_array("@first_last", $tags)); + + //right now, none of the is matched (unsupported) +// $this->assertFalse(in_array("@Mike@campino@friendica.eu", $tags)); +// $this->assertTrue(in_array("@campino@friendica.eu", $tags)); +// $this->assertTrue(in_array("@campino@friendica.eu is", $tags)); + } + + /** + * Test with an empty string + */ + public function testGetTagsEmpty() { + $tags=get_tags(""); + $this->assertEquals(0, count($tags)); + } +} diff --git a/tests/unit/get_tags_test.php b/tests/unit/get_tags_test.php deleted file mode 100644 index bdffd8311..000000000 --- a/tests/unit/get_tags_test.php +++ /dev/null @@ -1,317 +0,0 @@ -15, - 'attag'=>'', 'network'=>'dfrn', - 'name'=>'Mike Lastname', 'alias'=>'Mike', - 'nick'=>'Mike', 'url'=>"http://justatest.de")); - - $args=func_get_args(); - - //last parameter is always (in this test) uid, so, it should be 11 - if($args[count($args)-1]!=11) { - return; - } - - - if(3==count($args)) { - //first call in handle_body, id only - if($result[0]['id']==$args[1]) { - return $result; - } - //second call in handle_body, name - if($result[0]['name']===$args[1]) { - return $result; - } - } - //third call in handle_body, nick or attag - if($result[0]['nick']===$args[2] || $result[0]['attag']===$args[1]) { - return $result; - } -} - -/** - * replacement for dbesc. - * I don't want to test dbesc here, so - * I just return the input. It won't be a problem, because - * the test does not use a real database. - * - * DON'T USE HAT FUNCTION OUTSIDE A TEST! - * - * @param string $str - * @return input - */ -function dbesc($str) { - return $str; -} - -/** - * TestCase for tag handling. - * - * @author alexander - * @package test.util - */ -class GetTagsTest extends PHPUnit_Framework_TestCase { - /** the mock to use as app */ - private $a; - - /** - * initialize the test. That's a phpUnit function, - * don't change its name. - */ - public function setUp() { - $this->a=new MockApp(); - } - - /** - * test with one Person tag - */ - public function testGetTagsShortPerson() { - $text="hi @Mike"; - - $tags=get_tags($text); - - $str_tags=''; - foreach($tags as $tag) { - handle_tag($text, $str_tags, 11, $tag); - } - - //correct tags found? - $this->assertEquals(1, count($tags)); - $this->assertTrue(in_array("@Mike", $tags)); - - //correct output from handle_tag? - $this->assertEquals("@[url=http://justatest.de]Mike Lastname[/url]", $str_tags); - $this->assertEquals("hi @[url=http://justatest.de]Mike Lastname[/url]", $text); - } - - /** - * test with one Person tag. - * There's a minor spelling mistake... - */ - public function testGetTagsShortPersonSpelling() { - $text="hi @Mike.because"; - - $tags=get_tags($text); - - //correct tags found? - $this->assertEquals(1, count($tags)); - $this->assertTrue(in_array("@Mike.because", $tags)); - - $str_tags=''; - handle_tag($text, $str_tags, 11, $tags[0]); - - // (mike) - This is a tricky case. - // we support mentions as in @mike@example.com - which contains a period. - // This shouldn't match anything unless you have a contact named "Mike.because". - // We may need another test for "@Mike. because" - which should return the contact - // as we ignore trailing periods in tags. - -// $this->assertEquals("cid:15", $inform); -// $this->assertEquals("@[url=http://justatest.de]Mike Lastname[/url]", $str_tags); -// $this->assertEquals("hi @[url=http://justatest.de]Mike Lastname[/url].because", $text); - - $this->assertEquals("", $str_tags); - - } - - /** - * test with two Person tags. - * There's a minor spelling mistake... - */ - - public function testGetTagsPerson2Spelling() { - $text="hi @Mike@campino@friendica.eu"; - - $tags=get_tags($text); - -// This construct is not supported. Results are indeterminate -// $this->assertEquals(2, count($tags)); -// $this->assertTrue(in_array("@Mike", $tags)); -// $this->assertTrue(in_array("@campino@friendica.eu", $tags)); - } - - /** - * Test with one hash tag. - */ - public function testGetTagsShortTag() { - $text="This is a #test_case"; - - $tags=get_tags($text); - - $this->assertEquals(1, count($tags)); - $this->assertTrue(in_array("#test_case", $tags)); - } - - /** - * test with a person and a hash tag - */ - public function testGetTagsShortTagAndPerson() { - $text="hi @Mike This is a #test_case"; - - $tags=get_tags($text); - - $this->assertEquals(3, count($tags)); - $this->assertTrue(in_array("@Mike", $tags)); - $this->assertTrue(in_array("@Mike This", $tags)); - $this->assertTrue(in_array("#test_case", $tags)); - - $str_tags=''; - foreach($tags as $tag) { - handle_tag($text, $str_tags, 11, $tag); - } - - $this->assertEquals("@[url=http://justatest.de]Mike Lastname[/url],#[url=baseurl/search?tag=test%20case]test case[/url]", $str_tags); - $this->assertEquals("hi @[url=http://justatest.de]Mike Lastname[/url] This is a #[url=baseurl/search?tag=test%20case]test case[/url]", $text); - - } - - /** - * test with a person, a hash tag and some special chars. - */ - public function testGetTagsShortTagAndPersonSpecialChars() { - $text="hi @Mike, This is a #test_case."; - - $tags=get_tags($text); - - $this->assertEquals(2, count($tags)); - $this->assertTrue(in_array("@Mike", $tags)); - $this->assertTrue(in_array("#test_case", $tags)); - } - - /** - * Test with a person tag and text behind it. - */ - public function testGetTagsPersonOnly() { - $text="@Test I saw the Theme Dev group was created."; - - $tags=get_tags($text); - - $this->assertEquals(2, count($tags)); - $this->assertTrue(in_array("@Test I", $tags)); - $this->assertTrue(in_array("@Test", $tags)); - } - - /** - * this test demonstrates strange behaviour by intval. - * It makes the next test fail. - */ - public function testIntval() { - $this->assertEquals(15, intval("15 it")); - } - - /** - * test a tag with an id in it - */ - public function testIdTag() { - $text="Test with @mike+15 id tag"; - - $tags=get_tags($text); - - $this->assertEquals(2, count($tags)); - $this->assertTrue(in_array("@mike+15", $tags)); - - //happens right now, but it shouldn't be necessary - $this->assertTrue(in_array("@mike+15 id", $tags)); - - $str_tags=''; - foreach($tags as $tag) { - handle_tag($text, $str_tags, 11, $tag); - } - - $this->assertEquals("Test with @[url=http://justatest.de]Mike Lastname[/url] id tag", $text); - $this->assertEquals("@[url=http://justatest.de]Mike Lastname[/url]", $str_tags); - } - - /** - * test with two persons and one special tag. - */ - public function testGetTags2Persons1TagSpecialChars() { - $text="hi @Mike, I'm just writing #test_cases, so" - ." so @somebody@friendica.com may change #things."; - - $tags=get_tags($text); - - $this->assertEquals(5, count($tags)); - $this->assertTrue(in_array("@Mike", $tags)); - $this->assertTrue(in_array("#test_cases", $tags)); - $this->assertTrue(in_array("@somebody@friendica.com", $tags)); - $this->assertTrue(in_array("@somebody@friendica.com may", $tags)); - $this->assertTrue(in_array("#things", $tags)); - } - - /** - * test with a long text. - */ - public function testGetTags() { - $text="hi @Mike, I'm just writing #test_cases, " - ." so @somebody@friendica.com may change #things. Of course I " - ."look for a lot of #pitfalls, like #tags at the end of a sentence " - ."@comment. I hope noone forgets about @fullstops.because that might" - ." break #things. @Mike@campino@friendica.eu is also #nice, isn't it? " - ."Now, add a @first_last tag. "; - - $tags=get_tags($text); - - $this->assertTrue(in_array("@Mike", $tags)); - $this->assertTrue(in_array("#test_cases", $tags)); - $this->assertTrue(in_array("@somebody@friendica.com", $tags)); - $this->assertTrue(in_array("#things", $tags)); - $this->assertTrue(in_array("#pitfalls", $tags)); - $this->assertTrue(in_array("#tags", $tags)); - $this->assertTrue(in_array("@comment", $tags)); - $this->assertTrue(in_array("@fullstops.because", $tags)); - $this->assertTrue(in_array("#things", $tags)); - $this->assertTrue(in_array("@Mike", $tags)); - $this->assertTrue(in_array("#nice", $tags)); - $this->assertTrue(in_array("@first_last", $tags)); - - //right now, none of the is matched (unsupported) -// $this->assertFalse(in_array("@Mike@campino@friendica.eu", $tags)); -// $this->assertTrue(in_array("@campino@friendica.eu", $tags)); -// $this->assertTrue(in_array("@campino@friendica.eu is", $tags)); - } - - /** - * test with an empty string - */ - public function testGetTagsEmpty() { - $tags=get_tags(""); - $this->assertEquals(0, count($tags)); - } -} \ No newline at end of file -- cgit v1.2.3 From d5c1a4998489c73acbf9121edccb1ff797331a1b Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Wed, 12 Jul 2023 21:38:10 +0200 Subject: tests: AccessListTest collect defaults in one testcase. Just no need to have them spread around in various other tests cases. --- tests/unit/Access/AccessListTest.php | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) (limited to 'tests') diff --git a/tests/unit/Access/AccessListTest.php b/tests/unit/Access/AccessListTest.php index 3c8d3e859..0ae024640 100644 --- a/tests/unit/Access/AccessListTest.php +++ b/tests/unit/Access/AccessListTest.php @@ -69,23 +69,24 @@ class AccessListTest extends UnitTestCase { // Causes: "Illegal string offset 'channel_allow_cid'" } */ - public function testDefaultGetExplicit() { + /** + * Test that the defaults are as expected when constructed with + * an empty array. + */ + public function testDefaults() { $accessList = new AccessList([]); $this->assertFalse($accessList->get_explicit()); - } + $this->assertFalse($accessList->is_private()); - public function testDefaultGet() { - $arr = [ + $expected = [ 'allow_cid' => '', 'allow_gid' => '', 'deny_cid' => '', 'deny_gid' => '' ]; - $accessList = new AccessList([]); - - $this->assertEquals($arr, $accessList->get()); + $this->assertEquals($expected, $accessList->get()); } public function testSet() { @@ -161,9 +162,6 @@ class AccessListTest extends UnitTestCase { * @dataProvider isprivateProvider */ public function testIsPrivate($channel) { - $accessListPublic = new AccessList([]); - $this->assertFalse($accessListPublic->is_private()); - $accessListPrivate = new AccessList($channel); $this->assertTrue($accessListPrivate->is_private()); } -- cgit v1.2.3 From 09fe271b25895c38f823fb4820cdfa9836ee9a8b Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Wed, 12 Jul 2023 21:39:37 +0200 Subject: tests: AccessListTests nitpicking on doc comment. --- tests/unit/Access/AccessListTest.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/unit/Access/AccessListTest.php b/tests/unit/Access/AccessListTest.php index 0ae024640..0952949f5 100644 --- a/tests/unit/Access/AccessListTest.php +++ b/tests/unit/Access/AccessListTest.php @@ -124,7 +124,9 @@ class AccessListTest extends UnitTestCase { */ /** - * set_from_array() calls some other functions, too which are not yet unit tested. + * The set_from_array() function calls some other functions, too which are + * not yet unit tested. + * * @uses ::perms2str * @uses ::sanitise_acl * @uses ::notags -- cgit v1.2.3 From 3f9d5e11d1b2c47a69a929bb63479c49f6a52d7a Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Wed, 12 Jul 2023 21:56:09 +0200 Subject: tests: AccessListTest move type annotation to code This cleans up the doc comment. Should probably try to do this everywhere. --- tests/unit/Access/AccessListTest.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'tests') diff --git a/tests/unit/Access/AccessListTest.php b/tests/unit/Access/AccessListTest.php index 0952949f5..1b18a8340 100644 --- a/tests/unit/Access/AccessListTest.php +++ b/tests/unit/Access/AccessListTest.php @@ -34,10 +34,9 @@ use Zotlabs\Access\AccessList; class AccessListTest extends UnitTestCase { /** - * @brief Expected result for most tests. - * @var array + * Expected result for most tests. */ - protected $expectedResult = [ + protected array $expectedResult = [ 'allow_cid' => '', 'allow_gid' => '', 'deny_cid' => '', -- cgit v1.2.3 From b04e0d0fd4173f77f5035860df74ee43097b9256 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Wed, 12 Jul 2023 21:57:27 +0200 Subject: tests: AccessListTest expand is_private test. --- tests/unit/Access/AccessListTest.php | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/unit/Access/AccessListTest.php b/tests/unit/Access/AccessListTest.php index 1b18a8340..2f185db17 100644 --- a/tests/unit/Access/AccessListTest.php +++ b/tests/unit/Access/AccessListTest.php @@ -160,6 +160,8 @@ class AccessListTest extends UnitTestCase { } /** + * The AccessList should be private if any of the fields are set, + * * @dataProvider isprivateProvider */ public function testIsPrivate($channel) { @@ -175,12 +177,30 @@ class AccessListTest extends UnitTestCase { 'channel_deny_cid' => '', 'channel_deny_gid' => '' ]], - 'only one set' => [[ + 'only allow_cid set' => [[ 'channel_allow_cid' => '', 'channel_allow_gid' => '', 'channel_deny_cid' => '', 'channel_deny_gid' => '' ]], + 'only allow_gid set' => [[ + 'channel_allow_cid' => '', + 'channel_allow_gid' => '', + 'channel_deny_cid' => '', + 'channel_deny_gid' => '' + ]], + 'only deny_cid set' => [[ + 'channel_allow_cid' => '', + 'channel_allow_gid' => '', + 'channel_deny_cid' => '', + 'channel_deny_gid' => '' + ]], + 'only deny_gid set' => [[ + 'channel_allow_cid' => '', + 'channel_allow_gid' => '', + 'channel_deny_cid' => '', + 'channel_deny_gid' => '' + ]], 'acid+null' => [[ 'channel_allow_cid' => '', 'channel_allow_gid' => null, -- cgit v1.2.3 From a06e8bfaee7de3bc8c2691e8d6462a52d5345e09 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Tue, 18 Jul 2023 20:18:42 +0200 Subject: Zotlabs: Improve type safety for AccessList class. Add type annotations for constructor and set* methods, and throw an exception if the passed in arrays are missing required keys. This means that both invalid input types and missing keys will throw and exception rather than just die with a runtime error. There's not checks to verify that the contents of the required array keys are valid or make sense, though. They are just assigned, and returned as is by the get method when requested. Also, the set_from_array method is not well tested at the moment. --- tests/unit/Access/AccessListTest.php | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) (limited to 'tests') diff --git a/tests/unit/Access/AccessListTest.php b/tests/unit/Access/AccessListTest.php index 2f185db17..635f09930 100644 --- a/tests/unit/Access/AccessListTest.php +++ b/tests/unit/Access/AccessListTest.php @@ -60,14 +60,24 @@ class AccessListTest extends UnitTestCase { } /** - * @expectedException PHPUnit\Framework\Error\Error + * AccessList constructor should throw an exception if input is not + * an array. */ -/* - public function testPHPErrorOnInvalidConstructor() { + public function testConstructorThrowsOnInvalidInputType() { + $this->expectException("TypeError"); $accessList = new AccessList('invalid'); - // Causes: "Illegal string offset 'channel_allow_cid'" } -*/ + + /** + * AccessList constructor should throw an exception on + * invalid input. + */ + public function testConstructorThrowsOnMissingKeysInArray() { + $this->expectException("Exception"); + $this->expectExceptionMessage("Invalid AccessList object"); + $accessList = new AccessList(['something_else' => 'should_this_fail?']); + } + /** * Test that the defaults are as expected when constructed with * an empty array. @@ -111,16 +121,22 @@ class AccessListTest extends UnitTestCase { } /** - * @expectedException PHPUnit\Framework\Error\Error + * The set method should throw an exception if input is not an array. */ -/* - public function testPHPErrorOnInvalidSet() { + public function testSetThrowsOnInvalidInputType() { + $this->expectException('TypeError'); $accessList = new AccessList([]); $accessList->set('invalid'); - // Causes: "Illegal string offset 'allow_cid'" } -*/ + + public function testSetThrowsOnMissingKeysInArray() { + $this->expectException('Exception'); + $this->expectExceptionMessage('Invalid AccessList object'); + + $accessList = new AccessList([]); + $accessList->set(['something_else' => 'should_this_fail?']); + } /** * The set_from_array() function calls some other functions, too which are -- cgit v1.2.3