aboutsummaryrefslogtreecommitdiffstats
path: root/tests/unit
diff options
context:
space:
mode:
Diffstat (limited to 'tests/unit')
-rw-r--r--tests/unit/Access/AccessListTest.php189
-rw-r--r--tests/unit/Lib/PermissionDescriptionTest.php131
-rw-r--r--tests/unit/TextTest.php33
-rw-r--r--tests/unit/includes/FeedutilsText.php55
-rw-r--r--tests/unit/includes/TextTest.php84
-rw-r--r--tests/unit/template_test.php6
6 files changed, 387 insertions, 111 deletions
diff --git a/tests/unit/Access/AccessListTest.php b/tests/unit/Access/AccessListTest.php
new file mode 100644
index 000000000..3dbe5cd65
--- /dev/null
+++ b/tests/unit/Access/AccessListTest.php
@@ -0,0 +1,189 @@
+<?php
+/*
+ * Copyright (c) 2017 Hubzilla
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+namespace Zotlabs\Tests\Unit\Access;
+
+use Zotlabs\Tests\Unit\UnitTestCase;
+use Zotlabs\Access\AccessList;
+
+/**
+ * @brief Unit Test case for AccessList class.
+ *
+ * @covers Zotlabs\Access\AccessList
+ */
+class AccessListTest extends UnitTestCase {
+
+ /**
+ * @brief Expected result for most tests.
+ * @var array
+ */
+ protected $expectedResult = [
+ 'allow_cid' => '<acid><acid2>',
+ 'allow_gid' => '<agid>',
+ 'deny_cid' => '',
+ 'deny_gid' => '<dgid><dgid2>'
+ ];
+
+
+
+ public function testConstructor() {
+ $channel = [
+ 'channel_allow_cid' => '<acid><acid2>',
+ 'channel_allow_gid' => '<agid>',
+ 'channel_deny_cid' => '',
+ 'channel_deny_gid' => '<dgid><dgid2>'
+ ];
+
+ $accessList = new AccessList($channel);
+
+ $this->assertEquals($this->expectedResult, $accessList->get());
+ $this->assertFalse($accessList->get_explicit());
+ }
+
+ /**
+ * @expectedException PHPUnit\Framework\Error\Error
+ */
+ public function testPHPErrorOnInvalidConstructor() {
+ $accessList = new AccessList('invalid');
+ // Causes: "Illegal string offset 'channel_allow_cid'"
+ }
+
+ public function testDefaultGetExplicit() {
+ $accessList = new AccessList([]);
+
+ $this->assertFalse($accessList->get_explicit());
+ }
+
+ public function testDefaultGet() {
+ $arr = [
+ 'allow_cid' => '',
+ 'allow_gid' => '',
+ 'deny_cid' => '',
+ 'deny_gid' => ''
+ ];
+
+ $accessList = new AccessList([]);
+
+ $this->assertEquals($arr, $accessList->get());
+ }
+
+ public function testSet() {
+ $arr = [
+ 'allow_cid' => '<acid><acid2>',
+ 'allow_gid' => '<agid>',
+ 'deny_cid' => '',
+ 'deny_gid' => '<dgid><dgid2>'
+ ];
+ $accessList = new AccessList([]);
+
+ // default explicit true
+ $accessList->set($arr);
+
+ $this->assertEquals($this->expectedResult, $accessList->get());
+ $this->assertTrue($accessList->get_explicit());
+
+ // set explicit false
+ $accessList->set($arr, false);
+
+ $this->assertEquals($this->expectedResult, $accessList->get());
+ $this->assertFalse($accessList->get_explicit());
+ }
+
+ /**
+ * @expectedException PHPUnit\Framework\Error\Error
+ */
+ public function testPHPErrorOnInvalidSet() {
+ $accessList = new AccessList([]);
+
+ $accessList->set('invalid');
+ // Causes: "Illegal string offset 'allow_cid'"
+ }
+
+ /**
+ * set_from_array() calls some other functions, too which are not yet unit tested.
+ * @uses ::perms2str()
+ */
+ public function testSetFromArray() {
+ // array
+ $arraySetFromArray = [
+ 'contact_allow' => ['acid', 'acid2'],
+ 'group_allow' => ['agid'],
+ 'contact_deny' => [],
+ 'group_deny' => ['dgid', 'dgid2']
+ ];
+ $accessList = new AccessList([]);
+ $accessList->set_from_array($arraySetFromArray);
+
+ $this->assertEquals($this->expectedResult, $accessList->get());
+ $this->assertTrue($accessList->get_explicit());
+
+
+ // string
+ $stringSetFromArray = [
+ 'contact_allow' => 'acid,acid2',
+ 'group_allow' => 'agid',
+ 'contact_deny' => '',
+ 'group_deny' => 'dgid, dgid2'
+ ];
+ $accessList2 = new AccessList([]);
+ $accessList2->set_from_array($stringSetFromArray, false);
+
+ $this->assertEquals($this->expectedResult, $accessList2->get());
+ $this->assertFalse($accessList2->get_explicit());
+ }
+
+ /**
+ * @dataProvider isprivateProvider
+ */
+ public function testIsPrivate($channel) {
+ $accessListPublic = new AccessList([]);
+ $this->assertFalse($accessListPublic->is_private());
+
+ $accessListPrivate = new AccessList($channel);
+ $this->assertTrue($accessListPrivate->is_private());
+ }
+
+ public function isprivateProvider() {
+ return [
+ 'all set' => [[
+ 'channel_allow_cid' => '<acid>',
+ 'channel_allow_gid' => '<agid>',
+ 'channel_deny_cid' => '<dcid>',
+ 'channel_deny_gid' => '<dgid>'
+ ]],
+ 'only one set' => [[
+ 'channel_allow_cid' => '<acid>',
+ 'channel_allow_gid' => '',
+ 'channel_deny_cid' => '',
+ 'channel_deny_gid' => ''
+ ]],
+ 'acid+null' => [[
+ 'channel_allow_cid' => '<acid>',
+ 'channel_allow_gid' => null,
+ 'channel_deny_cid' => '',
+ 'channel_deny_gid' => ''
+ ]]
+ ];
+ }
+
+} \ No newline at end of file
diff --git a/tests/unit/Lib/PermissionDescriptionTest.php b/tests/unit/Lib/PermissionDescriptionTest.php
index b1da5a0fd..96c381d0c 100644
--- a/tests/unit/Lib/PermissionDescriptionTest.php
+++ b/tests/unit/Lib/PermissionDescriptionTest.php
@@ -1,6 +1,6 @@
<?php
/*
- * Copyright (c) 2016 Hubzilla
+ * Copyright (c) 2016-2017 Hubzilla
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -21,90 +21,77 @@
* SOFTWARE.
*/
-// Global namespace for fully qualified \App class.
-namespace {
- // General channel permissions in boot.php
- // 0 = Only you
- define ( 'PERMS_PUBLIC' , 0x0001 ); // anybody
- define ( 'PERMS_NETWORK' , 0x0002 ); // anybody in this network
- define ( 'PERMS_SITE' , 0x0004 ); // anybody on this site
- define ( 'PERMS_CONTACTS' , 0x0008 ); // any of my connections
- define ( 'PERMS_SPECIFIC' , 0x0080 ); // only specific connections
- define ( 'PERMS_AUTHED' , 0x0100 ); // anybody authenticated (could include visitors from other networks)
- define ( 'PERMS_PENDING' , 0x0200 ); // any connections including those who haven't yet been approved
- // log levels in boot.php
- define ( 'LOGGER_DEBUG', 2 );
+namespace Zotlabs\Tests\Unit\Lib;
- // Stub global fully qualified \App class for static function calls
- class App {
- // Stub get_hostname()
- public static function get_hostname() {
- return 'phpunit';
- }
- }
-}
+use phpmock\phpunit\PHPMock;
+use Zotlabs\Tests\Unit\UnitTestCase;
+use Zotlabs\Lib\PermissionDescription;
-// Stub global functions used in PermissionDescription with the help of
-// PHP's namespace resolution rules.
-namespace Zotlabs\Lib {
- // Stub global translate function t()
- function t($s) {
- return $s;
- }
- // Stub global log function logger()
- function logger($msg, $level = LOGGER_NORMAL, $priority = LOG_INFO) {
- // doesn't matter
- }
-}
+/**
+ * @brief Unit Test case for PermissionDescription class.
+ *
+ * @covers Zotlabs\Lib\PermissionDescription
+ */
+class PermissionDescriptionTest extends UnitTestCase {
-// regular namespace for this unit test
-namespace Zotlabs\Tests\Unit\Lib {
+ use PHPMock;
- use Zotlabs\Tests\Unit\UnitTestCase;
- use Zotlabs\Lib\PermissionDescription;
+ public function testFromDescription() {
+ $permDesc = PermissionDescription::fromDescription('test');
+ $permDesc2 = PermissionDescription::fromDescription('test');
+ $permDesc3 = PermissionDescription::fromDescription('test2');
- /**
- * @brief Unit Test case for ConnectionPool class.
- */
- class PermissionDescriptionTest extends UnitTestCase {
+ $this->assertEquals($permDesc, $permDesc2);
+ $this->assertNotEquals($permDesc, $permDesc3);
+ }
- public function testFromDescription() {
- $permDesc = PermissionDescription::fromDescription('test');
- $permDesc2 = PermissionDescription::fromDescription('test');
- $permDesc3 = PermissionDescription::fromDescription('test2');
+ public function testFromStandalonePermission() {
+ // Create a stub for global function t()
+ $t = $this->getFunctionMock('Zotlabs\Lib', 't');
+ $t->expects($this->atLeastOnce())->willReturnCallback(
+ function ($string) {
+ return $string;
+ }
+ );
+ // Create a mock for global function logger()
+ $this->getFunctionMock('Zotlabs\Lib', 'logger');
- $this->assertEquals($permDesc, $permDesc2);
- $this->assertNotEquals($permDesc, $permDesc3);
- }
+ $permDescUnknown = PermissionDescription::fromStandalonePermission(-1);
+ $permDescSelf = PermissionDescription::fromStandalonePermission(0);
- public function testFromStandalonePermission() {
- $permDescUnknown = PermissionDescription::fromStandalonePermission(-1);
- $permDescSelf = PermissionDescription::fromStandalonePermission(0);
+ $this->assertNull($permDescUnknown);
+ $this->assertNotNull($permDescSelf);
+ }
- $this->assertNull($permDescUnknown);
- $this->assertNotNull($permDescSelf);
- }
+ public function testFromGlobalPermission() {
+ //$permDesc = PermissionDescription::fromGlobalPermission('view_profile');
- public function testFromGlobalPermission() {
- //$permDesc = PermissionDescription::fromGlobalPermission('view_profile');
+ $this->markTestIncomplete(
+ 'The method fromGlobalPermission() is not yet testable ...'
+ );
+ }
- $this->markTestIncomplete(
- 'For this test we need more stubs...'
- );
- }
+ public function testGetPermissionDescription() {
+ // Create a stub for global function t()
+ $t = $this->getFunctionMock('Zotlabs\Lib', 't');
+ $t->expects($this->atLeastOnce())->willReturnCallback(
+ function ($string) {
+ return $string;
+ }
+ );
+ // Create a mock for global function logger()
+ $this->getFunctionMock('Zotlabs\Lib', 'logger');
- public function testGetPermissionDescription() {
+ // Create a stub for the PermissionDescription class
+ $stub = $this->createMock(PermissionDescription::class);
+ $stub->method('get_permission_description')
+ ->will($this->returnArgument(0));
- // fromStandalonePermission uses get_permission_description(), so that will not help
- //$permDescSelf = PermissionDescription::fromStandalonePermission(0);
- //$permDescPublic = PermissionDescription::fromStandalonePermission(PERMS_PUBLIC);
+ $permDescSelf = PermissionDescription::fromStandalonePermission(0);
+ $this->assertInstanceOf(PermissionDescription::class, $permDescSelf);
+ $this->assertEquals($permDescSelf->get_permission_description(), 'Only me');
- $this->markTestIncomplete(
- 'For this test we need a mock of PermissionDescription...'
- );
- //$permDescSelf =
- //$this->assertEquals($permDescSelf->, 'Only me');
- //$this->assertEquals($permDescPublic, 'Public');
- }
+ $permDescPublic = PermissionDescription::fromStandalonePermission(PERMS_PUBLIC);
+ $this->assertEquals($permDescPublic->get_permission_description(), 'Public');
}
}
diff --git a/tests/unit/TextTest.php b/tests/unit/TextTest.php
deleted file mode 100644
index 48c04bc54..000000000
--- a/tests/unit/TextTest.php
+++ /dev/null
@@ -1,33 +0,0 @@
-<?php
-/**
- * this file contains tests for text.php
- *
- * @package test.util
- */
-
-use PHPUnit\Framework\TestCase;
-
-/** required, it is the file under test */
-require_once('include/text.php');
-
-/**
- * TestCase for the texter
- *
- * @author ken restivo
- * @package test.util
- */
-class TextTest extends TestCase {
- public function testGoodEmail() {
- $this->assertTrue(valid_email_regex('ken@spaz.org'));
- }
- public function testGoodEmail2() {
- $this->assertTrue(valid_email_regex('ken@restivo.org'));
- }
- public function testGoodEmail3() {
- $this->assertTrue(valid_email_regex('nobody@hubzilla.com'));
- }
- public function testBadEmail() {
- $this->assertFalse(valid_email_regex('nobody!uses!these!any.more'));
- }
-
-} \ No newline at end of file
diff --git a/tests/unit/includes/FeedutilsText.php b/tests/unit/includes/FeedutilsText.php
new file mode 100644
index 000000000..932a1b3a1
--- /dev/null
+++ b/tests/unit/includes/FeedutilsText.php
@@ -0,0 +1,55 @@
+<?php
+
+namespace Zotlabs\Tests\Unit\includes;
+
+use Zotlabs\Tests\Unit\UnitTestCase;
+
+/**
+ * @brief Unit Test case for include/feedutils.php file.
+ */
+class FeedutilsTest extends UnitTestCase {
+
+ public function test_normalise_id() {
+ $this->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 = '<tag>
+ <id>uri</id>
+ <name>nick</name>
+ <uri>uri</uri>
+ <link rel="photo" type="png" media:width="72" media:height="72" href="http://photourl" />
+ <link rel="avatar" type="png" media:width="72" media:height="72" href="http://photourl" />
+ <poco:preferredUsername>nick</poco:preferredUsername>
+ <poco:displayName>name<poco:displayName>
+</tag>';
+
+ $this->assertXmlStringEqualsXmlString($a, atom_author('tag', 'nick', 'name', 'uri', 72, 72, 'png', 'http://photourl'));
+ }
+}
diff --git a/tests/unit/includes/TextTest.php b/tests/unit/includes/TextTest.php
new file mode 100644
index 000000000..e2c7cbb9a
--- /dev/null
+++ b/tests/unit/includes/TextTest.php
@@ -0,0 +1,84 @@
+<?php
+
+namespace Zotlabs\Tests\Unit\includes;
+
+use Zotlabs\Tests\Unit\UnitTestCase;
+
+/**
+ * @brief Unit Test case for include/texter.php file.
+ *
+ * @author ken restivo
+ */
+class TextTest extends UnitTestCase {
+
+ public function testGoodEmail() {
+ $this->assertTrue(valid_email_regex('ken@spaz.org'));
+ $this->assertTrue(valid_email_regex('ken@restivo.org'));
+ $this->assertTrue(valid_email_regex('nobody@hubzilla.org'));
+ $this->assertTrue(valid_email_regex('foo+nobody@hubzilla.org'));
+ }
+
+ public function testBadEmail() {
+ $this->assertFalse(valid_email_regex('nobody!uses!these!any.more'));
+ $this->assertFalse(valid_email_regex('foo@bar@hubzilla.org'));
+ }
+
+ public function testPurifyHTML() {
+ // linebreaks
+ $htmlbr = 'first line<br />
+ one tab preserved
+
+empty line above';
+ $this->assertEquals($htmlbr, purify_html($htmlbr));
+
+ // HTML5 is not supported by HTMLPurifier yet, test our own configuration
+ $html5elements = '<section>section<nav>navigation</nav><article>main<a href="http://hubzilla.org/">hubzilla.org</a></article></section><footer>footer</footer>';
+ $this->assertEquals($html5elements, purify_html($html5elements));
+ $this->assertEquals('<button>button label</button>', purify_html('<button>button label</button>'));
+
+ // unsupported HTML5 elements
+ $this->assertEquals('Your HTML parser does not support HTML5 video.', purify_html('<video controls><source src="movie.ogg" type="video/ogg">Your HTML parser does not support HTML5 video.</video>'));
+ $this->assertEquals('Your HTML parser does not support HTML5 audio.', purify_html('<audio controls><source src="movie.ogg" "type="audio/ogg">Your HTML parser does not support HTML5 audio.</audio>'));
+
+ // preserve f6 and bootstrap additional data attributes from our own configuration
+ $this->assertEquals('<div data-title="title">text</div>', purify_html('<div data-title="title">text</div>'));
+ $this->assertEquals('<ul data-accordion-menu=""><li>item1</li></ul>', purify_html('<ul data-accordion-menu><li>item1</li></ul>'));
+ $this->assertEquals('<ul><li>item1</li></ul>', purify_html('<ul data-accordion-menu-unknown><li>item1</li></ul>'));
+ }
+
+ public function testPurifyHTML_html() {
+ $this->assertEquals('<div id="id01"><p class="class01">ids und classes</p></div>', purify_html('<div id="id01"><p class="class01">ids und classes</p></div>'));
+ $this->assertEquals('<div><p>close missing tags</p></div>', purify_html('<div><p>close missing tags'));
+ $this->assertEquals('<center>deprecated tag</center>', purify_html('<center>deprecated tag</center>'));
+ $this->assertEquals('<span></span><div>illegal nesting</div>', purify_html('<span><div>illegal nesting</div></span>'));
+ $this->assertEquals('<a href="#">link with target</a>', purify_html('<a href="#" target="_blank">link with target</a>'));
+ $this->assertEquals('<a href="#">link with rel="nofollow"</a>', purify_html('<a href="#" rel="nofollow">link with rel="nofollow"</a>'));
+ $this->assertEquals('a b', purify_html('a&nbsp;b'));
+ $this->assertEquals('ä ä € €', purify_html('ä &auml; &euro; &#8364;'));
+ $this->assertEquals('<img src="picture.png" alt="text" />', purify_html('<img src="picture.png" alt="text">'));
+ $this->assertEquals('', purify_html('<iframe width="560" height="315" src="https://www.youtube.com/embed/kiNGx5oL7hk" frameborder="0" allowfullscreen></iframe>'));
+ }
+
+ public function testPurifyHTML_js() {
+ $this->assertEquals('<div></div>', purify_html('<div><img src="javascript:evil();" onload="evil();"></div>'));
+ $this->assertEquals('<a href="#">link</a>', purify_html('<a href="#" onclick="alert(\'xss\')">link</a>'));
+ $this->assertEquals('', purify_html('<IMG SRC="javascript:alert(&#039;XSS&#039;);">'));
+ $this->assertEquals('', purify_html('<script>alter("42")</script>'));
+ }
+
+ public function testPurifyHTML_css() {
+ $this->assertEquals('<p style="color:#FF0000;background-color:#fff;">red</p>', purify_html('<p style="color:red; background-color:#fff">red</p>'));
+ $this->assertEquals('<p>invalid color</p>', purify_html('<p style="color:invalid; background-color:#jjkkmm">invalid color</p>'));
+ $this->assertEquals('<p>invalid style</p>', purify_html('<p style="foo:bar">invalid style</p>'));
+
+ // test our own CSS configuration
+ $this->assertEquals('<div>position removed</div>', purify_html('<div style="position:absolut">position removed</div>'));
+ $this->assertEquals('<div style="position:fixed;">position preserved</div>', purify_html('<div style="position:fixed">position preserved</div>', true));
+ $this->assertEquals('<div>invalid position removed</div>', purify_html('<div style="position:invalid">invalid position removed</div>', true));
+
+ $this->assertEquals('<div>position removed</div>', purify_html('<div style="top:10px; left:3em;">position removed</div>'));
+ $this->assertEquals('<div style="top:10px;left:3em;right:50%;">position preserved</div>', purify_html('<div style="top:10px; left:3em; right:50%;">position preserved</div>', true));
+ $this->assertEquals('<div>invalid position removed</div>', purify_html('<div style="top:10p">invalid position removed</div>', true));
+ }
+
+}
diff --git a/tests/unit/template_test.php b/tests/unit/template_test.php
index 1f9f80531..dfaecb4a1 100644
--- a/tests/unit/template_test.php
+++ b/tests/unit/template_test.php
@@ -25,12 +25,6 @@ function x($s,$k = NULL) {
}
}
-if(!function_exists('get_app')) {
-function get_app() {
- return new TemplateMockApp();
-}
-}
-
/**
* TestCase for the template engine
*