diff options
author | Mario Vavti <mario@mariovavti.com> | 2016-12-23 10:09:46 +0100 |
---|---|---|
committer | Mario Vavti <mario@mariovavti.com> | 2016-12-23 10:09:46 +0100 |
commit | 3b9b03cf86979b28e7fa249133176bed84b0105c (patch) | |
tree | 336dc8b8b9627e7f4a93e5c35fe3e98555274616 /tests | |
parent | 2e5a993f880d619aedf3693927e7b3e164fbfcc0 (diff) | |
parent | ef39c1e94b5149a3019d417d08dc7c16c8aef9c1 (diff) | |
download | volse-hubzilla-3b9b03cf86979b28e7fa249133176bed84b0105c.tar.gz volse-hubzilla-3b9b03cf86979b28e7fa249133176bed84b0105c.tar.bz2 volse-hubzilla-3b9b03cf86979b28e7fa249133176bed84b0105c.zip |
Merge branch '2.0RC'
Diffstat (limited to 'tests')
16 files changed, 416 insertions, 119 deletions
diff --git a/tests/README.md b/tests/README.md new file mode 100644 index 000000000..395333159 --- /dev/null +++ b/tests/README.md @@ -0,0 +1,25 @@ +The folder tests/ contains resources for automated testing tools. + +Here you will find PHPUnit, Behat, etc. files to test the functionaly +of Hubzilla. Right now it only contains some basic tests to see if feasable +this can help improve the project. + +# Contents + +* unit/ PHPUnit tests +These are unit tests to check the smallest parts, like single functions. +It uses the tool PHPUnit https://phpunit.de/ + +* acceptance/ functional/acceptance testing +These are behavioral or so called functional/acceptance testing. They +are used to test business logic. They are written in Gherkin and use +the tool Behat http://behat.org/ + +# How to use? +You need the dev tools which are defined in the composer.json in the +require-dev configuration. +Run ```composer install``` without --no-dev to install these tools. + +To run unit tests run ```vendor/bin/phpunit tests/unit/``` + +To run acceptance tests run ```vendor/bin/behat --config tests/acceptance/behat.yml``` diff --git a/tests/acceptance/behat.yml b/tests/acceptance/behat.yml new file mode 100644 index 000000000..933571e5e --- /dev/null +++ b/tests/acceptance/behat.yml @@ -0,0 +1,27 @@ +default: + suites: + default: + paths: + - %paths.base%/features + contexts: + - Behat\MinkExtension\Context\MinkContext + admin_features: + filters: { role: admin } + contexts: + - AdminContext + api_features: + paths: + - %paths.base%/features/api + filters: + tags: "@api" + contexts: + - ApiContext + gherkin: + filters: + tags: ~@wip + extensions: + Behat\MinkExtension: + base_url: 'http://localhost' + sessions: + default: + goutte: ~ diff --git a/tests/acceptance/features/bootstrap/AdminContext.php b/tests/acceptance/features/bootstrap/AdminContext.php new file mode 100644 index 000000000..aa4dced67 --- /dev/null +++ b/tests/acceptance/features/bootstrap/AdminContext.php @@ -0,0 +1,23 @@ +<?php + +use Behat\Behat\Context\Context; +use Behat\Behat\Context\SnippetAcceptingContext; +use Behat\Gherkin\Node\PyStringNode; +use Behat\Gherkin\Node\TableNode; + +/** + * Defines application features from the specific context. + */ +class AdminContext implements Context, SnippetAcceptingContext +{ + /** + * Initializes context. + * + * Every scenario gets its own context instance. + * You can also pass arbitrary arguments to the + * context constructor through behat.yml. + */ + public function __construct() + { + } +} diff --git a/tests/acceptance/features/bootstrap/ApiContext.php b/tests/acceptance/features/bootstrap/ApiContext.php new file mode 100644 index 000000000..9b772b209 --- /dev/null +++ b/tests/acceptance/features/bootstrap/ApiContext.php @@ -0,0 +1,23 @@ +<?php + +use Behat\Behat\Context\Context; +use Behat\Behat\Context\SnippetAcceptingContext; +use Behat\Gherkin\Node\PyStringNode; +use Behat\Gherkin\Node\TableNode; + +/** + * Defines application features from the specific context. + */ +class ApiContext implements Context, SnippetAcceptingContext +{ + /** + * Initializes context. + * + * Every scenario gets its own context instance. + * You can also pass arbitrary arguments to the + * context constructor through behat.yml. + */ + public function __construct() + { + } +} diff --git a/tests/acceptance/features/bootstrap/FeatureContext.php b/tests/acceptance/features/bootstrap/FeatureContext.php new file mode 100644 index 000000000..2ec02da85 --- /dev/null +++ b/tests/acceptance/features/bootstrap/FeatureContext.php @@ -0,0 +1,23 @@ +<?php + +use Behat\Behat\Context\Context; +use Behat\Behat\Context\SnippetAcceptingContext; +use Behat\Gherkin\Node\PyStringNode; +use Behat\Gherkin\Node\TableNode; + +/** + * Defines application features from the specific context. + */ +class FeatureContext implements Context, SnippetAcceptingContext +{ + /** + * Initializes context. + * + * Every scenario gets its own context instance. + * You can also pass arbitrary arguments to the + * context constructor through behat.yml. + */ + public function __construct() + { + } +} diff --git a/tests/acceptance/features/login_local.feature b/tests/acceptance/features/login_local.feature new file mode 100644 index 000000000..a7814b8f6 --- /dev/null +++ b/tests/acceptance/features/login_local.feature @@ -0,0 +1,18 @@ +Feature: Local login + In order to login locally + As a member + I need to successfully authenticate + + Scenario: Provide wrong credentials + Given I am on "/login" + When I fill in "id_username" with "foo" + And I fill in "id_password" with "bar" + And I press "submit" + Then I should be on "/login" + + Scenario: Provide correct credentials + Given I am on "/login" + When I fill in "id_username" with "behat" + And I fill in "id_password" with "behat" + And I press "submit" + Then I should be on "/apps" diff --git a/tests/xss_filter_test.php b/tests/unit/AntiXSSTest.php index 3fb6ac310..b45042a1e 100644 --- a/tests/xss_filter_test.php +++ b/tests/unit/AntiXSSTest.php @@ -1,13 +1,15 @@ <?php /** * tests several functions which are used to prevent xss attacks - * + * * @package test.util */ +use PHPUnit\Framework\TestCase; + require_once('include/text.php'); -class AntiXSSTest extends PHPUnit_Framework_TestCase { +class AntiXSSTest extends TestCase { /** * test, that tags are escaped @@ -27,31 +29,31 @@ class AntiXSSTest extends PHPUnit_Framework_TestCase { */ public function testXmlify() { $text="<tag>I want to break\n this!11!<?hard?></tag>"; - $xml=xmlify($text); + $xml=xmlify($text); $retext=unxmlify($text); $this->assertEquals($text, $retext); } - - /**
- * xmlify and put in a document
- */
- public function testXmlifyDocument() {
- $tag="<tag>I want to break</tag>";
+ + /** + * xmlify and put in a document + */ + public function testXmlifyDocument() { + $tag="<tag>I want to break</tag>"; $xml=xmlify($tag); - $text='<text>'.$xml.'</text>';
- - $xml_parser=xml_parser_create(); + $text='<text>'.$xml.'</text>'; + + $xml_parser=xml_parser_create(); //should be possible to parse it - $values=array(); $index=array(); - $this->assertEquals(1, xml_parse_into_struct($xml_parser, $text, $values, $index)); - - $this->assertEquals(array('TEXT'=>array(0)), - $index); - $this->assertEquals(array(array('tag'=>'TEXT', 'type'=>'complete', 'level'=>1, 'value'=>$tag)),
+ $values=array(); $index=array(); + $this->assertEquals(1, xml_parse_into_struct($xml_parser, $text, $values, $index)); + + $this->assertEquals(array('TEXT'=>array(0)), + $index); + $this->assertEquals(array(array('tag'=>'TEXT', 'type'=>'complete', 'level'=>1, 'value'=>$tag)), $values); - - xml_parser_free($xml_parser);
+ + xml_parser_free($xml_parser); } /** diff --git a/tests/autoname_test.php b/tests/unit/AutonameTest.php index 702e05bef..9f92f736f 100644 --- a/tests/autoname_test.php +++ b/tests/unit/AutonameTest.php @@ -1,76 +1,78 @@ <?php /** * this file contains tests for the autoname function - * + * * @package test.util */ +use PHPUnit\Framework\TestCase; + /** required, it is the file under test */ require_once('include/text.php'); /** * TestCase for the autoname function - * + * * @author Alexander Kampmann * @package test.util */ -class AutonameTest extends PHPUnit_Framework_TestCase { - /**
- *autonames should be random, even length
- */
- public function testAutonameEven() {
- $autoname1=autoname(10);
- $autoname2=autoname(10);
-
- $this->assertNotEquals($autoname1, $autoname2);
- }
-
- /**
- *autonames should be random, odd length
- */
- public function testAutonameOdd() {
- $autoname1=autoname(9);
- $autoname2=autoname(9);
-
- $this->assertNotEquals($autoname1, $autoname2);
- }
-
- /**
- * try to fail autonames
- */
- public function testAutonameNoLength() {
- $autoname1=autoname(0);
- $this->assertEquals(0, strlen($autoname1));
- }
- +class AutonameTest extends TestCase { + /** + *autonames should be random, even length + */ + public function testAutonameEven() { + $autoname1=autoname(10); + $autoname2=autoname(10); + + $this->assertNotEquals($autoname1, $autoname2); + } + + /** + *autonames should be random, odd length + */ + public function testAutonameOdd() { + $autoname1=autoname(9); + $autoname2=autoname(9); + + $this->assertNotEquals($autoname1, $autoname2); + } + + /** + * try to fail autonames + */ + public function testAutonameNoLength() { + $autoname1=autoname(0); + $this->assertEquals(0, strlen($autoname1)); + } + /** * try to fail it with invalid input - * + * * TODO: What's corect behaviour here? An exception? - */
- public function testAutonameNegativeLength() {
- $autoname1=autoname(-23);
- $this->assertEquals(0, strlen($autoname1));
- }
-
- // public function testAutonameMaxLength() {
- // $autoname2=autoname(PHP_INT_MAX);
- // $this->assertEquals(PHP_INT_MAX, count($autoname2));
- // }
- + */ + public function testAutonameNegativeLength() { + $autoname1=autoname(-23); + $this->assertEquals(0, strlen($autoname1)); + } + + // public function testAutonameMaxLength() { + // $autoname2=autoname(PHP_INT_MAX); + // $this->assertEquals(PHP_INT_MAX, count($autoname2)); + // } + /** * test with a length, that may be too short - */
- public function testAutonameLength1() {
- $autoname1=autoname(1);
+ */ + public function testAutonameLength1() { + $autoname1=autoname(1); $this->assertEquals(1, count($autoname1)); - - $autoname2=autoname(1);
+ + $autoname2=autoname(1); $this->assertEquals(1, count($autoname2)); // The following test is problematic, with only 26 possibilities // generating the same thing twice happens often aka // birthday paradox -// $this->assertFalse($autoname1==$autoname2);
+// $this->assertFalse($autoname1==$autoname2); } }
\ No newline at end of file diff --git a/tests/contains_attribute_test.php b/tests/unit/ContainsAttributeTest.php index b0bb06acf..0930d9837 100644 --- a/tests/contains_attribute_test.php +++ b/tests/unit/ContainsAttributeTest.php @@ -1,51 +1,53 @@ <?php -/**
- * this test tests the contains_attribute function
- *
- * @package test.util
- */
-
-/** required, it is the file under test */
-require_once('include/text.php');
-
-/**
- * TestCase for the contains_attribute function
- *
- * @author Alexander Kampmann
- * @package test.util
- */
-class ContainsAttributeTest extends PHPUnit_Framework_TestCase { - /**
- * test attribute contains
- */
- public function testAttributeContains1() {
- $testAttr="class1 notclass2 class3";
- $this->assertTrue(attribute_contains($testAttr, "class3"));
- $this->assertFalse(attribute_contains($testAttr, "class2"));
- }
-
- /**
- * test attribute contains
- */
- public function testAttributeContains2() {
- $testAttr="class1 not-class2 class3";
- $this->assertTrue(attribute_contains($testAttr, "class3"));
- $this->assertFalse(attribute_contains($testAttr, "class2"));
- }
- +/** + * this test tests the contains_attribute function + * + * @package test.util + */ + +use PHPUnit\Framework\TestCase; + +/** required, it is the file under test */ +require_once('include/text.php'); + +/** + * TestCase for the contains_attribute function + * + * @author Alexander Kampmann + * @package test.util + */ +class ContainsAttributeTest extends TestCase { + /** + * test attribute contains + */ + public function testAttributeContains1() { + $testAttr="class1 notclass2 class3"; + $this->assertTrue(attribute_contains($testAttr, "class3")); + $this->assertFalse(attribute_contains($testAttr, "class2")); + } + + /** + * test attribute contains + */ + public function testAttributeContains2() { + $testAttr="class1 not-class2 class3"; + $this->assertTrue(attribute_contains($testAttr, "class3")); + $this->assertFalse(attribute_contains($testAttr, "class2")); + } + /** * test with empty input - */
- public function testAttributeContainsEmpty() {
- $testAttr="";
- $this->assertFalse(attribute_contains($testAttr, "class2"));
- }
- + */ + public function testAttributeContainsEmpty() { + $testAttr=""; + $this->assertFalse(attribute_contains($testAttr, "class2")); + } + /** * test input with special chars - */
- public function testAttributeContainsSpecialChars() {
- $testAttr="--... %\$ä() /(=?}";
- $this->assertFalse(attribute_contains($testAttr, "class2"));
+ */ + public function testAttributeContainsSpecialChars() { + $testAttr="--... %\$ä() /(=?}"; + $this->assertFalse(attribute_contains($testAttr, "class2")); } }
\ No newline at end of file diff --git a/tests/unit/Lib/PermissionDescriptionTest.php b/tests/unit/Lib/PermissionDescriptionTest.php new file mode 100644 index 000000000..b1da5a0fd --- /dev/null +++ b/tests/unit/Lib/PermissionDescriptionTest.php @@ -0,0 +1,110 @@ +<?php +/* + * Copyright (c) 2016 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. + */ + +// 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 ); + + // Stub global fully qualified \App class for static function calls + class App { + // Stub get_hostname() + public static function get_hostname() { + return 'phpunit'; + } + } +} + +// 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 + } +} + +// regular namespace for this unit test +namespace Zotlabs\Tests\Unit\Lib { + + use Zotlabs\Tests\Unit\UnitTestCase; + use Zotlabs\Lib\PermissionDescription; + + /** + * @brief Unit Test case for ConnectionPool class. + */ + class PermissionDescriptionTest extends UnitTestCase { + + public function testFromDescription() { + $permDesc = PermissionDescription::fromDescription('test'); + $permDesc2 = PermissionDescription::fromDescription('test'); + $permDesc3 = PermissionDescription::fromDescription('test2'); + + $this->assertEquals($permDesc, $permDesc2); + $this->assertNotEquals($permDesc, $permDesc3); + } + + public function testFromStandalonePermission() { + $permDescUnknown = PermissionDescription::fromStandalonePermission(-1); + $permDescSelf = PermissionDescription::fromStandalonePermission(0); + + $this->assertNull($permDescUnknown); + $this->assertNotNull($permDescSelf); + } + + public function testFromGlobalPermission() { + //$permDesc = PermissionDescription::fromGlobalPermission('view_profile'); + + $this->markTestIncomplete( + 'For this test we need more stubs...' + ); + } + + public function testGetPermissionDescription() { + + // fromStandalonePermission uses get_permission_description(), so that will not help + //$permDescSelf = PermissionDescription::fromStandalonePermission(0); + //$permDescPublic = PermissionDescription::fromStandalonePermission(PERMS_PUBLIC); + + $this->markTestIncomplete( + 'For this test we need a mock of PermissionDescription...' + ); + //$permDescSelf = + //$this->assertEquals($permDescSelf->, 'Only me'); + //$this->assertEquals($permDescPublic, 'Public'); + } + } +} diff --git a/tests/text_test.php b/tests/unit/TextTest.php index d1e210b68..48c04bc54 100644 --- a/tests/text_test.php +++ b/tests/unit/TextTest.php @@ -1,20 +1,22 @@ <?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 PHPUnit_Framework_TestCase { +class TextTest extends TestCase { public function testGoodEmail() { $this->assertTrue(valid_email_regex('ken@spaz.org')); } diff --git a/tests/unit/UnitTestCase.php b/tests/unit/UnitTestCase.php new file mode 100644 index 000000000..7d706d5be --- /dev/null +++ b/tests/unit/UnitTestCase.php @@ -0,0 +1,38 @@ +<?php +/* Copyright (c) 2016 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; + +use PHPUnit\Framework\TestCase; + +/** + * @brief Base class for our Unit Tests. + * + * Empty class at the moment, but you should extend this class for unit test + * cases, so we could and for sure we will need to implement basic behaviour + * for all of our unit tests. + * + * @author Klaus Weidenbach + */ +abstract class UnitTestCase extends TestCase { + // when needed we can define functionality here which is used in UnitTests. +} diff --git a/tests/upload_test.php b/tests/unit/UploadTest.php index 031315fba..ab9d0e50a 100644 --- a/tests/upload_test.php +++ b/tests/unit/UploadTest.php @@ -1,20 +1,22 @@ <?php /** * this file contains tests for the uploader - * + * * @package test.util */ +use PHPUnit\Framework\TestCase; + /** required, it is the file under test */ require_once('include/attach.php'); /** * TestCase for the uploader - * + * * @author ken restivo * @package test.util */ -class UploadTest extends PHPUnit_Framework_TestCase { +class UploadTest extends TestCase { public function testFileNameMutipleDots() { $multidots = "foo.bar.baz.0.1.3.ogg"; $this->assertEquals("audio/ogg", z_mime_content_type($multidots)); diff --git a/tests/expand_acl_test.php b/tests/unit/expand_acl_test.php index 154bc921d..154bc921d 100644 --- a/tests/expand_acl_test.php +++ b/tests/unit/expand_acl_test.php diff --git a/tests/get_tags_test.php b/tests/unit/get_tags_test.php index 40f016747..40f016747 100644 --- a/tests/get_tags_test.php +++ b/tests/unit/get_tags_test.php diff --git a/tests/template_test.php b/tests/unit/template_test.php index 1f9f80531..1f9f80531 100644 --- a/tests/template_test.php +++ b/tests/unit/template_test.php |