<?php
+/**
+ * @copyright Copyright (C) 2010-2023, the Friendica project
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
namespace Friendica\Test\src\Util;
use PHPUnit\Framework\TestCase;
/**
- * @brief ACLFormater utility testing class
+ * ACLFormatter utility testing class
*/
-class ACLFormaterTest extends TestCase
+class ACLFormatterTest extends TestCase
{
- /**
- * test expand_acl, perfect input
- */
- public function testExpandAclNormal()
+ public function assertAcl($text, array $assert = [])
{
$aclFormatter = new ACLFormatter();
- $text='<1><2><3><' . Group::FOLLOWERS . '><' . Group::MUTUALS . '>';
- $this->assertEquals(array('1', '2', '3', Group::FOLLOWERS, Group::MUTUALS), $aclFormatter->expand($text));
- }
+ $acl = $aclFormatter->expand($text);
- /**
- * test with a big number
- */
- public function testExpandAclBigNumber()
- {
- $aclFormatter = new ACLFormatter();
+ self::assertEquals($assert, $acl);
- $text='<1><' . PHP_INT_MAX . '><15>';
- $this->assertEquals(array('1', (string)PHP_INT_MAX, '15'), $aclFormatter->expand($text));
+ self::assertMergable($acl);
}
- /**
- * test with a string in it.
- *
- * @todo is this valid input? Otherwise: should there be an exception?
- */
- public function testExpandAclString()
+ public function assertMergable(array $aclOne, array $aclTwo = [])
{
- $aclFormatter = new ACLFormatter();
+ self::assertTrue(is_array($aclOne));
+ self::assertTrue(is_array($aclTwo));
- $text="<1><279012><tt>";
- $this->assertEquals(array('1', '279012'), $aclFormatter->expand($text));
- }
-
- /**
- * test with a ' ' in it.
- *
- * @todo is this valid input? Otherwise: should there be an exception?
- */
- public function testExpandAclSpace()
- {
- $aclFormatter = new ACLFormatter();
+ $aclMerged = array_unique(array_merge($aclOne, $aclTwo));
+ self::assertTrue(is_array($aclMerged));
- $text="<1><279 012><32>";
- $this->assertEquals(array('1', '32'), $aclFormatter->expand($text));
+ return $aclMerged;
}
- /**
- * test empty input
- */
- public function testExpandAclEmpty()
+ public function dataExpand()
{
- $aclFormatter = new ACLFormatter();
-
- $text="";
- $this->assertEquals(array(), $aclFormatter->expand($text));
+ return [
+ 'normal' => [
+ 'input' => '<1><2><3><' . Group::FOLLOWERS . '><' . Group::MUTUALS . '>',
+ 'assert' => ['1', '2', '3', Group::FOLLOWERS, Group::MUTUALS],
+ ],
+ 'nigNumber' => [
+ 'input' => '<1><' . PHP_INT_MAX . '><15>',
+ 'assert' => ['1', (string)PHP_INT_MAX, '15'],
+ ],
+ 'string' => [
+ 'input' => '<1><279012><tt>',
+ 'assert' => ['1', '279012'],
+ ],
+ 'space' => [
+ 'input' => '<1><279 012><32>',
+ 'assert' => ['1', '32'],
+ ],
+ 'empty' => [
+ 'input' => '',
+ 'assert' => [],
+ ],
+ /// @todo should there be an exception?
+ 'noBrackets' => [
+ 'input' => 'According to documentation, that\'s invalid. ', //should be invalid
+ 'assert' => [],
+ ],
+ /// @todo should there be an exception?
+ 'justOneBracket' => [
+ 'input' => '<Another invalid string', //should be invalid
+ 'assert' => [],
+ ],
+ /// @todo should there be an exception?
+ 'justOneBracket2' => [
+ 'input' => 'Another invalid> string', //should be invalid
+ 'assert' => [],
+ ],
+ /// @todo should there be an exception?
+ 'closeOnly' => [
+ 'input' => 'Another> invalid> string>', //should be invalid
+ 'assert' => [],
+ ],
+ /// @todo should there be an exception?
+ 'openOnly' => [
+ 'input' => '<Another< invalid string<', //should be invalid
+ 'assert' => [],
+ ],
+ /// @todo should there be an exception?
+ 'noMatching1' => [
+ 'input' => '<Another<> invalid <string>', //should be invalid
+ 'assert' => [],
+ ],
+ 'emptyMatch' => [
+ 'input' => '<1><><3>',
+ 'assert' => ['1', '3'],
+ ],
+ ];
}
/**
- * test invalid input, no < at all
- *
- * @todo should there be an exception?
+ * @dataProvider dataExpand
*/
- public function testExpandAclNoBrackets()
+ public function testExpand($input, array $assert)
{
- $aclFormatter = new ACLFormatter();
-
- $text="According to documentation, that's invalid. "; //should be invalid
- $this->assertEquals(array(), $aclFormatter->expand($text));
+ self::assertAcl($input, $assert);
}
/**
- * test invalid input, just open <
- *
- * @todo should there be an exception?
+ * Test nullable expand (=> no ACL set)
*/
- public function testExpandAclJustOneBracket1()
+ public function testExpandNull()
{
$aclFormatter = new ACLFormatter();
- $text="<Another invalid string"; //should be invalid
- $this->assertEquals(array(), $aclFormatter->expand($text));
- }
+ $allow_people = $aclFormatter->expand();
+ $allow_groups = $aclFormatter->expand();
- /**
- * test invalid input, just close >
- *
- * @todo should there be an exception?
- */
- public function testExpandAclJustOneBracket2()
- {
- $aclFormatter = new ACLFormatter();
+ self::assertEmpty($aclFormatter->expand(null));
+ self::assertEmpty($aclFormatter->expand());
- $text="Another invalid> string"; //should be invalid
- $this->assertEquals(array(), $aclFormatter->expand($text));
+ $recipients = array_unique(array_merge($allow_people, $allow_groups));
+ self::assertEmpty($recipients);
}
- /**
- * test invalid input, just close >
- *
- * @todo should there be an exception?
- */
- public function testExpandAclCloseOnly()
+ public function dataAclToString()
{
- $aclFormatter = new ACLFormatter();
-
- $text="Another> invalid> string>"; //should be invalid
- $this->assertEquals(array(), $aclFormatter->expand($text));
- }
-
- /**
- * test invalid input, just open <
- *
- * @todo should there be an exception?
- */
- public function testExpandAclOpenOnly()
- {
- $aclFormatter = new ACLFormatter();
-
- $text="<Another< invalid string<"; //should be invalid
- $this->assertEquals(array(), $aclFormatter->expand($text));
- }
-
- /**
- * test invalid input, open and close do not match
- *
- * @todo should there be an exception?
- */
- public function testExpandAclNoMatching1()
- {
- $aclFormatter = new ACLFormatter();
-
- $text="<Another<> invalid <string>"; //should be invalid
- $this->assertEquals(array(), $aclFormatter->expand($text));
+ return [
+ 'empty' => [
+ 'input' => '',
+ 'assert' => '',
+ ],
+ 'string' => [
+ 'input' => '1,2,3,4',
+ 'assert' => '<1><2><3><4>',
+ ],
+ 'array' => [
+ 'input' => [1, 2, 3, 4],
+ 'assert' => '<1><2><3><4>',
+ ],
+ 'invalid' => [
+ 'input' => [1, 'a', 3, 4],
+ 'assert' => '<1><3><4>',
+ ],
+ 'invalidString' => [
+ 'input' => 'a,bsd23,4',
+ 'assert' => '<4>',
+ ],
+ /** @see https://github.com/friendica/friendica/pull/7787 */
+ 'bug-7778-angle-brackets' => [
+ 'input' => ["<40195>"],
+ 'assert' => "<40195>",
+ ],
+ Group::FOLLOWERS => [
+ 'input' => [Group::FOLLOWERS, 1],
+ 'assert' => '<' . Group::FOLLOWERS . '><1>',
+ ],
+ Group::MUTUALS => [
+ 'input' => [Group::MUTUALS, 1],
+ 'assert' => '<' . Group::MUTUALS . '><1>',
+ ],
+ 'wrong-angle-brackets' => [
+ 'input' => ["<asd>","<123>"],
+ 'assert' => "<123>",
+ ],
+ ];
}
/**
- * test invalid input, empty <>
- *
- * @todo should there be an exception? Or array(1, 3)
- * (This should be array(1,3) - mike)
+ * @dataProvider dataAclToString
*/
- public function testExpandAclEmptyMatch()
+ public function testAclToString($input, string $assert)
{
$aclFormatter = new ACLFormatter();
- $text="<1><><3>";
- $this->assertEquals(array('1', '3'), $aclFormatter->expand($text));
+ self::assertEquals($assert, $aclFormatter->toString($input));
}
}