From b92e3ca3ee3f8bed1ff52311d29b4ce1e4297a83 Mon Sep 17 00:00:00 2001 From: Klaus Weidenbach Date: Wed, 12 Oct 2016 21:50:49 +0200 Subject: [PATCH] [TASK] Restructure tests/ folder. Move unit tests to tests/unit/. Get the old still working UnitTests into a working state again. Updated composer.json with required-dev packages. Create a new folder tests/acceptance for Behat functional/acceptance testing. Add a first Feature draft of a Behat functional test for local login. --- composer.json | 18 +++- tests/README.md | 25 +++++ tests/acceptance/behat.yml | 27 +++++ .../features/bootstrap/AdminContext.php | 23 ++++ .../features/bootstrap/ApiContext.php | 23 ++++ .../features/bootstrap/FeatureContext.php | 23 ++++ tests/acceptance/features/login_local.feature | 18 ++++ .../AntiXSSTest.php} | 42 ++++---- .../AutonameTest.php} | 100 +++++++++--------- .../ContainsAttributeTest.php} | 90 ++++++++-------- tests/{text_test.php => unit/TextTest.php} | 8 +- tests/unit/UnitTestCase.php | 38 +++++++ .../{upload_test.php => unit/UploadTest.php} | 8 +- tests/{ => unit}/expand_acl_test.php | 0 tests/{ => unit}/get_tags_test.php | 0 tests/{ => unit}/template_test.php | 0 16 files changed, 321 insertions(+), 122 deletions(-) create mode 100644 tests/README.md create mode 100644 tests/acceptance/behat.yml create mode 100644 tests/acceptance/features/bootstrap/AdminContext.php create mode 100644 tests/acceptance/features/bootstrap/ApiContext.php create mode 100644 tests/acceptance/features/bootstrap/FeatureContext.php create mode 100644 tests/acceptance/features/login_local.feature rename tests/{xss_filter_test.php => unit/AntiXSSTest.php} (82%) rename tests/{autoname_test.php => unit/AutonameTest.php} (89%) rename tests/{contains_attribute_test.php => unit/ContainsAttributeTest.php} (91%) rename tests/{text_test.php => unit/TextTest.php} (89%) create mode 100644 tests/unit/UnitTestCase.php rename tests/{upload_test.php => unit/UploadTest.php} (90%) rename tests/{ => unit}/expand_acl_test.php (100%) rename tests/{ => unit}/get_tags_test.php (100%) rename tests/{ => unit}/template_test.php (100%) diff --git a/composer.json b/composer.json index 7326fbc6e..abf583875 100644 --- a/composer.json +++ b/composer.json @@ -27,8 +27,15 @@ "ext-gd" : "*", "ext-mbstring" : "*", "ext-xml" : "*", - "sabre/dav" : "~3.2", - "ext-openssl" : "*" + "ext-openssl" : "*", + "sabre/dav" : "~3.2" + }, + "require-dev" : { + "php" : ">=5.6", + "phpunit/phpunit" : "^5.6", + "behat/behat" : "@stable", + "behat/mink-extension": "@stable", + "behat/mink-goutte-driver": "@stable" }, "autoload" : { "psr-4" : { @@ -36,8 +43,13 @@ "Zotlabs\\" : "Zotlabs/" } }, + "autoload-dev" : { + "psr-4" : { + "Zotlabs\\Tests\\Unit\\" : "tests/unit" + } + }, "minimum-stability" : "stable", "config" : { "notify-on-install" : false } -} +} \ No newline at end of file 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 @@ +I want to break\n this!11!"; - $xml=xmlify($text); + $xml=xmlify($text); $retext=unxmlify($text); $this->assertEquals($text, $retext); } - - /** - * xmlify and put in a document - */ - public function testXmlifyDocument() { - $tag="I want to break"; + + /** + * xmlify and put in a document + */ + public function testXmlifyDocument() { + $tag="I want to break"; $xml=xmlify($tag); - $text=''.$xml.''; - - $xml_parser=xml_parser_create(); + $text=''.$xml.''; + + $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 similarity index 89% rename from tests/autoname_test.php rename to tests/unit/AutonameTest.php index 702e05bef..9f92f736f 100644 --- a/tests/autoname_test.php +++ b/tests/unit/AutonameTest.php @@ -1,76 +1,78 @@ 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 similarity index 91% rename from tests/contains_attribute_test.php rename to tests/unit/ContainsAttributeTest.php index b0bb06acf..0930d9837 100644 --- a/tests/contains_attribute_test.php +++ b/tests/unit/ContainsAttributeTest.php @@ -1,51 +1,53 @@ 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/text_test.php b/tests/unit/TextTest.php similarity index 89% rename from tests/text_test.php rename to tests/unit/TextTest.php index d1e210b68..48c04bc54 100644 --- a/tests/text_test.php +++ b/tests/unit/TextTest.php @@ -1,20 +1,22 @@ 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 @@ +assertEquals("audio/ogg", z_mime_content_type($multidots)); diff --git a/tests/expand_acl_test.php b/tests/unit/expand_acl_test.php similarity index 100% rename from tests/expand_acl_test.php rename to tests/unit/expand_acl_test.php diff --git a/tests/get_tags_test.php b/tests/unit/get_tags_test.php similarity index 100% rename from tests/get_tags_test.php rename to tests/unit/get_tags_test.php diff --git a/tests/template_test.php b/tests/unit/template_test.php similarity index 100% rename from tests/template_test.php rename to tests/unit/template_test.php