From 8c1310c334b9bb73010cb56e57dcecda16ec4736 Mon Sep 17 00:00:00 2001 From: El RIDO Date: Sun, 3 Dec 2023 13:40:04 +0100 Subject: [PATCH] update unit test doc & PHP 8 fixes having had to re-do all of these steps on a new environment, I noticed some inconsistencies and updated the doc: - moved unit testing doc into common doc folder, so it is easier to find - removed no longer supported Janitor reference - removed note regarding generated test duration, current version takes less than a minute - addressed each function removal & phpunit deprecations --- Makefile | 2 +- bin/configuration-test-generator | 31 +++++++++++----------- tst/README.md => doc/Running Unit Tests.md | 14 +++------- 3 files changed, 20 insertions(+), 27 deletions(-) rename tst/README.md => doc/Running Unit Tests.md (90%) diff --git a/Makefile b/Makefile index 7807108b..f37eefb5 100644 --- a/Makefile +++ b/Makefile @@ -18,7 +18,7 @@ coverage-js: ## Run JS unit tests and generate code coverage reports. coverage-php: ## Run PHP unit tests and generate code coverage reports. cd tst && phpunit 2> /dev/null - cd tst/log/php-coverage-report && sed -i "s#$(CURDIR)##g" *.html */*.html + cd tst/log/php-coverage-report && sed -i "s#$(CURDIR)/##g" *.html */*.html doc: doc-js doc-php ## Generate all code documentation. diff --git a/bin/configuration-test-generator b/bin/configuration-test-generator index a5db48a8..7cde2ab3 100755 --- a/bin/configuration-test-generator +++ b/bin/configuration-test-generator @@ -23,7 +23,7 @@ new ConfigurationTestGenerator(array( 'tests' => array( array( 'conditions' => array('steps' => $vd), - 'type' => 'RegExp', + 'type' => 'MatchesRegularExpression', 'args' => array( '#]*id="opendiscussionoption"[^>]*>#', '$content', @@ -53,7 +53,7 @@ new ConfigurationTestGenerator(array( 'setting' => false, 'tests' => array( array( - 'type' => 'NotRegExp', + 'type' => 'DoesNotMatchRegularExpression', 'args' => array( '#]*id="opendiscussionoption"[^>]*>#', '$content', @@ -70,7 +70,7 @@ new ConfigurationTestGenerator(array( 'tests' => array( array( 'conditions' => array('main/discussion' => true), - 'type' => 'RegExp', + 'type' => 'MatchesRegularExpression', 'args' => array( '#]+id="opendiscussion"[^>]*checked="checked"[^>]*>#', '$content', @@ -84,7 +84,7 @@ new ConfigurationTestGenerator(array( 'tests' => array( array( 'conditions' => array('main/discussion' => true), - 'type' => 'NotRegExp', + 'type' => 'DoesNotMatchRegularExpression', 'args' => array( '#]+id="opendiscussion"[^>]*checked="checked"[^>]*>#', '$content', @@ -100,7 +100,7 @@ new ConfigurationTestGenerator(array( 'setting' => true, 'tests' => array( array( - 'type' => 'RegExp', + 'type' => 'MatchesRegularExpression', 'args' => array( '#]+id="burnafterreading"[^>]*checked="checked"[^>]*>#', '$content', @@ -113,7 +113,7 @@ new ConfigurationTestGenerator(array( 'setting' => false, 'tests' => array( array( - 'type' => 'NotRegExp', + 'type' => 'DoesNotMatchRegularExpression', 'args' => array( '#]+id="burnafterreading"[^>]*checked="checked"[^>]*>#', '$content', @@ -129,7 +129,7 @@ new ConfigurationTestGenerator(array( 'setting' => true, 'tests' => array( array( - 'type' => 'RegExp', + 'type' => 'MatchesRegularExpression', 'args' => array( '#]*id="password"[^>]*>#', '$content', @@ -143,7 +143,7 @@ new ConfigurationTestGenerator(array( 'tests' => array( array( 'conditions' => array('main/discussion' => true), - 'type' => 'NotRegExp', + 'type' => 'DoesNotMatchRegularExpression', 'args' => array( '#]*id="password"[^>]*>#', '$content', @@ -159,14 +159,14 @@ new ConfigurationTestGenerator(array( 'setting' => 'page', 'tests' => array( array( - 'type' => 'RegExp', + 'type' => 'MatchesRegularExpression', 'args' => array( '#]+type="text/css"[^>]+rel="stylesheet"[^>]+href="css/privatebin\.css\\?\d[\d\.]+\d+"[^>]*/>#', '$content', 'outputs "page" stylesheet correctly', ), ), array( - 'type' => 'NotRegExp', + 'type' => 'DoesNotMatchRegularExpression', 'args' => array( '#]+type="text/css"[^>]+rel="stylesheet"[^>]+href="css/bootstrap/bootstrap-\d[\d\.]+\d\.css"[^>]*/>#', '$content', @@ -179,14 +179,14 @@ new ConfigurationTestGenerator(array( 'setting' => 'bootstrap', 'tests' => array( array( - 'type' => 'NotRegExp', + 'type' => 'DoesNotMatchRegularExpression', 'args' => array( '#]+type="text/css"[^>]+rel="stylesheet"[^>]+href="css/privatebin\.css\\?\d[\d\.]+\d+"[^>]*/>#', '$content', 'removes "page" stylesheet correctly', ), ), array( - 'type' => 'RegExp', + 'type' => 'MatchesRegularExpression', 'args' => array( '#]+type="text/css"[^>]+rel="stylesheet"[^>]+href="css/bootstrap/bootstrap-\d[\d\.]+\d\.css"[^>]*/>#', '$content', @@ -361,7 +361,7 @@ class ConfigurationTestGenerator } // skip if not all test conditions are met if (array_key_exists('conditions', $test)) { - while (list($path, $setting) = each($test['conditions'])) { + foreach ($test['conditions'] as $path => $setting) { if ($path == 'steps' && !in_array($step, $setting)) { continue 2; } elseif ($path != 'steps') { @@ -591,8 +591,9 @@ EOT; return $this->_configurations; } echo "generateConfigurations: iteration $this->_iterationCount", PHP_EOL; - $continue = list($path, $settings) = each($this->_options); - if ($continue === false) { + $path = key($this->_options); + $settings = current($this->_options); + if (next($this->_options) === false) { return $this->_configurations; } list($section, $option) = explode('/', $path); diff --git a/tst/README.md b/doc/Running Unit Tests.md similarity index 90% rename from tst/README.md rename to doc/Running Unit Tests.md index db310e9b..95078258 100644 --- a/tst/README.md +++ b/doc/Running Unit Tests.md @@ -28,11 +28,6 @@ docker run --rm --read-only -v ~/PrivateBin:/srv:ro privatebin/unit-testing phpu docker run --rm --read-only -v ~/PrivateBin:/srv:ro privatebin/unit-testing mocha ``` -We also provide a Janitor image that includes the Cloud9 and Theia WebIDEs as -well as the integrated unit testing utilities. See our [docker wiki -page](https://github.com/PrivateBin/PrivateBin/wiki/Docker#janitor-image-with-cloud9-and-theia-webide-janitortechnologyprivatebin) -for further details on this. - ## Running PHP Unit Tests In order to run these tests, you will need to install the following packages @@ -61,18 +56,15 @@ configurations defined in its constructor, it generates the unit test file of these configurations and tests for (most of the) valid combinations. Some of combinations can't be tested with this method, i.e. a valid option combined with an invalid one. Other very specific test cases (i.e. to trigger multiple errors) -are covered in `tst/PrivateBinTest.php`. Here is how to generate the +are covered in `tst/ControllerTest.php`. Here is how to generate the configuration test and run it: ```console $ cd PrivateBin/tst -$ php ConfigurationTestGenerator.php +$ ../bin/configuration-test-generator $ phpunit ConfigurationCombinationsTest.php ``` -Note that it can take an hour or longer to run the several thousand tests. - - ## Running JavaScript Unit Tests In order to run these tests, you will need to install the following packages @@ -134,7 +126,7 @@ associated random number generator (RNG) state, so you can reproduce it easily: 1 failing 1) Helper getCookie returns the requested cookie: - Error: Failed after 30 tests and 11 shrinks. rngState: 88caf85079d32e416b; Counterexample: ["{", "9", "9", "YD8%fT"]; [" ", "_|K:"]; + Error: Failed after 30 tests and 11 shrinks. rngState: 88caf85079d32e416b; Counterexample: ["{", "9", "9", "YD8%fT"]; [" ", "_|K:"]; [...] ```